package com.wechat.controller;

import java.util.List;

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

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.wechat.global.base.BaseController;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dto.ActivityDetailDto;
import com.wechat.model.dto.ActivityInfoDto;
import com.wechat.model.dto.BaseUserDto;
import com.wechat.model.dto.BeInvitedDtlDto;
import com.wechat.model.dto.ExchangeHisDto;
import com.wechat.model.dto.InvitationNumDto;
import com.wechat.model.responseDto.InvitationInitResp;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.InvitationService;


@Controller
@RequestMapping("invitation")
public class InvitationController extends BaseController {
	
	@Resource
	private CommonService commonService;
	
	@Resource
	private InvitationService invitationService;

    /**
     * 邀请页面初始化
     * 
     * @param actId 活动场次id
     * @return
     */
    @RequestMapping(value = "init", method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<InvitationInitResp> init(@RequestParam String actId, HttpServletRequest request) throws Exception {
    	InvitationInitResp result = new InvitationInitResp();
    	ResultEntity<InvitationInitResp> resp = new ResultEntity<InvitationInitResp>();
    	//活动场次id不能为空
    	if(StringUtils.isEmpty(actId))
    	{
    		log.error("活动场次id为空");
    		resp.setResultEntity(InfoMsg.ERROR_DATA_MISS);
    		return resp;
    	}
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	MdeUser userInfo = commonService.getUserInfo(userCode);
    	//将user和返回类中相同属性复制至返回集中
    	BaseUserDto baseUserInfo = new BaseUserDto();
		BeanUtils.copyProperties(userInfo, baseUserInfo);
    	
    	//拼装邀请人id
		String inviter = commonService.getInviter(userCode, actId);
    	
    	//获取用户邀请的数据
    	InvitationNumDto invitationNum = invitationService.getInvitationNum(inviter,userCode, actId);
    	
    	//活动相关内容
    	ActivityInfoDto activityInfo = invitationService.getActivityInfo(actId);
    	List<ActivityDetailDto> activityDetailList = invitationService.getActivityDetails(actId);
    	
    	//邀请明细
    	List<BeInvitedDtlDto> beInvitedDtlList = invitationService.getBeInvitedDtl(inviter);
    	
    	//结果封装
    	result.setActivityDetail(activityDetailList);
    	result.setActivityInfo(activityInfo);
    	result.setBaseUser(baseUserInfo);
    	result.setBeInvitedDtlList(beInvitedDtlList);
    	result.setInvitationNum(invitationNum);
    	result.setInviter(inviter);
    	resp.setResult(result);
    	
    	return resp;
    }
    
    /**
     * 获取兑换履历
     * 
     * @param actId 活动场次id
     * @return
     */
    @RequestMapping(value = "getExchangeHis", method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<List<ExchangeHisDto>> getExchangeHis(@RequestParam String actId, HttpServletRequest request) throws Exception {
    	ResultEntity<List<ExchangeHisDto>> resp = new ResultEntity<List<ExchangeHisDto>>();
    	//活动场次id不能为空
    	if(StringUtils.isEmpty(actId))
    	{
    		log.error("活动场次id为空");
    		resp.setResultEntity(InfoMsg.ERROR_DATA_MISS);
    		return resp;
    	}
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//获取兑换记录
    	List<ExchangeHisDto> exList = invitationService.getExchangeHis(actId,userCode);
    	
    	//结果封装
    	resp.setResult(exList);
    	return resp;
    }
    
    
    /**
     * 提交兑换申请
     * 
     * @param prizeId 奖品id
     * @return
     */
    @RequestMapping(value = "exchangeRequest", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<InvitationInitResp> exchangeRequest(@RequestParam String prizeId, HttpServletRequest request) throws Exception {
    	ResultEntity<InvitationInitResp> resp = new ResultEntity<InvitationInitResp>();
    	//奖品id不能为空
    	if(StringUtils.isEmpty(prizeId))
    	{
    		log.error("活动奖品id为空");
    		resp.setResultEntity(InfoMsg.ERROR_DATA_MISS);
    		return resp;
    	}

    	//兑换申请存储
    	int inNum = invitationService.exchangeRequest(prizeId, request);
    	if(inNum < 1)
    	{
    		resp.setMsg(InfoMsg.ERROR_INSERT_ERROR);
    	}
    	
    	return resp;
    }
    
    /**
     * 取消兑换申请
     * 
     * @param prizeId 奖品id
     * @return
     */
    @RequestMapping(value = "cancelExchangeRequest", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<InvitationInitResp> cancelExchangeRequest(@RequestParam Integer hisId, HttpServletRequest request) throws Exception {
    	ResultEntity<InvitationInitResp> resp = new ResultEntity<InvitationInitResp>();
    	//兑换请求id不能为空
    	if(hisId == null || hisId < 1)
    	{
    		log.error("兑换请求id为空");
    		resp.setResultEntity(InfoMsg.ERROR_DATA_MISS);
    		return resp;
    	}

    	//兑换申请删除
    	invitationService.cancelExchangeRequest(hisId, request);

    	return resp;
    }
    
}
