package com.td.boss.game.complayerscore.service;

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

import cn.hutool.core.util.StrUtil;
import com.td.boss.common.pojo.Result;
import com.td.boss.common.service.*;
import com.td.boss.game.commallseed.service.ComMallSeedService;
import com.td.boss.game.commallseed.vo.ComMallSeedVo;
import com.td.boss.game.complayercultivate.serivce.ComPlayerCultivateSerivce;
import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
import com.td.boss.game.complayergoods.service.ComPlayerGoodsService;
import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
import com.td.boss.game.complayerscore.pojo.ComPlayerScore;
import com.td.boss.game.complayerscore.vo.ComPlayerScoreVo;
import com.td.boss.game.complayerscore.repository.ComPlayerScoreRepository;
import com.td.boss.game.comusers.service.ComUsersService;
import com.td.boss.game.comusers.vo.ComUsersVo;
import com.td.boss.util.DappUtil;
import com.td.boss.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Service
public class ComPlayerScoreServiceImpl extends CommonServiceImpl<ComPlayerScoreVo, ComPlayerScore, Integer> implements ComPlayerScoreService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComPlayerScoreRepository comPlayerScoreRepository;
    @Autowired
    private ComUsersService comUsersService;
    @Autowired
    private ComMallSeedService comMallSeedService;
    @Autowired
    private ComPlayerGoodsService comPlayerGoodsService;

    /**
     * 积分兑换种子
     *
     * @param userId 用户ID
     * @param amount 本次兑换数量
     * @param seedId 种子id
     * @return {@link Result}<{@link String}>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> exchange(String userId,
                                   Integer amount,
                                   Integer seedId) {
        ComUsersVo comUsersVo = comUsersService.findByUserId(userId);
        if (comUsersVo == null) {
            return Result.of(null, false, "用户不存在");
        }
        // 链端获取cnt总数
        Integer totalCount = DappUtil.getChildrenBuyLandAmount(userId);
        if (totalCount <= 0) {
            return Result.of(null, false, "消费土地的总cnt不足");
        }
        ComMallSeedVo mallSeed = comMallSeedService.findById(seedId);
        // 5 = 固定积分兑换种子
        if (mallSeed == null || mallSeed.getAmount().equals(0) || !"5".equals(mallSeed.getMallType())) {
            return Result.of(null, false, "种子不存在或已被下架");
        }
        // 本次总消费
        int totalCnt = mallSeed.getPriceCnt() * amount;
        if (totalCnt <= 0) {
            return Result.of(null, false, "种子数据异常");
        }
        if (totalCount < totalCnt) {
            return Result.of(null, false, "消费土地的总cnt不足");
        }
        // 以往兑换消费总cnt
        Integer oldTotalCnt = comPlayerScoreRepository.findAllByUserId(userId).stream()
                .map(ComPlayerScore::getTotalCnt).reduce(0, Integer::sum);
        if (totalCount < oldTotalCnt + totalCnt) {
            return Result.of(null, false, "消费土地的总cnt不足");
        }

        // 记录本次兑换信息
        ComPlayerScore comPlayerScore = new ComPlayerScore();
        comPlayerScore.setUserId(userId);
        comPlayerScore.setTotalCnt(totalCnt);
        comPlayerScore.setAmount(amount);
        comPlayerScore.setPriceCnt(mallSeed.getPriceCnt());
        comPlayerScore.setCreateTime(new Date());
        comPlayerScoreRepository.save(comPlayerScore);
        // 保存进仓库
        ComPlayerGoodsVo comPlayerGoods = new ComPlayerGoodsVo();
        comPlayerGoods.setGoodsId(UUIDUtil.getUUID());
        comPlayerGoods.setGoodsIndex(seedId);
        comPlayerGoods.setGoodsType(0);
        comPlayerGoods.setUserId(userId);
        comPlayerGoods.setName(mallSeed.getName());
        comPlayerGoods.setPictureName(mallSeed.getPicture());
        comPlayerGoods.setAmount(amount);
        comPlayerGoods.setAmountPart(0d);
        comPlayerGoods.setCreateTime(new Date());
        comPlayerGoods.setMallType(0);
        comPlayerGoodsService.save(comPlayerGoods);

        return Result.of(null, true, "兑换成功");
    }
}
