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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMissingPartService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesMissingPart;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MissingPartDataModel;
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.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN)
@Api(tags = "缺件记录")
public class MissingPartController extends MesPcnBaseController {

    @Autowired
    private IMissingPartService missingPartService;

    @PostMapping(value = "/missing-part/query")
    @ApiOperation(value = "查询缺件记录数据")
    public ResultBean queryMissingPartModelData(@RequestBody MissingPartDataModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(missingPartService.queryMissingPartModelData(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/missing-part/insert")
    @ApiOperation(value = "添加缺件记录")
    public ResultBean insertMissingPart(@RequestBody MissingPartDataModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(missingPartService.insertMissingPart(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/missing-part/delete")
    @ApiOperation(value = "删除缺件记录")
    public ResultBean deleteMissingPart(@RequestBody MissingPartDataModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(missingPartService.deleteMissingPart(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
