package com.fs.course.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsUserVideo;
import com.fs.course.service.IFsBlackTalentService;
import com.fs.course.service.IFsUserTalentFollowService;
import com.fs.course.service.IFsUserVideoService;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.his.utils.PhoneUtil;
import com.fs.huifuPay.sdk.opps.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserTalentMapper;
import com.fs.course.domain.FsUserTalent;
import com.fs.course.service.IFsUserTalentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 达人Service业务层处理
 *
 * @author fs
 * @date 2024-05-31
 */
@Service
public class FsUserTalentServiceImpl implements IFsUserTalentService
{
    @Autowired
    private FsUserTalentMapper fsUserTalentMapper;
    @Autowired
    private IFsUserTalentFollowService userTalentFollowService;
    @Autowired
    private IFsBlackTalentService blackTalentService;
    @Autowired
    private IFsUserVideoService fsUserVideoService;
    @Autowired
    private IFsUserService fsUserService;

    /**
     * 查询达人
     *
     * @param talentId 达人主键
     * @return 达人
     */
    @Override
    public FsUserTalent selectFsUserTalentByTalentId(Long talentId)
    {
        return fsUserTalentMapper.selectFsUserTalentByTalentId(talentId);
    }

    /**
     * 查询达人列表
     *
     * @param fsUserTalent 达人
     * @return 达人
     */
    @Override
    public List<FsUserTalent> selectFsUserTalentList(FsUserTalent fsUserTalent)
    {
        return fsUserTalentMapper.selectFsUserTalentList(fsUserTalent);
    }

    /**
     * 新增达人
     *
     * @param fsUserTalent 达人
     * @return 结果
     */
    @Override
    public int insertFsUserTalent(FsUserTalent fsUserTalent)
    {
        fsUserTalent.setCreateTime(DateUtils.getNowDate());
        return fsUserTalentMapper.insertFsUserTalent(fsUserTalent);
    }

    /**
     * 修改达人
     *
     * @param fsUserTalent 达人
     * @return 结果
     */
    @Override
    public int updateFsUserTalent(FsUserTalent fsUserTalent)
    {
        return fsUserTalentMapper.updateFsUserTalent(fsUserTalent);
    }

    /**
     * 批量删除达人
     *
     * @param talentIds 需要删除的达人主键
     * @return 结果
     */
    @Override
    public int deleteFsUserTalentByTalentIds(Long[] talentIds)
    {
        return fsUserTalentMapper.deleteFsUserTalentByTalentIds(talentIds);
    }

    /**
     * 删除达人信息
     *
     * @param talentId 达人主键
     * @return 结果
     */
    @Override
    public int deleteFsUserTalentByTalentId(Long talentId)
    {
        return fsUserTalentMapper.deleteFsUserTalentByTalentId(talentId);
    }

    @Override
    public int updateFans(Long talentId, Integer type) {
        if (type == 1){
            return fsUserTalentMapper.addFans(talentId);
        }else {
            return fsUserTalentMapper.minusFans(talentId);
        }
    }

    @Override
    public FsUserTalent queryTalentByUserId(Long userId){
        return fsUserTalentMapper.queryTalentByUserId(userId);
    }

    @Override
    @Transactional
    public int addFsUserTalent(Long userId) {
        //根据userID查询达人表中是否存在数据
        FsUserTalent fsUserTalent = queryTalentByUserId(userId);
        if (null!=fsUserTalent){
            return 0;
        }
        FsUser fsUser1 = fsUserService.selectFsUserByUserId(userId);
        fsUserTalent = new FsUserTalent();
        if (!StringUtil.isEmpty(fsUser1.getAvatar())){
            fsUserTalent.setAvatar(fsUser1.getAvatar());
        }
        fsUserTalent.setUserId(fsUser1.getUserId());
        if (!StringUtil.isEmpty(fsUser1.getPhone())){
            fsUserTalent.setNickName(fsUser1.getNickName());
        }
        if (!StringUtil.isEmpty(fsUser1.getPhone())){
            String phone = PhoneUtil.decryptPhone(fsUser1.getPhone());
            fsUserTalent.setPhone(phone);

        }
        fsUserTalent.setCreateTime(new Date());
        if (!StringUtil.isEmpty(fsUser1.getSex())){
            fsUserTalent.setSex(fsUser1.getSex().longValue());
        }
        fsUserTalent.setLevel(1l);
        fsUserTalent.setTalentType("1");
        fsUserTalent.setIsAudit(1l);
        fsUserTalent.setAuditTime(new Date());
        fsUserTalent.setStatus(1l);
        fsUserTalentMapper.insertFsUserTalent(fsUserTalent);
        return 1;
    }

    @Override
    public int updateFsUserTalentByUser(FsUserTalent fsUserTalent)
    {
        return fsUserTalentMapper.updateFsUserTalentByUser(fsUserTalent);
    }

    @Override
    public R getTalentDetail(Long userId, Long loginUser) {

        HashMap<String, Object> map = new HashMap<>();
        FsUserTalent fsUserTalent = queryTalentByUserId(userId);
        if (null==fsUserTalent){
            return R.error("该用户暂未注册达人身份");
        }

        //查询达人粉丝数
        Integer fansCount = userTalentFollowService.queryFansCount(fsUserTalent.getTalentId());
        //查询关注的达人数量
        Integer idolCount = userTalentFollowService.queryIdolCount(userId);
        //查询视频列表
        List<FsUserVideo> fsUserVideos = fsUserVideoService.selectVideoByTalentId(fsUserTalent.getTalentId());
        //所有视频的收藏量
//        Long favoriteNum=0l;
        Long likeNum = 0l;
        //拿到用户的所有视频id
        ArrayList<Long> videoIds = new ArrayList<>();
        for (FsUserVideo fsUserVideo : fsUserVideos) {
//            favoriteNum+=fsUserVideo.getFavoriteNum();
            likeNum+=fsUserVideo.getLikes();
            //videoIds.add(fsUserVideo.getVideoId());
        }
        //查询我的——> 我的视频  视频收藏数量
        int favoriteNum = fsUserVideoService.countFavoriteVideos(userId);
        //查询视频收藏数量
        /*if (fsUserVideos.size()>0){
            fsUserVideoFavoriteService.queryFavoriteCount(fsUserTalent.getTalentId());
        }*/
        //查询当前达人是否被查询的用户拉黑或举报
        if (loginUser>0){
            if (blackTalentService.selectBlackTalent(fsUserTalent.getTalentId(),loginUser)>0){
                map.put("isBlack",1);
            }
        }
        map.put("fsUserTalent",fsUserTalent);
        map.put("fansCount",fansCount);
        map.put("idolCount",idolCount);
        //map.put("fsUserVideos",fsUserVideos);
        map.put("favoriteNum",favoriteNum);
        map.put("likeNum",likeNum);

        return R.ok().put("data",map);
    }

}
