package com.ruoyi.cai.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.Anchor;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.admin.vo.UserAdminVo;
import com.ruoyi.cai.dto.app.query.index.GreetQuery;
import com.ruoyi.cai.dto.app.query.index.UserQuery;
import com.ruoyi.cai.dto.app.vo.user.UserInfoVo;
import com.ruoyi.cai.dto.app.vo.user.UserListVo;
import com.ruoyi.cai.dto.app.vo.user.OnlineStatusVo;
import com.ruoyi.cai.mapper.UserMapper;
import com.ruoyi.cai.service.*;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户Service业务层处理
 *
 * @author 77
 * @date 2023-12-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private AnchorService anchorService;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private UserAlbumService userAlbumService;
    @Autowired
    private GuardTotalService guardTotalService;
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserGiftService userGiftService;
    @Autowired
    private UserOnlineService userOnlineService;

    @Override
    public User getByUsername(String username) {
        return this.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getMobile,username));
    }

    @Override
    public Page<UserAdminVo> pageAdmin(PageQuery pageQuery, User query) {
        return baseMapper.pageAdmin(pageQuery.build(),query);
    }

    @Override
    public Page<UserListVo> pageApp(PageQuery page, UserQuery query) {
        return baseMapper.pageApp(page.build(),query);
    }

    @Override
    public Page<UserListVo> greetPageApp(PageQuery page, GreetQuery query) {
        return baseMapper.greetPageApp(page.build(),query);
    }

    @Override
    public UserInfoVo info(Long userId) {
        Long currentUserId = LoginHelper.getUserId();
        User user = this.getById(userId);
        if(user == null){
            return null;
        }
        Anchor anchor = anchorService.getByUserId(userId);
        UserInfoVo vo = new UserInfoVo();
        vo.setUserId(userId);
        vo.setIsAnchor(user.getIsAnchor());
        vo.setUsercode(user.getUsercode());
        vo.setNickname(user.getNickname());
        vo.setAvatar(user.getAvatar());
        vo.setPrice(0L);
        vo.setServiceCount(0L);
        if(anchor != null){
            vo.setPrice(anchor.getPrice());
            if(anchor.getOrderSwitch() == 2){ // 不隐藏接单数
                vo.setServiceCount(anchor.getServiceCount());
            }

        }
        vo.setCity(user.getCity());
        vo.setCityId(user.getCityId());
        vo.setFansNum(userFollowService.getFansNumByUserId(userId));
        vo.setStar(userFollowService.checkStar(currentUserId, userId));
        vo.setAlbumList(userAlbumService.getUserAlbum(userId, user.getIsAnchor()));
        // 在线状态
        OnlineStatusVo onlineStatusVo;
        if(user.getIsAnchor() == 1 && anchor != null){
            onlineStatusVo = userOnlineService.onlineStatus(userId,anchor.getOpenVideoStatus(),anchor.getVideoStatus(), user.getIsAnchor());
        }else{
            onlineStatusVo = userOnlineService.onlineStatus(userId,0,0, user.getIsAnchor());
        }
        vo.setOnlineStatus(onlineStatusVo);
        if(user.getIsAnchor() == 1){
            vo.setGuardIndexVo(guardTotalService.guardInfoVo(currentUserId,userId,10));
            vo.setDynamicList(dynamicService.selectDynamicImageList(userId,10));
            vo.setGiftList(userGiftService.selectGiftList(userId));
        }
        return vo;
    }

    @Override
    public User getByUserCode(String userCode){
        return this.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUsercode,userCode).last("limit 1"));
    }

    @Override
    public void updateVideoStatus(Long userId, int videoStatus) {
        this.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getId,userId)
                .set(User::getVideoStatus,videoStatus));
    }

    @Override
    public boolean agreeProtocol(Long userId) {
        this.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getId,userId)
                .set(User::getAgreeProtocol,1));
        return true;
    }

    @Override
    public void checkFinishStatus(Long userId) {
        User user = this.getById(userId);
        if(user == null || user.getIsAnchor() == 1 || user.getFinishStatus() == 1){
            return;
        }
        if(user.getCity() != null && user.getBirthday() != null && StringUtils.isNotEmpty(user.getNickname())
                && user.getAvatarState() == 1){
            this.update(Wrappers.lambdaUpdate(User.class)
                    .eq(User::getId,user.getId())
                    .set(User::getFinishStatus,1));
        }
    }

    @Override
    public void resetPassword(String mobile, String password) {
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new ServiceException("参数异常");
        }
        this.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getMobile,mobile)
                .set(User::getPassword,BCrypt.hashpw(password)));
    }
}
