package com.microservices.dms.behaviorImage.service.impl;

import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.dms.achievementLibrary.domain.KeyValueVo;
import com.microservices.dms.behaviorImage.domain.*;
import com.microservices.dms.behaviorImage.mapper.BehaviorImageMapper;
import com.microservices.dms.behaviorImage.service.IBehaviorImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class BehaviorImageServiceImpl implements IBehaviorImageService {
    @Autowired
    private BehaviorImageMapper behaviorImageMapper;
    @Override
    public AchievementImageVo getAchievementImageById(Long id) {
        AchievementImageVo achievementImageVo = new AchievementImageVo();
        //获取所有行为汇总数据
        AchievementBehaviorSumVo achievementBehaviorSumVo = behaviorImageMapper.getBehaviorSumListByType();
        if(StringUtils.isNotNull(achievementBehaviorSumVo)) {
            float score = 0f;
            //从行为表中获取成果行为数据权限
            List<KeyValueVo> behaviorImageWeightList = behaviorImageMapper.getWeightListByType(BehaviorContant.ACHIEVEMENTS_IMAGE);
            float clickResult = 0L;
            float watchResult = 0L;
            float searchResult = 0L;
            float favoriteResult = 0L;
            float fileDownloadResult = 0L;
            //简单算法
            for (KeyValueVo keyValueVo : behaviorImageWeightList) {
                String bhavior = keyValueVo.getKey();
                Long weight = keyValueVo.getValue();
                if (bhavior.equals(BehaviorContant.CLICK_BEHAVIOR)) {//点击
                    //根据当前成果ID,获取成果点击行为数量
                    Long clickSum = achievementBehaviorSumVo.getClickSum();
                    if(StringUtils.isNotNull(clickSum) && clickSum > 0) {
                        Long click = behaviorImageMapper.getClickSumByAchievementId(id, BehaviorContant.ACHIEVEMENTS_IMAGE);
                        if (StringUtils.isNotNull(click)) {
                            clickResult = click * weight / clickSum ;
                        }
                    }
                } else if (bhavior.equals(BehaviorContant.FAVORITE_BEHAVIOR)) {//收藏
                    //根据当前成果ID,获取成果收藏行为数量
                    Long favoriteSum = achievementBehaviorSumVo.getFavoriteSum();
                    if(StringUtils.isNotNull(favoriteSum) && favoriteSum > 0) {
                        Long favorite = behaviorImageMapper.getFavoriteSumByAchievementId(id,BehaviorContant.ACHIEVEMENTS_IMAGE);
                        if (StringUtils.isNotNull(favorite)) {
                            favoriteResult = favorite *weight / favoriteSum ;
                        }
                    }
                } else if (bhavior.equals(BehaviorContant.SEARCH_BEHAVIOR)) {//搜索
                    //根据当前成果ID,获取成果搜索行为数量
                    Long searchSum = achievementBehaviorSumVo.getSearchSum();
                    if(StringUtils.isNotNull(searchSum) && searchSum > 0) {
                        Long search = behaviorImageMapper.getSearchSumByAchievementId(id, BehaviorContant.ACHIEVEMENTS_IMAGE);
                        if (StringUtils.isNotNull(search)) {
                            searchResult = search  *weight / searchSum;
                        }
                    }
                } else if (bhavior.equals(BehaviorContant.WATCH_BEHAVIOR)) {//关注
                    //根据当前成果ID,获取成果关注行为数量
                    Long  watchSum = achievementBehaviorSumVo.getFavoriteSum();
                    if(StringUtils.isNotNull( watchSum) && watchSum > 0) {
                        Long  watch = behaviorImageMapper.getWatchSumByAchievementId(id,BehaviorContant.ACHIEVEMENTS_IMAGE);
                        if (StringUtils.isNotNull( watch) ) {
                            watchResult =  watch  *weight / watchSum;
                        }
                    }
                } else if (bhavior.equals(BehaviorContant.FILE_DOWNLOAD_BEHAVIOR)) {//文件下载
                    //根据当前成果ID,获取成果文件下载行为数量
                    Long fileDownloadSum = achievementBehaviorSumVo.getFavoriteSum();
                    if(StringUtils.isNotNull(fileDownloadSum) && fileDownloadSum > 0) {
                        Long fileDownload = behaviorImageMapper.getFileDownloadSumByAchievementId(id, BehaviorContant.ACHIEVEMENTS_IMAGE);
                        if (StringUtils.isNotNull(fileDownload)) {
                            fileDownloadResult = fileDownload *weight / fileDownloadSum ;
                        }
                    }
                }
            }

            score = clickResult+watchResult+searchResult+favoriteResult+fileDownloadResult;
            achievementImageVo.setScore(score);
        }
        return achievementImageVo;
    }
    /**
     * 查询画像行为权重
     *
     * @param id 画像行为权重主键
     * @return 画像行为权重
     */
    @Override
    public BehaviorImageWeight selectBehaviorImageWeightById(Long id)
    {
        return behaviorImageMapper.selectBehaviorImageWeightById(id);
    }

    /**
     * 查询画像行为权重列表
     *
     * @param behaviorImageWeight 画像行为权重
     * @return 画像行为权重
     */
    @Override
    public List<BehaviorImageWeight> selectBehaviorImageWeightList(BehaviorImageWeight behaviorImageWeight)
    {
        return behaviorImageMapper.selectBehaviorImageWeightList(behaviorImageWeight);
    }

    /**
     * 新增画像行为权重
     *
     * @param behaviorImageWeight 画像行为权重
     * @return 结果
     */
    @Override
    public int insertBehaviorImageWeight(BehaviorImageWeight behaviorImageWeight)
    {
        behaviorImageWeight.setCreateTime(DateUtils.getNowDate());
        return behaviorImageMapper.insertBehaviorImageWeight(behaviorImageWeight);
    }

    /**
     * 修改画像行为权重
     *
     * @param behaviorImageWeight 画像行为权重
     * @return 结果
     */
    @Override
    public int updateBehaviorImageWeight(BehaviorImageWeight behaviorImageWeight)
    {
        behaviorImageWeight.setUpdateTime(DateUtils.getNowDate());
        return behaviorImageMapper.updateBehaviorImageWeight(behaviorImageWeight);
    }

    /**
     * 批量删除画像行为权重
     *
     * @param ids 需要删除的画像行为权重主键
     * @return 结果
     */
    @Override
    public int deleteBehaviorImageWeightByIds(Long[] ids)
    {
        return behaviorImageMapper.deleteBehaviorImageWeightByIds(ids);
    }

    /**
     * 删除画像行为权重信息
     *
     * @param id 画像行为权重主键
     * @return 结果
     */
    @Override
    public int deleteBehaviorImageWeightById(Long id)
    {
        return behaviorImageMapper.deleteBehaviorImageWeightById(id);
    }


    /**
     * 用户新增分类数据统计
     * @param userId
     * @return
     */
    public List<UserTypeTotalVo> getUserTypeStatistic(Long userId) {
        return behaviorImageMapper.getUserTypeStatistic(userId);
    }



    /**
     * 根据用户ID,获取开源项目相关汇总
     * @param userId
     * @return
     */
    @Override
    public UserProjectTotalVo getUserProjectStatistic(Long userId) {
        UserProjectTotalVo userProjectTotalVo = new UserProjectTotalVo();
        List<KeyValueVo> resultVo = behaviorImageMapper.getUserProjecttatistic(userId);
        if(StringUtils.isNotNull(resultVo)) {
            for(KeyValueVo temResult : resultVo) {
                Long result = temResult.getValue();
                if(temResult.getKey().equals("createProjectCount")) {
                    userProjectTotalVo.setCreateProjectCount(result);
                }else if(temResult.getKey().equals("joinProjectCount")){
                    userProjectTotalVo.setJoinProjectCount(result);
                }else if(temResult.getKey().equals("forkProjectCount")){
                    userProjectTotalVo.setForkProjectCount(result);
                }else if(temResult.getKey().equals("issueSum")){
                    userProjectTotalVo.setIssueSum(result);
                }else if(temResult.getKey().equals("prSum")){
                    userProjectTotalVo.setPrSum(result);
                }else if(temResult.getKey().equals("commitSum")){
                    userProjectTotalVo.setCommitSum(result);
                }else if(temResult.getKey().equals("watchProjectCount")){
                    userProjectTotalVo.setWatchProjectCount(result);
                }else if(temResult.getKey().equals("praiseProjectCount")){
                    userProjectTotalVo.setPraiseProjectCount(result);
                }
            }
        }
        return userProjectTotalVo;
    }



    /**
     * 根据用户ID,获取创客任务相关汇总
     * @param userId
     * @return
     */
    @Override
    public UserTaskTotalVo getUserTaskStatistic(Long userId) {
        UserTaskTotalVo userTaskTotalVo = new UserTaskTotalVo();
        List<KeyValueVo> resultVo = behaviorImageMapper.getUserTaskStatistic(userId);
        if(StringUtils.isNotNull(resultVo)) {
            for(KeyValueVo temResult : resultVo) {
                Long result = temResult.getValue();
                if(temResult.getKey().equals("createTaskCount")) {
                    userTaskTotalVo.setCreateTaskCount(result);
                }else if(temResult.getKey().equals("submitPapersCount")){
                    userTaskTotalVo.setSubmitPapersCount(result);
                }else if(temResult.getKey().equals("watchTaskCount")){
                    userTaskTotalVo.setWatchTaskCount(result);
                }
            }
        }
        return userTaskTotalVo;
    }

    /**
     * 根据用户ID,获取开放竞赛相关汇总
     * @param userId
     * @return
     */
    @Override
    public UserCompetitionTotalVo getUserCompStatistic(Long userId) {
        UserCompetitionTotalVo userCompetitionTotalVo = new UserCompetitionTotalVo();
        List<KeyValueVo> resultVo = behaviorImageMapper.getUserCompStatistic(userId);
        if(StringUtils.isNotNull(resultVo)) {
            for(KeyValueVo temResult : resultVo) {
                Long result = temResult.getValue();
                if(temResult.getKey().equals("joinCompetitionCount")) {
                    userCompetitionTotalVo.setJoinCompetitionCount(result);
                }else if(temResult.getKey().equals("watchCompetitionCount")){
                    userCompetitionTotalVo.setWatchCompetitionCount(result);
                }else if(temResult.getKey().equals("signUpCount")){
                    userCompetitionTotalVo.setSignUpCount(result);
                }else if(temResult.getKey().equals("submitWorkCount")){
                    userCompetitionTotalVo.setSubmitWorkCount(result);
                }
            }
        }
        return userCompetitionTotalVo;
    }

    /**
     * 根据用户ID, 获取用户论坛交流相关数据
     * @param userId
     * @return
     */
    @Override
    public UserMemoTotalVo getUserMemoStatistic(Long userId) {
        UserMemoTotalVo userMemoTotalVo = new UserMemoTotalVo();
        List<KeyValueVo> resultVo = behaviorImageMapper.getUserMemoStatistic(userId);
        if(StringUtils.isNotNull(resultVo)) {
            for(KeyValueVo temResult : resultVo) {
                Long result = temResult.getValue();
                if(temResult.getKey().equals("createMemoCount")) {
                    userMemoTotalVo.setCreateMemoCount(result);
                }else if(temResult.getKey().equals("replayMemoCount")){
                    userMemoTotalVo.setReplayMemoCount(result);
                }else if(temResult.getKey().equals("watchMemoCount")){
                    userMemoTotalVo.setWatchMemoCount(result);
                }
            }
        }
        return userMemoTotalVo;
    }
    /**
     * 根据用户ID, 获取用户专家资源相关数据
     * @param userId
     * @return
     */
    @Override
    public UserExpertTotalVo getUserExpertStatistic(Long userId) {
        UserExpertTotalVo userExpertTotalVo = new UserExpertTotalVo();
        List<KeyValueVo> resultVo = behaviorImageMapper.getUserExperttatistic(userId);
        if(StringUtils.isNotNull(resultVo)) {
            for(KeyValueVo temResult : resultVo) {
                Long result = temResult.getValue();
                if(temResult.getKey().equals("watchExpertCount")) {
                    userExpertTotalVo.setWatchExpertCount(result);
                }else if(temResult.getKey().equals("favoriteExpertCount")){
                    userExpertTotalVo.setFavoriteExpertCount(result);
                }else if(temResult.getKey().equals("aduitCompetitionCount")){
                    userExpertTotalVo.setAduitCompetitionCount(result);
                }else if(temResult.getKey().equals("aduitTaskCount")){
                    userExpertTotalVo.setAduitTaskCount(result);
                }
            }
        }
        return userExpertTotalVo;
    }

    @Override
    public List<KeyValueVo> getActivityStatistic() {
        return behaviorImageMapper.getActivityStatistic();
    }

    /**
     * 用户资源库
     * @param activityUserLibrary 活跃用户资源库
     * @return
     */
//    @Override
//    public List<ActivityUserLibrary> selectActivityUserLibraryList(ActivityUserLibrary activityUserLibrary) {
//        List<ActivityUserLibrary> list = behaviorImageMapper.selectActivityUserLibraryList(activityUserLibrary);
//        for (ActivityUserLibrary a : list) {
//            Long relatedProject= behaviorImageMapper.getrelatedProjectCount(a.getUserId());
//            a.setRelatedProject(relatedProject);
//            Long relatedTask= behaviorImageMapper.getrelatedTaskCount(a.getUserId());
//            a.setRelatedTask(relatedTask);
//            Long relatedCompetition= behaviorImageMapper.getrelatedCompetitionCount(a.getUserId());
//            a.setRelatedCompetition(relatedCompetition);
//            Long relatedMemo= behaviorImageMapper.getrelatedMemoCount(a.getUserId());
//            a.setRelatedMemo(relatedMemo);
//        }
//        return list;
//    }

    @Override
    public List<ActivityUserLibrary> selectActivityUserLibraryList(ActivityUserLibrary activityUserLibrary) {
        List<ActivityUserLibrary> list = behaviorImageMapper.selectActivityUserLibraryListAll(activityUserLibrary);
        return list;
    }

    @Override
    public UserTypeTotalVo getUserBehaviorSum(Long userId) {
        return behaviorImageMapper.getUserBehaviorSum(userId);
    }
}
