package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.RedisConstant;
import com.cool.store.entity.MDMAreaDO;
import com.cool.store.mapper.MdmAreaMapper;
import com.cool.store.service.MDMAreaService;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.MessageFormat;
import java.util.List;

@Service
public class MDMAreaServiceImpl implements MDMAreaService {

    @Autowired
    private RedisUtilPool redisUtilPool;

    @Autowired
    private MdmAreaMapper mdmAreaMapper;

    /**
     * 获取省级数据
     */
    @Override
    public List<MDMAreaDO> getProvince() {
        String provincesJson = redisUtilPool.getString(RedisConstant.MDM_AREA_PROVINCE);
        if (StringUtils.isNotEmpty(provincesJson)) {
            return (List<MDMAreaDO>) JSONObject.parseObject(provincesJson, List.class);
        }
        List<MDMAreaDO> areaDOList = mdmAreaMapper.getProvince();
        //过期时间三小时
        redisUtilPool.setString(RedisConstant.MDM_AREA_PROVINCE, JSONObject.toJSONString(areaDOList), 3 * 60 * 60);
        return areaDOList;
    }

    /**
     * 获取子级地区
     * @param code 父级 code
     */
    @Override
    public List<MDMAreaDO> getSonArea(String code) {
        String provincesJson = redisUtilPool.getString(MessageFormat.format(RedisConstant.MDM_AREA_OTHERS, code));
        if (StringUtils.isNotEmpty(provincesJson)) {
            return (List<MDMAreaDO>) JSONObject.parseObject(provincesJson, List.class);
        }
        List<MDMAreaDO> areaDOList = mdmAreaMapper.getSonArea(code);
        //数据库也没有该数据就缓存空值，但是只缓存一分钟
        if (areaDOList == null || areaDOList.size() == 0) {
            redisUtilPool.setString(MessageFormat.format(RedisConstant.MDM_AREA_OTHERS, code), "", 60);
        }
        //过期时间三小时
        redisUtilPool.setString(MessageFormat.format(RedisConstant.MDM_AREA_OTHERS, code), JSONObject.toJSONString(areaDOList), 3 * 60 * 60);
        return areaDOList;
    }

    @Override
    public List<MDMAreaDO> getAllAreaCode(String code) {
        if (StringUtil.isEmpty(code)) {
            return null;
        }
        //获取省下所有数据
        List<MDMAreaDO>  provinceCodeList= mdmAreaMapper.getProvinceAllCode(code);
        if(CollectionUtils.isEmpty(provinceCodeList)){
            //获取市区下所有数据
            List<MDMAreaDO> cityCodeList= mdmAreaMapper.getSonArea(code);
            if(CollectionUtils.isEmpty(cityCodeList)){
                //获取区数据
                return mdmAreaMapper.getArea(code);
            }else {
                return cityCodeList;
            }
        }else {
            return provinceCodeList;
        }
    }

    /**
     * 省为true
     * 非省为false
     * @param code
     * @return
     */
    @Override
    public boolean getAreaProvinceType(String code) {
        if (StringUtil.isEmpty(code)) {
            return false;
        }
        //获取省下所有数据
        List<MDMAreaDO>  provinceCodeList= mdmAreaMapper.getProvinceAllCode(code);
        return CollectionUtils.isEmpty(provinceCodeList)?false:true;
    }

}
