package com.casic.missiles.controller.business;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.BaseApprovalSubmitRequest;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.print.CertificateListRequest;
import com.casic.missiles.dto.business.print.CertificateListResponse;
import com.casic.missiles.dto.business.print.CertificatePrintDetail;
import com.casic.missiles.dto.business.print.CertificateRefusePrintRequest;
import com.casic.missiles.dto.flowable.ApprovalAgreeRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessCertificatePrintService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 业务调度-证书打印
 * 1、证书打印模块是对样品检验完成生成的证书进行管理
 * 2、另，并通过工作流审批的方式严格控制证书打印次数
 * </p>
 *
 * @author cz
 * @since 2023-02-20
 */
@RestController
@RequestMapping("/business/certificatePrint")
@AllArgsConstructor
@Api(tags = "证书打印")
public class BusinessCertificatePrintController extends ExportController {

    private final IBusinessCertificatePrintService certificatePrintService;

    /**
     * 证书打印列表，除了基本的列表的信息，需要判断证书打印审批中的状态，进而达到权限的控制
     */
    @ApiOperation("证书打印-分页")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<CertificateListResponse>> certificatePrintListPage(@RequestBody @Valid CertificateListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<CertificateListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(certificatePrintService.certificatePrintListPage(request, page)));
    }

    @ApiOperation("证书详情")
    @PostMapping("/detail")
    public ReturnDTO<CertificatePrintDetail> certificatePrintDetail(@RequestBody @Valid IdDTO idDTO) throws Exception {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
       return ReturnUtil.success(certificatePrintService.certificatePrintDetail(idDTO.getId()));
    }

    @ApiOperation("证书打印导出")
    @PostMapping("/export")
    public void certificatePrintExport(@RequestBody @Valid CertificateListRequest request, HttpServletResponse response) throws Exception {
        Page<CertificateListResponse> page = PageFactory.defaultPage();
        certificatePrintService.certificatePrintExport(request, response, page);
    }

    /**
     * 证书状态从打印状态变更为不可打印状态
     */
    @ApiOperation("变更为不可打印")
    @PostMapping("/noPrintable")
    public ReturnDTO certificatePrintExport(@RequestBody @Valid IdDTO idDTO){
        return certificatePrintService.certificatePrintStatus(idDTO.getId(),"2");
    }

    @ApiOperation("证书审批-同意")
    @PostMapping("/agree")
    public ReturnDTO agreeAgainPrint(@RequestBody @Valid ApprovalAgreeRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return certificatePrintService.agreeAgainPrint(request);
    }

    @ApiOperation("证书打印审批-拒绝")
    @PostMapping("/refuse")
    public ReturnDTO refuseAgainPrint(@RequestBody @Valid CertificateRefusePrintRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return certificatePrintService.refuseAgainPrint(request);
    }

    @ApiOperation("证书打印审批-发起申请")
    @PostMapping("/submitApproval")
    public ReturnDTO submitAgainPrint(@RequestBody @Valid BaseApprovalSubmitRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return certificatePrintService.submitAgainPrint(request);
    }

}
