package com.ruoyi.frequency.onlinerecord.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.enums.WalletDetailEnums;
import com.ruoyi.frequency.onlinerecord.entity.OnlineRecord;
import com.ruoyi.frequency.onlinerecord.mapper.OnlineRecordMapper;
import com.ruoyi.frequency.onlinerecord.service.OnlineRecordService;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 在线支付记录Service业务层处理
 *
 * @author liwenlong
 * @date 2024-05-18
 */
@Service
public class OnlineRecordServiceImpl extends ServiceImpl<OnlineRecordMapper, OnlineRecord> implements OnlineRecordService {
    @Autowired
    private OnlineRecordMapper onlineRecordMapper;

    /**
     * 查询在线支付记录
     *
     * @param id 在线支付记录主键
     * @return 在线支付记录
     */
    @Override
    public OnlineRecord selectOnlineRecordById(Long id) {
        return onlineRecordMapper.selectOnlineRecordById(id);
    }

    /**
     * 查询在线支付记录列表
     *
     * @param onlineRecord 在线支付记录
     * @return 在线支付记录
     */
    @Override
    public List<OnlineRecord> selectOnlineRecordList(OnlineRecord onlineRecord) {
        return onlineRecordMapper.selectOnlineRecordList(onlineRecord);
    }

    /**
     * 新增在线支付记录
     *
     * @param onlineRecord 在线支付记录
     * @return 结果
     */
    @Override
    public int insertOnlineRecord(OnlineRecord onlineRecord) {
        onlineRecord.setCreateData();
        return onlineRecordMapper.insert(onlineRecord);
    }

    /**
     * 修改在线支付记录
     *
     * @param onlineRecord 在线支付记录
     * @return 结果
     */
    @Override
    public int updateOnlineRecord(OnlineRecord onlineRecord) {
        onlineRecord.setUpdateData();
        return onlineRecordMapper.updateOnlineRecord(onlineRecord);
    }

    /**
     * 批量删除在线支付记录
     *
     * @param ids 需要删除的在线支付记录主键
     * @return 结果
     */
    @Override
    public int deleteOnlineRecordByIds(Long[] ids, Integer delFlag) {
        this.lambdaUpdate().set(OnlineRecord::getDelFlag, delFlag).in(OnlineRecord::getId, ids).update();
        return ids.length;
    }


    /**
     * 增加余额记录
     *
     * @param userVo            用户
     * @param walletDetailEnums 类型名称
     * @param orderId           订单id
     * @param price             金额
     */
    @Override
    public void addOnlineRecord(UserVo userVo, Long orderId, BigDecimal price, WalletDetailEnums walletDetailEnums) {
        if (price.setScale(2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(0)) != 0) {
            OnlineRecord onlineRecord = new OnlineRecord();
            onlineRecord.setUserId(userVo.getUserId());
            onlineRecord.setUserType(userVo.getUserType());
            onlineRecord.setObjectId(orderId);
            onlineRecord.setObjectType(walletDetailEnums.getCode());
            if (price.compareTo(BigDecimal.ZERO) == 1) {
                onlineRecord.setPrice(price.negate());
            } else {
                onlineRecord.setPrice(price);
            }


            this.save(onlineRecord);
        }
    }

}
