package com.fastbee.ggroup.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.ggroup.domain.GSites;
import com.fastbee.ggroup.enums.SiteTypeCategoryEnum;
import com.fastbee.ggroup.mapper.GSitesMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.fastbee.ggroup.mapper.GLegendMapper;
import com.fastbee.ggroup.domain.GLegend;
import com.fastbee.ggroup.service.IGLegendService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图例Service业务层处理
 *
 * @author kerwincui
 * &#064;date  2024-10-08
 */
@Service
@Slf4j
public class GLegendServiceImpl implements IGLegendService
{

    private final GLegendMapper gLegendMapper;

    private  final GSitesMapper gSitesMapper;

    public GLegendServiceImpl(GLegendMapper gLegendMapper, GSitesMapper gSitesMapper) {
        this.gLegendMapper = gLegendMapper;
        this.gSitesMapper = gSitesMapper;
    }


    /**
     * 查询图例
     *
     * @param id 图例主键
     * @return 图例
     */
    @Override
    public GLegend selectGLegendById(Long id)
    {
        return new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId,GLegend::getIcon,GLegend::getName,GLegend::getCategory)
                .eq(GLegend::getId,id)
                .one();
    }

    /**
     * 查询/搜索图例列表
     * @param gLegend 图例
     * @return 图例
     */
    @Override
    public List<GLegend> selectGLegendList(GLegend gLegend) {
       return new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId,GLegend::getIcon,GLegend::getName,GLegend::getCategory)
                .like(StringUtils.isNotBlank(gLegend.getSearchValue()), GLegend::getName, gLegend.getSearchValue())
                .list();
    }

    /**
     * 新增图例
     *
     * @param gLegend 图例
     * @return 结果
     */
    @Override
    public int insertGLegend(GLegend gLegend)
    {
        //图例名称不能重复
        List<GLegend> duplicateNameList = new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId, GLegend::getName)
                .eq(GLegend::getName, gLegend.getName().trim())
                .list();
        if(!duplicateNameList.isEmpty()){
            throw new ServiceException("图例名称重复!");
        }
        gLegend.setCategory(gLegend.getCategory().trim());
        //图例类别校验
        if(!SiteTypeCategoryEnum.contains(gLegend.getCategory())){
           throw new ServiceException("图例类别不存在!");
        }
        gLegend.setCreateTime(DateUtils.getNowDate());
        return gLegendMapper.insertGLegend(gLegend);
    }

    /**
     * 修改图例
     *
     * @param gLegend 图例
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception. class)
    public int updateGLegend(GLegend gLegend)
    {
        //图例名称不能重复
        List<GLegend> duplicateNameList=new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId,GLegend::getName)
                .eq(GLegend::getName,gLegend.getName().trim())
                .ne(GLegend::getId,gLegend.getId()).list();
        if(!duplicateNameList.isEmpty()){
            throw new ServiceException("图例名称已存在!");
        }

        gLegend.setUpdateTime(DateUtils.getNowDate());
        return gLegendMapper.updateGLegend(gLegend);
    }
    /**
     * 删除图例
     *
     * @param id 需要删除的图例主键
     * @return 结果
     */
    @Override
    public int deleteGLegendById(Long id)
    {
        List<GLegend> list = new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId, GLegend::getName)
                .eq(GLegend::getId, id).list();
        if (list.isEmpty()){
            throw new ServiceException("图例不存在!");
        }

        List<GSites> gSitesList = new LambdaQueryChainWrapper<>(gSitesMapper)
                .select(GSites::getId, GSites::getType)
                .eq(GSites::getType, list.get(0).getName())
                .list();
        if(!gSitesList.isEmpty()){
            throw new ServiceException("图例已被站点使用，无法删除!");
        }
        return gLegendMapper.deleteById(id);
    }


    /**
     * 按照类别返回所有类别下图例列表
     * @return 图例列表
     */
    @Override
    public List<Map<String, Object>> getLegendListByCategory() {
        return new LambdaQueryChainWrapper<>(gLegendMapper)
                .select(GLegend::getId,GLegend::getIcon, GLegend::getName,GLegend::getCategory)
                .list().stream()
                //按照类别分组
                .collect(Collectors.groupingBy(GLegend::getCategory))
                .entrySet().stream()
                .map(entry -> {
                            Map<String, Object> itemMap = new HashMap<>();
                            itemMap.put("category", entry.getKey());
                            itemMap.put("list", entry.getValue());
                            return itemMap;
                        }
                ).collect((Collectors.toList()));
    }
}
