package com.ruoyi.cai.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserCount;
import com.ruoyi.cai.domain.UserOnline;
import com.ruoyi.cai.dto.admin.query.UserUpdateAdminReq;
import com.ruoyi.cai.dto.admin.vo.UserFullAdminVo;
import com.ruoyi.cai.service.*;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserAdminManager {
    @Autowired
    private UserService userService;

    public boolean updateUser(UserUpdateAdminReq userUpdateAdmin){
        User user = userService.getById(userUpdateAdmin.getId());
        if(!user.getGender().equals(userUpdateAdmin.getGender()) && user.getIsAnchor() == 1){
            throw new BaseException("主播不能修改性别");
        }
        if(StringUtils.isNotEmpty(userUpdateAdmin.getMobile())){
            long count = userService.count(Wrappers.lambdaQuery(User.class)
                    .eq(User::getMobile, userUpdateAdmin.getMobile())
                    .ne(User::getId, user.getId()));
            if(count > 0){
                throw new BaseException("手机号已存在，无法修改");
            }
        }
        User update = new User();
        update.setId(user.getId());
        update.setMobile(userUpdateAdmin.getMobile());
        update.setNickname(userUpdateAdmin.getNickname());
        update.setGender(userUpdateAdmin.getGender());
        update.setSendDynamic(userUpdateAdmin.getSendDynamic());
        update.setAvatar(userUpdateAdmin.getAvatar());
        // TODO 需要检测资料完成度
        userService.updateById(update);
        return true;
    }

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AnchorService anchorService;
    @Autowired
    private UnionService unionService;
    @Autowired
    private UserOnlineService userOnlineService;
    @Autowired
    private UserCountService userCountService;
    @Autowired
    private UserAlbumService userAlbumService;
    @Autowired
    private UserMemberService userMemberService;

    public UserFullAdminVo getUser(Long id) {
        UserFullAdminVo vo = new UserFullAdminVo();
        vo.setUser(userService.getById(id));
        vo.setUserInfo(userInfoService.getByUserId(id));
        vo.setAccount(accountService.getByUserId(id));
        vo.setAnchor(anchorService.getByUserId(id));
        vo.setUnion(unionService.getByUserId(id));
        vo.setUserOnline(userOnlineService.getByUserId(id));
        vo.setUserCount(userCountService.getByUserId(id));
        vo.setUserAlbums(userAlbumService.listByUserId(id));
        vo.setUserMember(userMemberService.getByUserId(id));
        return vo;
    }
}
