package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.StoreDynamicDiscount;
import shop.alien.entity.store.StoreDynamicDiscountInviteLog;
import shop.alien.entity.store.StoreDynamicDiscountReceiveLog;
import shop.alien.entity.store.vo.StoreDynamicDiscountInviteLogVo;
import shop.alien.mapper.StoreDynamicDiscountInviteLogMapper;
import shop.alien.store.service.LifeUserService;
import shop.alien.store.service.StoreDynamicDiscountInviteLogService;
import shop.alien.store.service.StoreDynamicDiscountReceiveLogService;
import shop.alien.store.service.StoreDynamicDiscountService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * 商户动态折扣邀请记录 服务实现类
 *
 * @author ssk
 * @since 2025-03-18
 */
@Service
@RequiredArgsConstructor
public class StoreDynamicDiscountInviteLogServiceImpl extends ServiceImpl<StoreDynamicDiscountInviteLogMapper, StoreDynamicDiscountInviteLog> implements StoreDynamicDiscountInviteLogService {

    private final LifeUserService lifeUserService;

    /**
     * 动态折扣领取记录
     */
    private final StoreDynamicDiscountReceiveLogService storeDynamicDiscountReceiveLogService;

    /**
     * 动态折扣
     */
    private final StoreDynamicDiscountService storeDynamicDiscountService;

    private final StoreDynamicDiscountInviteLogMapper storeDynamicDiscountInviteLogMapper;

    /**
     * 其他用户受邀参与
     *
     * @param discountId    折扣活动id
     * @param invitedUserId 被邀用户id
     * @param invitedNum    邀请码
     * @return 返回消息
     */
    @Override
    public String userAcceptInvitation(Integer discountId, Integer invitedUserId, String invitedNum) {
        LambdaQueryWrapper<LifeUser> lambdaQueryWrapperA = new LambdaQueryWrapper<>();
        lambdaQueryWrapperA.eq(LifeUser::getInvitedNum, invitedNum);
        LifeUser lifeUser = lifeUserService.getOne(lambdaQueryWrapperA);
        if (null == lifeUser) {
            return "邀请码不存在";
        }
        //判断用户是否已经接受过
        LambdaQueryWrapper<StoreDynamicDiscountInviteLog> lambdaQueryWrapperB = new LambdaQueryWrapper<>();
        lambdaQueryWrapperB.eq(StoreDynamicDiscountInviteLog::getDiscountId, discountId)
                .eq(StoreDynamicDiscountInviteLog::getInvitedUserId, invitedUserId);
        if (this.count(lambdaQueryWrapperB) > 0) {
            return "你已经是老用户了, 无法参与活动";
        }
        if (Objects.equals(lifeUser.getId(), invitedUserId)) {
            return "无法邀请自己";
        }
        LambdaQueryWrapper<StoreDynamicDiscountReceiveLog> lambdaQueryWrapperOne = new LambdaQueryWrapper<>();
        lambdaQueryWrapperOne.eq(StoreDynamicDiscountReceiveLog::getDiscountId, discountId).eq(StoreDynamicDiscountReceiveLog::getUserId, lifeUser.getId());
        StoreDynamicDiscountReceiveLog one = storeDynamicDiscountReceiveLogService.getOne(lambdaQueryWrapperOne);
        if (one.getStatus() == 1) {
            return "该用户已经领取折扣";
        }
        StoreDynamicDiscountInviteLog storeDynamicDiscountInviteLog = new StoreDynamicDiscountInviteLog();
        storeDynamicDiscountInviteLog.setDiscountId(discountId);
        storeDynamicDiscountInviteLog.setInvitedUserId(invitedUserId);
        storeDynamicDiscountInviteLog.setUserId(lifeUser.getId());
        //折扣详情
        StoreDynamicDiscount storeDynamicDiscountById = storeDynamicDiscountService.getById(discountId);
        //新增邀请记录
        boolean stepA = this.save(storeDynamicDiscountInviteLog);
        //最低折扣
        Integer discountPercent = storeDynamicDiscountById.getDiscountPercent();
        //最大邀请人数
        Integer invitedCount = storeDynamicDiscountById.getInvitedCount();
        //可减折扣
        int percent = 10 - discountPercent;
        //已邀比例
        LambdaQueryWrapper<StoreDynamicDiscountInviteLog> lambdaQueryWrapperC = new LambdaQueryWrapper<>();
        lambdaQueryWrapperC.eq(StoreDynamicDiscountInviteLog::getDiscountId, discountId)
                .eq(StoreDynamicDiscountInviteLog::getUserId, lifeUser.getId());
        int count = this.count(lambdaQueryWrapperC);
        if (count > invitedCount) {
            return "该用户已达最大邀请";
        }
        //已邀请的人数 / 最大邀请人数
        BigDecimal peoplePercent = new BigDecimal(count).divide(new BigDecimal(invitedCount), 2, RoundingMode.HALF_UP);
        //可减折扣 x 人数比例
        BigDecimal multiply = new BigDecimal(percent).multiply(peoplePercent);
        //10折-邀请折扣
        double resultPercent = 10 - multiply.doubleValue();
        //获取领取信息并修改
        LambdaUpdateWrapper<StoreDynamicDiscountReceiveLog> lambdaQueryWrapperD = new LambdaUpdateWrapper<>();
        lambdaQueryWrapperD.eq(StoreDynamicDiscountReceiveLog::getDiscountId, discountId)
                .eq(StoreDynamicDiscountReceiveLog::getUserId, lifeUser.getId())
                .set(StoreDynamicDiscountReceiveLog::getDiscountPercent, resultPercent);
        boolean stepB = storeDynamicDiscountReceiveLogService.update(lambdaQueryWrapperD);
        if (stepA && stepB) {
            return "success";
        }
        return "失败";
    }

    /**
     * 已邀请列表
     *
     * @param discountId 折扣活动id
     * @param userId     用户id
     * @return list
     */
    @Override
    public List<StoreDynamicDiscountInviteLogVo> getInviteLog(Integer discountId, Integer userId) {
        QueryWrapper<StoreDynamicDiscountInviteLogVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("a.discount_id", discountId)
                .eq("a.user_id", userId)
                .eq("a.delete_flag", 0)
                .eq("b.delete_flag", 0)
                .orderByDesc("a.created_time");
        return storeDynamicDiscountInviteLogMapper.getInviteLog(queryWrapper);
    }
}
