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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxBuWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesUnbindProduceSn;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.RequestModel;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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 cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/12 14:39
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/bu/work-order")
@Api(tags = "松下BU生产工单")
public class SxBuWorkOrderExtController extends MesPcnBaseController {

    @Autowired
    private ISxBuWorkOrderExtService workOrderExtService;

    @PutMapping(value = "/update-order-status")
    @ApiOperation(value = "生产工单状态批量调整")
    public ResultBean updateWorkOrderStatus(@RequestBody RequestModel requestModel) {
        try {
            List<String> orderNoList = null;
            Integer workOrderStatus = null;
            if (null != requestModel) {
                orderNoList = requestModel.getOrderNoList();
                workOrderStatus = requestModel.getDestStatus();
            }
            // 数据校验
            ValidatorBean.checkNotNull(workOrderStatus, "工单状态不能为空");
            ValidatorBean.checkNotNull(orderNoList, "工单号不能为空");

            workOrderExtService.updateWorkOrderStatus(workOrderStatus, orderNoList);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/unbind-sn-query-pager")
    @ApiOperation(value = "分页查询解绑条码")
    public ResultBean queryUnbindSnByPager(MesUnbindProduceSn produceSn, Pager pager) {
        try {
            produceSn.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            workOrderExtService.queryMesUnbindSnByPager(produceSn, pager);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
