package com.ifish.serviceImpl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifish.dao.GoldExpendRecordDao;
import com.ifish.dao.GoldGetRecordDao;
import com.ifish.dao.GoldRuleDao;
import com.ifish.dao.IfishDoctorDao;
import com.ifish.dao.ShopsInfoDao;
import com.ifish.dao.UserAssetDao;
import com.ifish.dto.JsonResult;
import com.ifish.entity.GoldExpendRecord;
import com.ifish.entity.GoldGetRecord;
import com.ifish.entity.IfishDoctor;
import com.ifish.entity.UserAsset;
import com.ifish.enums.BooleanEnum;
import com.ifish.enums.GoldExpendTypeEnum;
import com.ifish.enums.GoldGetTypeEnum;
import com.ifish.enums.PayTourTypeEnum;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.netease.NeteaseIM;
import com.ifish.service.IfishDoctorService;
import com.ifish.util.IfishUtil;
import com.ifish.util.ResultUtil;

@Service
@Transactional
public class IfishDoctorServiceImpl implements IfishDoctorService{
	
	@Autowired
	private GoldRuleDao goldRuleDao;
	@Autowired
	private UserAssetDao userAssetDao;
	@Autowired
	private GoldGetRecordDao goldGetRecordDao;
	@Autowired
	private GoldExpendRecordDao goldExpendRecordDao;
	@Autowired
	private IfishDoctorDao ifishDoctorDao;
	@Autowired
	private ShopsInfoDao shopsInfoDao;
	@Autowired
	private NeteaseIM neteaseIM;
	
	@Override
	public Object saveIfishDoctor(IfishDoctor ifishDoctor) {
		Integer userId = ifishDoctor.getUserId();
		String questionContent = ifishDoctor.getQuestionContent();
		if(userId!=null && questionContent!=null && questionContent.length()>0){
			ifishDoctor.setIsPayTour(BooleanEnum.NO.getKey());
			ifishDoctor.setPayTourSum(new BigDecimal(0));
			ifishDoctor.setCreateTime(new Date());
			this.ifishDoctorDao.save(ifishDoctor);
			return IfishUtil.toJson(ResultEnum.success.getKey(), "");
		}
		return IfishUtil.toJson(ResultEnum.fail101.getKey(), "");
	}

	@Override
	public JsonResult<?> payTourSubmitQuestion(IfishDoctor ifishDoctor,String payTour) {
		Integer userId = ifishDoctor.getUserId();
		//打赏金额
		BigDecimal payTourSum = PayTourTypeEnum.getValue(payTour);
		//
		if(payTourSum==null){
			throw new IfishException(ResultEnum.error401);
		}
		//用户资产
		UserAsset userAsset = userAssetDao.get(userId);
		if(userAsset==null){
			throw new IfishException(ResultEnum.warn211);
		}
		BigDecimal goldValue = userAsset.getGoldValue();
		//金币不足
		if(goldValue.compareTo(payTourSum)<0){
			JSONObject json = new JSONObject();
			json.put("goldValue", goldValue);
			return ResultUtil.warn(ResultEnum.warn215.getKey(),json);
		}
		//平分金币给10个商家
		List<Integer> userIds = shopsInfoDao.getShopsUserIdByRand(userId);
		Integer size = userIds.size();
		if(size<=0){
			throw new IfishException(ResultEnum.warn218);
		}
		//保存问题
		ifishDoctor.setIsPayTour(BooleanEnum.YES.getKey());
		ifishDoctor.setPayTourSum(payTourSum);
		ifishDoctor.setCreateTime(new Date());
		ifishDoctorDao.save(ifishDoctor);
		//扣除金币
		goldValue = goldValue.subtract(payTourSum);
		userAsset.setGoldValue(goldValue);
		//金币消耗记录
		GoldExpendRecord goldRecord = new GoldExpendRecord(userId, payTourSum, GoldExpendTypeEnum.questionPayTour);
		goldRecord.setRelationId(ifishDoctor.getQuestionId());
		goldExpendRecordDao.save(goldRecord);
		//平分金币
		MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);
		BigDecimal avgGoldValue = payTourSum.divide(new BigDecimal(size),mc);
		//已注册用户赠送20金币
		BigDecimal gold = new BigDecimal(20);
		for (Integer shopsUserId : userIds) {
			UserAsset shopsUserAsset = userAssetDao.get(shopsUserId);
			if(shopsUserAsset==null){
				shopsUserAsset = new UserAsset(shopsUserId, gold);
				userAssetDao.save(shopsUserAsset);
				//金币获取记录
				GoldGetRecord getRecord = new GoldGetRecord(shopsUserId,gold,GoldGetTypeEnum.registerGive);
				goldGetRecordDao.save(getRecord);
			}
			//加金币
			BigDecimal shopsGoldValue = shopsUserAsset.getGoldValue();
			shopsGoldValue = shopsGoldValue.add(avgGoldValue);
			shopsUserAsset.setGoldValue(shopsGoldValue);
			//金币获取记录
			GoldGetRecord getRecord = new GoldGetRecord(shopsUserId,avgGoldValue,GoldGetTypeEnum.questionPayTour);
			getRecord.setRelationId(ifishDoctor.getQuestionId());
			goldGetRecordDao.save(getRecord);
		}
		//发送云信通知
		neteaseIM.sendBatchMsg(userId.toString(),JSONArray.toJSONString(userIds), "【"+avgGoldValue+"金币打赏】来自鱼医生的提问："+ifishDoctor.getQuestionContent());
		//返回金币剩余数量
		JSONObject json = new JSONObject();
		json.put("goldValue", goldValue);
		return ResultUtil.success(json);
	}
}
