package cn.service.init.impl;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import cn.component.RedisKit;
import cn.orm.abs.ISqlToyDao;
import cn.orm.sqltoy.plus.Wrappers;
import cn.orm.sqltoy.plus.query.QueryWrapper;
import cn.pojo.atv.*;
import cn.service.init.ITableToCacheService;
import cn.util.core.LogKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TableToCacheServiceImpl implements ITableToCacheService
{
    @Autowired
    protected ISqlToyDao daoService;
    @Autowired
    private RedisKit redisUtil;
    Lock lock = new ReentrantLock();

    @Override
    public void atvTablesToCache()
    {
       //_removeCache();
        long startTime = System.currentTimeMillis();
        //加锁是否需要
        lock.lock();// 加锁，分布式下需使用分布式锁保证数据一致性
        try {
            _tableToCache();
        }finally {
            lock.unlock();//解锁
        }
        long endTime = System.currentTimeMillis();
        LogKit.info("缓存指定Atv系统表数据，总计用时：" + (endTime - startTime) + "毫秒");
    }

    /** 每次启动时先删除缓存 是否需要:
    private void _removeCache()
    {
        lock.lock();
        try {

        }finally {
            lock.unlock();
        }
    }*/
    private void _tableToCache()
    {
        QueryWrapper<AtvCountry> queryCountry = Wrappers.wrapper(AtvCountry.class);
        //         .select("id").select("name").select("countryCode");
        List<AtvCountry> atvCountries = daoService.findList(queryCountry);

        QueryWrapper<AtvArea> queryArea = Wrappers.wrapper(AtvArea.class);
        //.select("id").select("name").select("areaCode").select("countryId");
        List<AtvArea> atvAreas = daoService.findList(queryArea);

        QueryWrapper<AtvLine> queryLine = Wrappers.wrapper(AtvLine.class);
        //.select("id").select("sn").select("workers").select("areaId").select("countryId");
        List<AtvLine> atvLines = daoService.findList(queryLine);

        QueryWrapper<AtvTrain> queryTrain = Wrappers.wrapper(AtvTrain.class);
        List<AtvTrain> atvTrains = daoService.findList(queryTrain);

        QueryWrapper<AtvCarriage> queryCarriages = Wrappers.wrapper(AtvCarriage.class);
        List<AtvCarriage> atvCarriages = daoService.findList(queryCarriages);

        QueryWrapper<AtvEquipment> queryEquipments = Wrappers.wrapper(AtvEquipment.class);
        List<AtvEquipment> atvEquipments = daoService.findList(queryEquipments);

        QueryWrapper<AtvBoardCard> queryBoardCards = Wrappers.wrapper(AtvBoardCard.class);
        List<AtvBoardCard> atvBoardCards = daoService.findList(queryBoardCards);

        QueryWrapper<AtvLineSite> queryLineSites = Wrappers.wrapper(AtvLineSite.class);
        List<AtvLineSite> atvLineSites = daoService.findList(queryLineSites);

        QueryWrapper<AtvThreshold> queryThresholds = Wrappers.wrapper(AtvThreshold.class);
        List<AtvThreshold> atvThresholds = daoService.findList(queryThresholds);

        // 阈值表
        for (AtvThreshold atvThreshold : atvThresholds) {
            if (redisUtil.hget("threshold", atvThreshold.getBoardCardType().toString()) == null) {
                redisUtil.hset("threshold", atvThreshold.getBoardCardType().toString(), atvThreshold);
            }
        }
        // 国家
        for (AtvCountry atvCountry : atvCountries) {
            if (redisUtil.hget("country", atvCountry.getCountryCode()) == null) {
                redisUtil.hset("country", atvCountry.getCountryCode(), atvCountry);
            }

            // 地区
            for (AtvArea atvArea : atvAreas) {
                String areaKey = atvCountry.getId() + atvArea.getAreaCode();
                if (atvCountry.getId().equals(atvArea.getCountryId()) && redisUtil.hget("area", areaKey) == null) {
                    redisUtil.hset("area", areaKey, atvArea);
                }

                // 线路
                for (AtvLine atvLine : atvLines) {
                    String lineKey = atvArea.getId() + atvLine.getSn();
                    if (atvArea.getId().equals(atvLine.getAreaId()) && redisUtil.hget("line", lineKey) == null) {
                        redisUtil.hset("line", lineKey, atvLine);
                    }

                    // 站点
                    for (AtvLineSite atvLineSite : atvLineSites) {
                        String lineSiteKey = atvLine.getId() + atvLineSite.getSn();
                        if (atvLine.getId().equals(atvLineSite.getLineId()) && redisUtil.hget("lineSite", lineSiteKey) == null) {
                            redisUtil.hset("lineSite", lineKey, atvLineSite);
                        }
                    }

                    // 车辆
                    for (AtvTrain atvTrain : atvTrains) {
                        String trainKey = atvLine.getId() + atvTrain.getSn();
                        if (atvLine.getId().equals(atvTrain.getLineId()) && redisUtil.hget("train", trainKey) == null) {
                            redisUtil.hset("train", trainKey, atvTrain);
                        }

                        // 车厢
                        for (AtvCarriage atvCarriage : atvCarriages) {
                            String carriageKey = atvTrain.getId() + atvCarriage.getSn();
                            if (atvTrain.getId().equals(atvCarriage.getTrainId()) && redisUtil.hget("carriage", carriageKey) == null) {
                                redisUtil.hset("carriage", carriageKey, atvCarriage);
                            }

                            // 设备
                            for (AtvEquipment atvEquipment : atvEquipments) {
                                String equipmentKey = atvCarriage.getId() + atvEquipment.getSn();
                                if (atvCarriage.getId().equals(atvEquipment.getCarriageId()) && redisUtil.hget("equipment", equipmentKey) == null) {
                                    redisUtil.hset("equipment", equipmentKey, atvEquipment);
                                }

                                // 板卡
                                for (AtvBoardCard atvBoardCard : atvBoardCards) {
                                    String boardCardKey = atvBoardCard.getSn();
                                    if (atvEquipment.getId().equals(atvBoardCard.getEquipmentId()) && redisUtil.hget("boardCard", boardCardKey) == null) {
                                        redisUtil.hset("boardCard", boardCardKey, atvBoardCard);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
