package com.xinelu.manage.service.crowdinfo.impl;

import com.xinelu.common.core.domain.entity.SysDictData;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.crowdinfo.CrowdInfo;
import com.xinelu.manage.mapper.crowdinfo.CrowdInfoMapper;
import com.xinelu.manage.service.crowdinfo.ICrowdInfoService;
import com.xinelu.system.mapper.SysDictDataMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 人群信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-04
 */
@Service
public class CrowdInfoServiceImpl implements ICrowdInfoService {
    @Resource
    private CrowdInfoMapper crowdInfoMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询人群信息
     *
     * @param id 人群信息主键
     * @return 人群信息
     */
    @Override
    public CrowdInfo selectCrowdInfoById(Long id) {
        return crowdInfoMapper.selectCrowdInfoById(id);
    }

    /**
     * 查询人群信息列表
     *
     * @param crowdInfo 人群信息
     * @return 人群信息
     */
    @Override
    public List<CrowdInfo> selectCrowdInfoList(CrowdInfo crowdInfo) {
        List<CrowdInfo> crowdInfos = crowdInfoMapper.selectCrowdInfoList(crowdInfo);
        if (CollectionUtils.isEmpty(crowdInfos) || crowdInfos.size() == 0) {
            return crowdInfos;
        }
        //字典表匹配数据（特殊人群回显文字）
        List<SysDictData> crowdTypeList = sysDictDataMapper.selectDictDataByType("crowd_type");
        if (CollectionUtils.isEmpty(crowdTypeList) || crowdTypeList.size() == 0) {
            return crowdInfos;
        }
        List<String> collect = crowdTypeList.stream().filter(Objects::nonNull).map(SysDictData::getDictValue).distinct().map(String::toLowerCase).collect(Collectors.toList());
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByTypeList(collect);
        if (CollectionUtils.isEmpty(sysDictDataList) || sysDictDataList.size() == 0) {
            return crowdInfos;
        }
        for (CrowdInfo info : crowdInfos) {
            SysDictData crowdType = crowdTypeList.stream().filter(Objects::nonNull).filter(item -> item.getDictValue().equals(info.getCrowdType())).findFirst().orElse(new SysDictData());
            if (StringUtils.isNotBlank(crowdType.getDictLabel())) {
                info.setCrowdType(crowdType.getDictLabel());
            }
            SysDictData sysDictData = sysDictDataList.stream().filter(Objects::nonNull).filter(item -> item.getDictValue().equals(info.getSpecialType())).findFirst().orElse(new SysDictData());
            if (StringUtils.isNotBlank(sysDictData.getDictLabel())) {
                info.setSpecialType(sysDictData.getDictLabel());
            }
        }
        return crowdInfos;
    }

    /**
     * 新增人群信息
     *
     * @param crowdInfo 人群信息
     * @return 结果
     */
    @Override
    public int insertCrowdInfo(CrowdInfo crowdInfo) {
        crowdInfo.setCreateTime(LocalDateTime.now());
        crowdInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
	    crowdInfo.setDelFlag(0);
        return crowdInfoMapper.insertCrowdInfo(crowdInfo);
    }

    /**
     * 修改人群信息
     *
     * @param crowdInfo 人群信息
     * @return 结果
     */
    @Override
    public int updateCrowdInfo(CrowdInfo crowdInfo) {
        crowdInfo.setUpdateTime(LocalDateTime.now());
        crowdInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        return crowdInfoMapper.updateCrowdInfo(crowdInfo);
    }

    /**
     * 批量删除人群信息
     *
     * @param ids 需要删除的人群信息主键
     * @return 结果
     */
    @Override
    public int deleteCrowdInfoByIds(Long[] ids) {
        return crowdInfoMapper.deleteCrowdInfoByIds(ids);
    }

    /**
     * 删除人群信息信息
     *
     * @param id 人群信息主键
     * @return 结果
     */
    @Override
    public int deleteCrowdInfoById(Long id) {
        return crowdInfoMapper.deleteCrowdInfoById(id);
    }
}
