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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxEsopCenterActionService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxEsopCenterRemainCfgService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEsopReadRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu3.SxEsopCenterDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesEsopReadRecordRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/7/12 9:59
 **/
@Slf4j
public class SxEsopCenterActionWatchService implements ISxEsopCenterActionService {

    private ISxEsopCenterActionService actionService;

    public SxEsopCenterActionWatchService(ISxEsopCenterActionService actionService) {
        this.actionService = actionService;
    }

    @Override
    public SxEsopCenterDataModel exec(SxEsopCenterDataModel data) {

        actionService.check(data);

        actionService.exec(data);

        return execWatch(data);

    }

    private SxEsopCenterDataModel execWatch(SxEsopCenterDataModel data) {
        if (StringUtils.isEmpty(data.getWorkOrderNo())) return data.msg(MesPcnExtConstWords.ONE_SPACE);

        ISxEsopCenterRemainCfgService esopCenterRemainCfgService = (ISxEsopCenterRemainCfgService) SpringContextsUtil.getBean("sxEsopCenterRemainCfgService");
        String cachedReadOrder = esopCenterRemainCfgService.getCachedReadRecordByEsopCenter(data.getOrganizeCode(), data.getWorkCenterCode());

        Boolean flag = true;
        if (StringUtils.isEmpty(cachedReadOrder) || !cachedReadOrder.equals(data.getWorkOrderNo())) {
            flag = checkIsExistEsopReadRecord(data);
            if (flag) esopCenterRemainCfgService.doCachedReadRecordByEsopCenter(data.getOrganizeCode(), data.getWorkCenterCode(), data.getWorkOrderNo());
        }
        return data.msg(flag ? "已确认查阅" : "请确认查阅");
    }

    private Boolean checkIsExistEsopReadRecord(SxEsopCenterDataModel data) {
        MesEsopReadRecordRepository repository = (MesEsopReadRecordRepository) SpringContextsUtil.getBean("mesEsopReadRecordRepository");
        MesEsopReadRecord esopReadRecord = repository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.PROCESS_CODE},
                new Object[]{data.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        data.getWorkOrderNo(), data.getWorkCenterCode(), data.getWorkCellCode(), data.getProcessCode()});
        return null != esopReadRecord ? true : false;
    }

}
