package com.fs.course.service.impl;

import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.DictUtils;
import com.fs.company.cache.ICompanyCacheService;
import com.fs.course.param.FsCourseTrafficLogParam;
import com.fs.course.vo.FsCourseTrafficLogListVO;
import com.fs.store.service.cache.IFsUserCourseCacheService;
import com.hc.openapi.tool.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsCourseTrafficLogMapper;
import com.fs.course.domain.FsCourseTrafficLog;
import com.fs.course.service.IFsCourseTrafficLogService;

/**
 * 短链课程流量记录Service业务层处理
 *
 * @author fs
 * @date 2024-10-31
 */
@Service
public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
{
    @Autowired
    private FsCourseTrafficLogMapper fsCourseTrafficLogMapper;
    @Autowired
    private ICompanyCacheService companyCacheService;
    @Autowired
    private IFsUserCourseCacheService fsUserCourseCacheService;

    /**
     * 查询短链课程流量记录
     *
     * @param logId 短链课程流量记录主键
     * @return 短链课程流量记录
     */
    @Override
    public FsCourseTrafficLog selectFsCourseTrafficLogByLogId(Long logId)
    {
        return fsCourseTrafficLogMapper.selectFsCourseTrafficLogByLogId(logId);
    }

    /**
     * 查询短链课程流量记录列表
     *
     * @param fsCourseTrafficLog 短链课程流量记录
     * @return 短链课程流量记录
     */
    @Override
    public List<FsCourseTrafficLog> selectFsCourseTrafficLogList(FsCourseTrafficLog fsCourseTrafficLog)
    {
        List<FsCourseTrafficLog> fsCourseTrafficLogs = fsCourseTrafficLogMapper.selectFsCourseTrafficLogList(fsCourseTrafficLog);

        for (FsCourseTrafficLog log : fsCourseTrafficLogs) {
            if(ObjectUtils.isNotNull(log.getProject())){
                String sysCourseProject = DictUtils.getDictLabel("sys_course_project", String.valueOf(log.getProject()));
                if(StringUtils.isNotBlank(sysCourseProject)){
                    log.setProjectName(sysCourseProject);
                }
            }

            if(ObjectUtils.isNotNull(log.getCompanyId())){
                String companyName = companyCacheService.selectCompanyNameById(log.getCompanyId());
                if(StringUtils.isNotBlank(companyName)){
                    log.setCompanyName(companyName);
                }
            }

            if(ObjectUtils.isNotNull(log.getCourseId())){
                String courseName = fsUserCourseCacheService.selectCourseNameByCourseId(log.getCourseId());
                if(StringUtils.isNotBlank(courseName)){
                    log.setCourseName(courseName);
                }
            }
        }

        return fsCourseTrafficLogs;
    }

    /**
     * 新增短链课程流量记录
     *
     * @param fsCourseTrafficLog 短链课程流量记录
     * @return 结果
     */
    @Override
    public int insertFsCourseTrafficLog(FsCourseTrafficLog fsCourseTrafficLog)
    {
        fsCourseTrafficLog.setCreateTime(DateUtils.getNowDate());
        return fsCourseTrafficLogMapper.insertFsCourseTrafficLog(fsCourseTrafficLog);
    }

    /**
     * 修改短链课程流量记录
     *
     * @param fsCourseTrafficLog 短链课程流量记录
     * @return 结果
     */
    @Override
    public int updateFsCourseTrafficLog(FsCourseTrafficLog fsCourseTrafficLog)
    {
        return fsCourseTrafficLogMapper.updateFsCourseTrafficLog(fsCourseTrafficLog);
    }

    /**
     * 批量删除短链课程流量记录
     *
     * @param logIds 需要删除的短链课程流量记录主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseTrafficLogByLogIds(Long[] logIds)
    {
        return fsCourseTrafficLogMapper.deleteFsCourseTrafficLogByLogIds(logIds);
    }

    /**
     * 删除短链课程流量记录信息
     *
     * @param logId 短链课程流量记录主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseTrafficLogByLogId(Long logId)
    {
        return fsCourseTrafficLogMapper.deleteFsCourseTrafficLogByLogId(logId);
    }

    @Override
    public List<FsCourseTrafficLogListVO> selectTrafficByCompany(FsCourseTrafficLogParam param) {
        return fsCourseTrafficLogMapper.selectTrafficByCompany(param);
    }

    @Override
    public List<FsCourseTrafficLogListVO> selectTrafficNew(FsCourseTrafficLogParam param) {
        if(StringUtils.isBlank(param.getStartDate()) && StringUtils.isBlank(param.getEndDate())){
            throw new CustomException("搜索必须要一个时间范围!");
        }
        List<FsCourseTrafficLogListVO> fsCourseTrafficLogListVOS = fsCourseTrafficLogMapper.selectTrafficNew(param);
        for (FsCourseTrafficLogListVO log : fsCourseTrafficLogListVOS) {
            if (ObjectUtils.isNotNull(log.getProject())) {
                String sysCourseProject = DictUtils.getDictLabel("sys_course_project", String.valueOf(log.getProject()));
                if (StringUtils.isNotBlank(sysCourseProject)) {
                    log.setProjectName(sysCourseProject);
                }
            }

            if (ObjectUtils.isNotNull(log.getCompanyId())) {
                String companyName = companyCacheService.selectCompanyNameById(log.getCompanyId());
                if (StringUtils.isNotBlank(companyName)) {
                    log.setCompanyName(companyName);
                }
            }

            if (ObjectUtils.isNotNull(log.getCourseId())) {
                String courseName = fsUserCourseCacheService.selectCourseNameByCourseId(log.getCourseId());
                if (StringUtils.isNotBlank(courseName)) {
                    log.setCourseName(courseName);
                }
            }
        }
        return fsCourseTrafficLogListVOS;
    }
}
