package com.ruoyi.cai.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.cai.domain.Anchor;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.admin.vo.AnchorAdminVo;
import com.ruoyi.cai.dto.app.query.index.AnchorListQuery;
import com.ruoyi.cai.dto.app.vo.AnchorListVo;
import com.ruoyi.cai.enums.GenderEnum;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.manager.SystemConfigManager;
import com.ruoyi.cai.mapper.AnchorMapper;
import com.ruoyi.cai.service.AnchorService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 女神列表Service业务层处理
 *
 * @author 77
 * @date 2023-12-22
 */
@RequiredArgsConstructor
@Service
public class AnchorServiceImpl extends ServiceImpl<AnchorMapper, Anchor> implements AnchorService {

    @Autowired
    private UserService userService;
    @Autowired
    private SystemConfigManager systemConfigManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void joinAnchor(Long userId){
        User user = userService.getById(userId);
        Anchor anchor = this.getByUserId(userId);
        if(user == null){
            throw new ServiceException("用户不存在");
        }
        if(user.getIsAnchor() == 1 || anchor != null){
            throw new ServiceException("改用户已经是主播");
        }
        if(!GenderEnum.WOMEN.getCode().equals(user.getGender())){
            throw new ServiceException("只有女用户可以加入主播");
        }
        boolean boo = userService.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getId, user.getId()).eq(User::getIsAnchor, 0)
                .set(User::getIsAnchor, 1));
        if(!boo){
            throw new ServiceException("加入主播失败");
        }
        // 将用户的协议都自动完成
        userService.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getId,user.getId())
                .set(User::getCameraStatus,1)
                .set(User::getFinishStatus,1)
                .set(User::getAgreeProtocol,1)
                .set(User::getAvatarState,1));
        Long price = systemConfigManager.getSystemConfigOfLong(SystemConfigEnum.DEFAULT_ANCHOR_PRICE);
        Anchor save = new Anchor();
        save.setUserId(user.getId());
        save.setPrice(price);
        save.setVideoRate(systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.DEFAULT_ANCHOR_VIDEO_PRICE));
        save.setGuardRate(systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.DEFAULT_ANCHOR_GUARD_PRICE));
        save.setGiftRate(systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.DEFAULT_ANCHOR_GIFT_PRICE));
        this.save(save);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void closeAnchor(Long userId){
        User user = userService.getById(userId);
        Anchor anchor = this.getByUserId(userId);
        if(user == null){
            throw new ServiceException("用户不存在");
        }
        if(user.getIsAnchor() == 0){
            throw new ServiceException("该用户不是主播，无需取消");
        }
        boolean boo = userService.update(Wrappers.lambdaUpdate(User.class)
                .eq(User::getId, user.getId()).eq(User::getIsAnchor, 1)
                .set(User::getIsAnchor, 0));
        if(!boo){
            throw new ServiceException("取消主播失败");
        }
        this.removeById(anchor.getId());
    }

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

    @Override
    public Page<AnchorListVo> pageApp(PageQuery pageQuery, AnchorListQuery query) {
        if(query.getType() != null && query.getType() != 3){
            query.setCityId(null);
        }
        return baseMapper.pageApp(pageQuery.build(),query);
    }

    @Override
    public Anchor getByUserId(Long userId){
        return this.getOne(Wrappers.lambdaQuery(Anchor.class).eq(Anchor::getUserId,userId));
    }

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