package cn.estsh.i3plus.ext.mes.apiservice.controller.base.bu;

import cn.estsh.i3plus.ext.mes.api.base.bu.IBuProcessBomService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessBom;
import cn.estsh.i3plus.ext.mes.pojo.model.BatchIdModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
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.*;

/**
 * @Author: xinwang.yi
 * @CreateDate: 2022/09/30 14:06
 * @Description:
 **/
@RestController
@Api(tags = "BU工序BOM接口")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/bu/process-bom")
public class BuProcessBomController extends MesBaseController {
    @Autowired
    private IBuProcessBomService processBomService;

    /**
     * 新增工序BOM
     *
     * @param processBom
     * @return
     */
    @PostMapping(value = "/mes-process-bom/insert")
    @ApiOperation(value = "新增工序BOM")
    public ResultBean insertProcessBom(MesProcessBom processBom) {
        try {
            //条件验证
            ValidatorBean.beginValid(processBom)
                    .notNull(MesExtConstWords.PART_NO, processBom.getPartNo())
                    .notNull(MesExtConstWords.ITEM_PART_NO, processBom.getItemPartNo())
                    .notNull(MesExtConstWords.PROCESS_CODE, processBom.getProcessCode())
                    .notNull(MesExtConstWords.QTY, processBom.getQty())
                    .notNull(MesExtConstWords.IS_BIND_KEY, processBom.getIsBindKey());
            return ResultBean.success("新增工序BOM成功").setResultObject(processBomService.insertProcessBom(processBom))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-process-bom/batch/status-update")
    @ApiOperation(value = "启用禁用工序BOM")
    public ResultBean updateStatusBatchProcessBom(@RequestBody BatchIdModel model) {
        try {
            if (null == model || null == model.getIds()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            processBomService.updateStatusBatchProcessBom(model.getIds(), model.getStatus(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
    /**
     * 修改工序BOM
     *
     * @param processBom
     * @return
     */
    @PutMapping(value = "/mes-process-bom/update")
    @ApiOperation(value = "修改工序BOM")
    public ResultBean updateProcessBom(MesProcessBom processBom) {
        try {
            //条件验证
            ValidatorBean.beginValid(processBom)
                    .notNull(MesExtConstWords.PART_NO, processBom.getPartNo())
                    .notNull(MesExtConstWords.ITEM_PART_NO, processBom.getItemPartNo())
                    .notNull(MesExtConstWords.PROCESS_CODE, processBom.getProcessCode())
                    .notNull(MesExtConstWords.QTY, processBom.getQty())
                    .notNull(MesExtConstWords.IS_BIND_KEY, processBom.getIsBindKey());
            return ResultBean.success("修改工序BOM成功").setResultObject(processBomService.updateProcessBom(processBom))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询工序BOM分页
     *
     * @param processBom
     * @param pager
     * @return
     */
    @GetMapping(value = "/mes-process-bom/query")
    @ApiOperation(value = "查询工序BOM分页")
    public ResultBean queryProcessBom(MesProcessBom processBom, Pager pager) {
        try {
            return ResultBean.success("查询工序BOM成功").setListPager(processBomService.queryProcessBomByPager(processBom, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
