package com.kmall.api.api;

import com.kmall.api.annotation.IgnoreAuth;
import com.kmall.common.cache.RegionCacheUtil;
import com.kmall.api.entity.RegionVo;
import com.kmall.common.dao.SysRegionDao;
import com.kmall.common.entity.SysRegionEntity;
import com.kmall.api.service.ApiTopicService;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.common.security.session.JedisSessionDAO;
import com.kmall.common.service.SysRegionService;
import com.kmall.common.utils.StringUtils;
import com.kmall.common.utils.redis.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Scott <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/region")
public class ApiRegionController extends ApiBaseAction {
    @Autowired
    private ApiTopicService topicService;
    @Autowired
    private SysRegionService sysRegionService;
    // session 在redis过期时间是30分钟30*60
    private static int expireTime = 1800;

    /**
     */
    @IgnoreAuth
    @GetMapping("list")
    public Object list(Integer parentId) {
        List<SysRegionEntity> regionEntityList = new ArrayList();
        Map<String,Object> queryListMap = new HashMap<>();
        Map<String, Object> listMap = JedisUtil.getObjectMap("sysRegionEntityList");
        if(listMap == null){
            List<SysRegionEntity> sysRegionEntityList= sysRegionService.queryList(new HashMap<String, Object>());
            queryListMap.put("sysRegionEntityList",sysRegionEntityList);
            JedisUtil.setObjectMap("sysRegionEntityList", queryListMap, expireTime);
            for (SysRegionEntity areaVo : sysRegionEntityList) {
                if (null != areaVo.getParentId() && parentId.equals(areaVo.getParentId())) {
                    regionEntityList.add(areaVo);
                }
            }
        }else{
            Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
            for (Map.Entry<String, Object> e : listMap.entrySet()) {
                List<Object> objectList =  (List<Object>)listMap.get(e.getKey());
                for (Object o:objectList){
                    SysRegionEntity sysRegionEntity = (SysRegionEntity)o;
                    if (null != sysRegionEntity.getParentId() && parentId.equals(sysRegionEntity.getParentId())) {
                        regionEntityList.add(sysRegionEntity);
                    }
                }
            }
        }
        List<RegionVo> regionVoList = new ArrayList();
        if (null != regionEntityList && regionEntityList.size() > 0) {
            for (SysRegionEntity sysRegionEntity : regionEntityList) {
                regionVoList.add(new RegionVo(sysRegionEntity));
            }
        }
        return toResponsSuccess(regionVoList);
    }

    /**
     */
    @IgnoreAuth
    @GetMapping("provinceList")
    public Object provinceList() {
        List<SysRegionEntity> regionEntityList = RegionCacheUtil.getAllProvice();
        List<RegionVo> regionVoList = new ArrayList();
        if (null != regionEntityList && regionEntityList.size() > 0) {
            for (SysRegionEntity sysRegionEntity : regionEntityList) {
                regionVoList.add(new RegionVo(sysRegionEntity));
            }
        }
        return toResponsSuccess(regionVoList);
    }

    /**
     */
    @IgnoreAuth
    @GetMapping("cityList")
    public Object provinceList(String proviceName) {
        List<SysRegionEntity> regionEntityList = RegionCacheUtil.getChildrenCity(proviceName);
        List<RegionVo> regionVoList = new ArrayList();
        if (null != regionEntityList && regionEntityList.size() > 0) {
            for (SysRegionEntity sysRegionEntity : regionEntityList) {
                regionVoList.add(new RegionVo(sysRegionEntity));
            }
        }
        return toResponsSuccess(regionVoList);
    }

    /**
     */
    @IgnoreAuth
    @GetMapping("distinctList")
    public Object distinctList(String proviceName, String cityName) {
        List<SysRegionEntity> regionEntityList = RegionCacheUtil.getChildrenDistrict(proviceName, cityName);
        List<RegionVo> regionVoList = new ArrayList();
        if (null != regionEntityList && regionEntityList.size() > 0) {
            for (SysRegionEntity sysRegionEntity : regionEntityList) {
                regionVoList.add(new RegionVo(sysRegionEntity));
            }
        }
        return toResponsSuccess(regionVoList);
    }

    @IgnoreAuth
    @GetMapping("info")
    public Object info(Integer regionId) {
        SysRegionEntity regionEntity = RegionCacheUtil.getAreaByAreaId(regionId);
        return toResponsSuccess(new RegionVo(regionEntity));
    }

    @IgnoreAuth
    @GetMapping("regionIdsByNames")
    public Object regionIdsByNames(String provinceName, String cityName, String districtName) {
        Map resultObj = new HashMap();
        Integer provinceId = 0;
        Integer cityId = 0;
        Integer districtId = 0;
        if (null != provinceName) {
            provinceId = RegionCacheUtil.getProvinceIdByName(provinceName);
        }
        if (null != provinceId && !StringUtils.isNullOrEmpty(cityName)) {
            cityId = RegionCacheUtil.getCityIdByName(provinceId, cityName);
        }
        if (null != provinceId && null != cityId && !StringUtils.isNullOrEmpty(districtName)) {
            districtId = RegionCacheUtil.getDistrictIdByName(provinceId, cityId, districtName);
        }
        resultObj.put("provinceId", provinceId);
        resultObj.put("cityId", cityId);
        resultObj.put("districtId", districtId);
        return toResponsSuccess(resultObj);
    }
}