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

import cn.estsh.i3plus.ext.mes.api.base.IMesProcessCellService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessCell;
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.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: xiangming.liao
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Api(tags = "工序工位关系")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/process-cell")
public class ProcessCellController extends MesBaseController {

    @Autowired
    private IMesProcessCellService mesProcessCellService;

    @GetMapping(value = "/mes-process-cell/query")
    @ApiOperation(value = "按条件分页查询工序工位对应关系信息")
    public ResultBean queryMesProcessCellByPager(MesProcessCell mesProcessCell, Pager pager) {
        try {
            return ResultBean.success("操作成功").setListPager(mesProcessCellService.queryMesProcessCellByPager(mesProcessCell, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/mes-process-cell/insert")
    @ApiOperation(value = "新增工序工位对应关系信息")
    public ResultBean insertMesProcessCell(MesProcessCell mesProcessCell) {
        try {
            //条件验证
            ValidatorBean.beginValid(mesProcessCell)
                    .notNull(MesExtConstWords.PROCESS_CODE, mesProcessCell.getProcessCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, mesProcessCell.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, mesProcessCell.getWorkCellCode());
            mesProcessCell.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ConvertBean.modelInitialize(mesProcessCell, AuthUtil.getSessionUser());
            return ResultBean.success("添加成功").setResultObject(mesProcessCellService.insertMesProcessCell(mesProcessCell))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-process-cell/update")
    @ApiOperation(value = "更新工序工位对应关系信息")
    public ResultBean updateMesProcessCell(MesProcessCell mesProcessCell) {
        try {
            ValidatorBean.beginValid(mesProcessCell)
                    .notNull(MesExtConstWords.PROCESS_CODE, mesProcessCell.getProcessCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, mesProcessCell.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, mesProcessCell.getWorkCellCode());
            return ResultBean.success("修改成功").setResultObject(mesProcessCellService.updateMesProcessCell(mesProcessCell))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 启用禁用工序工位关系
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/mes-process-cell/status-update")
    @ApiOperation(value = "启用禁用工序工位关系")
    public ResultBean updateStatusProcessCell(long id,Integer status) {
        try {
            return ResultBean.success("启用/禁用工序工位关系成功").setResultObject(mesProcessCellService.updateStatusProcessCell(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
