package com.ruoyi.system.service.impl;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DFixAddressArea;
import com.ruoyi.system.mapper.DFixAddressAreaMapper;
import com.ruoyi.system.service.IDFixAddressAreaService;

/**
 * 固定地址区划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class DFixAddressAreaServiceImpl extends ServiceImpl<DFixAddressAreaMapper, DFixAddressArea>
		implements IDFixAddressAreaService {

	@Autowired
	private DFixAddressAreaMapper dFixAddressAreaMapper;
	@Autowired
	private RedisCache redisCache;
	
	public Map<String,DFixAddressArea>loadAll(){
		Map<String,DFixAddressArea>map=new HashMap<String,DFixAddressArea>();
		QueryWrapper<DFixAddressArea> queryWrapper = Wrappers.query();
		queryWrapper.orderByAsc("id");
		List<DFixAddressArea>list= this.list(queryWrapper);
		for(DFixAddressArea area:list) {
			if(StringUtils.isNotBlank(area.getAddress())) {
				map.put(area.getAddress(), area);
			}
		}
		return map;
	}
	
	private List<DFixAddressArea>getAllObscureArea(){
		String key="AllObscureArea-"+new SimpleDateFormat("yyyyMMdd").format(new Date());
		List<DFixAddressArea>list=getAllObscureAreaFromCache(key);
		if(list==null || list.isEmpty()) {
			list=getAllObscureAreaFormDb();
			if(list!=null && !list.isEmpty()) {
				String json=new Gson().toJson(list);
				redisCache.setCacheObject(key, json);
			}
		}
		
		return list;
	}
	
	
	private List<DFixAddressArea>getAllObscureAreaFromCache(String key){
		String json=redisCache.getCacheObject(key);
		if(StringUtils.isNotBlank(json) && "null".equalsIgnoreCase(json)) {
			Type type=new TypeToken<ArrayList<DFixAddressArea>>() {}.getType();
			Gson gson=new Gson();
			List<DFixAddressArea>list=gson.fromJson(json, type);
			if(list!=null || !list.isEmpty()) {
				return list;
			}
		}
		return null;
	}
	
	private List<DFixAddressArea>getAllObscureAreaFormDb(){
		QueryWrapper<DFixAddressArea> queryWrapper = Wrappers.query();
//		queryWrapper.eq("match_mode", "0");
		return this.list(queryWrapper);
	}

	public DFixAddressArea matchByAddress(String address, String cityName) {
		if(StringUtils.isBlank(address)) {
			return null;
		}
		
		DFixAddressArea area = dFixAddressAreaMapper.matchByAddress1(address);
		if(area!=null) {
			//精确匹配到
			return area;
		}
		
		//昆明
		if("昆明市".equals(cityName)) {
			List<DFixAddressArea>list=getAllObscureArea();
			if(list!=null && !list.isEmpty()) {
				for(DFixAddressArea a:list) {
					if(a!=null && StringUtils.isNotBlank(a.getAddress())) {
						if(address.contains(a.getAddress().trim())) {
							return a;
						}
					}
					
				}
			}
			
		}

		return null;
	}

	/**
	 * 查询固定地址区划列表
	 *
	 * @param dFixAddressArea 固定地址区划
	 * @return 固定地址区划集合
	 * @date 2023-07-05
	 */
	@Override
	public List<DFixAddressArea> queryList(DFixAddressArea dFixAddressArea) {
		QueryWrapper<DFixAddressArea> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(dFixAddressArea.getAreaCode())) {
			queryWrapper.eq("area_code", dFixAddressArea.getAreaCode());
		}
		if (StringUtils.isNotEmpty(dFixAddressArea.getAreaName())) {
			queryWrapper.like("area_name", dFixAddressArea.getAreaName());
		}
		if (StringUtils.isNotEmpty(dFixAddressArea.getAddress())) {
			queryWrapper.eq("address", dFixAddressArea.getAddress());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}
}
