package org.springblade.modules.api.service.impl;

import cn.hutool.core.lang.Assert;
import org.springblade.common.cache.ParamCache;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.OrderTypeEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.api.request.SubmitOrderRequest;
import org.springblade.modules.api.service.SubmitOrderService;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.platform.entity.BalanceRecord;
import org.springblade.modules.platform.service.IBalanceRecordService;
import org.springblade.modules.platform.service.IUserAppService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Author pangqijun
 * Date 2023/7/10
 * Description
 */
@Service("balanceOrderService")
public class BalanceOrderService implements SubmitOrderService {

    @Resource
    private IBalanceRecordService balanceRecordService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IUserService userService;
    @Resource
    private IUserAppService userAppService;

    @Override
    public String submitOrder(SubmitOrderRequest request) {
        if (request.getRechargeAmount() == null) {
            throw new ServiceException("请输入充值金额");
        }
        if (request.getRechargeAmount().doubleValue() <= 0) {
            throw new ServiceException("请输入正确的充值金额");
        }

        Long userId = AuthUtil.getUserId();
        User user = userService.getById(userId);
        Assert.notNull(user, "用户信息不存在");

        // 初始化订单数据
        Order order = orderService.init();
        order.setOrderType(OrderTypeEnum.BALANCE_RECHARGE.getValue());
        order.setQuantity(1);
        order.setConsignee(user.getRealName());
        order.setConsigneePhoneNumber(user.getPhone());
        order.setTotalAmount(request.getRechargeAmount());
        order.setDiscountAmount(BigDecimal.valueOf(0));// 优惠金额
        order.setPaymentAmount(order.getTotalAmount().subtract(order.getDiscountAmount()));
        orderService.save(order);
        return order.getOrderNo();
    }


    @Override
    public void paySuccessAfter(Order order) {
        BalanceRecord record = new BalanceRecord();
        record.setAmount(order.getPaymentAmount());
        record.setOrderNo(order.getOrderNo());
        record.setUserId(order.getCreateUserId());
        record.setAmountType(1);//收入
        record.setTradeMode(order.getPaymentMode());
        record.setType(1);// 充值
        record.setDescription("充值成功");
        balanceRecordService.save(record);

        UserApp userApp = userAppService.getByUserId(order.getCreateUserId());
        userApp.setNowMoney(userApp.getNowMoney().add(record.getAmount()));
        userAppService.updateById(userApp);
    }
}
