package com.wechat.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wechat.common.CommonHelper;
import com.wechat.dao.MdeActivityCategoryDao;
import com.wechat.dao.MdeUserDao;
import com.wechat.model.dbEntity.MdeExchangeHis;
import com.wechat.model.dto.ActivityDetailDto;
import com.wechat.model.dto.ActivityInfoDto;
import com.wechat.model.dto.BeInvitedDtlDto;
import com.wechat.model.dto.ExchangeHisDto;
import com.wechat.model.dto.InvitationNumDto;

@Service
public class InvitationService {
	private static final Log log = LogFactory.getLog(InvitationService.class);

	@Resource
	private CommonService commonService;
	
	@Resource
	protected CommonHelper commonHelper;

	@Resource
	private MdeUserService userService;

	@Resource
	private MdeActivityCategoryDao activityCategoryDao;
	
	@Resource
	private MdeUserDao mdeuserDao;


	/**
	 * 获取用户邀请数据
	 * @param userCode 
	 * 
	 * @return
	 */
	public InvitationNumDto getInvitationNum(String inviter,String userCode, String actId) {
		return mdeuserDao.getInvitationNum(inviter, userCode, actId);
	}

	/**
	 * 获取活动场次信息
	 * @param userCode 
	 * 
	 * @return
	 */
	public ActivityInfoDto getActivityInfo(String actId) {
		return activityCategoryDao.getActivityInfo(actId);
	}
	
	/**
	 * 获取活动奖品详细信息
	 * @param userCode 
	 * 
	 * @return
	 */
	public List<ActivityDetailDto> getActivityDetails(String actId) {
		List<ActivityDetailDto> list =  activityCategoryDao.getActivityDetails(actId);
		if (CollectionUtils.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setImgPc(commonService.addDomain(list.get(i).getImgPc()));
				list.get(i).setImgMobile(commonService.addDomain(list.get(i).getImgMobile()));
			}
		}
		return list;
	}
	
	
	/**
	 * 获取受邀人详情
	 * @param userCode 
	 * 
	 * @return
	 */
	public List<BeInvitedDtlDto> getBeInvitedDtl(String actId) {
		return mdeuserDao.getBeInvitedDtl(actId);
	}
	
	/**
	 * 提交兑换记录
	 * @param userCode 
	 * 
	 * @return
	 */
	public int exchangeRequest(String prizeId, HttpServletRequest request) {
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//数据填充
    	MdeExchangeHis exchangeHis = new MdeExchangeHis();
    	exchangeHis.setUserCode(userCode);
    	exchangeHis.setPrizeId(prizeId);
    	exchangeHis.setStatus("1");
    	
		return activityCategoryDao.insertExchangeRequest(exchangeHis);
	}
	
	/**
	 * 获取兑换记录
	 * @param userCode 
	 * 
	 * @return
	 */
	public List<ExchangeHisDto> getExchangeHis(String actId, String userCode) {
		List<ExchangeHisDto> list =  activityCategoryDao.getExchangeHis(actId,userCode);
		if (CollectionUtils.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setImgPc(commonService.addDomain(list.get(i).getImgPc()));
				list.get(i).setImgMobile(commonService.addDomain(list.get(i).getImgMobile()));
			}
		}
		return list;
	}

	/**
	 * 撤销兑换记录
	 * @param userCode 
	 * 
	 * @return
	 */
	public int cancelExchangeRequest(Integer hisId, HttpServletRequest request) {
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//数据填充
    	MdeExchangeHis exchangeHis = new MdeExchangeHis();
    	exchangeHis.setUserCode(userCode);
    	exchangeHis.setHisId(hisId);
    	exchangeHis.setStatus("1");
    	
		int delNum = activityCategoryDao.cancelExchangeRequest(exchangeHis);
    	if(delNum < 1)
    	{
    		log.error("***********兑换记录删除异常！！***************");
    		log.error("*******ip："+commonHelper.getIRealIPAddr(request)+"**用户code："+userCode+"**删除对象："+hisId+"******");
    	}
    	
    	return delNum;
	}
	
}
