package com.boman.web.core.service.qianshanGrid.impl;

import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.IdUtils;
import com.boman.common.redis.service.RedisService;
import com.boman.domain.QianshanGrid;
import com.boman.domain.constant.CacheConstants;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.mapper.QianshanGridMapper;
import com.boman.web.core.service.qianshanGrid.IQianshanGridService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 潜山网格信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-20
 */
@Service
public class QianshanGridServiceImpl implements IQianshanGridService {
    @Autowired
    private QianshanGridMapper qianshanGridMapper;

    @Resource
    private RedisService redisService;

    /**
     * 查询潜山网格信息
     *
     * @param id 潜山网格信息主键
     * @return 潜山网格信息
     */
    @Override
    public QianshanGrid selectQianshanGridById(Long id) {
        return qianshanGridMapper.selectQianshanGridById(id);
    }

    /**
     * 查询潜山网格信息列表
     *
     * @param qianshanGrid 潜山网格信息
     * @return 潜山网格信息
     */
    @Override
    public List<QianshanGrid> selectQianshanGridList(QianshanGrid qianshanGrid) {
        Long id = qianshanGrid.getId();
        if (id != null) {
            qianshanGrid.setParentGridId(String.valueOf(id));
            qianshanGrid.setGridLevel("4");
        } else {
            qianshanGrid.setGridLevel("3");
        }


        List<QianshanGrid> qianshanGrids = qianshanGridMapper.selectQianshanGridList(qianshanGrid);
        // 这里的逻辑是：根据id获取到这个下面的所有子网格，每个网格判断是否有下一级别
        if (qianshanGrids != null && qianshanGrids.size() > 0) {

            List<QianshanGrid> hasChirlds = qianshanGridMapper.validateHasChirld(qianshanGrids);

            if (hasChirlds != null && hasChirlds.size() > 0) {
                Map<Long, QianshanGrid> hasChirldMap = new HashMap<>();
                for (QianshanGrid hasChirld : hasChirlds) {
                    hasChirldMap.put(hasChirld.getId(), hasChirld);
                }
                for (QianshanGrid qianshan : qianshanGrids) {
                    if (hasChirldMap.get(qianshan.getId()) == null) {
                        qianshan.setHasChildren(Boolean.FALSE);
                    }
                }
            } else {
                for (QianshanGrid qianshan : qianshanGrids) {
                    qianshan.setHasChildren(Boolean.FALSE);
                }
            }
        }
        return qianshanGrids;
    }

    /**
     * 新增潜山网格信息
     *
     * @param qianshanGrid 潜山网格信息
     * @return 结果
     */
    @Override
    public int insertQianshanGrid(QianshanGrid qianshanGrid) {
        qianshanGrid.setId(System.currentTimeMillis());
        qianshanGrid.setCreateTime(DateUtils.getNowDate());
        int count = qianshanGridMapper.insertQianshanGrid(qianshanGrid);

        if(count>0){
            //将数据更新存入redis
            //查询同一级的所有网格
            QianshanGrid grid = new QianshanGrid();
            grid.setParentGridId(qianshanGrid.getParentGridId());
            List<QianshanGrid> gridList = qianshanGridMapper.selectQianshanGridList(grid);

            if(gridList != null && gridList.size() > 0){
                List<Map<String,Object>> chinaAreasMap = new ArrayList<>();

                for (QianshanGrid grids : gridList) {
                    Map<String,Object> chinaAreMap = new HashMap<>();

                    chinaAreMap.put("areaId",String.valueOf(grids.getId()));
                    chinaAreMap.put("children",new ArrayList<>());
                    chinaAreMap.put("name",grids.getGridName());
                    chinaAreMap.put("params",new HashMap<String, Object>());
                    chinaAreMap.put("pid",grids.getParentGridId());
                    chinaAreMap.put("sort",0);
                    chinaAreasMap.add(chinaAreMap);
                }
                //先将原有的删除
                redisService.deleteObject(CacheConstants.CHINA_AREA_DP+qianshanGrid.getParentGridId());
                redisService.setCacheList(CacheConstants.CHINA_AREA_DP+qianshanGrid.getParentGridId(),chinaAreasMap);
            }

        }


        return count;
    }

    /**
     * 修改潜山网格信息
     *
     * @param qianshanGrid 潜山网格信息
     * @return 结果
     */
    @Override
    public int updateQianshanGrid(QianshanGrid qianshanGrid) {
        qianshanGrid.setUpdateTime(DateUtils.getNowDate());
        int count = qianshanGridMapper.updateQianshanGrid(qianshanGrid);
        if(count>0){
            //将数据更新存入redis
            //查询同一级的所有网格
            QianshanGrid grid = new QianshanGrid();
            grid.setParentGridId(qianshanGrid.getParentGridId());
            List<QianshanGrid> gridList = qianshanGridMapper.selectQianshanGridList(grid);

            if(gridList != null && gridList.size() > 0){
                List<Map<String,Object>> chinaAreasMap = new ArrayList<>();

                for (QianshanGrid grids : gridList) {
                    Map<String,Object> chinaAreMap = new HashMap<>();

                    chinaAreMap.put("areaId",String.valueOf(grids.getId()));
                    chinaAreMap.put("children",new ArrayList<>());
                    chinaAreMap.put("name",grids.getGridName());
                    chinaAreMap.put("params",new HashMap<String, Object>());
                    chinaAreMap.put("pid",grids.getParentGridId());
                    chinaAreMap.put("sort",0);
                    chinaAreasMap.add(chinaAreMap);
                }
                //先将原有的删除
                redisService.deleteObject(CacheConstants.CHINA_AREA_DP+qianshanGrid.getParentGridId());
                redisService.setCacheList(CacheConstants.CHINA_AREA_DP+qianshanGrid.getParentGridId(),chinaAreasMap);
            }

        }
        return count;
    }

    /**
     * 批量删除潜山网格信息
     *
     * @param ids 需要删除的潜山网格信息主键
     * @return 结果
     */
    @Override
    public int deleteQianshanGridByIds(Long[] ids) {
        return qianshanGridMapper.deleteQianshanGridByIds(ids);
    }

    /**
     * 删除潜山网格信息信息
     *
     * @param id 潜山网格信息主键
     * @return 结果
     */
    @Override
    public int deleteQianshanGridById(Long id) {
        return qianshanGridMapper.deleteQianshanGridById(id);
    }

    /***
     * 通过村ID获取下级网格信息
     */
    @Override
    public AjaxResult queryZu(QianshanGrid qianshanGrid) {
        List<QianshanGrid> qianshanGridList = qianshanGridMapper.selectQianshanGridList(qianshanGrid);
        return AjaxResult.success(qianshanGridList);
    }

    @Override
    public List<QianshanGrid> listApi(String pid) {
        QianshanGrid qianshanGrid = new QianshanGrid();
        qianshanGrid.setParentGridId(pid);
        List<QianshanGrid> qianshanGrids = qianshanGridMapper.selectQianshanGridList(qianshanGrid);
        return qianshanGrids;
    }
}
