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

import cn.estsh.i3plus.ext.mes.api.base.jx.ISxWorkCellTaktCollectPlcCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxWorkCellTaktCollectPlcCfg;
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.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xinwang.yi
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "松下工位节拍PLC配置 控制层")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/sx" + "/work-cell-takt-collect-plc-cfg")
public class SxWorkCellTaktCollectPlcCfgController {
    public static final Logger LOGGER = LoggerFactory.getLogger(SxWorkCellTaktCollectPlcCfgController.class);

    @Autowired
    private ISxWorkCellTaktCollectPlcCfgService workCellTaktCollectPlcCfgService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加MES_松下工位节拍PLC配置信息")
    public ResultBean insertSxWorkCellTaktCollectPlcCfg(SxWorkCellTaktCollectPlcCfg workCellTaktCollectPlcCfg) {
        try {
            //条件验证
            ValidatorBean.beginValid(workCellTaktCollectPlcCfg)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE,workCellTaktCollectPlcCfg.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE,workCellTaktCollectPlcCfg.getWorkCellCode())
                    .notNull(MesExtConstWords.PLC_CODE, workCellTaktCollectPlcCfg.getPlcCode())
                    .notNull(MesExtConstWords.STATUS, workCellTaktCollectPlcCfg.getStatus());
            workCellTaktCollectPlcCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            workCellTaktCollectPlcCfgService.insertSxWorkCellTaktCollectPlcCfg(workCellTaktCollectPlcCfg);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新MES_松下工位节拍PLC配置项目")
    public ResultBean updateSxWorkCellTaktCollectPlcCfg(SxWorkCellTaktCollectPlcCfg workCellTaktCollectPlcCfg) {
        try {
            //条件验证
            ValidatorBean.beginValid(workCellTaktCollectPlcCfg)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE,workCellTaktCollectPlcCfg.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE,workCellTaktCollectPlcCfg.getWorkCellCode())
                    .notNull(MesExtConstWords.PLC_CODE, workCellTaktCollectPlcCfg.getPlcCode())
                    .notNull(MesExtConstWords.STATUS, workCellTaktCollectPlcCfg.getStatus());

            if (workCellTaktCollectPlcCfg.getId() == null || workCellTaktCollectPlcCfg.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            workCellTaktCollectPlcCfgService.updateSxWorkCellTaktCollectPlcCfg(workCellTaktCollectPlcCfg, 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 = "/query")
    @ApiOperation(value = "按条件分页查询MES_松下工位节拍PLC配置项目信息", notes = "按条件分页查询MES_松下工位节拍PLC配置项目信息")
    public ResultBean querySxWorkCellTaktCollectPlcCfgByPager(SxWorkCellTaktCollectPlcCfg workCellTaktCollectPlcCfg, Pager pager) {
        try {
            workCellTaktCollectPlcCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(workCellTaktCollectPlcCfgService.querySxWorkCellTaktCollectPlcCfgByPager(workCellTaktCollectPlcCfg, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update-valid-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateSxWorkCellTaktCollectPlcCfgValid(String[] ids, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(ids, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            workCellTaktCollectPlcCfgService.updateSxWorkCellTaktCollectPlcCfgValid(ids, status, user.getUserName());
            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 = "/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateSxWorkCellTaktCollectPlcCfgStatus(String[] ids, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(ids, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            workCellTaktCollectPlcCfgService.updateSxWorkCellTaktCollectPlcCfgStatus(ids, status, user.getUserName());
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean deleteWeaklySxWorkCellTaktCollectPlcCfgByIds(String[] ids) {
        try {
            ConvertBean.modelSafeArrayNumber(ids, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            workCellTaktCollectPlcCfgService.deleteWeaklySxWorkCellTaktCollectPlcCfgByIds(ids, user.getUserName());
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
