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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxWeighManageService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WeighManageInfoDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WeighManageInfoModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WeighManageWorkOrderModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN)
@Api(tags = "称重管理作业")
public class SxWeighManageController extends MesPcnBaseController {

    @Autowired
    private ISxWeighManageService sxWeighManageService;

    @GetMapping(value = "/sx-weigh-manage/center-list/query")
    @ApiOperation(value = "查询生产线下拉框数据")
    public ResultBean queryCenterList(WeighManageWorkOrderModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(sxWeighManageService.queryCenterList(model.getOrganizeCode()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-weigh-manage/work-order-list/query")
    @ApiOperation(value = "查询启动工单列表数据,计划开始时间倒序")
    public ResultBean queryWorkOrderList(WeighManageWorkOrderModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(sxWeighManageService.queryWorkOrderList(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-weigh-manage/weigh-order-list/query")
    @ApiOperation(value = "查询称重单据列表数据,创建时间倒序")
    public ResultBean queryWeighManageOrderList(WeighManageWorkOrderModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(sxWeighManageService.queryWeighManageOrderList(model.getOrganizeCode(), model.getWorkOrderNo()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-weigh-manage/weigh-order-info/query")
    @ApiOperation(value = "查询称重单据信息,右边列表创建时间倒序")
    public ResultBean queryWeighManageOrderInfo(WeighManageInfoModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.ID, model.getId())
                    .notNull(MesPcnExtConstWords.ORDER_JOB_NO, model.getOrderJobNo());
            WeighManageInfoModel result = sxWeighManageService.queryWeighManageOrderInfo(model.getId());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(result).setResultList(sxWeighManageService.queryWeighManageOrderInfoDetail(model.getOrganizeCode(), model.getOrderJobNo(), result));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-weigh-manage/weigh-order/insert")
    @ApiOperation(value = "创建称重单据信息")
    public ResultBean insertWeighManageOrder(WeighManageWorkOrderModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.PART_NO, model.getPartNo())
                    .notNull(MesPcnExtConstWords.PART_NAME_RDD, model.getPartNameRdd())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
            sxWeighManageService.insertWeighManageOrder(model);
            return ResultBean.success("创建成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-weigh-manage/weigh-order-detail/insert")
    @ApiOperation(value = "创建称重单据信息明细")
    public ResultBean insertWeighManageOrderDetail(WeighManageInfoDetailModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.ID, model.getId())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.PACKAGENO, model.getPackageNo())
                    .notNull(MesPcnExtConstWords.REAL_WEIGH, model.getRealWeigh());
            sxWeighManageService.insertWeighManageOrderDetail(model);
            return ResultBean.success("确认成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-weigh-manage/weigh-order-carton/update")
    @ApiOperation(value = "确认纸箱")
    public ResultBean updateWeighManageOrderCarton(WeighManageInfoModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.ID, model.getId())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo());
            sxWeighManageService.updateWeighManageOrderCarton(model);
            return ResultBean.success("确认成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
