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.outsource.*;
import com.casic.missiles.dto.flowable.ApprovalDeleteRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessOutsourceProject;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessOutsourceProjectService;
import com.casic.missiles.utils.DictCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 分包项目表 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-03-25
 */
@Api(tags = "分包管理接口-分包项目验收")
@Slf4j
@RestController
@RequestMapping("/business/outsourceProject/check")
public class BusinessOutsourceProjectCheckController extends ExportController {
    @Autowired
    private IBusinessOutsourceProjectService projectService;

    @ApiOperation("分包项目验收审批列表及详情（分页）")
    @PostMapping("/approval/listPage")
    public ReturnDTO<PageInfoBT<OutsourceProjectApprovalListResponse>> approvalListPage(@RequestBody @Valid OutsourceProjectApprovalListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(request);
        Page<OutsourceProjectApprovalListResponse> page = PageFactory.defaultPage();
        request.setTypeFlag(2);
        request.setCheckerId(request.getCreateUserId());
        List<OutsourceProjectApprovalListResponse> responseList = projectService.projectApplyApprovalListPage(request, page);
        page.setRecords(responseList);
        return ReturnUtil.success(super.packForBT(page));
    }

    @ApiOperation("分包项目验收审批保存（保存至草稿箱）")
    @PostMapping("/save")
    @ResponseBody
    public ReturnDTO<OutsourceProjectSaveResponse> save(@RequestBody @Valid OutsourceProjectCheckSaveOrUpdateRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.saveOrUpdateProjectCheck(request);
    }

    @ApiOperation("草稿箱分包项目验收编辑")
    @PostMapping("/draftUpdate")
    @ResponseBody
    public ReturnDTO draftUpdate(@RequestBody @Valid OutsourceProjectCheckSaveOrUpdateRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.saveOrUpdateProjectCheck(request);
    }

    @ApiOperation("分包项目验收提交（未调用过保存接口保存至草稿箱需先调用保存接口，再调用该接口）")
    @PostMapping("/submit")
    @ResponseBody
    public ReturnDTO submit(@RequestBody @Valid BaseApprovalSubmitRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.submitProjectCheck(request);
    }

    /**
     * 只清空验收所填字段及验收审批草稿状态
     */
    @ApiOperation("分包项目验收删除（草稿删除也调用该接口）")
    @PostMapping("/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return projectService.projectCheckDelete(idDTO.getId());
    }

    /**
     * 复用分包项目申请详情service
     */
    @ApiOperation("分包项目验收详情")
    @PostMapping("/detail")
    @ResponseBody
    public ReturnDTO<BusinessOutsourceProject> supplierDetail(@RequestBody @Valid IdDTO idDTO) throws Exception {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return projectService.projectDetail(idDTO.getId());
    }

    /**
     * 清空验收所填字段及验收审批状态
     */
    @ApiOperation("分包项目验收审批操作-删除")
    @PostMapping("/approval/operate/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid ApprovalDeleteRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.projectCheckApprovalDelete(request);
    }

    @ApiOperation("未通过分包项目验收编辑（驳回后的分包项目验收重新编辑，仅未通过列表中的编辑按钮可调用）")
    @PostMapping("/failUpdate")
    @ResponseBody
    public ReturnDTO failUpdate(@RequestBody @Valid OutsourceProjectCheckSaveOrUpdateRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.projectCheckFailUpdate(request);
    }

    /**
     * 分包方项目记录，根据分包方id查项目审批通过项目列表
     */
    @ApiOperation("分包方项目记录-审批通过项目列表（分页）")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<BusinessOutsourceProject>> listPage(@RequestBody @Valid OutsourceProjectCheckListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<BusinessOutsourceProject> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(projectService.projectCheckListPage(request, page)));
    }
}
