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

import cn.estsh.i3plus.ext.mes.api.base.IProcessCheckService;
import cn.estsh.i3plus.ext.mes.api.base.IQManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessCheck;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessQManage;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesQManageTaskDetail;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
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.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 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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/03
 * @Description:
 **/
@RestController
@Api(tags = "工序点检项目")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/process-check-data")
public class ProcessCheckController {
    public static final Logger LOGGER = LoggerFactory.getLogger(ProcessCheckController.class);

    @Autowired
    private IProcessCheckService processCheckService;

    @PostMapping(value = "/process-check/insert")
    @ApiOperation(value = "新增工序点检项目", notes = "新增工序点检项目")
    public ResultBean insertProcessCheck(@RequestBody MesProcessCheck processCheck) {
        try {
            processCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            processCheckService.insertProcessCheck(processCheck);
            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);
        }
    }

    @PutMapping(value = "/process-check/update")
    @ApiOperation(value = "更新工序点检项目", notes = "更新工序点检项目")
    public ResultBean updateProcessCheck(@RequestBody MesProcessCheck processCheck) {
        try {

            if (processCheck.getId() == null || processCheck.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            processCheckService.updateProcessCheck(processCheck);
            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 = "/process-check/query")
    @ApiOperation(value = "按条件分页查询工序点检项目", notes = "按条件分页查询工序点检项目")
    public ResultBean queryProcessCheckByPager(MesProcessCheck processCheck, Pager pager) {
        try {
            processCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setListPager(processCheckService.queryProcessCheckByPager(processCheck,pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/process-check/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateProcessCheckValidByIds(String[] idsStr, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            processCheckService.updateProcessCheckValidByIds(idsStr,status);
            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 = "/process-check/status-update")
    @ApiOperation(value = "禁用/启用详情", notes = "禁用/启用详情")
    public ResultBean updateProcessCheckValidById(Long id, int status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            processCheckService.updateProcessCheckValidById(id, status, 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);
        }
    }

}
