package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.AreaAttr;
import com.ruoyi.ems.mapper.AreaMapper;
import com.ruoyi.ems.service.IAreaAttrService;
import com.ruoyi.ems.service.IAreaService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 区域对象Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-01
 */
@Service
public class AreaServiceImpl implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private IAreaAttrService attrService;

    /**
     * 查询区域对象
     *
     * @param id 区域对象主键
     * @return 区域对象
     */
    @Override
    public Area selectAreaById(Long id) {
        Area area = areaMapper.selectAreaById(id);

        if (area != null) {
            area.setAreaAttr(attrService.selectAreaAttrByCode(area.getAreaCode()));
        }

        return area;
    }

    @Override
    public Area selectAreaByCode(String areaCode) {
        return areaMapper.selectAreaByCode(areaCode);
    }

    /**
     * 查询区域对象列表
     *
     * @param param 区域对象
     * @return 区域对象
     */
    @Override
    public List<Area> selectArea(Area param) {
        List<Area> list = areaMapper.selectAreaList(param);
        return list;
    }

    /**
     * 查询区域对象列表&详情
     *
     * @param param 区域对象
     * @return 区域对象
     */
    @Override
    public List<Area> selectAreaDetail(Area param) {
        List<Area> list = areaMapper.selectAreaList(param);

        if (CollectionUtils.isNotEmpty(list)) {
            List<String> areaCodes = list.stream().map(Area::getAreaCode).collect(Collectors.toList());
            List<AreaAttr> attrList = attrService.selectAreaAttrList(areaCodes);
            Map<String, AreaAttr> attrMap = attrList.stream()
                .collect(Collectors.toMap(AreaAttr::getAreaCode, Function.identity()));

            for (Area area : list) {
                if (attrMap.containsKey(area.getAreaCode())) {
                    AreaAttr attr = attrMap.get(area.getAreaCode());
                    area.setAreaAttr(attr);
                }
            }
        }

        return list;
    }

    @Override
    public List<Area> selectAreaByTag(String tagCode) {
        return areaMapper.selectAreaByTag(tagCode);
    }

    @Override
    public List<Area> selectAreaTree(String parentCode, boolean recursion) {
        List<Area> dbAreas = areaMapper.selectAreaList(new Area());
        Map<String, List<Area>> areaMap = dbAreas.stream()
            .collect(Collectors.groupingBy(Area::getParentCode, Collectors.toList()));
        return recursionBuild(parentCode, recursion, areaMap);
    }

    /**
     * 新增区域对象
     *
     * @param area 区域对象
     * @return 结果
     */
    @Override
    public int insertArea(Area area) {
        fillAncestors(area);
        int cnt = areaMapper.insertArea(area);

        if (null != area.getAreaAttr()) {
            attrService.insertAreaAttr(area.getAreaAttr());
        }

        return cnt;
    }

    /**
     * 修改区域对象
     *
     * @param area 区域对象
     * @return 结果
     */
    @Override
    public int updateArea(Area area) {
        fillAncestors(area);
        int cnt = areaMapper.updateArea(area);

        if (null != area.getAreaAttr()) {
            attrService.updateAreaAttr(area.getAreaAttr());
        }

        return cnt;
    }

    /**
     * 批量删除区域对象
     *
     * @param ids 需要删除的区域对象主键
     * @return 结果
     */
    @Override
    public int deleteAreaByIds(Long[] ids) {
        for (Long id : ids) {
            Area area = areaMapper.selectAreaById(id);

            if (null != area) {
                attrService.deleteAreaAttrByCode(area.getAreaCode());
            }
        }

        return areaMapper.deleteAreaByIds(ids);
    }

    /**
     * 删除区域对象信息
     *
     * @param id 区域对象主键
     * @return 结果
     */
    @Override
    public int deleteAreaById(Long id) {
        Area area = areaMapper.selectAreaById(id);

        if (null != area) {
            areaMapper.deleteAreaById(id);
            attrService.deleteAreaAttrByCode(area.getAreaCode());
        }

        return areaMapper.deleteAreaById(id);
    }

    private void fillAncestors(Area area) {
        Area parent = areaMapper.selectAreaByCode(area.getParentCode());

        if (null != parent) {
            area.setAncestors(parent.getAncestors() + "," + parent.getAreaCode());
        }
        else if (StringUtils.equals("0", area.getParentCode())) {
            area.setAncestors(area.getParentCode());
        }
    }

    /**
     * 递归构建树
     *
     * @param areaCode  区域代码
     * @param parentMap 区域集合
     * @return List<TreeEntity>
     */
    public List<Area> recursionBuild(String areaCode, boolean isRecursion, Map<String, List<Area>> parentMap) {
        List<Area> retList = null;
        List<Area> areaList = parentMap.get(areaCode);

        if (CollectionUtils.isNotEmpty(areaList)) {
            retList = new ArrayList<>();

            for (Area area : areaList) {
                if (isRecursion) {
                    area.setChildren(recursionBuild(area.getAreaCode(), isRecursion, parentMap));
                }

                retList.add(area);
            }
        }

        return retList;
    }
}
