package com.qnfhq.modules.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qnfhq.common.service.impl.BaseServiceImpl;
import com.qnfhq.modules.user.dao.AppAssetDao;
import com.qnfhq.modules.user.entity.AppAssetEntity;
import com.qnfhq.modules.user.entity.AppUserEntity;
import com.qnfhq.modules.user.enums.AssetEnum;
import com.qnfhq.modules.user.service.AppAssetService;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 玩家资产表
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-13
 */
@Service
public class AppAssetServiceImpl extends BaseServiceImpl<AppAssetDao, AppAssetEntity> implements AppAssetService {

    @Resource
    private AppAssetDao appAssetDao;

//    public QueryWrapper<AppAssetEntity> getWrapper(Map<String, Object> params){
//        String id = (String)params.get("id");
//
//        QueryWrapper<AppAssetEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
//
//        return wrapper;
//    }


    /**
     * 登陆用户资产
     * @return
     */
    @Override
    public List<AppAssetEntity> getUserAssets() {
        List<AppAssetEntity> list = appAssetDao.selectList(new LambdaQueryWrapper<AppAssetEntity>().eq(AppAssetEntity::getUserId, StpUtil.getLoginIdAsLong()).eq(AppAssetEntity::getSymbol, "usdt"));
        return list;
    }


    @Override
    public List<AppAssetEntity> selectList(AppAssetEntity appAssetEntity) {
        Wrapper<AppAssetEntity> queryWrapper = new LambdaQueryWrapper<AppAssetEntity>()
                .eq(Objects.nonNull(appAssetEntity.getUserId()),AppAssetEntity::getUserId, appAssetEntity.getUserId())
                .eq(StrUtil.isNotBlank(appAssetEntity.getSymbol()),AppAssetEntity::getSymbol, appAssetEntity.getSymbol())
                .eq(Objects.nonNull(appAssetEntity.getType()),AppAssetEntity::getType, appAssetEntity.getType());
        List<AppAssetEntity> list = appAssetDao.selectList(queryWrapper);
        return list;
    }

    @Override
    public AppAssetEntity getAppAssetEntity(Long userId, String symbol,Integer type) {
        return baseDao.selectOne(new LambdaQueryWrapper<AppAssetEntity>()
                .eq(Objects.nonNull(userId),AppAssetEntity::getUserId, userId)
                .eq(StrUtil.isNotBlank(symbol),AppAssetEntity::getSymbol, symbol)
                .eq(Objects.nonNull(type),AppAssetEntity::getType, type)
                .last(" limit 1")
        );
    }

    @Override
    public int frozeAssetByUserId(Long userId, String symbol, BigDecimal amout) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("symbol", symbol);
        map.put("money", amout);
        map.put("type", AssetEnum.PLATFORM_ASSETS.getCode());
        return appAssetDao.frozeAssetByUserId(map);
    }

    @Override
    public int unblockAssetByUserId(Long userId, String symbol, BigDecimal amout) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("symbol", symbol);
        map.put("money", amout);
        map.put("type", AssetEnum.PLATFORM_ASSETS.getCode());
        return appAssetDao.unblockAssetByUserId(map);
    }

    @Override
    public boolean createAsset(AppUserEntity user, String coin, Integer type) {
        AppAssetEntity tAppAsset = new AppAssetEntity();
        tAppAsset.setUserId(user.getId());
        tAppAsset.setSymbol(coin);
        tAppAsset.setTotal(BigDecimal.ZERO);
        tAppAsset.setFrozen(BigDecimal.ZERO);
        tAppAsset.setAvailable(BigDecimal.ZERO);
        tAppAsset.setType(type);
        tAppAsset.setCreateTime(new Date());
        tAppAsset.setUpdateTime(new Date());
        return insert(tAppAsset);

    }


    /**
     * c2c买币 增加资产
     * @param userId
     * @param symbol
     * @param amout
     * @return
     */
    @Override
    public int addAssetByUserId(Long userId, String symbol, BigDecimal amout) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("symbol", symbol);
        map.put("money", amout);
        map.put("type", AssetEnum.PLATFORM_ASSETS.getCode());
        return appAssetDao.addAssetByUserId(map);
    }

    /**
     * c2c放行 接冻资产
     * @param userId
     * @param symbol
     * @param amout
     * @return
     */
    @Override
    public int releaseAssetByUserId(Long userId, String symbol, BigDecimal amout) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("symbol", symbol);
        map.put("money", amout);
        map.put("type", AssetEnum.PLATFORM_ASSETS.getCode());
        return appAssetDao.releaseAssetByUserId(map);
    }
}