package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi.bu3;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.IBuPersonCheckDetailService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPersonCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkLicense;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Api(tags = "人员点检")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/bu/process-check-data")
public class BuPersonCheckController extends MesPcnBaseController {
    public static final Logger LOGGER = LoggerFactory.getLogger(BuPersonCheckController.class);

    @Autowired
    private IBuPersonCheckDetailService personCheckDetailService;

    /**
     * 工位录入作业人员
     *
     * @param personCheckDetail
     * @return
     */
    @PutMapping(value = "task-user/update")
    @ApiOperation(value = "工位录入作业人员")
    public ResultBean updateTaskUserByCell(MesPersonCheckDetail personCheckDetail) {
        try {
            personCheckDetailService.updateTaskUserByCell(personCheckDetail);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据上岗证编码获取作业人姓名
     *
     * @param personCheckDetail
     * @return
     */
    @GetMapping(value = "task-user/get")
    @ApiOperation(value = "根据上岗证编码获取作业人姓名")
    public ResultBean getTaskUserByWorkLicense(MesPersonCheckDetail personCheckDetail) {
        try {
            String code = ResourceEnumUtil.MESSAGE.SUCCESS.getCode();
            MesWorkLicense taskUserByWorkLicense = personCheckDetailService.getTaskUserByWorkLicenseCode(personCheckDetail);
            String remindMsg = personCheckDetailService.checkWorkLicenseValidityDate(personCheckDetail.getWorkLicenseCode(), taskUserByWorkLicense);
            if(!StringUtils.isEmpty(remindMsg)){
                // 黄色背景颜色提醒临时code
                code = "20004";
            }else{
                remindMsg = "查询成功";
            }
            return ResultBean.success(remindMsg).setCode(code).setResultObject(taskUserByWorkLicense);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
