package com.starry.admin.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.modules.system.mapper.SysAdministrativeAreaDictInfoMapper;
import com.starry.admin.modules.system.module.entity.SysAdministrativeAreaDictInfoEntity;
import com.starry.admin.modules.system.service.ISysAdministrativeAreaDictInfoService;
import com.starry.admin.modules.system.vo.AdministrativeAreaQueryReturnVo;
import com.starry.common.utils.ConvertUtil;
import com.starry.common.utils.IdUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 行政区域字典信息Service业务层处理
 *
 * @author admin
 * @since 2024-04-03
 */
@Service
public class SysAdministrativeAreaDictInfoServiceImpl
        extends
            ServiceImpl<SysAdministrativeAreaDictInfoMapper, SysAdministrativeAreaDictInfoEntity>
        implements
            ISysAdministrativeAreaDictInfoService {
    @Resource
    private SysAdministrativeAreaDictInfoMapper sysAdministrativeAreaDictInfoMapper;

    /**
     * 查询行政区域字典信息
     *
     * @param id
     *            行政区域字典信息主键
     * @return 行政区域字典信息
     */
    @Override
    public SysAdministrativeAreaDictInfoEntity selectPlayAdministrativeAreaDictInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<AdministrativeAreaQueryReturnVo> selectTree(String level) {
        LambdaQueryWrapper<SysAdministrativeAreaDictInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.le(SysAdministrativeAreaDictInfoEntity::getLevel, level);
        List<SysAdministrativeAreaDictInfoEntity> list = this.baseMapper.selectList(lambdaQueryWrapper);
        Map<String, List<SysAdministrativeAreaDictInfoEntity>> collect = list.stream()
                .filter(a -> a != null && a.getPCode() != null)
                .collect(Collectors.groupingBy(SysAdministrativeAreaDictInfoEntity::getPCode));
        return this.assembleTree(collect, collect.get("00"));
    }

    /**
     * 组装数据
     *
     * @param data
     *            数据，key=区域编码,value=区域列表
     * @param list
     *            区域略表
     * @return List<com.starry.admin.modules.system.entity.SysAdministrativeAreaDictInfoEntity>
     * @author admin
     * @since 2024/4/10 15:14
     **/
    public List<AdministrativeAreaQueryReturnVo> assembleTree(
            Map<String, List<SysAdministrativeAreaDictInfoEntity>> data,
            List<SysAdministrativeAreaDictInfoEntity> list) {
        if (list == null) {
            return new ArrayList<>();
        }
        List<AdministrativeAreaQueryReturnVo> result = ConvertUtil.entityToVoList(list,
                AdministrativeAreaQueryReturnVo.class);
        for (AdministrativeAreaQueryReturnVo entity : result) {
            entity.setChild(assembleTree(data, data.get(entity.getCode())));
        }
        return result;
    }

    @Override
    public List<SysAdministrativeAreaDictInfoEntity> selectAll() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 查询行政区域字典信息列表
     *
     * @param playAdministrativeAreaDictInfo
     *            行政区域字典信息
     * @return 行政区域字典信息
     */
    @Override
    public IPage<SysAdministrativeAreaDictInfoEntity> selectPlayAdministrativeAreaDictInfoByPage(
            SysAdministrativeAreaDictInfoEntity playAdministrativeAreaDictInfo) {
        Page<SysAdministrativeAreaDictInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增行政区域字典信息
     *
     * @param playAdministrativeAreaDictInfo
     *            行政区域字典信息
     * @return 结果
     */
    @Override
    public boolean create(SysAdministrativeAreaDictInfoEntity playAdministrativeAreaDictInfo) {
        if (StrUtil.isBlankIfStr(playAdministrativeAreaDictInfo.getId())) {
            playAdministrativeAreaDictInfo.setId(IdUtils.getUuid());
        }
        return save(playAdministrativeAreaDictInfo);
    }

    @Override
    public boolean create(List<SysAdministrativeAreaDictInfoEntity> list) {
        return this.saveBatch(list);
    }

    /**
     * 修改行政区域字典信息
     *
     * @param playAdministrativeAreaDictInfo
     *            行政区域字典信息
     * @return 结果
     */
    @Override
    public boolean update(SysAdministrativeAreaDictInfoEntity playAdministrativeAreaDictInfo) {
        return updateById(playAdministrativeAreaDictInfo);
    }

    /**
     * 批量删除行政区域字典信息
     *
     * @param ids
     *            需要删除的行政区域字典信息主键
     * @return 结果
     */
    @Override
    public int deletePlayAdministrativeAreaDictInfoByIds(String[] ids) {
        return sysAdministrativeAreaDictInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除行政区域字典信息信息
     *
     * @param id
     *            行政区域字典信息主键
     * @return 结果
     */
    @Override
    public int deletePlayAdministrativeAreaDictInfoById(String id) {
        return sysAdministrativeAreaDictInfoMapper.deleteById(id);
    }
}
