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

import java.util.HashMap;
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.MdeOrders;
import com.wechat.model.dto.OrderListDto;

/**
 * 单表生成DAO接口
 * 
 * @author tangwc
 * @version 2018-06-08
 */
@Repository
public class MdeOrdersDao extends BaseDao {

	public MdeOrders findOneByTradeCode(String out_trade_no) {
		return super.selectOne("MdeOrdersMapper.findOneByTradeCode", out_trade_no);
	}

	public PagingResult<OrderListDto> getBuyRecord(Pagination page) {
		return super.selectPagination("MdeOrdersMapper.getBuyRecord", "MdeOrdersMapper.getBuyRecordCount", page);
	}
	public int deleteOrderByUser(String orderId, String userCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		map.put("orderId", orderId);
		return super.sqlSession.update("MdeOrdersMapper.deleteOrderByUser", map);
	}
	
	/**
	 * 获取有效的会员订单
	 * @param userCode
	 * @return
	 */
	public MdeOrders getValidOrdersByUserCode(String userCode) {
		return super.selectOne("MdeOrdersMapper.getValidOrdersByUserCode", userCode);
	}

	public MdeOrders getOrderInfoByCode(String orderId, String userCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		map.put("orderId", orderId);
		return super.selectOne("MdeOrdersMapper.getOrderInfoByCode", map);
	}
	
	/**
	 * 获取指定的待支付订单
	 * @param userCode
	 * @return
	 */
	public MdeOrders getNoPayOrder(String orderId, String userCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		map.put("orderId", orderId);
		map.put("ordStatus", "1");
		return super.selectOne("MdeOrdersMapper.getOrderInfoByCode", map);
	}

	public int insertNewOrder(MdeOrders order) {
		return super.insert("MdeOrdersMapper.insert", order);
	}
	
	public int updateById(MdeOrders mdeOrders) {
		return super.update("MdeOrdersMapper.updateById", mdeOrders);
	}
	
	/**
	 * 已支付订单
	 * @param mdeOrders
	 * @return
	 */
	public int updatePaidOrder(MdeOrders mdeOrders) {
		return super.update("MdeOrdersMapper.updatePaidOrder", mdeOrders);
	}
	
	
	/**
	 * 获取用户购买商品次数
	 * @param orderId
	 * @param userCode
	 * @return
	 */
	public int getBuyTimesOfGoods(int goodsId, String userCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		map.put("goodsId", goodsId);
		return super.selectOne("MdeOrdersMapper.getBuyTimesOfGoods", map);
	}
	
	/**
	 * 获取用户购买商品次数
	 * @param goodsId
	 * @return
	 */
	public int getGoodsSaleNums(int goodsId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goodsId", goodsId);
		return super.selectOne("MdeOrdersMapper.getGoodsSaleNums", map);
	}
	
}