package com.iamberry.wechat.handles.order.orderListener;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.relationship.DealerRelation;
import com.iamberry.wechat.core.entity.relationship.WdRelation;
import com.iamberry.wechat.face.drp.PlaceInfoService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.order.AdminOrderService;
import com.iamberry.wechat.face.relationship.DealerRelationService;
import com.iamberry.wechat.face.relationship.WdRelationService;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.utils.SendMessageUtil;

/**
 * 定时任务：30天后自动奖励积分
 * @author 刘志伟
 *
 */
public class OrderListener{
	
	@Autowired
	private AdminOrderService adminOrderService;

	@Autowired
	private SendMessageUtil sendMessageUtil;
	
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private WdRelationService wdRelationService;
	
	
	@Autowired
	private DealerRelationService dealerRelationService;
	
	@Autowired
	private PlaceInfoService placeInfoService;
	
	public void executeTimerTask() throws Exception {
		ShopSystemRule shopSystemRule = adminOrderService.getReluByRid(102);
		ShopSystemRule agencySystemRule = adminOrderService.getReluByRid(120);
		int minPage = 0;
		int maxPage = 10;
		Order orders = new Order();
		int deadline = (int)(shopSystemRule.getRuleNum()*24*60*60);
		orders.setDeadline(deadline);
		orders.getPage().setRecordBegin(minPage);
		orders.getPage().setPageSize(maxPage);
		//根据条件获取没有奖励积分但可以奖励积分的数据
		Integer num = adminOrderService.getOrderNum(orders);
		//每次执行10条数据，判断一共需要执行几次
		int count = num % maxPage == 0? num/maxPage:(num/maxPage)+1;
		if (count > 0) {
			for (int i = 0;i < count;i++) {
				//每次查询10条数据执行
				List<Order> orderList = adminOrderService.selectOrderByIntegral(orders);
				if (null != orderList && orderList.size() > 0) {
					for (Order order : orderList) {
						//修改订单表数据，并添加线下代理现金记录表数据
						adminOrderService.updateOrderIntegral(order,agencySystemRule);
		
						
					//推送消息
						
					//推送金额变动消息
					Member member = memberService.getMemberByUserOpenId(order.getSalesOpenid());
//System.out.println("============="+i);
					if(member != null || member.getUserResType() != null) {
						String toopenid = "";
						switch(member.getUserResType().intValue()){//用户来源
							case 2:		//用户来源	微代理
							WdRelation wdrelation = wdRelationService.getWdRelationByOpenid(order.getSalesOpenid());
								Member fatherMember = memberService.getMemberByUserId(wdrelation.getWdFirst());	//微代理父节点信息
								if(fatherMember != null && StringUtils.isNotEmpty(fatherMember.getUserOpenid())){
									toopenid=fatherMember.getUserOpenid();
								}
								break;
							case 3:		//用户来源	线下
								DealerRelation dealerrelation= dealerRelationService.getDealerRelationById(member.getUserDealers());
								int Dealerid = 0;
								if(dealerrelation!=null){
									if(dealerrelation.getDealerThird()!=null){ //第三极
										Dealerid = dealerrelation.getDealerThird();
									}else if(dealerrelation.getDealerSecond()!=null){ //第二级
										Dealerid = dealerrelation.getDealerSecond();
									}else if(dealerrelation.getDealerFifth()!=null){ //第一级
										Dealerid = dealerrelation.getDealerFifth();}
								}
								PlaceInfo fatherPlace = placeInfoService.getPlaceInfoById(Dealerid);//分销父节点信息
								if(fatherPlace!=null){
									toopenid=fatherPlace.getOpenid();
								}
								break;
						}
						String temp = ResultInfo.REWARDSREMINDTEMP;
						temp = temp.replaceFirst("firstValueIamberry", ResultInfo.REWARDSHASMESSAGE)
							.replaceFirst("orderValueIamberry", order.getSalesOrderid())
							.replaceFirst("moneyValueIamberry", "返现到账")
							.replaceFirst("remarkValueIamberry", ResultInfo.INTOSHOP);
						sendMessageUtil.sendTemplateMessageToOpenid(temp, toopenid, ResultInfo.INDEX_PAGE,1);	
					}
						
						
						
						
						
						
						
					}
				}
				if (maxPage + 10 < num) {
					minPage += 10;
					maxPage += 10;
					orders.getPage().setRecordBegin(minPage);
					orders.getPage().setPageSize(maxPage);
				} else {
					minPage += 10;
					orders.getPage().setRecordBegin(minPage);
					orders.getPage().setPageSize(num);
				}
			}
		}
	}
}
