package com.yolo.keyboard.service.userwallet;

import cn.hutool.core.collection.CollUtil;
import com.yolo.keyboard.dal.dataobject.userwallet.KeyboardWalletTransactionDO;
import com.yolo.keyboard.dal.mysql.userwallet.KeyboardWalletTransactionMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yolo.keyboard.controller.admin.userwallet.vo.*;
import com.yolo.keyboard.dal.dataobject.userwallet.KeyboardUserWalletDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.userwallet.KeyboardUserWalletMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.USER_WALLET_NOT_EXISTS;

/**
 * 用户钱包 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardUserWalletServiceImpl implements KeyboardUserWalletService {

    @Resource
    private KeyboardUserWalletMapper userWalletMapper;
    @Resource
    private KeyboardWalletTransactionMapper walletTransactionMapper;


    @Override
    public Long createUserWallet(KeyboardUserWalletSaveReqVO createReqVO) {
        // 插入
        KeyboardUserWalletDO userWallet = BeanUtils.toBean(createReqVO, KeyboardUserWalletDO.class);
        userWalletMapper.insert(userWallet);

        // 返回
        return userWallet.getId();
    }

    @Override
    public void updateUserWallet(KeyboardUserWalletSaveReqVO updateReqVO) {
        // 校验存在
        validateUserWalletExists(updateReqVO.getId());
        // 更新
        KeyboardUserWalletDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserWalletDO.class);
        userWalletMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserWallet(Long id) {
        // 校验存在
        validateUserWalletExists(id);
        // 删除
        userWalletMapper.deleteById(id);
    }

    @Override
        public void deleteUserWalletListByIds(List<Long> ids) {
        // 删除
        userWalletMapper.deleteByIds(ids);
        }


    private void validateUserWalletExists(Long id) {
        if (userWalletMapper.selectById(id) == null) {
            throw exception(USER_WALLET_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserWalletDO getUserWallet(Long id) {
        return userWalletMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserWalletDO> getUserWalletPage(KeyboardUserWalletPageReqVO pageReqVO) {
        return userWalletMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（钱包交易记录） ====================

    @Override
    public PageResult<KeyboardWalletTransactionDO> getWalletTransactionPage(PageParam pageReqVO, Long userId) {
        return walletTransactionMapper.selectPage(pageReqVO, userId);
    }

    @Override
    public Long createWalletTransaction(KeyboardWalletTransactionDO walletTransaction) {
        walletTransactionMapper.insert(walletTransaction);
        return walletTransaction.getId();
    }

    @Override
    public void updateWalletTransaction(KeyboardWalletTransactionDO walletTransaction) {
        // 校验存在
        validateWalletTransactionExists(walletTransaction.getId());
        // 更新
        walletTransactionMapper.updateById(walletTransaction);
    }

    @Override
    public void deleteWalletTransaction(Long id) {
        // 删除
        walletTransactionMapper.deleteById(id);
    }

    @Override
    public void deleteWalletTransactionListByIds(List<Long> ids) {
        // 删除
        walletTransactionMapper.deleteByIds(ids);
    }

    @Override
    public KeyboardWalletTransactionDO getWalletTransaction(Long id) {
        return walletTransactionMapper.selectById(id);
    }

    private void validateWalletTransactionExists(Long id) {
        if (walletTransactionMapper.selectById(id) == null) {
            throw exception(null);
        }
    }

    private void deleteWalletTransactionByUserId(Long userId) {
        walletTransactionMapper.deleteByUserId(userId);
    }

    private void deleteWalletTransactionByUserIds(List<Long> userIds) {
        walletTransactionMapper.deleteByUserIds(userIds);
    }


}