/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.wechat.controller;

import java.util.List;
import java.util.Map;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
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.google.common.collect.Maps;
import com.wechat.global.base.BaseController;
import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeTicketCategory;
import com.wechat.model.dbEntity.MdeTicketDetail;
import com.wechat.model.requestDto.PageWithTicketDetail;
import com.wechat.model.requestDto.TicketReq;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.MdeTicketCategoryService;
import com.wechat.service.MdeTicketDetailService;


/**
 * 用户券Controller
 * @author tangwc
 * @version 2018-09-09
 */
@Controller
@RequestMapping(value = "ticket")
public class TicketController extends BaseController {

	@Resource
	private MdeTicketCategoryService ticketCategoryService;
	@Resource
	private MdeTicketDetailService mdeTicketDetailService;
	@Resource
	private CommonService commonService;
	
	/**
	 * 获取券信息
	 * @param request
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getTicketInfo", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<MdeTicketCategory> getTicketInfo(HttpServletRequest request,@RequestParam String id) throws Exception {
		String lang = commonService.getLanguage(request);
		MdeTicketCategory result = mdeTicketDetailService.getByCode(id,lang);
		return new ResultEntity<MdeTicketCategory>(InfoMsg.SUCCESS_REQUEST,result);
	}

	/**
	 * 获取用户券 分页
	 * @param request
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getTicketDetails", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<PagingResult<MdeTicketDetail>> getTicketDetails(HttpServletRequest request,@RequestBody PageWithTicketDetail page) throws Exception {
		String userCode = commonService.getUserCode(request);
//		String identityFlag = commonService.getIdentityFlag(request);
		String lang = commonService.getLanguage(request);
		
		Pagination pagination = new Pagination(page.getIndex(), page.getSize());
		// 获取券信息
		Map<Object, Object> params = Maps.newHashMap();
		params.put("userCode", userCode);
//		params.put("identityFlag", identityFlag);
		params.put("usedFlag", page.getUsedFlag());
		params.put("lang", lang);
		pagination.setParams(params);
		PagingResult<MdeTicketDetail> result = mdeTicketDetailService.getTicketDetails(pagination);
		
		return new ResultEntity<PagingResult<MdeTicketDetail>>(InfoMsg.SUCCESS_REQUEST,result);
	}
	
	/**
	 * 领取券 营销券和分享券
	 * @param request
	 * @param param tcCode tdCode contactInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="acquireTicket", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<Integer> acquireSharedTicket(HttpServletRequest request,@RequestBody TicketReq param) throws Exception {
		String lang = commonService.getLanguage(request);
		int result = mdeTicketDetailService.acquireTicket(param,lang);
		if(result<0)
		{
		    return new ResultEntity<Integer>(InfoMsg.ERROR_TICKET_OUT,result);
		}else
		{
		    return new ResultEntity<Integer>(InfoMsg.SUCCESS_REQUEST,result);
		}
	}
	
//	/**
//	 * 领取营销的券
//	 * @param request
//	 * @param page
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value="acquireMarketingTicket", method = RequestMethod.POST)
//	@ResponseBody
//	public ResultEntity<Integer> acquireMarketingTicket(HttpServletRequest request,@RequestBody MarketingTicketReq param) throws Exception {
//		String lang = commonService.getLanguage(request);
//		int result = mdeTicketDetailService.acquireMarketingTicket(param,lang);
//		return new ResultEntity<Integer>(InfoMsg.SUCCESS_REQUEST,result);
//	}
	/**
	 * 获取用户所有可使用券 日期倒叙排序
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getValidTicketByUser", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<MdeTicketDetail>> getValidTicketByUser(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		String identityFlag = commonService.getIdentityFlag(request);
		String lang = commonService.getLanguage(request);
		List<MdeTicketDetail> list = mdeTicketDetailService.getValidTicketByUser(userCode,identityFlag,lang);
		return new ResultEntity<List<MdeTicketDetail>>(InfoMsg.SUCCESS_REQUEST,list);
	}
	

}