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

import cn.estsh.i3plus.ext.mes.api.base.ISxContainerService;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesContainer;
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.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/bu")
@Api(tags = "容器控制层")
public class BuContainerController extends MesBaseController {

    //---------------------------容器信息---------------------------------
    @Autowired
    @Qualifier("buContainerService")
    private ISxContainerService sxContainerService;
    @PostMapping(value = "/container-ext/insert")
    @ApiOperation(value = "新增容器信息")
    public ResultBean insertContainer(MesContainer container) {
        try {
            ValidatorBean.beginValid(container)
                    .notNull(MesExtConstWords.CT_NO, container.getCtNo())
                    .notNull(MesExtConstWords.CT_CODE, container.getCtCode());

            container.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            container.setCreateUser(AuthUtil.getSessionUser().getUserName());
            sxContainerService.insertContainer(container);

            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 = "/container-ext/update")
    @ApiOperation(value = "更新容器信息")
    public ResultBean updateContainer(MesContainer container) {
        try {
            ValidatorBean.beginValid(container)
                    .notNull(MesExtConstWords.ID, container.getId())
                    .notNull(MesExtConstWords.CT_NO, container.getCtNo())
                    .notNull(MesExtConstWords.CT_CODE, container.getCtCode());

            container.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            container.setModifyUser(AuthUtil.getSessionUser().getUserName());
            sxContainerService.updateContainer(container);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
