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.StateManageRequest;
import com.casic.missiles.dto.flowable.ApprovalDeleteRequest;
import com.casic.missiles.dto.equipment.StateManageResponse;
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.IEquipmentStateManageService;
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;

/**
 ** <p>
 *  * 状态管理列表
 *  * </p>
 *
 * @author xiezhuangz
 * @version 1.0
 * @date 2022/12/27
 */
@Api(tags = "状态管理相关接口")
@Slf4j
@RestController
@RequestMapping("meter/stateManage")
public class EquipmentStateManageController extends ExportController {

    @Resource
    private IEquipmentStateManageService stateManageService;

    @Resource
    private IEquipmentApplyService meterEquipmentApplyService;

    /**
     * 状态管理申请列表
     */
    @ApiOperation("状态管理申请列表")
    @PostMapping("/stateManageList")
    @ResponseBody
    public ReturnDTO<PageInfoBT<StateManageResponse>> applyList(@RequestBody @Valid StateManageRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<StateManageResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(stateManageService.stateManageList(page, request)));
    }

    /**
     * 状态管理申请列表删除
     */
    @ApiOperation("状态管理申请列表删除")
    @PostMapping("/deleteStateManageList")
    @ResponseBody
    public ReturnDTO deleteStateManageList(@RequestBody @Valid ApprovalDeleteRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return meterEquipmentApplyService.deleteEquipmentApply(request);
    }

    /**
     * 状态管理申请提交
     */
    @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 meterEquipmentApplyService.submitEquipmentApply(request);
    }

    /**
     * 状态管理申请详细信息
     */
    @ApiOperation("状态管理申请详细信息")
    @PostMapping("/selectStateManageInfo")
    @ResponseBody
    public ReturnDTO<StateManageResponse> selectStateManageInfo(@RequestBody @Valid IdDTO idDto) throws Exception{
        Assert.isFalse(Objects.isNull(idDto.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(stateManageService.selectStateManageInfo(idDto));
    }

    /**
     * 设状态管理申请新建
     */
    @ApiOperation("设状态管理申请新建")
    @PostMapping("/addStateManageInfo")
    @ResponseBody
    public ReturnDTO addStateManageInfo(@RequestBody @Valid EquipmentApply meterEquipmentApply, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return meterEquipmentApplyService.addEquipmentApply(meterEquipmentApply);
    }


    /**
     * 状态管理申请编辑
     * @return
     */
    @ApiOperation("状态管理申请编辑")
    @PostMapping("/updateStateManageInfo")
    @ResponseBody
    public ReturnDTO updateStateManageInfo(@RequestBody @Valid StateManageResponse stateManageResponse, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return stateManageService.updateEquipmentApply(stateManageResponse);
    }

    /**
     * 状态管理列表导出
     */
    @ApiOperation("状态管理列表导出")
    @PostMapping("/exportStateManageList")
    @ResponseBody
    public void exportStateManageList(@RequestBody StateManageRequest request, HttpServletResponse response) {
        try {
            stateManageService.exportStateManageList(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 审批未通过驳回后-编辑
     */
    @ApiOperation("审批未通过驳回后-编辑")
    @PostMapping("/failUpdate")
    @ResponseBody
    public ReturnDTO failUpdate(@RequestBody @Valid StateManageResponse stateManageResponse, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(stateManageResponse.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return stateManageService.failUpdate(stateManageResponse);
    }

}
