package com.ywt.service;

import com.ywt.core.BaseResponse;
import com.ywt.core.DataResponse;
import com.ywt.core.utils.Checker;
import com.ywt.domain.entity.center.Area;
import com.ywt.domain.entity.center.AreaRepository;

import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author daiyihua
 * @create 2022-02-14 10:08
 * @program glasses_service
 * @description 地区服务
 **/
@DubboService
@Service
public class AreaService {
    private static final Logger logger = LoggerFactory.getLogger(AreaService.class);

    @Autowired
    private AreaRepository areaRepository;

    private final int AREA_LEVEL = 4;

    public BaseResponse getAreaList() {
        DataResponse response = new DataResponse<>();
        try {
            List<Area> areaList = areaRepository.getAreaList();
            // 得到一级
            List<Area> firstAreaList = areaList.stream().filter(p -> Checker.getIntegerValue(p.getParentId()) == 0).collect(Collectors.toList());
            // 分别表示父类ID，n则用来记录最前面的"全部"，只使用一次，level:则用来控制显示曾数
            int parentId = 0, n = 0, level = 1;
            List<Map<String, Object>> firstItemList = getAreaListRecursion(areaList, firstAreaList, parentId, n, level);

            Map<String, Object> rtnData = new HashMap<>(16);
            rtnData.put("list", firstItemList);
            response.setData(rtnData);
            return response.succeed("处理成功");
        } catch (Exception e) {
            logger.error("AreaService#getAreaList(): {}", e.getMessage(), e);
            return response.error();
        }
    }

    /**
     * 递归
     *
     * @param areaList      总的list集合
     * @param recursionList 当前循环的层级
     * @return {@link List<Map<String, Object>>}
     */
    private List<Map<String, Object>> getAreaListRecursion(List<Area> areaList, List<Area> recursionList,
                                                           int parentId, int n, int level) {

        List<Map<String, Object>> firstList = new LinkedList<>();

        //最前面的全部，这个只用一次
        if (n == 0) {
            Map<String, Object> firstItem = new HashMap<>(16);
            firstItem.put("id", parentId);
            firstItem.put("name", "全部");

            Map<String, Object> item = new HashMap<>(16);
            List<Map<String, Object>> itemLst = new LinkedList<>();
            item.put("id", 0);
            item.put("name", "全部");
            itemLst.add(item);
            firstItem.put("secondList", itemLst);
            firstList.add(firstItem);
            n++;
        }
        //指显示到区
        else if (level < AREA_LEVEL) {
            Map<String, Object> firstItem = new HashMap<>(16);
            firstItem.put("id", parentId);
            firstItem.put("name", "全部");
            firstItem.put("parentId", parentId);
            firstList.add(firstItem);
        }
        //循环
        if (!Checker.isNone(recursionList)) {
            for (Area area : recursionList) {
                Map<String, Object> item = new HashMap<>(16);
                item.put("id", area.getId());
                item.put("name", area.getName());
                item.put("parentId", area.getParentId());

                List<Area> lst = areaList.stream().filter(p -> Checker.getIntegerValue(p.getParentId()) == area.getId()).collect(Collectors.toList());
                List<Map<String, Object>> secondItem = getAreaListRecursion(areaList, lst, area.getId(), n, area.getLevel());
                item.put("secondList", secondItem);
                firstList.add(item);
            }
            return firstList;
        }
        return firstList;
    }
}
