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.NdCompanySecurityProductExecute;
import com.ningdatech.carapi.company.entity.dto.CompanySecurityProductExecuteParam;
import com.ningdatech.carapi.company.entity.dto.ReqSecurityProductExecuteDTO;
import com.ningdatech.carapi.company.entity.dto.ResSecurityProductExecuteDTO;
import com.ningdatech.carapi.company.entity.dto.UploadExecutionDTO;
import com.ningdatech.carapi.company.entity.vo.ComSecProExeVO;
import com.ningdatech.carapi.company.manage.CompanySecurityManage;
import com.ningdatech.carapi.company.service.INdCompanySecurityProductExecuteService;
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-execute")
public class NdCompanySecurityProductExecuteController {

    private final INdCompanySecurityProductExecuteService iNdCompanySecurityProductExecuteService;

    private final CompanySecurityManage securityManage;
    @PostMapping
    private Boolean saveSecurityProductExecute(@RequestBody ReqSecurityProductExecuteDTO reqSecurityProductExecuteDTO) {
        NdCompanySecurityProductExecute entity = new NdCompanySecurityProductExecute();
        BeanUtil.copyProperties(reqSecurityProductExecuteDTO, entity);
        entity.setRegionId(LoginUserUtil.loginUserDetail().getRegionId());
        entity.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        return iNdCompanySecurityProductExecuteService.save(entity);
    }

    @GetMapping
    private Page<ResSecurityProductExecuteDTO> securityProductExecuteList(@RequestParam(value = "name", required = false) String name,
                                                                          @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 iNdCompanySecurityProductExecuteService.securityProductExecuteList(name, startTime, endTime,fileType, pageNum, pageSize);
    }


    @ApiOperation(value = "安全生产制度执行情况列表", notes = "安全生产制度执行情况列表")
    @GetMapping("/safety-product-execute-list")
    public PageVo<ComSecProExeVO> listExecutes(@Valid @ModelAttribute CompanySecurityProductExecuteParam param) {
        return securityManage.pageExecutes(param);
    }

    @PostMapping("/upload-execution")
    private Boolean uploadExecution(@Valid @RequestBody UploadExecutionDTO dto) {
        return securityManage.uploadExecution(dto);
    }

    @ApiOperation("删除安全生产制度执行情况文件")
    @DeleteMapping("/delete-execute")
    public void deleteCommitmentFile(@RequestParam Long id) {
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        securityManage.deleteExecuteFile(id,roleId);
    }
}
