package com.fs.course.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.course.param.UserCourseComplaintRecordParam;
import com.fs.course.vo.FsUserCourseComplaintRecordPageListVO;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.vo.QwExternalContactComplaintVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCourseComplaintRecordMapper;
import com.fs.course.domain.FsUserCourseComplaintRecord;
import com.fs.course.service.IFsUserCourseComplaintRecordService;

/**
 * 看课投诉记录Service业务层处理
 *
 * @author fs
 * @date 2025-06-04
 */
@Service
public class FsUserCourseComplaintRecordServiceImpl extends ServiceImpl<FsUserCourseComplaintRecordMapper, FsUserCourseComplaintRecord> implements IFsUserCourseComplaintRecordService {

    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;

    /**
     * 查询看课投诉记录
     *
     * @param recordId 看课投诉记录主键
     * @return 看课投诉记录
     */
    @Override
    public FsUserCourseComplaintRecordPageListVO selectFsUserCourseComplaintRecordByRecordId(Long recordId)
    {
        return baseMapper.selectFsUserCourseComplaintRecordByRecordId(recordId);
    }

    @Override
    public List<QwExternalContactComplaintVO> selectFsUserCourseComplaintRecordByUserId(Long userId) {
        return qwExternalContactMapper.selectFsUserCourseComplaintRecordByUserId(userId);
    }

    /**
     * 查询看课投诉记录列表
     *
     * @param fsUserCourseComplaintRecord 看课投诉记录
     * @return 看课投诉记录
     */
    @Override
    public List<FsUserCourseComplaintRecordPageListVO> selectFsUserCourseComplaintRecordList(FsUserCourseComplaintRecord fsUserCourseComplaintRecord)
    {
        List<FsUserCourseComplaintRecordPageListVO> list = baseMapper.selectFsUserCourseComplaintRecordPageList(fsUserCourseComplaintRecord);

        // 获取外部联系人看课状态
        List<Long> userIds = list.stream().map(FsUserCourseComplaintRecordPageListVO::getUserId).collect(Collectors.toList());
        if(userIds.isEmpty()){
            return Collections.emptyList();
        }
        List<QwExternalContact> qwExternalContacts = qwExternalContactMapper.selectExternalByFsUserIds(userIds);
        Map<Long, QwExternalContact> qwExternalContactMap = qwExternalContacts.stream().collect(Collectors.toMap(QwExternalContact::getFsUserId, Function.identity(), (v1, v2) -> v1));

        for (FsUserCourseComplaintRecordPageListVO vo : list) {
            QwExternalContact contact = qwExternalContactMap.get(vo.getUserId());
            if(contact != null) {
                vo.setStatus(contact.getCommentStatus() != null && contact.getCommentStatus() == 1 ? "已拉黑" : "正常");
            } else {
                vo.setStatus("正常");
            }
        }
        return list;
    }

    /**
     * 新增看课投诉记录
     *
     * @param fsUserCourseComplaintRecord 看课投诉记录
     * @return 结果
     */
    @Override
    public int insertFsUserCourseComplaintRecord(FsUserCourseComplaintRecord fsUserCourseComplaintRecord)
    {
        fsUserCourseComplaintRecord.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsUserCourseComplaintRecord(fsUserCourseComplaintRecord);
    }

    /**
     * 修改看课投诉记录
     *
     * @param fsUserCourseComplaintRecord 看课投诉记录
     * @return 结果
     */
    @Override
    public int updateFsUserCourseComplaintRecord(FsUserCourseComplaintRecord fsUserCourseComplaintRecord)
    {
        return baseMapper.updateFsUserCourseComplaintRecord(fsUserCourseComplaintRecord);
    }

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

    /**
     * 删除看课投诉记录信息
     *
     * @param recordId 看课投诉记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseComplaintRecordByRecordId(Long recordId)
    {
        return baseMapper.deleteFsUserCourseComplaintRecordByRecordId(recordId);
    }

    @Override
    public int submitRecord(UserCourseComplaintRecordParam userCourseComplaintRecordParam) {
        FsUserCourseComplaintRecord fsUserCourseComplaintRecord = new FsUserCourseComplaintRecord();
        BeanUtils.copyProperties(userCourseComplaintRecordParam, fsUserCourseComplaintRecord);
        fsUserCourseComplaintRecord.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsUserCourseComplaintRecord(fsUserCourseComplaintRecord);
    }

}
