package com.tax.zone.service.impl;

import com.alibaba.fastjson.JSON;
import com.tax.zone.mapper.FinZoneInfoMapper;
import com.tax.zone.model.FinZoneInfo;
import com.tax.zone.model.ZoneModel;
import com.tax.zone.service.api.FinZoneInfoService;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.constants.RedisContants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Component
public class FinZoneInfoServiceImpl implements FinZoneInfoService {

    @Autowired
    private FinZoneInfoMapper finZoneInfoMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public List<FinZoneInfo> findAllZoneList(ZoneModel model) {
        List<FinZoneInfo> zoneInfoList = finZoneInfoMapper.selectAllList(model);
        return zoneInfoList;
    }

    @Override
    public List<FinZoneInfo> findAllZoneListAndSetCache(ZoneModel model) {
        List<FinZoneInfo> zoneInfoList = finZoneInfoMapper.selectAllList(model);
        for(FinZoneInfo zoneInfo:zoneInfoList) {
            if(model.equals("0")) {
                redisService.set(RedisContants.ZONE_CODE + zoneInfo.getZoneCode(), JSON.toJSONString(zoneInfo));
            } else {
                redisService.set(RedisContants.ELE_ZONE_CODE + zoneInfo.getZoneCode(), JSON.toJSONString(zoneInfo));
            }
        }
        return zoneInfoList;
    }

    @Override
    public FinZoneInfo getZoneInfoByCode(String zoneCode) {
        if(redisService.exists(RedisContants.ZONE_CODE + zoneCode)) {
            return JSON.parseObject(redisService.get(RedisContants.ZONE_CODE + zoneCode),  FinZoneInfo.class);
        }
        return null;
    }


}
