package com.ruoyi.xq.service.impl;

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.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserStatus;
import com.ruoyi.xq.domain.UserVip;
import com.ruoyi.xq.dto.admin.userstatus.UserStatusAdminVo;
import com.ruoyi.xq.dto.app.userstatus.UserStatusUpdateAppReq;
import com.ruoyi.xq.dto.app.userstatus.UserStatusUpdateMasterAppReq;
import com.ruoyi.xq.enums.ErrorEnum;
import com.ruoyi.xq.enums.userstatus.PersonalsStatusEnum;
import com.ruoyi.xq.enums.userstatus.ShowAvatarEnum;
import com.ruoyi.xq.mapper.UserStatusMapper;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.UserStatusService;
import com.ruoyi.xq.service.UserVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 用户状态Service业务层处理
 *
 * @author 77
 * @date 2024-03-28
 */
@Service
public class UserStatusServiceImpl extends ServiceImpl<UserStatusMapper,UserStatus> implements UserStatusService {
    @Autowired
    private UserVipService userVipService;
    @Autowired
    private UserService userService;

    @Override
    public UserStatus getByUserId(Long userId) {
        return this.getOne(Wrappers.lambdaQuery(UserStatus.class).eq(UserStatus::getUserId,userId).last("limit 1"));
    }

    @Override
    public void updateStatus(UserStatusUpdateAppReq req) {
        UserStatus userStatus = this.getByUserId(req.getUserId());
        if(req.getShowAvatar() != null && !req.getShowAvatar().equals(ShowAvatarEnum.ALL.getCode())){
            User user = userService.getById(req.getUserId());
            if(!user.getOpenVip()){
                throw new ServiceException(ErrorEnum.VIP_AUTH);
            }
        }
        UserStatus update = new UserStatus();
        update.setId(userStatus.getId());
        update.setAllowTransMobile(req.getAllowTransMobile());
        update.setAllowTransWx(req.getAllowTransWx());
        update.setShowAvatar(req.getShowAvatar());
        if(req.getPushVip() != null && req.getPushVip() == 1){
            User user = userService.getById(req.getUserId());
            if(!user.getOpenVip()){
                throw new ServiceException(ErrorEnum.VIP_AUTH);
            }
        }
        update.setPushVip(req.getPushVip());
        this.updateById(update);
    }

    @Override
    public Page<UserStatusAdminVo> pageAdmin(PageQuery pageQuery, UserStatusAdminVo bo) {
        return baseMapper.pageAdmin(pageQuery.build(),bo);
    }

    @Override
    public void updateStatusMaster(UserStatusUpdateMasterAppReq req) {
        UserStatus userStatus = this.getByUserId(req.getUserId());
        Integer personalsStatus = req.getPersonalsStatus();
        UserStatus update = new UserStatus();
        update.setId(userStatus.getId());
        update.setUpdateTime(LocalDateTime.now());
        update.setPersonalsStatus(req.getPersonalsStatus());
        if(req.getPersonalsStatus() != null  && (PersonalsStatusEnum.CAO.getCode().equals(personalsStatus)
                || PersonalsStatusEnum.HIDING.getCode().equals(personalsStatus))){
            update.setShowAvatar(ShowAvatarEnum.NO.getCode()); // 已脱单和隐藏资料，则强制不允许看头像
        }else{
            update.setShowAvatar(ShowAvatarEnum.ALL.getCode());
        }
        this.updateById(update);
    }
}
