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.StandardEquipmentApplyRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.equipment.EquipmentStandardEquipment;
import com.casic.missiles.model.equipment.EquipmentStandardApply;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.equipment.IEquipmentStandardApplyService;
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/19 9:03
 */

@Slf4j
@Api(tags = "标准装置申请相关接口")
@RestController
@RequestMapping("/standard")
public class EquipmentStandardApplyController extends ExportController {

    @Resource
    private IEquipmentStandardApplyService standardEquipmentApplyService;

    /**
     * 标准装置列表
     */
    @ApiOperation("标准装置申请列表")
    @PostMapping("/standardEquipmentApplyList")
    @ResponseBody
    public ReturnDTO<PageInfoBT<EquipmentStandardApply>> standardEquipmentApplyList(@RequestBody @Valid StandardEquipmentApplyRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<EquipmentStandardApply> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(standardEquipmentApplyService.standardEquipmentApplyList(page, request)));
    }

    /**
     * 标准装置申请详细信息
     */
    @ApiOperation("标准装置申请详细信息")
    @PostMapping("/standardEquipmentApplyInfo")
    @ResponseBody
    public ReturnDTO<EquipmentStandardApply> standardEquipmentInfo(@RequestBody @Valid IdDTO idDTO) throws Exception{
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(standardEquipmentApplyService.standardEquipmentApplyInfo(idDTO));
    }

    /**
     * 标准装置申请新建
     */
    @ApiOperation("标准装置申请新建")
    @PostMapping("/addStandardEquipmentApply")
    @ResponseBody
    public ReturnDTO addStandardEquipmentApply(@RequestBody @Valid EquipmentStandardApply meterStandardApply, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return standardEquipmentApplyService.addStandardEquipmentApply(meterStandardApply);
    }

    /**
     * 标准装置申请提交
     */
    @ApiOperation("标准装置申请提交")
    @PostMapping("/submitStandardEquipmentApply")
    @ResponseBody
    public ReturnDTO submitStandardEquipmentApply(@RequestBody @Valid BaseApprovalSubmitRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return standardEquipmentApplyService.submitStandardEquipmentApply(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 standardEquipmentApplyService.approvalDelete(request);
    }

    /**
     * 标准装置申请信息编辑
     * @return
     */
    @ApiOperation("标准装置申请信息编辑")
    @PostMapping("/updateStandardEquipmentApply")
    @ResponseBody
    public ReturnDTO updateStandardEquipmentApply(@RequestBody @Valid EquipmentStandardApply meterStandardApply, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return standardEquipmentApplyService.updateStandardEquipmentApply(meterStandardApply);
    }

    /**
     * 标准装置列表导出
     */
    @ApiOperation("标准装置列表导出")
    @PostMapping("/exportList")
    @ResponseBody
    public void exportList(@RequestBody StandardEquipmentApplyRequest request, HttpServletResponse response) throws Exception{
        try {
            standardEquipmentApplyService.exportList(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

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

}
