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

import cn.estsh.i3plus.ext.mes.api.base.ISxContainerService;
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.i3plus.pojo.mes.bean.MesContainer;
import cn.estsh.i3plus.pojo.mes.bean.MesContainerType;
import cn.estsh.i3plus.pojo.mes.bean.MesPartContainerCapacity;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

    @Autowired
    private ISxContainerService sxContainerService;

    //---------------------------容器类型信息---------------------------------

    @GetMapping(value = "/container-type-ext/query")
    @ApiOperation(value = "按条件分页查询容器类型信息")
    public ResultBean queryContainerTypeByPager(MesContainerType containerType, Pager pager) {
        try {
            containerType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(sxContainerService.queryContainerTypeByPager(containerType, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/container-type-ext/insert")
    @ApiOperation(value = "新增容器类型信息")
    public ResultBean insertContainerType(MesContainerType containerType) {
        try {
            ValidatorBean.beginValid(containerType)
                    .notNull(MesExtConstWords.CT_CODE, containerType.getCtCode())
                    .notNull(MesExtConstWords.USE_LIMIT, containerType.getUseLimit())
                    .notNull(MesExtConstWords.CT_NAME, containerType.getCtName());

            containerType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            containerType.setCreateUser(AuthUtil.getSessionUser().getUserName());
            sxContainerService.insertContainerType(containerType);

            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-type-ext/update")
    @ApiOperation(value = "更新容器类型信息")
    public ResultBean updateContainerType(MesContainerType containerType) {
        try {
            ValidatorBean.beginValid(containerType)
                    .notNull(MesExtConstWords.ID, containerType.getId())
                    .notNull(MesExtConstWords.CT_CODE, containerType.getCtCode())
                    .notNull(MesExtConstWords.USE_LIMIT, containerType.getUseLimit())
                    .notNull(MesExtConstWords.CT_NAME, containerType.getCtName());

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

    @PutMapping(value = "/container-type-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用容器类型信息")
    public ResultBean updateContainerTypeValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            sxContainerService.updateContainerTypeValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), 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);
        }
    }

    //---------------------------容器信息---------------------------------

    @GetMapping(value = "/container-ext/query")
    @ApiOperation(value = "按条件分页查询容器信息")
    public ResultBean queryContainerByPager(MesContainer container, Pager pager) {
        try {
            container.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(sxContainerService.queryContainerByPager(container, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @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())
                    .notNull(MesExtConstWords.USE_STATUS, container.getUseStatus());

            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())
                    .notNull(MesExtConstWords.USE_STATUS, container.getUseStatus());

            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);
        }
    }

    @PutMapping(value = "/container-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用容器信息")
    public ResultBean updateContainerValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            sxContainerService.updateContainerValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), 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);
        }
    }

    //---------------------------产品容器容量配置---------------------------------

    @GetMapping(value = "/part-container-capacity-ext/query")
    @ApiOperation(value = "按条件分页查询产品容器容量配置信息")
    public ResultBean queryPartContainerCapacityByPager(MesPartContainerCapacity partContainerCapacity, Pager pager) {
        try {
            partContainerCapacity.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(sxContainerService.queryPartContainerCapacityByPager(partContainerCapacity, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/part-container-capacity-ext/insert")
    @ApiOperation(value = "新增产品容器容量配置信息")
    public ResultBean insertPartContainerCapacity(MesPartContainerCapacity partContainerCapacity) {
        try {
            ValidatorBean.beginValid(partContainerCapacity)
                    .notNull(MesExtConstWords.PART_NO, partContainerCapacity.getPartNo())
                    .notNull(MesExtConstWords.CT_CODE, partContainerCapacity.getCtCode())
                    .notNull(MesExtConstWords.CT_QTY, partContainerCapacity.getCtQty());

            partContainerCapacity.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            partContainerCapacity.setCreateUser(AuthUtil.getSessionUser().getUserName());
            sxContainerService.insertPartContainerCapacity(partContainerCapacity);

            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 = "/part-container-capacity-ext/update")
    @ApiOperation(value = "更新产品容器容量配置信息")
    public ResultBean updatePartContainerCapacity(MesPartContainerCapacity partContainerCapacity) {
        try {
            ValidatorBean.beginValid(partContainerCapacity)
                    .notNull(MesExtConstWords.ID, partContainerCapacity.getId())
                    .notNull(MesExtConstWords.PART_NO, partContainerCapacity.getPartNo())
                    .notNull(MesExtConstWords.CT_CODE, partContainerCapacity.getCtCode())
                    .notNull(MesExtConstWords.CT_QTY, partContainerCapacity.getCtQty());

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

    @PutMapping(value = "/part-container-capacity-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用产品容器容量配置信息")
    public ResultBean updatePartContainerCapacityValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            sxContainerService.updatePartContainerCapacityValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), 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);
        }
    }
}
