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/apply")
public class BusinessOutsourceProjectApplyController 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(1);
        request.setApplicantId(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 BusinessOutsourceProject outsourceProject, BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(outsourceProject);
        return projectService.saveProjectApply(outsourceProject);
    }

    @ApiOperation("草稿箱分包项目申请编辑")
    @PostMapping("/draftUpdate")
    @ResponseBody
    public ReturnDTO draftUpdate(@RequestBody @Valid BusinessOutsourceProject outsourceProject, BindingResult bindingResult) throws Exception {
        Assert.isFalse(Objects.isNull(outsourceProject.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(outsourceProject);
        return projectService.projectApplyDraftUpdate(outsourceProject);
    }

    @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.submitProjectApply(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.projectApplyDelete(idDTO.getId());
    }

    @ApiOperation("分包项目申请详情")
    @PostMapping("/detail")
    @ResponseBody
    public ReturnDTO<BusinessOutsourceProject> detail(@RequestBody @Valid OutsourceProjectDetailRequest request) throws Exception {
        Assert.isFalse(Objects.isNull(request.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return projectService.projectApplyDetail(request);
    }

    @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.projectApplyApprovalDelete(request);
    }

    @ApiOperation("未通过分包项目申请编辑（驳回后的分包项目申请重新编辑，仅未通过列表中的编辑按钮可调用）")
    @PostMapping("/failUpdate")
    @ResponseBody
    public ReturnDTO failUpdate(@RequestBody @Valid BusinessOutsourceProject outsourceProject, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(outsourceProject.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return projectService.projectApplyFailUpdate(outsourceProject);
    }

    /**
     * 查询分包项目申请审批通过列表
     */
    @ApiOperation("分包项目申请审批通过列表（分页）")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<BusinessOutsourceProject>> listPage(@RequestBody @Valid OutsourceProjectApplyListRequest 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.projectApplyListPage(request, page)));
    }
}
