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.AnchorApply;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.admin.vo.AnchorApplyAdminVo;
import com.ruoyi.cai.dto.app.vo.AnchorJoinHomeVo;
import com.ruoyi.cai.mapper.AnchorApplyMapper;
import com.ruoyi.cai.service.AnchorApplyService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 播主申请管理Service业务层处理
 *
 * @author 77
 * @date 2023-12-22
 */
@Service
public class AnchorApplyServiceImpl extends ServiceImpl<AnchorApplyMapper, AnchorApply> implements AnchorApplyService {

    @Autowired
    private UserService userService;

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

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

    @Override
    public AnchorJoinHomeVo joinHome(Long userId) {
        User user = userService.getById(userId);
        AnchorApply anchorApply = this.getByUserId(userId);
        AnchorJoinHomeVo vo = new AnchorJoinHomeVo();
        vo.setFinishStatus(user.getFinishStatus());
        vo.setPhotoStatus(user.getPhotoStatus());
        vo.setCameraStatus(user.getCameraStatus());
        vo.setAgreeProtocol(user.getAgreeProtocol());
        if(anchorApply != null){
            vo.setApplyStatus(user.getStatus());
        }else{
            vo.setApplyStatus(0);
        }
        return vo;
    }

    @Override
    public boolean joinAnchor(Long userId) {
        User user = userService.getById(userId);
        if(user.getIsAnchor() == 1){
            throw new ServiceException("您已经是主播，不能重复申请");
        }
        if(user.getFinishStatus() != 1){
            throw new ServiceException("您还未完善个人资料");
        }
        if(user.getPhotoStatus() != 1){
            throw new ServiceException("请选上传相册");
        }
        if(user.getCameraStatus() != 1){
            throw new ServiceException("您还未完成自拍认证");
        }
        if(user.getAgreeProtocol() != 1){
            throw new ServiceException("您还未同意个人协议");
        }
        AnchorApply anchorApply = this.getByUserId(userId);
        if(anchorApply != null && anchorApply.getStatus() == 1){
            throw new ServiceException("您的提交的申请正在审核中请耐心等待");
        }
        if(anchorApply == null){
            anchorApply = new AnchorApply();
        }else{
            anchorApply.setAuditCount(anchorApply.getAuditCount()+1);
        }
        anchorApply.setStatus(1);
        anchorApply.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(anchorApply);
        return true;
    }
}
