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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxDailyCheckConfirmRecordDataService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxDailyCheckConfirmRecordService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesDailyCheckConfirmRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxDailyCheckConfirmRecordDataModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxDailyCheckConfirmRecordModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesDailyCheckConfirmRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Service
public class SxDailyCheckConfirmRecordService implements ISxDailyCheckConfirmRecordService {

    @Autowired
    private MesDailyCheckConfirmRecordRepository dailyCheckConfirmRecordRepository;

    @Override
    public List<MesDailyCheckConfirmRecord> confirmList(SxDailyCheckConfirmRecordModel data) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(data.getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(data.getOrderType(), MesExtConstWords.ORDER_TYPE, packBean);
        DdlPreparedPack.getStringEqualPack(data.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(data.getWorkTime(), MesExtConstWords.WORK_TIME, packBean);
        DdlPreparedPack.getStringEqualPack(data.getShiftCode(), MesExtConstWords.SHIFT_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(data.getEquNo(), MesExtConstWords.EQU_NO, packBean);
        DdlPreparedPack.getStringEqualPack(data.getLevel(), MesExtConstWords.LEVEL, packBean);
        return dailyCheckConfirmRecordRepository.findByHqlWhere(packBean);
    }

    @Override
    public void insertConfirm(SxDailyCheckConfirmRecordModel data) {
        MesDailyCheckConfirmRecord confirmRecord = new MesDailyCheckConfirmRecord();
        BeanUtils.copyProperties(data, confirmRecord);
        confirmRecord.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(confirmRecord, data.getUserInfo());
        confirmRecord.setConfirmDatetime(confirmRecord.getCreateDatetime());
        dailyCheckConfirmRecordRepository.insert(confirmRecord);
    }

    @Override
    public SxDailyCheckConfirmRecordDataModel queryDailyCheckConfirmRecordData(SxDailyCheckConfirmRecordDataModel data) {
        log.info("MES日常点检报表确认记录报表 --- QUERY START --- DATA[{}] --- THREAD:[{}]", JSONObject.toJSONString(data), Thread.currentThread().getName());
        return ((ISxDailyCheckConfirmRecordDataService) SpringContextsUtil.getBean(STRATEGY_CLASS.getClazzByCode(data.getOrderType()))).query(data);
    }

    @Override
    public void doConfirmDailyCheckConfirmRecordData(SxDailyCheckConfirmRecordModel data) {
        ((ISxDailyCheckConfirmRecordDataService) SpringContextsUtil.getBean(STRATEGY_CLASS.getClazzByCode(data.getOrderType()))).doConfirm(data);
    }

    @Override
    public void doDownLoadDailyCheckConfirmRecordData(HttpServletResponse resp, SxDailyCheckConfirmRecordDataModel data) {
        log.info("MES日常点检报表确认记录报表 --- DOWNLOAD START --- DATA[{}] --- THREAD:[{}]", JSONObject.toJSONString(data), Thread.currentThread().getName());
        ((ISxDailyCheckConfirmRecordDataService) SpringContextsUtil.getBean(STRATEGY_CLASS.getClazzByCode(data.getOrderType()))).doDownLoad(resp, data);
    }

    enum STRATEGY_CLASS {

        EQU_CHECK(40, "sxDailyCheckConfirmRecordDataEquCheckService", "设备点检");

        private Integer code;
        private String clazz;
        private String desc;

        STRATEGY_CLASS(Integer code, String clazz, String desc) {
            this.code = code;
            this.clazz = clazz;
            this.desc = desc;
        }

        public static String getClazzByCode(Integer code) {
            String tmp = null;
            for (int i = 0; i < values().length; i++) {
                if (values()[i].code == code) {
                    tmp = values()[i].clazz;
                }
            }
            return tmp;
        }
    }
}
