package com.fs.course.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.DictUtils;
import com.fs.company.cache.ICompanyCacheService;
import com.fs.company.cache.ICompanyUserCacheService;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyUser;
import com.fs.course.domain.FsCourseAnswerLogs;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.mapper.FsCourseAnswerLogsMapper;
import com.fs.course.param.FsCourseAnswerLogsParam;
import com.fs.course.service.IFsCourseAnswerLogsService;
import com.fs.course.service.cache.IFsUserCourseVideoCacheService;
import com.fs.course.vo.FsCourseAnswerLogsListVO;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.store.service.cache.IFsUserCacheService;
import com.hc.openapi.tool.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 答题日志Service业务层处理
 *
 * @author fs
 * @date 2024-10-26
 */
@Service
@RequiredArgsConstructor
public class FsCourseAnswerLogsServiceImpl implements IFsCourseAnswerLogsService
{
    private final IFsUserCourseVideoCacheService fsUserCourseVideoCacheService;

    @Autowired
    private FsCourseAnswerLogsMapper fsCourseAnswerLogsMapper;

    private final IFsUserCacheService fsUserCacheService;

    private final ICompanyCacheService companyCacheService;

    private final ICompanyUserCacheService companyUserCacheService;

    private final IFsUserService fsUserService;

    /**
     * 查询答题日志
     *
     * @param logId 答题日志主键
     * @return 答题日志
     */
    @Override
    public FsCourseAnswerLogs selectFsCourseAnswerLogsByLogId(Long logId)
    {
        return fsCourseAnswerLogsMapper.selectFsCourseAnswerLogsByLogId(logId);
    }

    /**
     * 查询答题日志列表
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 答题日志
     */
    @Override
    public List<FsCourseAnswerLogs> selectFsCourseAnswerLogsList(FsCourseAnswerLogs fsCourseAnswerLogs)
    {
        return fsCourseAnswerLogsMapper.selectFsCourseAnswerLogsList(fsCourseAnswerLogs);
    }

    @Override
    public List<FsCourseAnswerLogsListVO> selectFsCourseAnswerLogsListVO(FsCourseAnswerLogsParam param) {
        return fsCourseAnswerLogsMapper.selectFsCourseAnswerLogsListVO(param);
    }

    /**
     * 新增答题日志
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 结果
     */
    @Override
    public int insertFsCourseAnswerLogs(FsCourseAnswerLogs fsCourseAnswerLogs)
    {
        fsCourseAnswerLogs.setCreateTime(DateUtils.getNowDate());
        return fsCourseAnswerLogsMapper.insertFsCourseAnswerLogs(fsCourseAnswerLogs);
    }

    /**
     * 修改答题日志
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 结果
     */
    @Override
    public int updateFsCourseAnswerLogs(FsCourseAnswerLogs fsCourseAnswerLogs)
    {
        return fsCourseAnswerLogsMapper.updateFsCourseAnswerLogs(fsCourseAnswerLogs);
    }

    /**
     * 批量删除答题日志
     *
     * @param logIds 需要删除的答题日志主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseAnswerLogsByLogIds(Long[] logIds)
    {
        return fsCourseAnswerLogsMapper.deleteFsCourseAnswerLogsByLogIds(logIds);
    }

    /**
     * 删除答题日志信息
     *
     * @param logId 答题日志主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseAnswerLogsByLogId(Long logId)
    {
        return fsCourseAnswerLogsMapper.deleteFsCourseAnswerLogsByLogId(logId);
    }

    @Override
    public List<FsCourseAnswerLogsListVO> selectFsCourseAnswerLogsListVONew(FsCourseAnswerLogsParam param) {

        if(StringUtils.isNotEmpty(param.getUserName())){
            List<FsUser> fsUsers = fsUserService.selectFsUserListByJointUserNameKey(param.getUserName());
            if(fsUsers.isEmpty()) {
                return Collections.emptyList();
            }
            Set<Long> userIds = fsUsers.stream().map(FsUser::getUserId).collect(Collectors.toSet());
            param.setUserIds(userIds);
        }

        List<FsCourseAnswerLogsListVO> data = fsCourseAnswerLogsMapper.selectFsCourseAnswerLogsListVONew(param);
        for (FsCourseAnswerLogsListVO datum : data) {
            FsUserCourseVideo fsUserCourseVideo = fsUserCourseVideoCacheService.selectFsUserCourseVideoByVideoId(datum.getVideoId());
            if (fsUserCourseVideo != null) {
                datum.setVideoName(fsUserCourseVideo.getTitle());
                datum.setCourseId(fsUserCourseVideo.getCourseId());
            }
            // 用户
            FsUser fsUser = fsUserCacheService.selectFsUserById(datum.getUserId());
            if(fsUser != null) {
                datum.setUserName(String.format("%s_%d",fsUser.getNickName(),fsUser.getUserId()));
                datum.setFsAvatar(fsUser.getAvatar());
            }

            // 公司名称
            if(datum.getCompanyId() != null) {
                Company company = companyCacheService.selectCompanyById(datum.getCompanyId());
                if(company != null) {
                    datum.setCompanyName(String.format("%s_%d", company.getCompanyName(), company.getCompanyId()));
                }
            }
            // 销售
            if(datum.getCompanyUserId() != null) {
                CompanyUser companyUser = companyUserCacheService.selectCompanyUserById(datum.getCompanyUserId());
                if(companyUser != null) {
                    datum.setCompanyUserName(String.format("%s_%d",companyUser.getUserName(),companyUser.getUserId()));
                }
            }
            // 项目名
            if(datum.getProject() != null) {
                String sysCourseProject = DictUtils.getDictLabel("sys_course_project", datum.getProject());
                if(StringUtils.isNotBlank(sysCourseProject)){
                    datum.setProjectName(sysCourseProject);
                }
            }

            // 是否全部正确
            if(ObjectUtil.isNotNull(datum.getIsRight())){
                String sysCompanyOr = DictUtils.getDictLabel("sys_company_or", String.valueOf(datum.getIsRight()));
                if(StringUtils.isNotBlank(sysCompanyOr)){
                    datum.setIsRightText(sysCompanyOr);
                }
            }
        }

        return data;
    }

    @Override
    public Long selectFsCourseAnswerLogsListVONewCount(FsCourseAnswerLogsParam param) {
        if(StringUtils.isNotEmpty(param.getUserName())){
            List<FsUser> fsUsers = fsUserService.selectFsUserListByJointUserNameKey(param.getUserName());
            if(fsUsers.isEmpty()) {
                return 0L;
            }
            Set<Long> userIds = fsUsers.stream().map(FsUser::getUserId).collect(Collectors.toSet());
            param.setUserIds(userIds);
        }
        return fsCourseAnswerLogsMapper.selectFsCourseAnswerLogsListVONewCount(param);
    }

}
