package com.ruoyi.bst.vipOrder.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.ruoyi.bst.bonus.domain.enums.BonusBstType;
import com.ruoyi.bst.bonus.service.BonusService;
import com.ruoyi.bst.pay.domain.PayVO;
import com.ruoyi.bst.pay.interfaces.PayHandler;
import com.ruoyi.bst.vipOrder.domain.VipOrder;
import com.ruoyi.bst.vipOrder.domain.VipOrderQuery;
import com.ruoyi.bst.vipOrder.domain.VipOrderVO;
import com.ruoyi.bst.vipOrder.domain.enums.VipOrderStatus;
import com.ruoyi.bst.vipOrder.service.VipOrderService;
import com.ruoyi.bst.vipUser.domain.VipUser;
import com.ruoyi.bst.vipUser.service.VipUserConverter;
import com.ruoyi.bst.vipUser.service.VipUserService;
import com.ruoyi.common.utils.ServiceUtil;

@Service
public class VipOrderPayHandlerImpl implements PayHandler {

    @Autowired
    private VipOrderService vipOrderService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private BonusService bonusService;

    @Autowired
    private VipUserService vipUserService;

    @Autowired
    private VipUserConverter vipUserConverter;

    @Override
    public boolean onPaySuccess(PayVO pay) {

        // 查询订单
        VipOrderVO old = vipOrderService.selectVipOrderById(pay.getBstId());
        if (old == null) {
            return false;
        }

        Integer result = transactionTemplate.execute(status -> {
            // 更新订单状态
            VipOrder data = new VipOrder();
            data.setStatus(VipOrderStatus.PAID.getCode());
            data.setPayId(pay.getId());
            VipOrderQuery query = new VipOrderQuery();
            query.setId(old.getId());
            int rows = vipOrderService.updateByQuery(data, query);

            // 创建或者合并VIP信息（续费）
            this.renew(old);

            // 分成打款
            boolean bonus = bonusService.prepayByBst(BonusBstType.VIP_ORDER, old.getId(), old.getVipPrice(), LocalDateTime.now());
            ServiceUtil.assertion(!bonus, "ID为%s的VIP订单分成打款失败", old.getId());

            return rows;
        });

        return result != null && result == 1;
    }

    public int renew(VipOrder order) {
        // NOTE: 2025-05-29 统一改为新建卡券，否则可能出现剩余次数100次，但是只差1天过期的情况，续费30天，100次后变成200次，31天。
        VipUser po = vipUserConverter.toPo(order);
        int insertVip = vipUserService.insertVipUser(po);
        ServiceUtil.assertion(insertVip != 1, "新建用户卡券失败");
        return insertVip;

        // // 续费兼容的VIP时长
        // VipUserVO vip = vipUserService.selectUserSameVip(order);
        // // 可兼容操作
        // if (vip != null ) {
        //     int renew = vipUserService.renew(vip.getId(), order.getVipValidDays(), order.getVipLimitTotal());
        //     ServiceUtil.assertion(renew != 1, "续费ID为%s的用户卡券失败", vip.getId());
        //     return renew;
        // }
        // // 不兼容操作
        // else {
        //     VipUser po = vipUserConverter.toPo(order);
        //     int insertVip = vipUserService.insertVipUser(po);
        //     ServiceUtil.assertion(insertVip != 1, "新建用户卡券失败");
        //     return insertVip;
        // }
    }
    
}
