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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeAndProcessBindService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeAndProcessBind;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.StringTool;
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.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.*;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES+"/employee-and-process-bind")
@Api(tags = "人员与工序关系")
public class MesEmployeeAndProcessBindController extends MesBaseController {

    @Autowired
    private IMesEmployeeAndProcessBindService employeeAndProcessBindService;

    @GetMapping(value = "/query-list-page")
    @ApiOperation(value = "人员与工序关系分页查询")
    public ResultBean getEmployeeAndProcessBindPage(MesEmployeeAndProcessBind employeeAndProcessBind, Pager pager) {
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            //请求参数非空
            if (null == employeeAndProcessBind || null == pager) {
                return ResultBean.fail("请求参数不完整");
            }

            ResultBean resultBean = new ResultBean(true);
            return ResultBean.success("人员与工序关系分页查询")
                    .setListPager(employeeAndProcessBindService.getemployeeAndProcessBindPage(employeeAndProcessBind, pager, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增人员与工序关系")
    public ResultBean insertEmployeeAndProcessBind(MesEmployeeAndProcessBind employeeAndProcessBind) {
        String userName = AuthUtil.getSessionUser().getUserName();
        SessionUser sessionUser = AuthUtil.getSessionUser();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            /*ValidatorBean.beginValid(mesWorkOrder)
                    .notNull("id", mesWorkOrder.getId());*/
            employeeAndProcessBindService.insertEmployeeAndProcessBind(employeeAndProcessBind, organizeCode, userName);
            return new ResultBean(true).setMsg("新增成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "更新人员与工序关系")
    public ResultBean updateEmployeeAndProcessBind(MesEmployeeAndProcessBind employeeAndProcessBind) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(employeeAndProcessBind)
                    .notNull("id", employeeAndProcessBind.getId());
            employeeAndProcessBindService.updateEmployeeAndProcessBind(employeeAndProcessBind, organizeCode, userName);
            return new ResultBean(true).setMsg("更新成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update-status")
    @ApiOperation(value = "根据id批量禁用/启用人员与工序关系", notes = "根据id批量禁用/启用枚举人员与工序关系")
    public ResultBean updateWmsDockValidStatusByIds(String[] idsStr, int status) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(idsStr, "idsStr不能为空");
            //数组验证
            idsStr = ConvertBean.modelSafeArrayNumber(idsStr, true);

            if (idsStr.length == 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.WMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            employeeAndProcessBindService.updateEmployeeAndProcessBindValidStatusByIds(StringTool.getArrayLong(idsStr), status, AuthUtil.getSessionUser().getUserName());
            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);
        }
    }

}
