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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxSubassemblyRemadeService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxSubassemblyRemadeModel;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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 + "/ext")
@Api(tags = "组件返工")
public class SxSubassemblyRemadeController extends MesPcnBaseController {

    @Autowired
    private ISxSubassemblyRemadeService subassemblyRemadeService;

    @PostMapping("/subassembly-remade/query")
    @ApiOperation(value = "组件返工查询")
    public ResultBean querySubassemblyRemadeData(@RequestBody SxSubassemblyRemadeModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(subassemblyRemadeService.querySubassemblyRemadeData(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/subassembly-remade/reset")
    @ApiOperation(value = "组件返工重置")
    public ResultBean resetSubassemblyRemadeData(@RequestBody SxSubassemblyRemadeModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(subassemblyRemadeService.resetSubassemblyRemadeData(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/subassembly-remade/scan")
    @ApiOperation(value = "组件返工扫描")
    public ResultBean doScanSubassemblyRemadeSn(@RequestBody SxSubassemblyRemadeModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.SN, model.getSn())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("扫描成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(subassemblyRemadeService.doScanSubassemblyRemadeSn(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/subassembly-remade/update")
    @ApiOperation(value = "修改列表条码")
    public ResultBean updateSubassemblyRemadeSn(@RequestBody SxSubassemblyRemadeModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.SN, model.getSn())
                    .notNull(MesPcnExtConstWords.QTY, model.getQty())
                    .notNull(MesPcnExtConstWords.ACTION_TYPE, model.getActionType())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(subassemblyRemadeService.updateSubassemblyRemadeSn(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/subassembly-remade/insert")
    @ApiOperation(value = "提交列表条码")
    public ResultBean insertSubassemblyRemadeSn(@RequestBody SxSubassemblyRemadeModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("提交成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(subassemblyRemadeService.insertSubassemblyRemadeSn(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
