package com.iamberry.wechat.service.drp;

import com.iamberry.wechat.core.entity.AdminUtils;
import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.drp.ApplyPlaceLogs;
import com.iamberry.wechat.core.entity.drp.ApplyUserInfo;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.ProbationOrderDto;
import com.iamberry.wechat.core.entity.reback.RebackInfo;
import com.iamberry.wechat.core.entity.relationship.DealerRelation;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.drp.PlaceInfoService;
import com.iamberry.wechat.service.mapper.DealerRelationMapper;
import com.iamberry.wechat.service.mapper.PlaceInfoMapper;
import com.iamberry.wechat.service.mapper.PlaceWithDrawalsLogsMapper;
import com.iamberry.wechat.service.mapper.ProbationShopOrderMapper;
import com.iamberry.wechat.service.member.dao.MemberDao;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResponseJson;
import com.iamberry.wechat.tools.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 
 * @author 欧阳明
 * @Description: 代理者信息服务-业务处理
 * @Date:2016年4月19日
 *
 */
@Service("placeInfoServiceImpl")
public class PlaceInfoServiceImpl implements PlaceInfoService {

	@Autowired
	private PlaceInfoMapper placeInfoMapper;
	@Autowired
	PlaceWithDrawalsLogsMapper placeWithDrawalsLogsMapper;
	@Autowired
	private AdminUtils adminUtils;
	@Autowired
	private DealerRelationMapper dealerRelationMapper;
	@Autowired
	private SystemService systemService;
	@Autowired
	private PlaceInfoService placeInfoService;
	@Autowired
	private ProbationShopOrderMapper probationShopOrderMapper;
	@Autowired
	private MemberDao memberDao;

	/**
	 * 增加一个商户
	 */
	public ResultMsg addOne(PlaceInfo placeInfo) {
		ResultMsg remsg=new ResultMsg();
		placeInfo.setCreateDate(new Date());
		placeInfo.setStatus(1);
		// 产生安全的code
		String code = adminUtils.byte2hex(adminUtils.createRandomByte());
		try {
			code = NameUtils.getConfig("PLACE_CODE_PREFIX") + adminUtils.byte2hex(adminUtils.createRandomByte()).substring(0, 10);
		} catch (Exception e) {
			code = NameUtils.getConfig("PLACE_CODE_PREFIX") + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
		}
		if (placeInfo.getName().equals("IAMBERRY")) {
			placeInfo.setName("IAMBERRY" + code);
		}
		placeInfo.setInitCode(code);
		
		placeInfo.setName(placeInfo.getName().trim());
		placeInfo.setUserName(placeInfo.getUserName().trim());
		
		int isExist = placeInfoMapper.testNameExist(placeInfo);
		if(isExist > 0 ){
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.repeatNameError);
			remsg.setStatus(false);
			return remsg;
		}
		int i = placeInfoMapper.insertObj(placeInfo);
		if(i < 1){
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			remsg.setStatus(false);
			return remsg;
		}
		
