package com.ruoyi.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.BaseCoupon;
import com.ruoyi.base.service.IBaseCouponService;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.member.domain.MemberCoupon;
import com.ruoyi.member.domain.MemberOrder;
import com.ruoyi.member.mapper.MemberCouponMapper;
import com.ruoyi.member.service.IMemberCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户通告券变动记录Service业务层处理
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@Service
public class MemberCouponServiceImpl extends ServiceImpl<MemberCouponMapper,MemberCoupon>  implements IMemberCouponService
{
    @Autowired
    private MemberCouponMapper memberCouponMapper;
    @Autowired
    private IBaseCouponService baseCouponService;


    /**
     * @param order 订单
     * @author PQZ
     * @date 15:02 2025/3/28
     **/
    @Override
    public void dealMemberCoupon(MemberOrder order) {
        saveCoupon(order.getUserId(), "2",null,order.getGoodsNum());
    }

    /**
     * 保存通告券变动记录
     *
     * @param userId    用户id
     * @param type      变动类型（1增加2减少）
     * @param noticeId  通告id
     * @param couponNum 变动数量
     * @author PQZ
     * @date 15:19 2025/3/28
     **/
    @Override
    public void saveCoupon(Long userId, String type, String noticeId, Integer couponNum) {
        //查询当前用户最新变动记录
        LambdaQueryWrapper<MemberCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(MemberCoupon::getUserId,userId)
                .eq(DlBaseEntity::getDelFlag,0)
                .orderByDesc(DlBaseEntity::getCreateTime);
        List<MemberCoupon> list = list(lambdaQueryWrapper);
        MemberCoupon memberCoupon = new MemberCoupon();
        memberCoupon.setUserId(userId);
        memberCoupon.setType(type);
        memberCoupon.setNoticeId(noticeId);
        memberCoupon.setCouponNum(couponNum);
        //如果无记录则为初始新增
        if (list.isEmpty()) {
            memberCoupon.setBalance(couponNum);
        } else {
            //如果有记录则取最新记录中的剩余值进行计算后得出新值
            Integer oldBalance = list.get(0).getBalance();
            Integer newBalance;
            if ("1".equals(type)) {
                newBalance = oldBalance + couponNum;
            } else {
                newBalance = oldBalance - couponNum;
            }
            memberCoupon.setBalance(newBalance);
        }
        save(memberCoupon);

    }

    /**
     * 查询剩余通告券
     *
     * @param userId 用户id
     * @return java.lang.Integer
     * @author PQZ
     * @date 13:37 2025/4/11
     **/
    @Override
    public Integer getCoupon(Long userId) {
        LambdaQueryWrapper<MemberCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag,0)
                .eq(MemberCoupon::getUserId,userId)
                .orderByDesc(DlBaseEntity::getCreateTime);
        List<MemberCoupon> list = list(lambdaQueryWrapper);
        if (list.isEmpty()){
            return 0;
        } else {
            return list.get(0).getBalance();
        }
    }


}
