package com.mcwl.resource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.SysAdvice;
import com.mcwl.resource.domain.SysUserAttention;
import com.mcwl.resource.domain.SysUserInfo;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.ModelImageMapper;
import com.mcwl.resource.mapper.ModelMapper;
import com.mcwl.resource.mapper.SysUserAttentionMapper;
import com.mcwl.resource.mapper.WorkFlowMapper;
import com.mcwl.resource.service.SysUserAttentionService;
import com.mcwl.system.domain.enums.AdviceEnum;
import com.mcwl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 关注表 业务实现层
 *
 * @author DaiZibo
 * @date 2025/1/3
 * @apiNote
 */

@Service
public class SysUserAttentionServiceImpl extends ServiceImpl<SysUserAttentionMapper, SysUserAttention> implements SysUserAttentionService {


    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private SysUserAttentionMapper sysUserAttentionMapper;

    @Autowired
    private ModelImageMapper modelImageMapper;

    @Autowired
    private WorkFlowMapper workFlowMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R addAttention(Long userId) {

        if (userId.equals(SecurityUtils.getUserId())) {

            return R.fail(HttpStatus.SHOW_ERROR_MSG,"不能对自己操作");
        }

        //查看是否已关注
        Boolean aBoolean = selectAttention(userId);
        if (aBoolean) {
            //取关
            sysUserAttentionMapper.deleteByUserId(SecurityUtils.getUserId(), userId);
            return R.ok(false);
        }

        //关注
        SysUserAttention sysUserAttention = SysUserAttention.builder().userId(SecurityUtils.getUserId())
                .toUserId(userId)
                .createName(SecurityUtils.getLoginUser().getUser().getNickName())
                .createTime(new Date())
                .build();
        sysUserAttentionMapper.insert(sysUserAttention);


        // 添加通知
        String content = StringUtils.format("{}关注了您，快去看看吧！", SecurityUtils.getUsername());
        SysAdvice sysAdvice = new SysAdvice();
        sysAdvice.setSenderId(SecurityUtils.getUserId());
        sysAdvice.setReceiverId(userId);
        sysAdvice.setContent(content);
        sysAdvice.setIsRead(0);
        sysAdvice.setType(AdviceEnum.FANS_REMIND);


        return R.ok(true);
    }

    @Override
    public Boolean selectAttention(Long userId) {

        SysUserAttention sysUserAttention = sysUserAttentionMapper.selectAttention(SecurityUtils.getUserIdMax(), userId);

        return sysUserAttention != null;
    }

    @Override
    public SysUserInfo selectUserInfo(Long userId) {

        if (userId == null) {
            userId = SecurityUtils.getUserIdMax();
        }

        PageHelper.clearPage();

        // 粉丝数
        Long userBeanNum = sysUserAttentionMapper.selectBean(userId);

        // 关注数
        Long userAttentionNum = sysUserAttentionMapper.selectAttentionCount(userId);

        // 模型下载数
        Long modelDownloadNum = modelMapper.sumNumber(userId);

        //

        // 模型运行次数
        Long modelRunNum = modelMapper.sumRunNumber(userId);

        // 模型点赞次数
        Long modelLikeNum = modelMapper.sumLikeNumber(userId);

        // 图片点赞次数
        Long imageLikeNum = modelImageMapper.sumLikeNumber(userId);

        // 作品数量
        Long modelNumber = modelMapper.sumModelNumber(userId);
        Long imageNumber = modelImageMapper.sumImageNumber(userId);
        Long workFlowNumber = workFlowMapper.sumWorkFlowNumber(userId);

        return SysUserInfo.builder()
                .bean(userBeanNum)
                .attention(userAttentionNum)
                .modelDownloadNum(modelDownloadNum)
                .modelRunNum(modelRunNum)
                .modelLikeNum(modelLikeNum)
                .imageLikeNum(imageLikeNum)
                .productNum(modelNumber + imageNumber + workFlowNumber)
                .build();
    }

    @Override
    public PageInfo<SysUser> selectAttentionPage(PageVo pageVo) {

        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageVo.getPageNumber(), pageVo.getPageSize());

        Long userIdMax = SecurityUtils.getUserIdMax();

        if (userIdMax == 0L) {

            return new PageInfo<SysUser>(new ArrayList<SysUser>());
        }

        if (pageVo.getType() == null) {

            pageVo.setType(userIdMax);

        }
        //条件构造器
        List<SysUser> sysUserList = sysUserMapper.selectUserPage(pageVo.getType());

        for (SysUser sysUser : sysUserList) {

            if (sysUser != null) {
                sysUser.setAttention(selectAttention(sysUser.getUserId()));

            }


        }

        return new PageInfo<SysUser>(sysUserList);
    }

    @Override
    public PageInfo<SysUser> selectToAttentionPage(PageVo pageVo) {

        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageVo.getPageNumber(), pageVo.getPageSize());

        Long userIdMax = SecurityUtils.getUserIdMax();

        if (userIdMax == 0L) {

            return new PageInfo<SysUser>(new ArrayList<SysUser>());
        }

        if (pageVo.getType() == null) {

            pageVo.setType(userIdMax);

        }

        //条件构造器
        List<SysUser> sysUserList = sysUserMapper.selectToUserPage(pageVo.getType());

        for (SysUser sysUser : sysUserList) {

            if (sysUser != null) {
                sysUser.setAttention(selectAttention(sysUser.getUserId()));
            }


        }


        return new PageInfo<SysUser>(sysUserList);
    }
}
