package com.ruoyi.frequency.refereeuser.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.resp.RefereeUserPageResp;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.refereeuser.entity.RefereeUser;
import com.ruoyi.frequency.refereeuser.mapper.RefereeUserMapper;
import com.ruoyi.frequency.refereeuser.service.RefereeUserService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户邀请Service业务层处理
 *
 * @author liwenlong
 * @date 2023-07-20
 */
@Service
public class RefereeUserServiceImpl extends ServiceImpl<RefereeUserMapper, RefereeUser>  implements RefereeUserService
{
    @Autowired
    private RefereeUserMapper refereeUserMapper;

    /**
     * 查询用户邀请
     *
     * @param id 用户邀请主键
     * @return 用户邀请
     */
    @Override
    public RefereeUser selectRefereeUserById(Long id)
    {
        return refereeUserMapper.selectRefereeUserById(id);
    }

    @Autowired
    private UserService userService;
    /**
     * 查询用户邀请列表
     *
     * @param refereeUser 用户邀请
     * @return 用户邀请
     */
    @Override
    public List<RefereeUser> selectRefereeUserList(RefereeUser refereeUser)
    {

        List<RefereeUser> refereeUsers = refereeUserMapper.selectRefereeUserList(refereeUser);

        if (refereeUsers.size() > 0){
            Set<UserVo> userVoSet = refereeUsers.stream().map(s->new UserVo(s.getUserType(), s.getUserId())).collect(Collectors.toSet());

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            refereeUsers.stream().forEach(s->{
                s.setUserInfo(userMap.get(new UserVo(s.getUserType(), s.getUserId())));
            });
        }

        return refereeUsers;
    }

    /**
     * 新增用户邀请
     *
     * @param refereeUser 用户邀请
     * @return 结果
     */
    @Override
    public int insertRefereeUser(RefereeUser refereeUser)
    {
        refereeUser.setCreateTime(DateUtils.getNowDate());
        return refereeUserMapper.insertRefereeUser(refereeUser);
    }

    /**
     * 修改用户邀请
     *
     * @param refereeUser 用户邀请
     * @return 结果
     */
    @Override
    public int updateRefereeUser(RefereeUser refereeUser)
    {
        refereeUser.setUpdateTime(DateUtils.getNowDate());
        return refereeUserMapper.updateRefereeUser(refereeUser);
    }

    /**
     * 批量删除用户邀请
     *
     * @param ids 需要删除的用户邀请主键
     * @return 结果
     */
    @Override
    public int deleteRefereeUserByIds(Long[] ids)
    {
        return refereeUserMapper.deleteRefereeUserByIds(ids);
    }

    /**
     * 删除用户邀请信息
     *
     * @param id 用户邀请主键
     * @return 结果
     */
    @Override
    public int deleteRefereeUserById(Long id)
    {
        return refereeUserMapper.deleteRefereeUserById(id);
    }


    @Override
    public ResponseData refereeUserPage(PageBasic req, UserVo userVo) {
        userService.storeByEnterprise(userVo);

        Page<RefereeUserPageResp> page = this.baseMapper.refereeUserPage(new Page<>(req.getPageNo(),req.getPageSize()),userVo);

        if (!page.getRecords().isEmpty()){

            Set<UserVo> userVoSet = page.getRecords().stream().map(resp -> new UserVo(resp.getUserType(), resp.getUserId())).collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,userVo);

            Integer i = (req.getPageNo() - 1) * req.getPageSize() + 1;

            for (RefereeUserPageResp resp : page.getRecords()) {
                resp.setUserInfo(userMap.get(new UserVo(resp.getUserType(), resp.getUserId())));
                resp.setRanking(i++);
            }
        }

        return ResponseData.success(new PageResult<>(page));
    }
}
