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.NdCompanySecurityProductDuty;
import com.ningdatech.carapi.company.entity.dto.*;
import com.ningdatech.carapi.company.entity.vo.CompanySecurityCommitmentVO;
import com.ningdatech.carapi.company.entity.vo.CompanySecurityGovernmentVO;
import com.ningdatech.carapi.company.manage.CompanySecurityManage;
import com.ningdatech.carapi.company.service.ICompanySecurityCommitmentService;
import com.ningdatech.carapi.company.service.ICompanySecurityGovernmentService;
import com.ningdatech.carapi.company.service.INdCompanySecurityProductDutyService;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
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-duty")
public class NdCompanySecurityProductDutyController {

    private final INdCompanySecurityProductDutyService iNdCompanySecurityProductDutyService;
    private final RegionsCacheHelper regionsCacheHelper;

    private final CompanySecurityManage securityManage;

    private final ICompanySecurityCommitmentService commitmentService;

    private final ICompanySecurityGovernmentService governmentService;

    @PostMapping
    private Boolean saveSecurityProductDuty(@RequestBody ReqSecurityProductDutyDTO reqSecurityProductDutyDTO) {
        NdCompanySecurityProductDuty entity = new NdCompanySecurityProductDuty();
        BeanUtil.copyProperties(reqSecurityProductDutyDTO, entity);
        entity.setRegionName(regionsCacheHelper.getDisplayName(reqSecurityProductDutyDTO.getRegionId()));
        return iNdCompanySecurityProductDutyService.save(entity);
    }

    @GetMapping
    private Page<ResSecurityProductDutyDTO> securityProductDutyList(@RequestParam(value = "regionId", required = false) Long regionId,
                                                                    @RequestParam(value = "companyName", required = false) String companyName,
                                                                    @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 iNdCompanySecurityProductDutyService.securityProductDutyList(regionId, companyName,startTime,endTime, fileType, pageNum, pageSize);
    }

    @ApiOperation(value = "安全承诺书列表", notes = "安全承诺书列表")
    @GetMapping("/list-commitments")
    public PageVo<CompanySecurityCommitmentVO> listCommitments(@Valid @ModelAttribute CompanySecurityCommitmentParam param) {
        return securityManage.pageCommitments(param);
    }

    @ApiOperation(value = "管理机构列表", notes = "管理机构列表")
    @GetMapping("/list-governments")
    public PageVo<CompanySecurityGovernmentVO> listGovernments(@Valid @ModelAttribute CompanySecurityCommitmentParam param) {
        return securityManage.pageGovernments(param);
    }

    @PostMapping("/upload-commitment")
    private Boolean uploadCommitment(@Valid @RequestBody UploadCommitmentDTO dto) {
        return securityManage.uploadCommitment(dto);
    }

    @PostMapping("/upload-government")
    private Boolean uploadGovernment(@Valid @RequestBody UploadGovernmentDTO dto) {
        return securityManage.uploadGovernment(dto);
    }

    @ApiOperation("删除管理机构文件")
    @DeleteMapping("/delete-government")
    public void deleteGovernmentFile(@RequestParam Long id) {
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        securityManage.deleteGovernmentFile(id,roleId);
    }

    @ApiOperation("删除行业企业安全生产责任书")
    @DeleteMapping("/delete-commitment")
    public void deleteCommitmentFile(@RequestParam Long id) {
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        securityManage.deleteCommitmentFile(id,roleId);
    }

    @ApiOperation("增加下载次数")
    @GetMapping("/incrementDownCount/{id}")
    public void incrementDownCount(@PathVariable Long id) {
        securityManage.incrementDownCount(id);
    }
}
