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

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.common.utils.file.FileUploadUtils;
import com.xinelu.manage.dto.infocategory.InfoCategoryDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.xinelu.manage.mapper.infocategory.InfoCategoryMapper;
import com.xinelu.manage.domain.infocategory.InfoCategory;
import com.xinelu.manage.service.infocategory.IInfoCategoryService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 资讯分类Service业务层处理
 *
 * @author xinelu
 * @date 2024-04-11
 */
@Service
public class InfoCategoryServiceImpl implements IInfoCategoryService {
    @Resource
    private InfoCategoryMapper infoCategoryMapper;

    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    /**
     * 查询资讯分类
     *
     * @param id 资讯分类主键
     * @return 资讯分类
     */
    @Override
    public InfoCategory selectInfoCategoryById(Long id) {
        return infoCategoryMapper.selectInfoCategoryById(id);
    }

    /**
     * 查询资讯分类列表
     *
     * @param infoCategory 资讯分类
     * @return 资讯分类
     */
    @Override
    public List<InfoCategory> selectInfoCategoryList(InfoCategory infoCategory) {
        return infoCategoryMapper.selectInfoCategoryList(infoCategory);
    }

    /**
     * 新增资讯分类
     *
     * @param infoCategoryDTO 资讯分类
     * @return 结果
     */
    @Override
    public AjaxResult insertInfoCategory(InfoCategoryDTO infoCategoryDTO) {
        //筛选不为空资讯分类名称
        List<String> categoryNameList = infoCategoryDTO.getInfoCategoryList().stream().filter(Objects::nonNull).map(InfoCategory::getInfoCategoryName).collect(Collectors.toList());
        //资讯分类名称去重
        List<String> distinctNameList = infoCategoryDTO.getInfoCategoryList().stream().filter(Objects::nonNull).map(InfoCategory::getInfoCategoryName).distinct().collect(Collectors.toList());

        //集合取差集
        List<String> subtractNameList = new ArrayList<>(CollectionUtils.subtract(categoryNameList, distinctNameList));
        if (subtractNameList.size() > 0) {
            return AjaxResult.error("'" + subtractNameList.get(0) + "'重复，请修改后重新录入！");
        }
        //数据库查询资讯分类名称
        List<String> existCategoryName = infoCategoryMapper.selectInfoCategoryName(categoryNameList);

        List<String> existRetainAllNameList = new ArrayList<>(CollectionUtils.intersection(distinctNameList, existCategoryName));
        if (existRetainAllNameList.size() > 0) {
            return AjaxResult.error("'" + existRetainAllNameList.get(0) + "'已存在，请修改后重新录入！");
        }
        for (InfoCategory infocategory : infoCategoryDTO.getInfoCategoryList()) {

            infocategory.setCreateTime(LocalDateTime.now());//设置添加时间
            infocategory.setInfoCategoryCreator(SecurityUtils.getUsername());//设置当前的用户
            infocategory.setInfoCategoryType(infocategory.getInfoCategoryName());//类型 无用  直接赋值和名称相同
            infocategory.setInfoCategoryCode(Constants.INFO_CATEGORY_CODE + generateSystemCodeUtil.generateSystemCode(Constants.INFO_CATEGORY_CODE));
        }
        int insertCount = infoCategoryMapper.insertInfoCategoryList(infoCategoryDTO.getInfoCategoryList());
        if (insertCount < 0) {
            throw new RuntimeException("新增资讯分类信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改资讯分类
     *
     * @param infoCategory 资讯分类
     * @return 结果
     */
    @Override
    public AjaxResult updateInfoCategory(InfoCategory infoCategory) {
        //根据咨询分类id查询数据库
       // InfoCategory infoCategoryByName = infoCategoryMapper.selectInfoCategoryByName(infoCategory.getInfoCategoryName());
        InfoCategory infoCategoryByName = infoCategoryMapper.selectInfoCategoryById(infoCategory.getId());
        if (Objects.isNull(infoCategoryByName)) {
            return AjaxResult.error("当前资讯分类信息不存在，无法修改，请联系管理员！");
        }
        //两名称都不为空，且不相等
        if (StringUtils.isNotBlank(infoCategory.getInfoCategoryName()) && StringUtils.isNotBlank(infoCategoryByName.getInfoCategoryName()) && !infoCategory.getInfoCategoryName().equals(infoCategoryByName.getInfoCategoryName())) {
            int infoCategoryCount = infoCategoryMapper.selectInfoCategoryCount(infoCategory.getInfoCategoryName());//查找要修改的名称是否已经存在了
            if (infoCategoryCount > 0) {
                return AjaxResult.error("已有该资讯分类信息名称:" + infoCategory.getInfoCategoryName());
            }
        }
        infoCategoryByName.setUpdateTime(LocalDateTime.now());//设置时间
        infoCategoryByName.setInfoCategoryName(infoCategory.getInfoCategoryName());//设置名称为修改的名称
        infoCategoryByName.setInfoCategorySort(infoCategory.getInfoCategorySort());//设置排序
        infoCategoryByName.setUpdateBy(SecurityUtils.getUsername());//设置当前用户
        infoCategoryByName.setInfoCategoryType(infoCategory.getInfoCategoryName());//类型
        int updateInfoCategory = infoCategoryMapper.updateInfoCategory(infoCategoryByName);//更新到数据库
        if (updateInfoCategory < 0) {
            return AjaxResult.error("修改资讯分类信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除资讯分类
     *
     * @param ids 需要删除的资讯分类主键
     * @return 结果
     */
    @Override
    public int deleteInfoCategoryByIds(Long[] ids) {
        return infoCategoryMapper.deleteInfoCategoryByIds(ids);
    }

    /**
     * * 删除资讯分类信息
     *
     * @param id 资讯分类主键
     * @return 结果
     */
    @Override
    public int deleteInfoCategoryById(Long id) {
        return infoCategoryMapper.deleteInfoCategoryById(id);
    }


}
