package com.its.pis.process;

import com.its.app.AppUtils;
import com.its.app.utils.SysUtils;
import com.its.pis.dao.mapper.PrltCtlrMapper;
import com.its.pis.dao.mapper.batch.PisInfrDao;
import com.its.pis.dao.mapper.batch.PrltCtlrDao;
import com.its.pis.entity.TbPisInfr;
import com.its.pis.entity.TbPisInfrStts;
import com.its.pis.entity.TbPrkPlceRt;
import com.its.pis.entity.TbPrkPlceStts;
import com.its.pis.websocket.message.c2f.C2FMessage;
import com.its.pis.websocket.message.c2f.PrkPlceRlTimeResponseInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DbmsJobProcess extends AbstractDbmsJobProcess {

    private final PisInfrDao pisInfrDao;
    private final PrltCtlrDao prltCtlrDao;

//    private PisInfrMapper pisInfrMapper;
    private PrltCtlrMapper prltCtlrMapper;

    @Override
    protected void postConstruct() {
//        this.pisInfrMapper = (PisInfrMapper) AppUtils.getBean(PisInfrMapper.class);
        this.prltCtlrMapper = (PrltCtlrMapper) AppUtils.getBean(PrltCtlrMapper.class);
    }

    @Override
    public void process(DbmsJobData data) {
        try {
            int type = data.getType();
            TbPisInfr pis = data.getObj();
            switch(type) {
                case DbmsJobType.DATA_TYPE_RL_TIME:
                    if (pis != null) {
                        String DATA_COLCT_TIME = SysUtils.getSysTime();
                        C2FMessage<PrkPlceRlTimeResponseInfo> rlTimeInfo = (C2FMessage<PrkPlceRlTimeResponseInfo>) data.getData();
                        if (rlTimeInfo != null) {
                            PrkPlceRlTimeResponseInfo info = rlTimeInfo.getData().getPayload();
                            TbPrkPlceRt rt = TbPrkPlceRt.toEntity(pis.getPIS_NMBR(), DATA_COLCT_TIME, info);

                            this.prltCtlrMapper.updatePrkPlceRlTime(rt);
                            this.prltCtlrDao.updateRlTimeFlr(pis.getPIS_NMBR(), DATA_COLCT_TIME, info.getFlr_info());
                            this.prltCtlrDao.updateRlTimeAr(pis.getPIS_NMBR(), info.getAr_info());
                            this.prltCtlrDao.updateRlTimePrv(pis.getPIS_NMBR(), info.getPrvuse_prkar_info());
                            this.prltCtlrDao.updateRlTimeDev(pis.getPIS_NMBR(), info.getPrk_colct_device_info());
                        }
                        else {
                            log.error("RL TIME Data NULL...............");
                        }
                    }
                    else {
                        log.error("PIS Object null");
                    }
                    break;
                case DbmsJobType.DATA_TYPE_PIS_STTS:
                    List<TbPisInfrStts> pisSttsList = (List<TbPisInfrStts>)data.getData();
                    this.pisInfrDao.updateStts(pisSttsList, data.isHistory());
                    if (data.isHistory()) {
                        //this.pisInfrDao.insertStts(pisSttsList);
                    }
                    pisSttsList.clear();
                    break;
                case DbmsJobType.DATA_TYPE_PRLT_STTS:
                    List<TbPrkPlceStts> prltSttsList = (List<TbPrkPlceStts>)data.getData();
                    this.prltCtlrDao.updateStts(prltSttsList, data.isHistory());
//                    if (data.isHistory()) {
//                        this.prltCtlrDao.insertStts(prltSttsList);
//                    }
                    prltSttsList.clear();
                    break;
                default:
                    log.error("DbmsJobProcess.process: Unknown Job Type: {}", type);
                    break;
            }
        } catch (Exception e) {
            log.error("DbmsJobProcess.process: Exception: {}", e.toString());
        }
    }

}
