package com.iamberry.wechat.service.drp;

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

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.drp.PlaceLogs;
import com.iamberry.wechat.core.entity.drp.PlaceWithDrawalsLogs;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.member.Withdraw;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.drp.PlaceWithDrawalsLogsService;
import com.iamberry.wechat.face.member.WithdrawService;
import com.iamberry.wechat.service.mapper.AdminOrderMapper;
import com.iamberry.wechat.service.mapper.MemberMapper;
import com.iamberry.wechat.service.mapper.PlaceInfoMapper;
import com.iamberry.wechat.service.mapper.PlaceLogsMapper;
import com.iamberry.wechat.service.mapper.PlaceWithDrawalsLogsMapper;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StaticInfo;


/**
 * @description 提现申请处理
 * @author ouyang
 * @date 2016-4-20
 */
@Service("placeWithDrawalsLogsServiceImpl")
@SuppressWarnings(value="unchecked")
public class PlaceWithDrawalsLogsServiceImpl implements
		PlaceWithDrawalsLogsService {

	@Autowired
	private PlaceWithDrawalsLogsMapper placeWithDrawalsLogsMapper;
	
	@Autowired 
	private PlaceInfoMapper  placeInfoMapper;
	
	@Autowired
	private PlaceLogsMapper placeLogsMapper;
	
	@Autowired
	private AdminOrderMapper adminOrderMapper;
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private SystemService  systemService;
	
	@Autowired
	private WithdrawService  withdrawService;
	
	
	
	//提现规则页面返回信息
	public String getTheRule() {
		return null;
	}

	//申请页面
	public Map<String,Object> apply(String openid) {
		PlaceInfo placeInfo= placeInfoMapper.getOnePlaceInfo2(openid);
		//int role=placeInfo.getPlaceRole();
		Map<String,Object> map=new HashMap<String,Object>();
		/*//判断是否是导购
		if(role==5 || role==6){
			Member member=memberMapper.getMemberByUserOpenId(openid);
			map.put("amount", member.getUserIncome());
		}else{
			map.put("amount", placeInfo.getAmount());
		}*/
		map.put("amount", placeInfo.getAmount());
		map.put("withdrawalsCount", systemService.selectOneShopRuleById(100).getRuleNum().intValue());
		map.put("lessMoney",systemService.selectOneShopRuleById(101).getRuleNum().intValue());
		/** 拼装数据 */
		//获取当前用户上一次的提款记录
		PlaceWithDrawalsLogs withdrawalslogs = new PlaceWithDrawalsLogs();
		PageBean pb = new PageBean();
		pb.setPageNumber(1);
		pb.setPageSize(1);
		withdrawalslogs.setPage(pb);
		withdrawalslogs.setOpenid(openid);
		List<PlaceWithDrawalsLogs> list = placeWithDrawalsLogsMapper.selectAll(withdrawalslogs); //这里有且最多一条数据，或者没有数据
		if(list!=null && list.size()>0 ){
			map.put("Withdraw", list.get(0));		//最大可取金额
		}
		//map.put("lessMoney", ResultInfo.getMoneyLimitDown);
		return map;
	}

	//提现申请提交
	public ResultMsg submitApply(PlaceWithDrawalsLogs placeWithDrawalsLogs) {
		ResultMsg remsg=new ResultMsg();
		HashMap countMap=new HashMap();
		placeWithDrawalsLogs.setWithDrawalsNum(placeWithDrawalsLogs.getWithDrawalsNum() * 100);
		countMap.put("dateMonth", DateFormatUtils.format(new Date(), "yyyyMM"));
		countMap.put("id", placeWithDrawalsLogs.getOpenid());
		int count=placeWithDrawalsLogsMapper.getApplyCount(countMap);
		//提现的次数
		ShopSystemRule sr=systemService.selectOneShopRuleById(100);
		
		if(count>sr.getRuleNum().intValue()){
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.getMoneyCountError);
			remsg.setStatus(false);
			return remsg;
		}
		
		placeWithDrawalsLogs.setCreateDate(new Date());
		placeWithDrawalsLogs.setStatus(1);
		//根据openid设置placeId
		PlaceInfo placeInfo=placeInfoMapper.getOnePlaceInfo2(placeWithDrawalsLogs.getOpenid());
		placeWithDrawalsLogs.setPlaceId(placeInfo.getId());
		
		int money=placeWithDrawalsLogs.getWithDrawalsNum();
		//int role=placeInfo.getPlaceRole();
		Member member=null;
		//判断是否是导购
		/*if(role==5 || role==6){
			 member=memberMapper.getMemberByUserOpenId(placeWithDrawalsLogs.getOpenid());
			 placeInfo.setAmount(member.getUserIncome());
		}*/
		//取现金额大于账户余额
       if(money>placeInfo.getAmount()){
    	   remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.getMoneyLimitError);
			return remsg;
		}
       //提现的下限金额
 		ShopSystemRule sr2=systemService.selectOneShopRuleById(101);
 		int limitDown=sr2.getRuleNum().intValue()  / 100;
		//每一次的取现金额 大小限制
		//if(money<ResultInfo.getMoneyLimitDown || money>ResultInfo.getMoneyLimitTop){
 		if(money<limitDown || money>ResultInfo.getMoneyLimitTop){
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.getMoneyconditionGT + limitDown
					+ ResultInfo.getMoneyconditionLT +ResultInfo.getMoneyLimitTop);
			return remsg;
		}
		
		int i=placeWithDrawalsLogsMapper.insert(placeWithDrawalsLogs);
		int backMoney=placeWithDrawalsLogs.getWithDrawalsNum();
		
		/*if(role==5 || role==6){
			member.setUserIncome(-backMoney);
		   // i=memberMapper.updateUserIncomeByOpenId(member);
			i=adminOrderMapper.updateUserByUserId(member);
		}else{
			placeInfo.setAmount(-backMoney);
		  //  i=placeInfoMapper.update(placeInfo);
			i=adminOrderMapper.updatePlaceInfo(placeInfo);
		}*/
		
		placeInfo.setAmount(-backMoney);
		i=adminOrderMapper.updatePlaceInfo(placeInfo);
		
		if(i>0){
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setMessage(ResultInfo.SUCCESSINFO);
			remsg.setStatus(true);
		}else{
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.ERRORINFO);
			remsg.setStatus(false);
		}
		return remsg;
	}

	//处理提现申请
	public ResultMsg dealApply(PlaceWithDrawalsLogs placeWithDrawalsLogs) {
		ResultMsg remsg=new ResultMsg();
		int status=placeWithDrawalsLogs.getStatus();
		placeWithDrawalsLogs=placeWithDrawalsLogsMapper.getOneWithDrawalsLogs(placeWithDrawalsLogs.getId());
		placeWithDrawalsLogs.setStatus(status);
		int i=10;
		if(placeWithDrawalsLogs.getStatus()==2){
		
			//更新提现的状态
			i=placeWithDrawalsLogsMapper.update(placeWithDrawalsLogs);
			//插入提现记录到现金记录表
			placeWithDrawalsLogs=placeWithDrawalsLogsMapper.getOneWithDrawalsLogs(placeWithDrawalsLogs.getId());
			PlaceLogs placeLogs=new PlaceLogs();
			placeLogs.setPlaceId(placeWithDrawalsLogs.getPlaceId());
			placeLogs.setType(2);
			placeLogs.setResType(2);
			placeLogs.setNums(placeWithDrawalsLogs.getWithDrawalsNum());
			placeLogs.setIntroduction("现金提现");
			placeLogs.setCreateDate(new Date());
			i=placeLogsMapper.insert(placeLogs);
					
		}else{
			PlaceInfo placeInfo=placeInfoMapper.getOnePlaceInfo(placeWithDrawalsLogs.getPlaceId());
			int num=placeWithDrawalsLogs.getWithDrawalsNum();
			
			/*if(placeInfo.getPlaceRole()==5 || placeInfo.getPlaceRole()==6){
				Member member=memberMapper.getMemberByUserOpenId(placeInfo.getOpenid());
				member.setUserIncome(num);
				//i=memberMapper.updateUserIncomeByOpenId(member);
				i=adminOrderMapper.updateUserByUserId(member);
			}else{
				placeInfo.setAmount(num);
				//更新总金额数据
				// i=placeInfoMapper.update(placeInfo);
				i=adminOrderMapper.updatePlaceInfo(placeInfo);
			}*/
			
			placeInfo.setAmount(num);
			//更新总金额数据
			// i=placeInfoMapper.update(placeInfo);
			i=adminOrderMapper.updatePlaceInfo(placeInfo);
			
			i=placeWithDrawalsLogsMapper.update(placeWithDrawalsLogs);
		}

		if(i>0){
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setMessage(ResultInfo.SUCCESSINFO);
			remsg.setStatus(true);
		}else{
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(ResultInfo.ERRORINFO);
			remsg.setStatus(false);
		}
		return remsg;
	}
	
    //历史提现记录
	public Map HistoryRecords(String openid) {
		Map hm=new HashMap();
		PlaceInfo placeInfo= placeInfoMapper.getOnePlaceInfo2(openid);
		/*int role=placeInfo.getPlaceRole();
		
		if(role==5 || role==6){
			Member member=memberMapper.getMemberByUserOpenId(openid);
			hm.put("amount", member.getUserIncome());
		}else{
			hm.put("amount", placeInfo.getAmount());
		}*/
		hm.put("amount", placeInfo.getAmount());
		PlaceWithDrawalsLogs pwd=new PlaceWithDrawalsLogs();
		pwd.setOpenid(openid);
		//pwd.setStatus(2);
		PageBean pb=new PageBean();
		pwd.setPage(pb);
		
		List<PlaceWithDrawalsLogs>  placeWithDrawalsLogsList=
		placeWithDrawalsLogsMapper.selectAll(pwd);
		hm.put("list",placeWithDrawalsLogsList);
		
		//pb.setCount(placeWithDrawalsLogsMapper.getAllCount(pwd));
		hm.put("page",pb);
		
		return hm;
	}
	
	//后台提现记录的查询
	public Map HistoryRecordsManager(PageBean pb) {
		Map hm=new HashMap();
		PlaceWithDrawalsLogs  pwd=
				(PlaceWithDrawalsLogs)pb.getCondition();
		if(pwd.getEndDate()!=null && !"".equals(pwd.getEndDate()) ){
			String enddate=pwd.getEndDate();
			pwd.setEndDate(enddate+StaticInfo.endDateC);
		}
		
		//作为查询条件的
		PageBean page=new PageBean();
		page.setPageNumber(pb.getPageNumber());
		page.setPageSize(pb.getPageSize());
		
		if(pb.getPageCount()>0){
			page.setCount(pb.getPageCount());
		}else{
			page.setCount(placeWithDrawalsLogsMapper.getAllCountMore(pwd));
		}
		
		pwd.setPage(page);
		page.initRecordBegin();
		
		List<PlaceWithDrawalsLogs>  placeWithDrawalsLogsList=
		placeWithDrawalsLogsMapper.selectAllMore(pwd);
		hm.put("data",placeWithDrawalsLogsList);
		
		hm.put("page",page);
		
		return hm;
	}
	
	/**
	 * 获取下一个提现分页记录
	 * @param pwd 提现记录
	 *   pwd 必须带有placeId,page的分页 页码pageNumber
	 * @return
	 */
	public Map HistoryRecordsNext(PlaceWithDrawalsLogs pwd) {
		HashMap hm=new HashMap();
		
		PageBean page=pwd.getPage();
		page.initRecordBegin();
		
		page.setCount(placeWithDrawalsLogsMapper.getAllCount(pwd));
		PlaceInfo placeInfo= placeInfoMapper.getOnePlaceInfo2(pwd.getOpenid());
		/*int role=placeInfo.getPlaceRole();
		if(role==5 || role==6){
			Member member=memberMapper.getMemberByUserOpenId(pwd.getOpenid());
			hm.put("amount", member.getUserIncome());
		}else{
			hm.put("amount", placeInfo.getAmount());
		}*/
		hm.put("amount",placeInfo.getAmount());
		
		//pwd.setStatus(2);
		List<PlaceWithDrawalsLogs>  placeWithDrawalsLogsList=
				placeWithDrawalsLogsMapper.selectAll(pwd);
		hm.put("list", placeWithDrawalsLogsList);
		hm.put("pageCount",page.getPageCount());
		
		return hm;
	}

	
	//测试插入对象
	public void insertTest(PlaceWithDrawalsLogs pwd){
		placeWithDrawalsLogsMapper.insert(pwd);
	}

	@Override
	public PlaceWithDrawalsLogs getOne(int id) {
		return placeWithDrawalsLogsMapper.getOneWithDrawalsLogs(id);
	}

	@Override
	public int getTotalMoneyByPlaceId(int placeId) {
		// TODO Auto-generated method stub
		return placeWithDrawalsLogsMapper.getTotalMoneyByPlaceId(placeId);
	}

	@Override
	public List<PlaceWithDrawalsLogs> selectAllMore(
			PlaceWithDrawalsLogs placeWithDrawalsLogs) {
		// TODO Auto-generated method stub
		return placeWithDrawalsLogsMapper.selectAllMore(placeWithDrawalsLogs);
	}


}
