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

import com.td.Dawa.common.service.*;
import com.td.Dawa.config.enums.ResultEnum;
import com.td.Dawa.config.exception.AuthorizeException;
import com.td.Dawa.game.comusers.pojo.ComUsers;
import com.td.Dawa.game.comusers.vo.ComUsersVo;
import com.td.Dawa.game.comusers.repository.ComUsersRepository;
import com.td.Dawa.game.excelparameter.service.ExcelParameterService;
import com.td.Dawa.game.excelparameter.vo.ExcelParameterVo;
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;
import java.util.Optional;

@Service
@Transactional
public class ComUsersServiceImpl extends CommonServiceImpl<ComUsersVo, ComUsers, String> implements ComUsersService{

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComUsersRepository comUsersRepository;

    @Autowired
    private ExcelParameterService excelParameterService;

    @Override
    public ComUsersVo findByUserId(String userId) {
        Optional<ComUsers>  comUsers =  comUsersRepository.findById(userId);
        if(comUsers.orElse(null) == null){
            return null;
        }else{
            return CopyUtil.copy(comUsers.get(), ComUsersVo.class);
        }
    }


    @Override
    public ComUsersVo findByAddress(String address) {
        Optional<ComUsers>  comUsers =  comUsersRepository.findByAddress(address);
        if(comUsers.orElse(null) == null){
            return null;
        }else{
            return CopyUtil.copy(comUsers.get(), ComUsersVo.class);
        }
    }

    /**
     * 统一交易double snb 操作
     * @param tranSnb
     * @param userId
     * @return
     */
    @Override
    public ComUsersVo tranSNBByUserId(Double tranSnb, String userId) {

        return null;
    }

    @Override
    public Long findAllCount() {
        return comUsersRepository.count();
    }

    @Override
    public Long newToday() {
        return comUsersRepository.newToday();
    }


    @Override
    public void consumesGoldFromGetRole(String userId) {
        ExcelParameterVo characterConsumesGold = excelParameterService.getVo(3); //召唤角色时候消耗的金币
        if (characterConsumesGold == null) {
            throw new AuthorizeException(ResultEnum.SETTING_IS_NULL);
        }
        ComUsersVo attriVo = super.getVo(userId);
        //todo 改成double
        Integer _gold = attriVo.getGold() - characterConsumesGold.getValue().intValue();
        if( _gold <0){
            //XX不足，无法召唤
            throw new AuthorizeException(ResultEnum.GOLD_NOT_GET_ROLE);
        }
        //记录当前金币
        attriVo.setGold(_gold);
        super.save(attriVo);
    }


    @Override
    public ComUsersVo addGold(String userId,Integer gold) {
        ComUsersVo attriVo = super.getVo(userId);
        //记录当前金币
        attriVo.setGold(attriVo.getGold() + gold);
        return super.save(attriVo).getData();
    }
}
