package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi.spot;


import cn.estsh.i3plus.ext.mes.pcn.api.busi.spot.IMesSpotCheckOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesSpotCheckOrderModel;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentSpotCheck;
import cn.estsh.i3plus.pojo.mes.bean.MesSpotCheckOrder;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "点检单页面")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/spot-check-order")
@Slf4j
public class MesSpotCheckOrderController {

    @Autowired
    private IMesSpotCheckOrderService spotCheckOrderService;

    @GetMapping("/find-equipment-spot")
    @ApiOperation(value = "查询设备点检")
    public ResultBean findEquipmentSpotCheck(MesEquipmentSpotCheck spotCheckOrder) {
        try {
            spotCheckOrder.setOrganizeCode(!StringUtils.isEmpty(spotCheckOrder.getOrganizeCode())?spotCheckOrder.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("查询成功!").setResultList(spotCheckOrderService.findEquipmentSpotCheck(spotCheckOrder));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping("/query-equipment-spot")
    @ApiOperation(value = "查询设备点检")
    public ResultBean queryEquipmentSpotCheck(MesEquipmentSpotCheck spotCheckOrder) {

        try {

            spotCheckOrder.setOrganizeCode(!StringUtils.isEmpty(spotCheckOrder.getOrganizeCode())?spotCheckOrder.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("查询成功!").setResultList(spotCheckOrderService.queryEquipmentSpotCheck(spotCheckOrder));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增点检单")
    public ResultBean insert(@RequestBody MesSpotCheckOrder bean) {

        try {

            if (StringUtils.isEmpty(bean)) {
                throw new ImppBusiException("点检单不能为空");
            }
            bean.setOrganizeCode(!StringUtils.isEmpty(bean.getOrganizeCode())?bean.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("扫描产品条码成功!").setResultObject(spotCheckOrderService.insert(bean));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-pager")
    @ApiOperation(value = "查询点检单")
    public ResultBean querySpotCheckOrderByPager(MesSpotCheckOrder spotCheckOrder, Pager pager) {

        try {

            spotCheckOrder.setOrganizeCode(!StringUtils.isEmpty(spotCheckOrder.getOrganizeCode())?spotCheckOrder.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("查询成功!").setListPager(spotCheckOrderService.querySpotCheckOrder(spotCheckOrder, pager));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-result")
    @ApiOperation(value = "查询点检单明细")
    public ResultBean queryWorkOrderByPager(MesSpotCheckOrder spotCheckOrder) {

        try {
            spotCheckOrder.setOrganizeCode(!StringUtils.isEmpty(spotCheckOrder.getOrganizeCode())?spotCheckOrder.getOrganizeCode():AuthUtil.getOrganizeCode());

            return ResultBean.success("查询成功!").setResultObject(spotCheckOrderService.querySpotCheckOrderResult(spotCheckOrder));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/scan-produce")
    @ApiOperation(value = "扫描产品条码")
    public ResultBean scanProduceSn(@RequestBody MesSpotCheckOrderModel model) {

        try {

            if (StringUtils.isEmpty(model.getSpotCheckOrder()) || StringUtil.isEmpty(model.getSpotCheckOrder().getId())) {
                throw new ImppBusiException("点检单不能为空");
            }
            if (StringUtils.isEmpty(model.getProduceSn())) {
                throw new ImppBusiException("扫描条码不能为空");
            }
            if (CollectionUtils.isEmpty(model.getOrderPartList())) {
                throw new ImppBusiException("点检明细零件不能为空");
            }
            return ResultBean.success("扫描产品条码成功!").setResultObject(spotCheckOrderService.scanProduceSn(model, !StringUtils.isEmpty(model.getOrganizeCode())?model.getOrganizeCode():AuthUtil.getOrganizeCode()));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("/query-equip-info")
    @ApiOperation(value = "开始点检-查询设备数据")
    public ResultBean queryEquipInfo(@RequestBody MesSpotCheckOrderModel model) {

        try {

            if (StringUtils.isEmpty(model.getSpotCheckOrder())|| StringUtil.isEmpty(model.getSpotCheckOrder().getId())) {
                throw new ImppBusiException("点检单不能为空");
            }
            if (CollectionUtils.isEmpty(model.getOrderResultList())) {
                throw new ImppBusiException("点检单明细不能为空");
            }
            return ResultBean.success("点检结果获取成功!").setResultObject(spotCheckOrderService.queryEquipInfo(model, !StringUtils.isEmpty(model.getOrganizeCode())?model.getOrganizeCode():AuthUtil.getOrganizeCode()));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/save-check")
    @ApiOperation(value = "完成点检")
    public ResultBean saveQualityCheck(@RequestBody MesSpotCheckOrderModel model) {

        try {

            if (StringUtils.isEmpty(model.getSpotCheckOrder()) || StringUtil.isEmpty(model.getSpotCheckOrder().getId())) {
                throw new ImppBusiException("点检单不能为空");
            }

            if (CollectionUtils.isEmpty(model.getOrderResultList())) {
                throw new ImppBusiException("点检单明细不能为空");
            }

            spotCheckOrderService.saveCheck(model, !StringUtils.isEmpty(model.getOrganizeCode())?model.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("完成点检成功!");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/re-check")
    @ApiOperation(value = "重新点检")
    public ResultBean saveReCheck(@RequestBody MesSpotCheckOrderModel model) {

        try {

            if (StringUtils.isEmpty(model.getSpotCheckOrder()) || StringUtils.isEmpty(model.getSpotCheckOrder().getId())) {
                throw new ImppBusiException("点检单不能为空");
            }


            spotCheckOrderService.saveReCheck(model, !StringUtils.isEmpty(model.getOrganizeCode())?model.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("重新点检成功!");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/batch-delete-ids")
    @ApiOperation(value = "删除点检")
    public ResultBean deleteBatchIds(Long[] ids, String userName, String organizeCode) {
        try {
            if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(userName) || null == ids) {
                throw new ImppBusiException("请检查参数");
            }
            spotCheckOrderService.deleteBatchIds(ids, userName, organizeCode);
            return ResultBean.success("删除成功!");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
