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.vo.IntegralListVO;
import com.fzm.mall.server.admin.finance_center.entity.vo.MerchantAssetsFlowVO;
import com.fzm.mall.server.admin.finance_center.entity.vo.MerchantAssetsVO;
import com.fzm.mall.server.admin.finance_center.model.MerchantAssets;
import com.fzm.mall.server.admin.finance_center.mapper.MerchantAssetsMapper;
import com.fzm.mall.server.admin.finance_center.service.IMerchantAssetsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 MerchantAssetsServiceImpl extends ServiceImpl<MerchantAssetsMapper, MerchantAssets> implements IMerchantAssetsService {
    @Override
    public MerchantAssets queryByMerchantIdAndCoin(String merchantId, String coin) {
        QueryWrapper<MerchantAssets> qw = new QueryWrapper<>();
        qw.lambda().eq(MerchantAssets::getMerchantId, merchantId)
                .eq(MerchantAssets::getCoin, coin);
        return getOne(qw);
    }

    @Override
    public MerchantAssets queryByMerchantIdAndCoin(String merchantId, boolean isBase) {
        QueryWrapper<MerchantAssets> qw = new QueryWrapper<>();
        qw.lambda().eq(MerchantAssets::getMerchantId, merchantId);
        if (isBase) {
            qw.lambda().eq(MerchantAssets::getCoin, MALLGlobalConfig.PLATFORM_BASE_INTEGRAL);
        } else {
            qw.lambda().ne(MerchantAssets::getCoin, MALLGlobalConfig.PLATFORM_BASE_INTEGRAL);
        }
        return getOne(qw);
    }

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

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

    @Override
    public void updateAssets(String merchantId, String account, String coin, BigDecimal amount) {
        MerchantAssets merchantAssets = queryByMerchantIdAndCoin(merchantId, coin);
        if (merchantAssets != null) {
            merchantAssets.setAmount(merchantAssets.getAmount().add(amount));
            updateById(merchantAssets);
        } else {
            MerchantAssets assets = new MerchantAssets();
            assets.setMerchantId(merchantId);
            assets.setAccount(account);
            assets.setCoin(coin);
            assets.setAmount(amount);
            save(assets);
        }
    }
}
