package com.ruoyi.mall.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.Vo.MallIndustryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallIndustryMapper;
import com.ruoyi.mall.domain.MallIndustry;
import com.ruoyi.mall.service.IMallIndustryService;

import javax.annotation.Resource;

/**
 * 行业Service业务层处理
 *
 * @author LCL
 * @date 2023-10-20
 */
@Service
public class MallIndustryServiceImpl implements IMallIndustryService
{
    @Resource
    private MallIndustryMapper mallIndustryMapper;

    /**
     * 查询行业
     *
     * @param industryId 行业主键
     * @return 行业
     */
    @Override
    public MallIndustry selectMallIndustryByIndustryId(Long industryId)
    {
        return mallIndustryMapper.selectMallIndustryByIndustryId(industryId);
    }

    /**
     * 查询行业列表
     *
     * @param mallIndustry 行业
     * @return 行业
     */
    @Override
    public List<MallIndustry> selectMallIndustryList(MallIndustry mallIndustry)
    {
        return mallIndustryMapper.selectMallIndustryList(mallIndustry);
    }

    /**
     * 查询行业列表 分组行业分类 xcx Api
     *
     * @param mallIndustry 行业
     * @return 行业集合
     */
    @Override
    public List<MallIndustryVo> selectMallIndustryListForxcx(MallIndustry mallIndustry)
    {
        List<MallIndustry> mallIndustries = mallIndustryMapper.selectMallIndustryList(mallIndustry);
        Map<String, List<MallIndustry>> groupedByLastName = mallIndustries.stream()
                .collect(Collectors.groupingBy(MallIndustry::getIndustryClassName));
        List<MallIndustryVo> result = groupedByLastName.entrySet().stream()
                .map(entry -> {
                    List<MallIndustry> sublist = entry.getValue();
                    String key = entry.getKey();

                    MallIndustryVo mallIndustryVo = new MallIndustryVo();
                    mallIndustryVo.setIndustryList(sublist);
                    mallIndustryVo.setClassName(key);

                    return mallIndustryVo;
                })
                .collect(Collectors.toList());
        return result;

    }

    /**
     * 新增行业
     *
     * @param mallIndustry 行业
     * @return 结果
     */
    @Override
    public int insertMallIndustry(MallIndustry mallIndustry)
    {
        mallIndustry.setCreateAt(DateUtils.getTime());
        return mallIndustryMapper.insertMallIndustry(mallIndustry);
    }

    /**
     * 修改行业
     *
     * @param mallIndustry 行业
     * @return 结果
     */
    @Override
    public int updateMallIndustry(MallIndustry mallIndustry)
    {
        mallIndustry.setUpdateAt(DateUtils.getTime());
        return mallIndustryMapper.updateMallIndustry(mallIndustry);
    }

    /**
     * 批量删除行业
     *
     * @param industryIds 需要删除的行业主键
     * @return 结果
     */
    @Override
    public int deleteMallIndustryByIndustryIds(Long[] industryIds)
    {
        return mallIndustryMapper.deleteMallIndustryByIndustryIds(industryIds);
    }

    /**
     * 删除行业信息
     *
     * @param industryId 行业主键
     * @return 结果
     */
    @Override
    public int deleteMallIndustryByIndustryId(Long industryId)
    {
        return mallIndustryMapper.deleteMallIndustryByIndustryId(industryId);
    }
}
