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.MemberPrice;
import com.ruoyi.cai.domain.MemberSkill;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserMember;
import com.ruoyi.cai.dto.admin.vo.UserMemberAdminVo;
import com.ruoyi.cai.dto.app.vo.user.MemberInfoVo;
import com.ruoyi.cai.enums.UserMemberTypeEnum;
import com.ruoyi.cai.mapper.UserMemberMapper;
import com.ruoyi.cai.pay.MemberStatusEnum;
import com.ruoyi.cai.service.MemberPriceService;
import com.ruoyi.cai.service.MemberSkillService;
import com.ruoyi.cai.service.UserMemberService;
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;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 会员管理Service业务层处理
 *
 * @author 77
 * @date 2023-12-21
 */
@Service
public class UserMemberServiceImpl extends ServiceImpl<UserMemberMapper, UserMember> implements UserMemberService {

    @Autowired
    private UserService userService;
    @Autowired
    private MemberSkillService memberSkillService;
    @Autowired
    private MemberPriceService memberPriceService;

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

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

    @Override
    public UserMember getByUserIdAndType(Long userId, Integer type){
        return this.getOne(Wrappers.lambdaQuery(UserMember.class)
                .eq(UserMember::getUserId,userId)
                .eq(UserMember::getMemberType,type)
                .last("limit 1"));
    }

    @Override
    public UserMemberTypeEnum checkUserMember(Long userId){
        List<UserMember> list = this.list(Wrappers.lambdaQuery(UserMember.class)
                .eq(UserMember::getUserId, userId)
                .eq(UserMember::getMemberStatus, MemberStatusEnum.NORMAL.getCode())
                .le(UserMember::getExpireDate, LocalDateTime.now())
                .orderByDesc(UserMember::getMemberType));
        for (UserMember userMember : list) {
            if(userMember.getMemberType().equals(UserMemberTypeEnum.SUPER_VIP.getCode())){
                return UserMemberTypeEnum.SUPER_VIP;
            }else{
                return UserMemberTypeEnum.NORMAL_VIP;
            }
        }
        return null;
    }

    @Override
    public void relieveMember(Long id) {
        this.update(Wrappers.lambdaUpdate(UserMember.class)
                .eq(UserMember::getId,id)
                .set(UserMember::getMemberStatus, MemberStatusEnum.FAIL.getCode()));
    }

    @Override
    public MemberInfoVo memberApp(Long userId, Integer type) {
        User user = userService.getById(userId);
        if(user == null){
            throw new ServiceException("用户不存在");
        }
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        memberInfoVo.setUserId(user.getId());
        memberInfoVo.setUsercode(user.getUsercode());
        memberInfoVo.setNickname(user.getNickname());
        memberInfoVo.setGender(user.getGender());
        memberInfoVo.setAvatar(user.getAvatar());
        memberInfoVo.setRankHide(user.getRankHide());
        memberInfoVo.setNoGreet(user.getNoGreet());
        List<MemberPrice> memberPriceList =  memberPriceService.listByType(type);
        memberInfoVo.setMemberPriceList(memberPriceList);
        List<MemberSkill> memberSkillList =  memberSkillService.listByType(type);
        memberInfoVo.setMemberSkillList(memberSkillList);
        UserMember userMember = this.getByUserIdAndType(userId, type);
        if(userMember == null || !MemberStatusEnum.NORMAL.getCode().equals(userMember.getMemberStatus())
            || userMember.getExpireDate().isBefore(LocalDateTime.now())){
            memberInfoVo.setLongs(0);
            if(userMember != null){
                memberInfoVo.setExpireTime(userMember.getExpireDate());
                memberInfoVo.setExpireTimeStr("已过期");
            }else{
                memberInfoVo.setExpireTimeStr("未开通");
            }
            memberInfoVo.setOn(0);
        }else {
            memberInfoVo.setLongs(0);
            memberInfoVo.setExpireTime(userMember.getExpireDate());
            memberInfoVo.setExpireTimeStr(userMember.getExpireDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            memberInfoVo.setOn(1);
        }
        return memberInfoVo;
    }
}