		 //插入分销商层次表
		DealerRelation dealerRelation = new DealerRelation();
		if(placeInfo.getPlacePid() == 0){
			dealerRelation.setDealerFirst(placeInfo.getId());
			dealerRelation.setDealerCount(1);
		}else{
			PlaceInfo placeInfoParent = placeInfoMapper.getOnePlaceInfo(placeInfo.getPlacePid());
			//查询父级商户的商户层关系 
			dealerRelation = dealerRelationMapper.getDealerRelationById(placeInfoParent.getPlaceIds());
			
			switch(dealerRelation.getDealerCount().intValue()){
			case 1:
				dealerRelation.setDealerCount(2);
				dealerRelation.setDealerSecond(placeInfo.getId());
				break;
			case 2:
				dealerRelation.setDealerCount(3);
				dealerRelation.setDealerThird(placeInfo.getId());
				break;
			case 3:
				dealerRelation.setDealerCount(4);
				dealerRelation.setDealerFourth(placeInfo.getId());
				break;
			case 4:
				dealerRelation.setDealerCount(5);
				dealerRelation.setDealerFifth(placeInfo.getId());
				break;
			case 5:
				dealerRelation.setDealerCount(6);
				dealerRelation.setDealerSixth(placeInfo.getId());
				break;	
			}
		}
		//商户关系表中插入数据
		i = dealerRelationMapper.insertDealerRelation(dealerRelation);
		if(i < 1){
		    remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			remsg.setStatus(false);
			return remsg;
		}
		//给分销商表插入id		因为插入关系表中需要存当前商户的id 所以只能先插入后修改
		placeInfo.setPlaceIds(dealerRelation.getDealerId());
		i = placeInfoMapper.update(placeInfo);
		if(i > 0){
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			remsg.setData(placeInfo);
			remsg.setStatus(true);
		}else{
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			remsg.setStatus(false);
		}
		 return remsg;
	}

	//获取分销商详情
	public PlaceInfo getPlaceInfoById(int id) {
	  return placeInfoMapper.getOnePlaceInfo(id);
	}

	/**
	 * 获取所有使用中的商户  (用在添加商户时搜索父级)
	 * 支持模糊条件查询：name userName roleId tel userNickname placePid
	 * 				supperId parentName
	 * 根据 place_create_date降序	分页
	 */
	public Map<String, Object> SearchPlaceInfo(PlaceInfo placeInfo) {
		Map<String, Object> hm = new HashMap<String, Object>();
		List<PlaceInfo> lists = placeInfoMapper.selectAllByParent(placeInfo);
		hm.put("data",lists);
		hm.put("totalNumber", placeInfoMapper.getAllByParentCount(placeInfo));
		return hm;
	}

	//分销商其他分页查询(只有后台)
	public Map SearchPlaceInfoPage(PageBean page) {
		Map hm=new HashMap();
		PageBean pageIndex=new PageBean();
		PlaceInfo placeInfo=(PlaceInfo)page.getCondition();
		
		pageIndex.setPageNumber(page.getPageNumber());
		pageIndex.setPageSize(page.getPageSize());
		
		if(page.getPageCount()>0){
			pageIndex.setCount(page.getPageCount());
		}else{
			pageIndex.setCount(placeInfoMapper.getAllCount(placeInfo));
		}
		
		pageIndex.initRecordBegin();
		placeInfo.setPage(pageIndex);
		List<PlaceInfo> lists=placeInfoMapper.selectAll(placeInfo);
        hm.put("data",lists);
		hm.put("page",pageIndex);
		return hm;
	}

	/**
	 * 分销商-商品信息管理列表查询列表数
	 */
	public int getAllCount(PlaceInfo placeInfo){
		int flag = placeInfoMapper.getAllCount(placeInfo);
		return  flag;
	}

	/**
	 * 分销商-商户信息管理列表查询
	 * @return
	 */
	public List getPlaceInfoList(PlaceInfo placeInfo){
		placeInfo.getPage().initRecordBegin();
		List<PlaceInfo> lists=placeInfoMapper.selectAll(placeInfo);
		for (PlaceInfo place : lists) {
			//获取下线用户数量
			int userNum = 0;
			if(54 == (place.getRoleId())){   // 公司分销员
				PlaceInfo plInfo = new PlaceInfo();
				plInfo.setPlacePid(place.getId());
				userNum = placeInfoMapper.getDistributorNumber(plInfo);
			}else if ( 55 == (place.getRoleId())){			// 个人分销员
				Member member = new Member();
				member.setUserDealers(place.getPlaceIds());
				userNum = memberDao.getOrdinaryNumber(member);
			}

			int rebackMoney = 0;//待返利金额
			List<RebackInfo> rebackList = placeInfoMapper.listPlaceReback(place);
			if (rebackList != null && rebackList.size() > 0) {
				for (RebackInfo rebackInfo : rebackList) {
					if (rebackInfo.getStatus() == 1) {
						rebackMoney += rebackInfo.getMoney();
					}
				}
			}
			place.setRebatePending(rebackMoney);	//待返利金额
			place.setOfflineubscriber(userNum);		//下线用户数量
        }
		return lists;
	}

	//更新对象
	public ResultMsg update(PlaceInfo placeInfo) {
		 HashMap<String,Integer> conditionMap=new HashMap<String,Integer>();
         int i=0;
         ResultMsg remsg=new ResultMsg();
         //获取更改前的对象
		 PlaceInfo orgin=placeInfoMapper.getOnePlaceInfo(placeInfo.getId());
		 i=placeInfoMapper.update(placeInfo);
		 if(i<0){
				remsg.setResultCode(ResultInfo.ERRORCODE);
				remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
				remsg.setStatus(false);
				return remsg;
			}
		 PlaceInfo parent=placeInfoMapper.getOnePlaceInfo(placeInfo.getPlacePid());
		 if(parent==null){
			 remsg.setStatus(true);
			 remsg.setResultCode(ResultInfo.SUCCESSCODE);
			 remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			 return remsg;
		 }

		 {
			 DealerRelation dealerRelation=dealerRelationMapper.getDealerRelationById(parent.getPlaceIds());
				
				switch(dealerRelation.getDealerCount().intValue()){
				case 1:
					dealerRelation.setDealerSecond(placeInfo.getId());
					dealerRelation.setDealerCount(2);
					dealerRelation.setDealerId(orgin.getPlaceIds());
					i=dealerRelationMapper.updateRelation(dealerRelation);
					break;
				case 2:
					dealerRelation.setDealerThird(placeInfo.getId());
					dealerRelation.setDealerCount(3);
					dealerRelation.setDealerId(orgin.getPlaceIds());
					i=dealerRelationMapper.updateRelation(dealerRelation);
					break;
				case 3:
					dealerRelation.setDealerFourth(placeInfo.getId());
					dealerRelation.setDealerCount(4);
					dealerRelation.setDealerId(orgin.getPlaceIds());
					i=dealerRelationMapper.updateRelation(dealerRelation);
					break;
				case 4:
					dealerRelation.setDealerFifth(placeInfo.getId());
					dealerRelation.setDealerCount(5);
					dealerRelation.setDealerId(orgin.getPlaceIds());
					i=dealerRelationMapper.updateRelation(dealerRelation);
					break;	
				case 5:
					dealerRelation.setDealerSixth(placeInfo.getId());
					dealerRelation.setDealerCount(6);
					dealerRelation.setDealerId(orgin.getPlaceIds());
					i=dealerRelationMapper.updateRelation(dealerRelation);
					break;	
				}
		 }
		 
		 if(orgin.getId()==orgin.getPlacePid() || orgin.getPlacePid()==0){
			 DealerRelation dealerRelation = new DealerRelation();
			 dealerRelation.setDealerId(orgin.getPlaceIds());
			 dealerRelation.setDealerFirst(orgin.getId());
			 dealerRelation.setDealerSecond(null);
			 dealerRelation.setDealerThird(null);
			 i=dealerRelationMapper.updateRelation(dealerRelation);
		 }

		 if(i>0){
				remsg.setResultCode(ResultInfo.SUCCESSCODE);
				remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			}else{
				remsg.setResultCode(ResultInfo.ERRORCODE);
				remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			}
		 return remsg;
	}

	//通过openid获取分销商信息
	public PlaceInfo getPlaceInfoByOpenId(String openid) {
		return placeInfoMapper.getOnePlaceInfo2(openid);
		
	}
	//查询分销商信息用于提供excel下载
	public List<PlaceInfo> selectPlaceInfosForDownLoad(Map map){
		return placeInfoMapper.getPlaceInfosForDownLoad(map);
	}
	
	@Override
	public Integer selectPlaceIdByOpenId(String openId) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectPlaceIdByOpenId(openId);
	}

	@Override
	public PlaceInfo getOnePlaceInfoByInitcode(PlaceInfo placeInfo) {
		// TODO Auto-generated method stub
		return placeInfoMapper.getOnePlaceInfoByInitcode(placeInfo);
	}
	
	//批量更新分銷商信息
	public int batchUpdate(List<PlaceInfo> placeInfos){
		return placeInfoMapper.batchUpdate(placeInfos);
	}

	@Override
	public List<PlaceInfo> getPlaceId(PlaceInfo placeInfo) {
		// TODO Auto-generated method stub
		return placeInfoMapper.getPlaceId(placeInfo);
	}

	@Override
	public List<ApplyPlaceLogs> selectApplyPlaceLogs(int pageNO, int pageSize) {
		// TODO Auto-generated method stub
		PaperBean bean = new PaperBean();
		bean.setPaperData(pageNO, pageSize);
		return placeInfoMapper.selectApplyPlaceLogs(bean);
	}

	@Override
	public int getCount(int pageSize) {
		int count = placeInfoMapper.getCount();
		return PaperBean.justPaperCount(count, pageSize);
	}

	@Override
	public int updateApplyStatus(ApplyPlaceLogs logs) {
		// TODO Auto-generated method stub
		return placeInfoMapper.updateApplyStatus(logs);
	}

	@Override
	public ApplyPlaceLogs selectApplyLogsByPlaceID(Integer placeID) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectApplyLogsByPlaceID(placeID);
	}

	@Override
	public boolean handlerApplyLogs(Integer applyID, Integer status) {
		// TODO Auto-generated method stub
		
		ApplyPlaceLogs logs = new ApplyPlaceLogs();
		logs.setApplyID(applyID);
		logs.setApplyStatus(status);
		
		if (status == 2) {
			// 通过
			logs = placeInfoMapper.selectApplyLogsByID(applyID);
			if (logs == null || logs.getApplyID() == null) {
				return false;
			}
			
			// 更新商户信息
			PlaceInfo info = new PlaceInfo();
			info.setId(logs.getApplyToPlaceID());
			info.setAreaName(logs.getApplyArea());
			info.setName(logs.getApplyName());
			info.setTel(logs.getApplyTel());
			info.setUserName(logs.getApplyUserName());
			info.setAddr(logs.getApplyAddr());
			
			placeInfoMapper.updatePlaceInfo(info);
			
			// 更新当前数据状态
			logs.setApplyStatus(status);
			return placeInfoMapper.updateApplyStatus(logs) >= 1 ? true : false;
		} else if (status == 3) {
			// 驳回
			return placeInfoMapper.updateApplyStatus(logs) >= 1 ? true : false;
		}
		return false;
	}

	@Override
	public ResponseJson handlerPlaceInfo(String openID) {
		// TODO Auto-generated method stub
		ResponseJson json = new ResponseJson();
		
		// 判断用户是否拥有权限管理对应的代理商
		Integer placeID = placeInfoMapper.selectPlaceIdByOpenId(openID);
		if (placeID == null) {
			json.setReturnCode(404);
			json.addResponseKeyValue("代理商信息没有找到!");
			return json;
		}
		
		// 判断代理商是否完善啦信息	完善后就不能修改？
//		int status = placeInfoMapper.selectPlaceInfoStatusByPlaceId(placeID);
//		if (status == 2) {
//			json.setReturnCode(200);
//			json.addResponseKeyValue("SUCCESS");
//			return json;
//		}
		
		// 用户需要完善信息
		ApplyPlaceLogs logs = placeInfoMapper.selectApplyLogsByPlaceID(placeID);
		if (logs == null) {
			json.setReturnCode(500);
			json.addResponseKeyValue("要求重填!");
			return json;
		}
		
		if (logs.getApplyStatus() == 1) {
			// 用户已经填过信息，而且是审核中，不要求填写
			json.setReturnCode(200);
			json.addResponseKeyValue("SUCCESS");
			return json;
		} else if (logs.getApplyStatus() == 3) {
			// 填写不正确，后台驳回，要求重填
			json.setReturnCode(302);
			json.addResponseKeyValue("applyInfo", logs);
			return json;
		}
		
		// 凡是到此，业务出现异常，需要重填
		json.setReturnCode(500);
		json.addResponseKeyValue("要求重填!");
		return json;
	}

	@Override
	public ResponseJson addApplyPlaceLogs(ApplyPlaceLogs applyPlaceLogs) {
		// TODO Auto-generated method stub
		// 判断用户是否拥有权限管理对应的代理商
		ResponseJson json = new ResponseJson();
		Integer placeID = placeInfoMapper.selectPlaceIdByOpenId(applyPlaceLogs.getApplyOpenID());
		if (placeID == null) {
			json.setReturnCode(404);
			json.addResponseKeyValue("代理商信息没有找到!");
			return json;
		}
		
		// 判断是否已经拥有在申请的数据
		ApplyPlaceLogs logsOld = placeInfoMapper.selectApplyLogsByPlaceID(placeID);
		if (logsOld != null && logsOld.getApplyStatus() == 1) {
			// 用户已经填过信息，而且是审核中，不要求填写
			json.setReturnCode(405);
			json.addResponseKeyValue("审核中!请稍后");
			return json;
		}
		
		// 充填
		applyPlaceLogs.setApplyToPlaceID(placeID);
		
		// 保存信息
		Integer i = placeInfoMapper.insertApplyLogs(applyPlaceLogs);
		
		//默认通过审核
		//int goldPlaceId = systemService.selectOneShopRuleById(206).getRuleNum().intValue();
		PlaceInfo placeInfo = placeInfoService.getPlaceInfoByOpenId(applyPlaceLogs.getApplyOpenID());
		if(placeInfo != null){
			//if(goldPlaceId == placeInfo.getPlacePid() && i != null && i > 0){
				//金牌会员直接修改为已通过
			if(i > 0){
				placeInfoService.handlerApplyLogs(applyPlaceLogs.getApplyID(), 2);
			}
			
			//}
		}
		 
		json.setReturnCode(200);
		json.addResponseKeyValue("SUCCESS");
		
		return json;
	}

	@Override
	public ResponseJson selectPlaceInfoDetailByOpenId(String openid) {
		// TODO Auto-generated method stub
		ResponseJson json = new ResponseJson();
		PlaceInfo place = placeInfoMapper.selectPlaceInfoDetailByOpenId(openid); 
		if(place == null || place.getId() == 0){
			json.setReturnCode(400);
			json.addResponseKeyValue("没有商户信息!");
			return json;
		}
		json.addResponseKeyValue("placeInfo", place);
		
		// 判断是否已经拥有在申请的数据
		ApplyPlaceLogs logsOld = placeInfoMapper.selectApplyLogsByPlaceID(place.getId());
		if(logsOld == null ){	//用户没有申请过
			json.setReturnCode(2001);
			json.addResponseKeyValue("FAILED");
			return json;
		}
		if (logsOld.getApplyStatus().intValue() == 1) {	// 用户已经填过信息，而且是审核中
			json.setReturnCode(2002);
			json.addResponseKeyValue("FAILED");
			return json;
		}else if(logsOld.getApplyStatus().intValue() == 3){		// 用户已经填过信息，而且是审核--未通过
			json.setReturnCode(2003);
			json.addResponseKeyValue("FAILED");
			return json;
		}else if(logsOld.getApplyStatus().intValue() == 2){		// 用户已经填过信息，而且是审核--通过
			json.setReturnCode(2005);
			json.addResponseKeyValue("FAILED");
			return json;
		}else {
			json.setReturnCode(200); 
			return json;
		}
	}

	@Override
	public ApplyPlaceLogs selectApplyLogsByOpenID(String openID) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectApplyLogsByOpenID(openID);
	}

	@Override
	public PlaceInfo selectOnePlaceInfoByCode(String code) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectOnePlaceInfoByCode(code);
	}

	@Override
	public Integer updateOpenidById(PlaceInfo info) {
		// TODO Auto-generated method stub
		return placeInfoMapper.updateOpenidById(info);
	}

	@Override
	public Integer addApplyAgent(ApplyUserInfo applyUserInfo) {
		// TODO Auto-generated method stub
		return placeInfoMapper.addApplyAgent(applyUserInfo);
	}

	@Override
	public List<ApplyUserInfo> getApplyAgentList(ApplyUserInfo applyUserInfo) {
		// TODO Auto-generated method stub
		return placeInfoMapper.getApplyAgentList(applyUserInfo);
	}

	@Override
	public Integer getApplyAgentCount(ApplyUserInfo applyUserInfo) {
		int count = placeInfoMapper.getApplyAgentCount(applyUserInfo);
		return count = count % applyUserInfo.getPage().getPageSize() == 0? count/applyUserInfo.getPage().getPageSize():(count/applyUserInfo.getPage().getPageSize())+1;
	}

	@Override
	public Integer selectApplyAgentByTel(String userTel) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectApplyAgentByTel(userTel);
	}

	@Override
	public PlaceInfo selectPlaceByPlacePids(Integer placePids) {
		// TODO Auto-generated method stub
		return placeInfoMapper.selectPlaceByPlacePids(placePids);
	}

	@Override
	public List<ProbationOrderDto> selectVipProbationList(ProbationOrderDto probationOrderDto) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectVipProbationList(probationOrderDto);
	}

	@Override
	public List<ProbationOrderDto> selectStoreProbationList(ProbationOrderDto probationOrderDto) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectStoreProbationList(probationOrderDto);
	}

	@Override
	public List<ProbationOrderDto> selectPartnerProbationList(ProbationOrderDto probationOrderDto) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectPartnerProbationList(probationOrderDto);
	}

	@Override
	public Integer selectVipProbationNum(String openId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectVipProbationNum(openId);
	}

	@Override
	public Integer selectStoreProbationNum(String openId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectStoreProbationNum(openId);
	}

	@Override
	public Integer selectPartnerProbationNum(String openId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectPartnerProbationNum(openId);
	}

	@Override
	public Map<String, Object> selectPlaceCenter(PlaceInfo placeInfo) {
		Map<String, Object> placeMap = new HashMap<String, Object>();
		int rebackMoney = 0;//待返利金额
		int userNum = 0;	//分销商直属下线数量
		int activateNum = 0;	//商户：激活数量，用户：未购买数量
		int flag = 2;	//标识：1：直属下线是商户，2：直属下线是用户
		placeMap.put("status",false);//初始化状态为失败
		if (placeInfo == null) {
			return placeMap;
		}

		//获取分销商购买过商品的下线数量
		int userSalesNum = placeInfoMapper.getPlaceSalesUser(placeInfo);

		PlaceInfo place = new PlaceInfo();
		place.setPlacePid(placeInfo.getId());

		if (placeInfo.getRoleId() == 54) {
			//查询直接下线商户信息
			List<PlaceInfo> placeList = placeInfoMapper.selectAll(place);
			userNum = placeList.size();
			List<PlaceInfo> placeInfoList = placeInfoMapper.listActivatePlace(place);
			activateNum = placeInfoList.size();
			flag = 1;
		} else {
			//获取分销商下线数量
			userNum = placeInfoMapper.getPlaceUser(placeInfo);
			activateNum = userNum - userSalesNum;
			flag = 2;
		}

		//获取商户待返利和已返利金额
		List<RebackInfo> rebackList = placeInfoMapper.listPlaceReback(placeInfo);
		if (rebackList != null && rebackList.size() > 0) {
			for (RebackInfo rebackInfo : rebackList) {
				if (rebackInfo.getStatus() == 1) {
					rebackMoney += rebackInfo.getMoney();
				}
			}
		}
		placeMap.put("flag",flag);
		placeMap.put("activateNum",activateNum);
		placeMap.put("rebackMoney",rebackMoney);
		placeMap.put("userNum",userNum);
		placeMap.put("userSalesNum",userSalesNum);
		placeMap.put("rebackList",rebackList);
		placeMap.put("status",true);
		return placeMap;
	}

	@Override
	public List<RebackInfo> listPlaceReback(PlaceInfo placeInfo) {
		return placeInfoMapper.listPlaceReback(placeInfo);
	}

	@Override
	public Integer getIsQrCode(String id) {
		return placeInfoMapper.getIsQrCode(id);
	}


	/**
	 * 获取公司分销员的下线列表数
	 */
	public int getDistributorNumber(PlaceInfo placeInfo){
		int flag = placeInfoMapper.getDistributorNumber(placeInfo);
		return  flag;
	}

	/**
	 *后台-Drp分销管理主界面 --> 获取公司分销员的下线列表
	 */
	public List<PlaceInfo> getDistributor(PlaceInfo placeInfo){
		placeInfo.getPage().initRecordBegin();
		List<PlaceInfo> lists = placeInfoMapper.getDistributor(placeInfo);
		return  lists;
	}
}
