package com.ningdatech.carapi.company.controller;


import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.company.entity.NdCompanySecurityProductRegulations;
import com.ningdatech.carapi.company.entity.dto.CompanySecurityProductRegulationParam;
import com.ningdatech.carapi.company.entity.dto.ReqSecurityProductRegulationsDTO;
import com.ningdatech.carapi.company.entity.dto.ResSecurityProductRegulationsDTO;
import com.ningdatech.carapi.company.entity.dto.UploadRegulationDTO;
import com.ningdatech.carapi.company.entity.vo.ComSecProRegVO;
import com.ningdatech.carapi.company.manage.CompanySecurityManage;
import com.ningdatech.carapi.company.service.INdCompanySecurityProductRegulationsService;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;


/**
 * <p>
 * 安全生产制度体系 前端控制器
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/company-security-product-regulations")
public class NdCompanySecurityProductRegulationsController {
    private final INdCompanySecurityProductRegulationsService iNdCompanySecurityProductRegulationsService;

    private final CompanySecurityManage securityManage;

    @PostMapping
    private Boolean saveSecurityProductRegulations(@RequestBody ReqSecurityProductRegulationsDTO reqSecurityProductRegulationsDTO) {
        NdCompanySecurityProductRegulations entity = new NdCompanySecurityProductRegulations();
        BeanUtil.copyProperties(reqSecurityProductRegulationsDTO, entity);
        entity.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        return iNdCompanySecurityProductRegulationsService.save(entity);
    }

    @GetMapping
    private Page<ResSecurityProductRegulationsDTO> securityProductRegulationsList(@RequestParam(value = "regulationsName", required = false) String regulationsName,
                                                                                  @RequestParam(value = "competentDepartment", required = false) String competentDepartment,
                                                                                  @RequestParam(value = "startTime", required = false) String startTime,
                                                                                  @RequestParam(value = "endTime", required = false) String endTime,
                                                                                  @RequestParam(value = "fileType") Integer fileType,
                                                                                  @RequestParam(value = "pageNumber") int pageNum,
                                                                                  @RequestParam(value = "pageSize") int pageSize) {
        return iNdCompanySecurityProductRegulationsService.securityProductRegulationsList(regulationsName, competentDepartment, startTime, endTime, fileType, pageNum, pageSize);
    }

    @ApiOperation(value = "安全生产制度体系列表", notes = "安全生产制度体系列表")
    @GetMapping("/safety-education-training-list")
    public PageVo<ComSecProRegVO> listRegulations(@Valid @ModelAttribute CompanySecurityProductRegulationParam param) {
        return securityManage.pageRegulations(param);
    }

    @PostMapping("/upload-regulation")
    private Boolean uploadRegulation(@Valid @RequestBody UploadRegulationDTO dto) {
        return securityManage.uploadRegulation(dto);
    }

    @ApiOperation("删除安全生产责任体系文件")
    @DeleteMapping("/delete-regulation")
    public void deleteCommitmentFile(@RequestParam Long id) {
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        securityManage.deleteRegulationFile(id,roleId);
    }

}
