package com.cku.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cab.dao.CabThirdPartyMapper;
import com.cab.model.CabThirdParty;
import com.cku.core.PageBeanResult;
import com.cku.dao.CkuDogMapper;
import com.cku.dao.CkuDvUserMapper;
import com.cku.dao.CkuGjdlMapper;
import com.cku.dao.CkuMatchMapper;
import com.cku.dao.CkuShowsBaoMingMapper;
import com.cku.dao.CkuUserMapper;
import com.cku.dao.CkuUserPayListMapper;
import com.cku.dao.CkuUserPayTypeMapper;
import com.cku.dao.SPCartsMapper;
import com.cku.dao.SPOrderInfoMapper;
import com.cku.model.CkuDvUserModel;
import com.cku.model.CkuGjdl;
import com.cku.model.CkuOrderInfo;
import com.cku.model.CkuShowsBaoMing;
import com.cku.model.CkuUserPayType;
import com.cku.model.OrderDetil;
import com.cku.model.OrderDetilInfo;
import com.cku.model.SPCarts;
import com.cku.model.SPOrderInfo;
import com.cku.util.PageBean;

/**
 *user chaixueteng
 *2016年4月14日
 */
@Service(value="OrderService")
public class OrderServiceImpl {
	@Autowired
	public CkuUserPayListMapper ckuUserPayListMapper;
	@Autowired
	public SPCartsMapper spCartsMapper;
	@Autowired
	public CkuUserMapper ckuUserMapper;
	@Autowired
	public CkuGjdlMapper ckuGjdlMapper;

	@Autowired
	public CabThirdPartyMapper cabThirdPartyMapper;
	@Autowired
	public CkuDvUserMapper ckuDvUserMapper;
	@Autowired
	public CkuShowsBaoMingMapper ckuShowsBaoMingMapper;
	@Autowired
	public CkuUserPayTypeMapper ckuUserPayTypeMapper;
	@Autowired
	public SPOrderInfoMapper spOrderInfoMapper;
	@Autowired
	public CkuMatchMapper ckuMatchMapper;
	
