package com.casic.missiles.controller.equipment;

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.flowable.ApprovalDeleteRequest;
import com.casic.missiles.dto.equipment.AcceptanceCheckRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.dto.equipment.AcceptanceCheck;
import com.casic.missiles.model.equipment.EquipmentApply;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.equipment.IEquipmentAcceptanceCheckService;
import com.casic.missiles.service.equipment.IEquipmentApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "设备检修保养验收单")
@Slf4j
@RestController
@RequestMapping("/acceptanceCheck")
public class EquipmentAcceptanceCheckController extends ExportController {

    @Resource
    private IEquipmentAcceptanceCheckService iEquipmentAcceptanceCheckService;

    @Resource

    private IEquipmentApplyService iEquipmentApplyService;

    /**
     * 设备检修保养验收单列表
     */
    @ApiOperation("设备检修保养验收单列表")
    @PostMapping("/acceptanceCheckList")
    @ResponseBody
    public ReturnDTO<PageInfoBT<AcceptanceCheck>> acceptanceCheckList(@RequestBody @Valid AcceptanceCheckRequest request, BindingResult bindingResult) throws Exception{
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<AcceptanceCheck> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(iEquipmentAcceptanceCheckService.acceptanceCheckList(page, request)));
    }

    /**
     * 设备检修保养验收单详细信息
     */
    @ApiOperation("设备检修保养验收单详细信息")
    @PostMapping("/acceptanceCheckInfo")
    @ResponseBody
    public ReturnDTO<EquipmentApply> acceptanceCheckInfo(@RequestBody @Valid IdDTO idDTO) throws Exception{
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(iEquipmentAcceptanceCheckService.acceptanceCheckInfo(idDTO));
    }

    /**
     * 设备检修保养验收单新建
     */
    @ApiOperation("设备检修保养验收单新建")
    @PostMapping("/addAcceptanceCheck")
    @ResponseBody
    public ReturnDTO addAcceptanceCheck(@RequestBody @Valid EquipmentApply equipmentApply, BindingResult bindingResult) throws Exception{
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentAcceptanceCheckService.addAcceptanceCheck(equipmentApply);
    }

    /**
     *
     * 设备检修保养验收单提交（必须先保存）
     */
    @ApiOperation("设备检修保养验收单提交（必须先保存）")
    @PostMapping("/submitEquipmentApply")
    @ResponseBody
    public ReturnDTO submitEquipmentApply(@RequestBody @Valid BaseApprovalSubmitRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentApplyService.submitEquipmentApply(request);
    }

    /**
     * 设备检修保养验收单删除
     */
    @ApiOperation("设备检修保养验收单审批操作-删除")
    @PostMapping("deleteEquipmentApply")
    @ResponseBody
    public ReturnDTO deleteEquipmentApply(@RequestBody @Valid ApprovalDeleteRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentApplyService.deleteEquipmentApply(request);
    }

    /**
     * 设备检修保养验收单编辑
     */
    @ApiOperation("设备检修保养验收单编辑")
    @PostMapping("/updateAcceptanceCheckInfo")
    @ResponseBody
    public ReturnDTO updateAcceptanceCheckInfo(@RequestBody @Valid EquipmentApply equipmentApply, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentAcceptanceCheckService.updateAcceptanceCheckInfo(equipmentApply);
    }

    /**
     * 设备检修保养验收单导出
     */
    @ApiOperation("设备检修保养验收单导出")
    @PostMapping("/exportList")
    @ResponseBody
    public void exportList(@RequestBody AcceptanceCheckRequest request, HttpServletResponse response) throws Exception{
        try {
            iEquipmentAcceptanceCheckService.exportList(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 审批未通过驳回后-编辑
     */
    @ApiOperation("审批未通过驳回后-编辑")
    @PostMapping("/failUpdate")
    @ResponseBody
    public ReturnDTO failUpdate(@RequestBody @Valid EquipmentApply equipmentApply, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(equipmentApply.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentAcceptanceCheckService.failUpdate(equipmentApply);
    }

}
