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.equipment.EquipmentApplyRequest;
import com.casic.missiles.dto.flowable.ApprovalDeleteRequest;
import com.casic.missiles.dto.equipment.EquipmentApplyProcessRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.equipment.EquipmentApply;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.equipment.IEquipmentApplyService;
import com.casic.missiles.utils.DictCodeUtils;
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;

/**
 ** <p>
 *  * 测量设备-设备借用/领用申请
 *  * </p>
 *
 * @author xiezhuangz
 * @version 1.0
 * @date 2022/12/20
 */

@Slf4j
@Api(tags = "设备借用-领用申请相关接口")
@RestController
@RequestMapping("/equipmentApply")
public class EquipmentApplyController extends ExportController {

    @Resource
    private IEquipmentApplyService equipmentApplyService;


    /**
     * 设备借用-领用申请列表
     */
    @ApiOperation("设备借用-领用申请列表查询")
    @PostMapping("/equipmentApplyList")
    @ResponseBody
    public ReturnDTO<PageInfoBT<EquipmentApply>> equipmentApplyList(@RequestBody @Valid EquipmentApplyRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<EquipmentApply> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(equipmentApplyService.equipmentApplyList(page, request)));
    }

    /**
     * 设备借用-领用申请详细信息
     */
    @ApiOperation("设备借用-领用申请详细信息")
    @PostMapping("/equipmentApplyInfo")
    @ResponseBody
    public ReturnDTO<EquipmentApply> equipmentApplyInfo(@RequestBody @Valid IdDTO idDTO) throws Exception{
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(equipmentApplyService.equipmentApplyInfo(idDTO));
    }

    /**
     * 设备借用-领用申请新建
     */
    @ApiOperation("设备借用-领用申请新建（保存）")
    @PostMapping("/addEquipmentApply")
    @ResponseBody
    public ReturnDTO addEquipmentApply(@RequestBody @Valid EquipmentApply equipmentApply, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return equipmentApplyService.addEquipmentApply(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 equipmentApplyService.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 equipmentApplyService.deleteEquipmentApply(request);
    }

    /**
     * 设备借用-领用申请信息编辑
     */
    @ApiOperation("设备借用-领用申请信息编辑")
    @PostMapping("/updateEquipmentApply")
    @ResponseBody
    public ReturnDTO updateEquipmentApply(@RequestBody @Valid EquipmentApply equipmentApply, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return equipmentApplyService.updateEquipmentApply(equipmentApply);
    }

    /**
     * 设备借用-领用申请信息导出
     */
    @ApiOperation("设备借用-领用申请信息导出")
    @PostMapping("/exportList")
    @ResponseBody
    public void exportList(@RequestBody EquipmentApplyRequest request, HttpServletResponse response) throws Exception{
        equipmentApplyService.exportList(request,response);
    }

    /**
     * 设备借用申请列表借用接口
     */
    @ApiOperation("更新设备处理状态-借用")
    @PostMapping("/borrowEquipmentApply")
    @ResponseBody
    public ReturnDTO borrowEquipmentApply(@RequestBody @Valid IdDTO idDto) {
        Assert.isFalse(Objects.isNull(idDto.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return equipmentApplyService.borrowEquipmentApply(idDto);
    }

    /**
     * 设备借用申请列表归还接口
     */
    @ApiOperation("更新设备处理状态-归还")
    @PostMapping("/returnEquipmentApply")
    @ResponseBody
    public ReturnDTO returnEquipmentApply(@RequestBody @Valid IdDTO idDto) {
        Assert.isFalse(Objects.isNull(idDto.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return equipmentApplyService.returnEquipmentApply(idDto);
    }


    @ApiOperation("更新设备处理状态（领用/退领）")
    @PostMapping("/updateProcessResult")
    @ResponseBody
    public ReturnDTO updateProcessResult(@RequestBody @Valid EquipmentApplyProcessRequest request, BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(request);
        return equipmentApplyService.updateProcessResult(request);
    }

    /**
     * 审批未通过驳回后-编辑
     */
    @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 equipmentApplyService.failUpdate(equipmentApply);
    }

}
