package com.fs.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsUserCourseComplaintType;
import com.fs.course.mapper.FsUserCourseComplaintTypeMapper;
import com.fs.course.service.IFsUserCourseComplaintTypeService;
import com.fs.course.vo.FsUserCourseComplaintTypeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 看课投诉类型Service业务层处理
 *
 * @author fs
 * @date 2025-06-04
 */
@Service
public class FsUserCourseComplaintTypeServiceImpl extends ServiceImpl<FsUserCourseComplaintTypeMapper, FsUserCourseComplaintType> implements IFsUserCourseComplaintTypeService {

    /**
     * 查询看课投诉类型
     *
     * @param complaintTypeId 看课投诉类型主键
     * @return 看课投诉类型
     */
    @Override
    public FsUserCourseComplaintType selectFsUserCourseComplaintTypeByComplaintTypeId(Long complaintTypeId)
    {
        return baseMapper.selectFsUserCourseComplaintTypeByComplaintTypeId(complaintTypeId);
    }

    /**
     * 查询看课投诉类型列表
     *
     * @param fsUserCourseComplaintType 看课投诉类型
     * @return 看课投诉类型
     */
    @Override
    public List<FsUserCourseComplaintType> selectFsUserCourseComplaintTypeList(FsUserCourseComplaintType fsUserCourseComplaintType)
    {
        return baseMapper.selectFsUserCourseComplaintTypeList(fsUserCourseComplaintType);
    }

    /**
     * 新增看课投诉类型
     *
     * @param fsUserCourseComplaintType 看课投诉类型
     * @return 结果
     */
    @Override
    public int insertFsUserCourseComplaintType(FsUserCourseComplaintType fsUserCourseComplaintType)
    {
        fsUserCourseComplaintType.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsUserCourseComplaintType(fsUserCourseComplaintType);
    }

    /**
     * 修改看课投诉类型
     *
     * @param fsUserCourseComplaintType 看课投诉类型
     * @return 结果
     */
    @Override
    public int updateFsUserCourseComplaintType(FsUserCourseComplaintType fsUserCourseComplaintType)
    {
        fsUserCourseComplaintType.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsUserCourseComplaintType(fsUserCourseComplaintType);
    }

    /**
     * 批量删除看课投诉类型
     *
     * @param complaintTypeIds 需要删除的看课投诉类型主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseComplaintTypeByComplaintTypeIds(Long[] complaintTypeIds)
    {
        return baseMapper.deleteFsUserCourseComplaintTypeByComplaintTypeIds(complaintTypeIds);
    }

    /**
     * 删除看课投诉类型信息
     *
     * @param complaintTypeId 看课投诉类型主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseComplaintTypeByComplaintTypeId(Long complaintTypeId)
    {
        return baseMapper.deleteFsUserCourseComplaintTypeByComplaintTypeId(complaintTypeId);
    }

    @Override
    public List<FsUserCourseComplaintTypeListVO> getAllComplaintTypeTree() {
        FsUserCourseComplaintType fsUserCourseComplaintType = new FsUserCourseComplaintType();
        List<FsUserCourseComplaintType> list = baseMapper.selectFsUserCourseComplaintTypeList(fsUserCourseComplaintType);
        List<FsUserCourseComplaintTypeListVO> collect = list.stream().map(v -> {
            FsUserCourseComplaintTypeListVO listVO = new FsUserCourseComplaintTypeListVO();
            BeanUtils.copyProperties(v, listVO);
            return listVO;
        }).collect(Collectors.toList());

        //构造树列表
        return this.buildTree(collect);
    }

    private List<FsUserCourseComplaintTypeListVO> buildTree(List<FsUserCourseComplaintTypeListVO> complaintTypeList) {
        List<FsUserCourseComplaintTypeListVO> complaintTypeVOList = new ArrayList<>();
        Map<Long, FsUserCourseComplaintTypeListVO> complaintTypeMap = new HashMap<>();

        for (FsUserCourseComplaintTypeListVO complaintTypeVO : complaintTypeList) {
            complaintTypeVO.setChildrenType(new ArrayList<>());
            complaintTypeMap.put(complaintTypeVO.getComplaintTypeId(), complaintTypeVO);
        }

        // 构造树列表
        for (FsUserCourseComplaintTypeListVO complaintTypeVO : complaintTypeList) {
            if(complaintTypeVO.getParentId() == null || complaintTypeVO.getParentId() == 0){
                complaintTypeVOList.add(complaintTypeVO);
            } else {
                FsUserCourseComplaintTypeListVO complaintTypeListVOChildren = complaintTypeMap.get(complaintTypeVO.getParentId());
                if(complaintTypeListVOChildren != null){
                    complaintTypeListVOChildren.getChildrenType().add(complaintTypeVO);
                }
            }
        }
        return complaintTypeVOList;
    }
}
