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

import cn.estsh.i3plus.ext.mes.api.base.IProdOrgExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAreaExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pojo.model.SxProdOrgModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
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: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "组织模型扩展控制层")
public class ProdOrgExtController extends MesBaseController {

    @Autowired
    private IProdOrgExtService prodOrgExtService;

    @PostMapping(value = "/prod-org-ext/insert")
    @ApiOperation(value = "新增mes组织模型信息(区域，生产线，工位)")
    public ResultBean insertProdOrg(SxProdOrgModel prodOrgModel) {
        try {
            prodOrgModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ConvertBean.modelInitialize(prodOrgModel, AuthUtil.getSessionUser());
            prodOrgExtService.insertProdOrg(prodOrgModel);
            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 = "/prod-org-ext/area-ext/update")
    @ApiOperation(value = "更新区域信息")
    public ResultBean updateAreaExt(MesAreaExt areaExt) {
        try {
            ValidatorBean.beginValid(areaExt)
                    .notNull(MesExtConstWords.ID, areaExt.getId())
                    .notNull(MesExtConstWords.AREA_CODE, areaExt.getAreaCode())
                    .notNull(MesExtConstWords.AREA_NAME, areaExt.getAreaName())
                    .notNull(MesExtConstWords.CATEGORY_CODE3, areaExt.getCategoryCode3());
            areaExt.setModifyUser(AuthUtil.getSessionUser().getUserName());
            prodOrgExtService.updateAreaExt(areaExt);
            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 = "/prod-org-ext/work-center-ext/update")
    @ApiOperation(value = "更新生产线信息")
    public ResultBean updateWorkCenterExt(MesWorkCenterExt workCenterExt) {
        try {
            ValidatorBean.beginValid(workCenterExt)
                    .notNull(MesExtConstWords.ID, workCenterExt.getId())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, workCenterExt.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CENTER_NAME, workCenterExt.getWorkCenterName())
                    .notNull(MesExtConstWords.AREA_CODE, workCenterExt.getAreaCode());
            workCenterExt.setModifyUser(AuthUtil.getSessionUser().getUserName());
            prodOrgExtService.updateWorkCenterExt(workCenterExt);
            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 = "/work-center-ext/is-dont/query-list")
    @ApiOperation(value = "查询生产线系别信息集合")
    public ResultBean queryWorkCenterIsDontList() {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(prodOrgExtService.queryWorkCenterIsDontList(AuthUtil.getOrganize().getOrganizeCode()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/work-center-ext/dont-class/query-list")
    @ApiOperation(value = "查询生产线班别信息集合")
    public ResultBean queryWorkCenterDontClassList() {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(prodOrgExtService.queryWorkCenterDontClassList(AuthUtil.getOrganize().getOrganizeCode()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/work-center-ext/query-all-list")
    @ApiOperation(value = "查询所有生产线信息集合")
    public ResultBean queryWorkCenterExtList(String organizeCode) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(prodOrgExtService.queryWorkCenterExtList(organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/prod-org-ext/mes-cell/query")
    @ApiOperation(value = "分页查询工位")
    public ResultBean queryMesWorkCellByPager(MesWorkCell workCell, Pager pager) {
        try {
            workCell.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager<MesWorkCell> workCellList = prodOrgExtService.queryWorkCellByPager(workCell, pager);
            return ResultBean.success("操作成功").setListPager(workCellList)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
