package com.kym.service.miniapp.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.entity.miniapp.Account;
import com.kym.entity.miniapp.UserCoupon;
import com.kym.entity.miniapp.UserRechargeRights;
import com.kym.entity.miniapp.vo.UserRightsAndCouponsVo;
import com.kym.mapper.miniapp.AccountMapper;
import com.kym.service.miniapp.AccountService;
import com.kym.service.miniapp.UserCouponService;
import com.kym.service.miniapp.UserRechargeRightsService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 用户账户表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-08-07
 */
@Service
public class AccountServiceImpl extends MPJBaseServiceImpl<AccountMapper, Account> implements AccountService {

    private final UserRechargeRightsService userRechargeRightsService;

    private final UserCouponService userCouponService;

    public AccountServiceImpl(UserRechargeRightsService userRechargeRightsService, UserCouponService userCouponService) {
        this.userRechargeRightsService = userRechargeRightsService;
        this.userCouponService = userCouponService;
    }

    @Override
    public Account getAccountByUserId(Long userId) {
        return lambdaQuery().eq(Account::getUserId, userId).one();
    }

    /**
     * 获取用户权益和优惠券
     *
     * @return
     */
    @Override
    public UserRightsAndCouponsVo listRightsAndCoupons() {
        var vo = new UserRightsAndCouponsVo();
        var userId = StpUtil.getLoginIdAsLong();
        vo.setUserId(userId);

        // 当前用户的充电权益（含已失效）
        var userRechargeRight = userRechargeRightsService.lambdaQuery()
                .eq(UserRechargeRights::getUserId, userId)
                .orderByDesc(UserRechargeRights::getStatus)
                .orderByAsc(UserRechargeRights::getEndTime)
                .list();
        vo.setUserRechargeRightsList(userRechargeRight);

        // 当前用户的优惠券列表（含已失效）
        var usesrCouponList = userCouponService.lambdaQuery()
                .eq(UserCoupon::getUserId, userId)
                .orderByDesc(UserCoupon::getStatus)
                .orderByAsc(UserCoupon::getUsageStatus)
                .orderByAsc(UserCoupon::getEndTime)
                .list();
        vo.setUserCouponList(usesrCouponList);

        return vo;
    }

    @Override
    public UserRightsAndCouponsVo listStationAvailableRightsAndCoupons(String stationId) {
        var vo = new UserRightsAndCouponsVo();
        var userId = StpUtil.getLoginIdAsLong();
        vo.setUserId(userId);
        // 当前用户有效的充电权益
        var userRechargeRights = userRechargeRightsService.listStationAvailableRights(Map.of("userId", userId, "stationId", stationId));
        vo.setUserRechargeRightsList(userRechargeRights);

        // 当前用户有效的优惠券列表
        var userCouponList = userCouponService.listStationAvailableCoupons(Map.of("userId", userId, "stationId", stationId));
        vo.setUserCouponList(userCouponList);
        return vo;
    }


}
