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.*;
import com.casic.missiles.dto.business.siteExecutive.BusinessSiteExecutiveDetail;
import com.casic.missiles.dto.business.siteExecutive.SiteExecutiveApprovalListRequest;
import com.casic.missiles.dto.business.siteExecutive.SiteExecutiveApprovalListResponse;
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.BusinessSiteExecutiveInfo;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.modular.system.service.IDeptService;
import com.casic.missiles.service.business.IBusinessSiteExecutiveInfoService;
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.util.CollectionUtils;
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 zt
 * @since 2023-03-25
 */
@Api(tags = "现场检测接口")
@Slf4j
@RestController
@RequestMapping("/business/siteExecutive")
public class BusinessSiteExecutiveInfoController extends ExportController {
    @Autowired
    private IBusinessSiteExecutiveInfoService siteExecutiveInfoService;
    @Autowired
    private IDeptService deptService;

    @ApiOperation("现场检测审批列表（分页）")
    @PostMapping("/approval/listPage")
    public ReturnDTO<PageInfoBT<SiteExecutiveApprovalListResponse>> siteExecutiveApprovalListPage(@RequestBody @Valid SiteExecutiveApprovalListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(request);
        Page<SiteExecutiveApprovalListResponse> page = PageFactory.defaultPage();
        List<SiteExecutiveApprovalListResponse> responseList = siteExecutiveInfoService.siteExecutiveApprovalListPage(request, page);
        page.setRecords(responseList);
        return ReturnUtil.success(super.packForBT(page));
    }

    @ApiOperation("现场检测审批保存（保存至草稿箱）")
    @PostMapping("/save")
    @ResponseBody
    public ReturnDTO save(@RequestBody @Valid BusinessSiteExecutiveInfo siteExecutiveInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(siteExecutiveInfoService.saveSiteExecutiveInfo(siteExecutiveInfo));
    }


    @ApiOperation("现场检测提交（未调用过保存接口保存至草稿箱需先调用保存接口，再调用该接口）")
    @PostMapping("/submit")
    @ResponseBody
    public ReturnDTO siteExecutiveInfoSubmit(@RequestBody @Valid BaseApprovalSubmitRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return siteExecutiveInfoService.siteExecutiveInfoSubmit(request);
    }


    /**
     * 审批操作-编辑
     * 驳回的流程放置在未通过列表中，实现进行编辑后自动审核通过发起人节点
     */
    @ApiOperation("未通过现场检测编辑（驳回后的现场检测重新编辑，仅未通过列表中的编辑按钮可调用）")
    @PostMapping("/failUpdate")
    @ResponseBody
    public ReturnDTO failUpdate(@RequestBody @Valid BusinessSiteExecutiveInfo siteExecutiveInfo, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(siteExecutiveInfo.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return siteExecutiveInfoService.failUpdate(siteExecutiveInfo);
    }

    /**
     * 草稿箱文件编辑
     */
    @ApiOperation("草稿箱文件更新")
    @PostMapping("/draftUpdate")
    @ResponseBody
    public ReturnDTO draftUpdate(@RequestBody @Valid BusinessSiteExecutiveInfo siteExecutiveInfo, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(siteExecutiveInfo.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return siteExecutiveInfoService.draftUpdate(siteExecutiveInfo);

    }


    @ApiOperation("现场检测批量删除")
    @PostMapping("/batchDelete")
    @ResponseBody
    public ReturnDTO siteExecutiveBatchDelete(@RequestBody @Valid IdsDTO idsDTO) {
        Assert.isFalse(CollectionUtils.isEmpty(idsDTO.getIds()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return siteExecutiveInfoService.deleteBatchSiteExecutive(idsDTO.getIds());
    }

    @ApiOperation("现场检测详情")
    @PostMapping("/detail")
    @ResponseBody
    public ReturnDTO<BusinessSiteExecutiveDetail> supplierDetail(@RequestBody @Valid IdDTO idDTO)throws Exception {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(siteExecutiveInfoService.siteExecutiveInfoDetail(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 siteExecutiveInfoService.approvalDelete(request);
    }
}