	@Autowired
	public CkuDogMapper ckuDogMapper;
	/**
	 * 查询订单
	 * @param pb 
	 * @param token
	 * @param payConfirm
	 * @return
	 */
	public PageBeanResult<CkuOrderInfo> getOrderListByCkuId(Integer userId, String payState, PageBean pb) {
		String dvUserId = "-1";
		String ckuId = "-1";
		//根据cab用户id查询出第三方表中对应的CKU账号信息
		CabThirdParty cabThirdParty = cabThirdPartyMapper.selectByUserId(userId);
		if(cabThirdParty != null){
			dvUserId = cabThirdParty.getCkuUserId();
		}
		//根据CKU账号id查询出CKU账号信息
		CkuDvUserModel ckuDvUserModel = ckuDvUserMapper.getckuMemberByUserId(dvUserId);
		if(ckuDvUserModel != null){
			ckuId = ckuDvUserModel.getCkuId();
		}
		PageBeanResult<CkuOrderInfo> pbr=new PageBeanResult<CkuOrderInfo>();
		List<CkuOrderInfo> resultList = new ArrayList<CkuOrderInfo>();
		long start = pb.get_start();
		long limit = pb.get_limit();
		//已支付订单
		if("1".equals(payState)){
			//根据用户id和订单的支付状态查询所有已支付订单
			List<SPOrderInfo> orderList=spOrderInfoMapper.selectPayOrder(ckuId,payState,start,limit);
			for (SPOrderInfo ordersList : orderList) {
				List<OrderDetil> detilList=new ArrayList<OrderDetil>();
				Integer pid=0;
				Integer pcount=0;
				String subjectName="";
				//得到订单的创建时间
				Date addTime = ordersList.getAddTime();
				long time = addTime.getTime();
				//得到订单里的所有订单号
				String orderid = ordersList.getOrderid();
				if (orderid!=null) {
					//根据订单号查找订单详细里面的业务类型和数量
					List<SPCarts> cartList=spCartsMapper.getPayTypeAndCount(orderid);
					//订单项目有多条就接着查询小的项目放入一个list中
					if (cartList.size()>0) {
						for (SPCarts spCarts : cartList) {
							//得到订单费用类型的数量
							pcount = spCarts.getPcount();
							//得到业务编号
							pid = spCarts.getPid();
							//查询具体的类型名称
							CkuUserPayType payType=ckuUserPayTypeMapper.getPayType(pid);
							//业务类型
							subjectName = payType.getSubjectName();
							detilList.add(new OrderDetil(pid, subjectName, pcount));
						}
					}
				}
				//订单的价钱
				BigDecimal prices = ordersList.getSpPrices();
				//根据订单号得到支付方式
				String payWay=ckuUserPayListMapper.getMoneyWay(orderid);
				//过滤掉非赛事的订单
				if (pid==45||pid==140||pid==141||pid==142) {
					resultList.add(new CkuOrderInfo(time, orderid, detilList, prices, payWay, payState));
				}
			}
			pbr.list=resultList;
			if (pb.getNeedCount()) {
				pbr.totalCount=spOrderInfoMapper.getCount(ckuId,payState);
			}
		//未支付订单
		}else if("0".equals(payState)){
			//根据用户id和订单的支付状态查询所有已支付订单
			List<SPOrderInfo> orderList=spOrderInfoMapper.selectPayOrder(ckuId,payState,start,limit);
			for (SPOrderInfo ordersList : orderList) {
				List<OrderDetil> detilList=new ArrayList<OrderDetil>();
				Integer pid=0;
				Integer pcount=0;
				String subjectName="";
				//得到订单的创建时间
				Date addTime = ordersList.getAddTime();
				long time = addTime.getTime();
				//得到订单里的所有订单号
				String orderid = ordersList.getOrderid();
				if (orderid!=null) {
					//根据订单号查找订单详细里面的业务类型和数量
					List<SPCarts> cartList=spCartsMapper.getPayTypeAndCount(orderid);
					//订单项目有多条就接着查询小的项目放入一个list中
					if (cartList.size()>0) {
						for (SPCarts spCarts : cartList) {
							//得到订单费用类型的数量
							pcount = spCarts.getPcount();
							//得到业务编号
							pid = spCarts.getPid();
							//查询具体的类型名称
							CkuUserPayType payType=ckuUserPayTypeMapper.getPayType(pid);
							//业务类型
							subjectName = payType.getSubjectName();
							detilList.add(new OrderDetil(pid, subjectName, pcount));
						}
					}
				}
				//订单的价钱
				BigDecimal prices = ordersList.getSpPrices();
				//过滤掉非赛事的订单
				if (pid==45||pid==140||pid==141||pid==142) {
					resultList.add(new CkuOrderInfo(time, orderid, detilList, prices, null, payState));
				}
			}
			pbr.list=resultList;
			if (pb.getNeedCount()) {
				pbr.totalCount=spOrderInfoMapper.getCount(ckuId,payState);
			}
		}
		return pbr;
	}
	/**
	 * 订单详情页
	 * 
	 * @param orderId
	 * @param payState
	 * @param typeId 
	 * @param payType 
	 * @return
	 */
	@SuppressWarnings("all")
	public OrderDetilInfo getOrderDetil(String orderId, Integer typeId) {
		
		if (typeId==45||typeId==140||typeId==141||typeId==142) {
			//判断类型是否为犬只参赛 是则查询报名表里的信息
			CkuShowsBaoMing ckuShowsBaoMing=ckuShowsBaoMingMapper.getCkuDagId(orderId);
			//根据订单号查询报名表里面的cku血统证号
			String ckuStudbookNum = ckuShowsBaoMing.getCkuStudbookNum();
			//业务状态
			String payout = ckuShowsBaoMing.getPayout();
			//得到赛事编号 查询赛事信息
			String showNum = ckuShowsBaoMing.getShowNum();
			//得到赛事编号
			String[] showNums = showNum.split(",");
			//新的存放赛事信息的集合 
			List matchMsg= new ArrayList();
			for (String string : showNums) {
				//查询赛事信息
				String matchMessage=ckuMatchMapper.getMatchMessage(string.trim());
				matchMsg.add(matchMessage);
			}   
			//年龄组别
			String ageGroup = ckuShowsBaoMing.getAgeGroup();
			return new OrderDetilInfo(ckuStudbookNum, null, null, payout, null, matchMsg, ageGroup);
		//其他类型的业务详情
		}else {
			//如果是冠军登录类型就查询冠军登录表
			CkuGjdl ckuGjdl=ckuGjdlMapper.getLoginType(orderId);
			StringBuffer sb=new StringBuffer();
			sb.append(ckuGjdl.getPayout());
			sb.append(" ");
			sb.append(ckuGjdl.getInfoConfirm());
			String string = sb.toString();
			//得到cku血统证书号
			String ckuZsid = ckuGjdl.getCkuZsid();
			//根据cku血统证书号得到芯片号
			String dogCord=ckuDogMapper.getDogCord(ckuZsid);
			//登录类型
			String ckuDltype = ckuGjdl.getCkuDltype();
			//领取方式
			String ckuYjtype = ckuGjdl.getCkuYjtype();
			return new OrderDetilInfo(ckuZsid, dogCord, ckuDltype, string, ckuYjtype, null, null);
		}

	}
	
}
