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

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.manage.domain.informationcategory.InformationCategory;
import com.xinelu.manage.dto.informationcategory.InformationCategoryDTO;
import com.xinelu.manage.mapper.informationcategory.InformationCategoryMapper;
import com.xinelu.manage.service.informationcategory.IInformationCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 资讯分类Service业务层处理
 *
 * @author xinyilu
 * @date 2023-02-14
 */
@Service
public class InformationCategoryServiceImpl implements IInformationCategoryService {
    @Resource
    private InformationCategoryMapper informationCategoryMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    /**
     * 查询资讯分类
     *
     * @param id 资讯分类主键
     * @return 资讯分类
     */
    @Override
    public InformationCategory selectInformationCategoryById(Long id) {
        return informationCategoryMapper.selectInformationCategoryById(id);
    }

    /**
     * 查询资讯分类列表
     *
     * @param informationCategory 资讯分类
     * @return 资讯分类
     */
    @Override
    public List<InformationCategory> selectInformationCategoryList(InformationCategory informationCategory) {
        return informationCategoryMapper.selectInformationCategoryList(informationCategory);
    }

    /**
     * 新增资讯分类
     *
     * @param informationCategoryList 资讯分类
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertInformationCategory(InformationCategoryDTO informationCategoryList) {
        //筛选资讯分类名称
        List<String> categoryNameList = informationCategoryList.getInformationCategoryList().stream().filter(Objects::nonNull).map(InformationCategory::getInformationCategoryName).collect(Collectors.toList());
        //资讯分类名称去重
        List<String> distinctNameList = informationCategoryList.getInformationCategoryList().stream().filter(Objects::nonNull).map(InformationCategory::getInformationCategoryName).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 = informationCategoryMapper.selectInformationCategoryName(categoryNameList);
        //集合取交集
        List<String> existRetainAllNameList = new ArrayList<>(CollectionUtils.intersection(distinctNameList, existCategoryName));
        if (existRetainAllNameList.size() > 0) {
            return AjaxResult.error("'" + existRetainAllNameList.get(0) + "'已存在，请修改后重新录入！");
        }
        //新增资讯分类信息
        for (InformationCategory informationCategory : informationCategoryList.getInformationCategoryList()) {
            informationCategory.setCreateTime(LocalDateTime.now());
            informationCategory.setCreateBy(SecurityUtils.getUsername());
            informationCategory.setInformationCategoryCode(Constants.INFORMATION_CATEGORY_CODE + generateSystemCodeUtil.generateSystemCode(Constants.DEPARTMENT_CODE));
        }
        int insertCount = informationCategoryMapper.insertInformationCategoryList(informationCategoryList.getInformationCategoryList());
        if (insertCount < 0) {
            throw new RuntimeException("新增咨询分类信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改资讯分类
     *
     * @param informationCategory 资讯分类
     * @return 结果
     */
    @Override
    public AjaxResult updateInformationCategory(InformationCategory informationCategory) {
        //根据咨询分类id查询数据库
        InformationCategory informationCategoryById = informationCategoryMapper.selectInformationCategoryById(informationCategory.getId());
        if (Objects.isNull(informationCategoryById)) {
            return AjaxResult.error("当前资讯分类信息不存在，无法修改，请联系管理员！");
        }
        if (StringUtils.isNotBlank(informationCategory.getInformationCategoryName())
                && StringUtils.isNotBlank(informationCategoryById.getInformationCategoryName())
                && !informationCategory.getInformationCategoryName().equals(informationCategoryById.getInformationCategoryName())) {
            int informationCategoryCount = informationCategoryMapper.selectInformationCategoryCount(informationCategory.getInformationCategoryName());
            if (informationCategoryCount > 0) {
                return AjaxResult.error("已有该资讯分类信息名称:" + informationCategory.getInformationCategoryName());
            }
        }
        informationCategory.setUpdateTime(LocalDateTime.now());
        informationCategory.setUpdateBy(SecurityUtils.getUsername());
        int updateInformationCategory = informationCategoryMapper.updateInformationCategory(informationCategory);
        if (updateInformationCategory < 0) {
            return AjaxResult.error("修改资讯分类信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除资讯分类
     *
     * @param ids 需要删除的资讯分类主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteInformationCategoryByIds(Long[] ids) {
        return informationCategoryMapper.deleteInformationCategoryByIds(ids);
    }

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