package com.ruoyi.member.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.BaseCategory;
import com.ruoyi.base.service.IBaseCategoryService;
import com.ruoyi.busi.domain.BusiNotice;
import com.ruoyi.busi.service.IBusiNoticeService;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.member.domain.MemberBusiCard;
import com.ruoyi.member.mapper.MemberBusiCardMapper;
import com.ruoyi.member.query.MemberBusiCardQuery;
import com.ruoyi.member.service.IMemberBusiCardService;
import com.ruoyi.member.vo.MemberBusiCardVO;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 博主名片Service业务层处理
 *
 * @author pqz
 * @date 2025-03-21
 */
@Service
public class MemberBusiCardServiceImpl extends ServiceImpl<MemberBusiCardMapper, MemberBusiCard> implements IMemberBusiCardService {
    @Autowired
    private MemberBusiCardMapper memberBusiCardMapper;
    @Resource
    private IBaseCategoryService categoryService;
    @Resource
    private IBusiNoticeService noticeService;
    @Autowired
    private ISysUserService userService;

    /**
     * 分页查询博主名片信息
     *
     * @param pageReqVO {@link MemberBusiCard}
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.ruoyi.member.domain.MemberBusiCard>
     * @author PQZ
     * @date 21:46 2025/3/21
     **/
    @Override
    public IPage<MemberBusiCard> queryListPage(MemberBusiCard pageReqVO, Page<MemberBusiCard> page) {
        return memberBusiCardMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 通过id查询博主名片信息
     *
     * @param id 博主名片id
     * @return com.ruoyi.member.vo.MemberBusiCardVO
     * @author PQZ
     * @date 22:36 2025/3/21
     **/
    @Override
    public MemberBusiCardVO queryById(String id) {
        return memberBusiCardMapper.queryById(id);
    }

    /**
     * 通过用户id查询名片
     *
     * @param userId 用户表id
     * @return java.util.List<com.ruoyi.member.domain.MemberBusiCard>
     * @author PQZ
     * @date 10:49 2025/3/22
     **/
    @Override
    public List<MemberBusiCard> queryListByUserId(Long userId,String approvalStatus) {
        LambdaQueryWrapper<MemberBusiCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag, 0)
                .eq(MemberBusiCard::getUserId, userId)
                .orderByDesc(MemberBusiCard::getApprovalStatus)
                .orderByDesc(DlBaseEntity::getCreateTime);
        if (null != approvalStatus) {
            lambdaQueryWrapper.eq(MemberBusiCard::getApprovalStatus,approvalStatus);
        }
        return list(lambdaQueryWrapper);
    }

    /**
     * 审核博主名片信息
     *
     * @param card {@link MemberBusiCard}
     * @author PQZ
     * @date 23:17 2025/3/21
     **/
    @Override
    public void checkBusiCard(MemberBusiCard card) {
        //获取当前登录用户id
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        //当前时间
        Date nowDate = new Date();
        card.setApprovalUserId(userId);
        card.setApprovalUserName(sysUser.getNickName());
        card.setApprovalTime(nowDate);
        updateById(card);
    }

    /**
     * 保存博主名片
     *
     * @param memberBusiCard {@link MemberBusiCard}
     * @author PQZ
     * @date 13:44 2025/4/3
     **/
    @Override
    public void saveBusiCard(MemberBusiCard memberBusiCard) {
        //获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = userService.selectUserById(loginUser.getUserId());
        //设置用户平台昵称
        memberBusiCard.setNickname(sysUser.getNickName());
        //设置用户id
        memberBusiCard.setUserId(loginUser.getUserId());
        //获取分类字典
        List<BaseCategory> list = categoryService.selectByCode(memberBusiCard.getPlatformCode());
        memberBusiCard.setPlatformName(list.get(0).getTitle());
        //设置用户审核状态
        memberBusiCard.setApprovalStatus("0");
        saveOrUpdate(memberBusiCard);
    }
    /**
     * 查询博主名片符合信息
     * @author zcy
     * &#064;date  21:47 2025/3/21
     **/
    @Override
    public JSONObject getSignCard(MemberBusiCardQuery req) {
        JSONObject res = new JSONObject();
        List<MemberBusiCardVO> resList =new ArrayList<>();
        BusiNotice busiNotice = noticeService.getById(req.getNoticeId());
        //先获取博主全部审核通过的该平台的名片
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<MemberBusiCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberBusiCard::getUserId, userId).eq(MemberBusiCard::getPlatformCode, busiNotice.getPlatformCode())
                .eq(MemberBusiCard::getApprovalStatus, "1");
        List<MemberBusiCard> list = list(lambdaQueryWrapper);
        for (MemberBusiCard memberBusiCard : list) {
            MemberBusiCardVO memberBusiCardVO = new MemberBusiCardVO();
            BeanUtils.copyProperties(memberBusiCard, memberBusiCardVO);
            memberBusiCardVO.setCanUse("1");
            resList.add(memberBusiCardVO);
        }
        int canUseNum =resList.size();
        //获取通告的粉丝限制
        //获取通告的粉丝限制
        for (MemberBusiCardVO re : resList) {
            if ((null != busiNotice.getFansUp() &&re.getFansNum() > busiNotice.getFansUp())||(null !=busiNotice.getFansDown() && re.getFansNum() < busiNotice.getFansDown()) ) {
                re.setCanUse("0");
                canUseNum--;
            }
        }
        res.put("list", resList);
        res.put("canUserNum", canUseNum);
        return res;


    }


}
