package com.wechat.dao;

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

import org.springframework.stereotype.Repository;

import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.base.dao.impl.BaseDao;
import com.wechat.model.dbEntity.MdeTicketDetail;
import com.wechat.model.requestDto.TicketReq;

/**
 * 
 * @author tangwc
 *
 */
@Repository
public class MdeTicketDetailDao extends BaseDao {
	public Map<String,Object> map = new HashMap<String,Object>();
	/**
	 * 根据id获取券
	 * @param tdId
	 * @return
	 */
	public MdeTicketDetail getByIdOrCode(String tdId,String lang) {
		map.clear();
		map.put("tdId", tdId);
		map.put("lang", lang);
		return super.selectOne("MdeTicketDetailMapper.get", map);
	}
	/**
	 * 判断该券是否在使用有效期内，然后是否可使用
	 * @param tdCode
	 * @param lang 
	 * @return
	 */
	public MdeTicketDetail getByTdCode(String tdCode, String lang) {
		map.clear();
		map.put("tdCode", tdCode);
		map.put("lang", lang);
		return super.selectOne("MdeTicketDetailMapper.getByTdCode", map);
	}
	/**
	 * 获取用户所有券 按照是否可使用与日期倒叙排序
	 * @param userCode
	 * @return
	 */
	public List<MdeTicketDetail> getByUserCode(String userCode,String lang){
		map.clear();
		map.put("userCode", userCode);
		map.put("lang", lang);
		return super.select("MdeTicketDetailMapper.getByUserCode",map);
	}
	/**
	 * 获取用户所有可使用券 按照日期倒叙排序
	 * @param userCode
	 * @param lang 
	 * @return
	 */
	public List<MdeTicketDetail> getValidTicketByUser(String userCode, String identityFlag, String lang) {
		map.clear();
		map.put("userCode", userCode);
		map.put("identityFlag", identityFlag);
		map.put("lang", lang);
		return super.select("MdeTicketDetailMapper.getValidTicketByUser",map);
	}
	/**
	 * 分页查询用户券
	 * @param page -> userCode,usedFlag,index,size
	 * @return
	 */
	public PagingResult<MdeTicketDetail> getTicketDetails(Pagination page) {
		return super.selectPagination("MdeTicketDetailMapper.getTicketDetails", "MdeTicketDetailMapper.getTicketDetailCounts", page);
	}
	/**
	 * 新增用户券
	 * @param detail
	 * @return
	 */
	public int insertTicket(MdeTicketDetail detail) {
		return super.insert("MdeTicketDetailMapper.insert", detail);
	}
	/**
	 * 更新用户券
	 * @param detail
	 * @return
	 */
	public int updateTicket(MdeTicketDetail detail) {
		return super.insert("MdeTicketDetailMapper.update", detail);
	}
	public int updateByUserName(String userName, String userCode) {
		map.clear();
		map.put("userName", userName);
		map.put("userCode", userCode);
		return sqlSession.update("MdeTicketDetailMapper.updateByUserName", map);
	}
	
	 /**
     * 更新领取的分享券
     * @param param
     * @return
     */
    public int updateShareTicket(MdeTicketDetail param) {
        return super.update("MdeTicketDetailMapper.updateShareTicket",param);
    }
	
	/**
	 * 检查营销券是否被领取过
	 * @param param
	 * @return
	 */
	public MdeTicketDetail getByTcCodeAndUserName(TicketReq param) {
		return super.selectOne("MdeTicketDetailMapper.getByTcCodeAndUserName",param);
	}
	/**
	 * 校验抵用券是否存在
	 * @param userCode
	 * @param tcCode
	 * @param tdCode
	 * @param lang
	 * @return
	 */
	public MdeTicketDetail checkTdByCode(String userCode, String tcCode, String tdCode, String lang) {
		map.clear();
		map.put("userCode", userCode);
		map.put("tcCode", tcCode);
		map.put("tdCode", tdCode);
		map.put("lang", lang);
		return super.selectOne("MdeTicketDetailMapper.checkTdByCode",map);
	}
	public int updateUsedFlag(String tdId) {
		return sqlSession.update("MdeTicketDetailMapper.updateUsedFlag",tdId);
	}
	
	/**
	 * 重新启用优惠券
	 * @param tdId
	 * @return
	 */
	public int restartTicket(String tdCode) {
		return sqlSession.update("MdeTicketDetailMapper.restartTicket",tdCode);
	}
	
	
}
