package com.fzm.mall.server.admin.finance_center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.finance_center.entity.dto.UserAssetsChangeDTO;
import com.fzm.mall.server.admin.finance_center.entity.vo.MerchantAssetsVO;
import com.fzm.mall.server.admin.finance_center.entity.vo.UserAssetsVO;
import com.fzm.mall.server.admin.finance_center.model.MerchantAssets;
import com.fzm.mall.server.admin.finance_center.model.PlatformIntegral;
import com.fzm.mall.server.admin.finance_center.model.UserAsset;
import com.fzm.mall.server.admin.finance_center.mapper.UserAssetMapper;
import com.fzm.mall.server.admin.finance_center.service.IUserAssetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.user_center.model.User;
import org.springframework.stereotype.Service;

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

/**
 * @author wangp
 * @date 2021/4/29 15:53
 * @description 用户资产信息表 服务实现类
 * @since JDK 1.8
 */
@Service
public class UserAssetServiceImpl extends ServiceImpl<UserAssetMapper, UserAsset> implements IUserAssetService {

    @Override
    public UserAsset queryByUidAndCoin(String uid, String coin) {
        QueryWrapper<UserAsset> qw = new QueryWrapper<>();
        qw.lambda().eq(UserAsset::getUid, uid)
                .eq(UserAsset::getCoin, coin);
        return getOne(qw);
    }

    @Override
    public IPage<UserAssetsVO> page(String uid, String account, String type, Integer pageNum, Integer pageSize) {
        IPage<UserAssetsVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.page(page, uid, account, type);
    }

    @Override
    public List<String> assetsType() {
        List<String> assetsTypes = list().stream().map(UserAsset::getCoin).distinct().collect(Collectors.toList());
        return assetsTypes;
    }

    @Override
    public void updateAsset(String uid, String coin, BigDecimal amount) {
        UserAsset userAsset = queryByUidAndCoin(uid, coin);
        if (userAsset != null) {
            userAsset.setAmount(userAsset.getAmount().add(amount));
            updateById(userAsset);
        } else {
            UserAsset newAsset = new UserAsset();
            newAsset.setUid(uid);
            newAsset.setCoin(coin);
            newAsset.setAmount(amount);
            save(newAsset);
        }
    }
}
