package com.td.Dawa.game.comloggoldtran.service;

import com.td.Dawa.common.service.*;
import com.td.Dawa.config.enums.GameEnum;
import com.td.Dawa.game.comloggoldtran.pojo.ComLogGoldTran;
import com.td.Dawa.game.comloggoldtran.vo.ComLogGoldTranVo;
import com.td.Dawa.game.comloggoldtran.repository.ComLogGoldTranRepository;
import com.td.Dawa.game.comusers.vo.ComUsersVo;
import com.td.Dawa.util.CopyUtil;
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
@Transactional
public class ComLogGoldTranServiceImpl extends CommonServiceImpl<ComLogGoldTranVo, ComLogGoldTran, String> implements ComLogGoldTranService{

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComLogGoldTranRepository comLogGoldTranRepository;

    /**
     * 召唤人物时候消耗金币记录
     * @return
     */
    @Override
    public ComLogGoldTranVo summonRoleConsumption(ComUsersVo usersVo, Integer consumptionGold) {
        Integer _gold = usersVo.getGold() - consumptionGold;
        ComLogGoldTranVo logGoldTranVo = new ComLogGoldTranVo();
        logGoldTranVo.setUserId(usersVo.getUserId());

        logGoldTranVo.setTranAmount(1d);
        logGoldTranVo.setTranPrice(consumptionGold);
        logGoldTranVo.setTranType(GameEnum.TRAN_GOLD_SUMMON.getValue());
        logGoldTranVo.setTranDescribe(GameEnum.TRAN_GOLD_SUMMON.getMessage());
        logGoldTranVo.setIsAdd(0);

        logGoldTranVo.setBeforePart(usersVo.getGold().doubleValue());
        logGoldTranVo.setTranPart(consumptionGold.doubleValue());
        logGoldTranVo.setAfterPart(_gold.doubleValue());
        //当前交易状态
        logGoldTranVo.setStatus(GameEnum.TRAN_STATUS_00.getValue());
        super.save(logGoldTranVo);
        return logGoldTranVo;
    }


    @Override
    public ComLogGoldTranVo findByUserIdAndStatus(String userId,Integer status) {
        ComLogGoldTran comLogGoldTran = comLogGoldTranRepository.findByUserIdAndTranTypeAndStatus(userId,0,status).orElse(null);
        return comLogGoldTran == null?null: CopyUtil.copy(comLogGoldTran,ComLogGoldTranVo.class);
    }
}
