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.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.equipment.CheckInfoRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.equipment.EquipmentCheckInfo;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.equipment.IEquipmentCheckInfoService;
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/26
 */
@Api(tags = "设备管理点检查相关接口")
@Slf4j
@RestController
@RequestMapping("/check")
public class EquipmentCheckInfoController extends ExportController {

    @Resource
    private IEquipmentCheckInfoService iEquipmentCheckInfoService;

    /**
     * 设备管理点检查列表
     */
    @ApiOperation("设备管理点检查列表")
    @PostMapping("/checkInfoList")
    @ResponseBody
    public ReturnDTO<PageInfoBT<EquipmentCheckInfo>> checkInfoList(@RequestBody @Valid CheckInfoRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<EquipmentCheckInfo> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(iEquipmentCheckInfoService.checkInfoList(page, request)));
    }

    /**
     * 设备管理点检查列表删除
     */
    @ApiOperation("设备管理点检查列表删除")
    @PostMapping("/deleteCheckInfo")
    @ResponseBody
    public ReturnDTO deleteCheckInfo(@RequestBody @Valid IdDTO idDto) {
        Assert.isFalse(Objects.isNull(idDto.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return iEquipmentCheckInfoService.deleteCheckInfo(idDto);
    }


    /**
     * 设备管理点检查详细信息
     */
    @ApiOperation("设备管理点检查详细信息")
    @PostMapping("/selectCheckInfo")
    @ResponseBody
    public ReturnDTO<EquipmentCheckInfo> selectStaffInfo(@RequestBody @Valid IdDTO idDto) throws Exception{
        Assert.isFalse(Objects.isNull(idDto.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(iEquipmentCheckInfoService.selectCheckInfo(idDto));
    }

    /**
     * 设备管理点检查新建
     */
    @ApiOperation("备管理点检查新建")
    @PostMapping("/addCheckInfo")
    @ResponseBody
    public ReturnDTO addCheckInfo(@RequestBody @Valid EquipmentCheckInfo equipmentCheckInfo, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return iEquipmentCheckInfoService.addCheckInfo(equipmentCheckInfo);
    }

    /**
     * 设备管理点检查列表导出
     */
    @ApiOperation("设备管理点检查列表导出")
    @PostMapping("/exportCheckInfoList")
    @ResponseBody
    public void exportCheckInfoList(@RequestBody CheckInfoRequest request, HttpServletResponse response) throws Exception {
        try {
            iEquipmentCheckInfoService.exportCheckInfoList(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
