<?php
/**
  * @method 跑街令 推广
  * @author soul
  * @copyright 2019/5/8
  */
class PjlTg extends PjlBase
{
	public $styleArr = ['pjlshop'=>'跑街令配件商入驻', 'pjlvip'=>'跑街令PC版会员', 'tixian'=>'提现'];
	
	/**
	  * @method 添加金额变化记录
	  * @param  $uid 推广人ID  uid、mobile二选一
	  * @param  $mobile 推广人手机号码
	  * @param  $dataArr =[
				'th_style'=>跑街令商家：pjlshop 跑街令PC版会员：pjlvip
				'th_txt'=>简要说明
				'th_data_id'=>和tml_style 配合使用 例如店铺ID、订单ID
				'th_u_id'=>被推荐人宜配ID
			]
	  * @author soul
	  * @copyright 2019/5/8
	  * @return {"status":true,"data": ,"code":0}
	  */
	public function addTg($uid, $dataArr, $mobile = '')
	{
		$uid = (int) $uid;
		if(empty($dataArr['th_style']) || empty($this->styleArr[$dataArr['th_style']]))
			return LibFc::ReturnData(false, '提供的th_style参数不正确！');

		if(empty($dataArr['th_txt'])) 
			return LibFc::ReturnData(false, '提供的简要说明th_txt参数不正确！');
		
		if(empty($dataArr['th_data_id']) || $dataArr['th_data_id'] < 1) 
			return LibFc::ReturnData(false, '提供的th_data_id参数不正确！');

		if(empty($dataArr['th_u_id']) || $dataArr['th_u_id'] < 1) 
			return LibFc::ReturnData(false, '提供的th_u_id参数不正确！');
		
		$dataArr['th_data_id'] = (int) $dataArr['th_data_id'];
		$dataArr['th_u_id'] = (int) $dataArr['th_u_id'];

		$this->PjlDb->Begin();
		//通过手机号码查询账号，如果不存在，直接创建
		if($uid < 1 && preg_match('/^1\d{10}$/', $mobile))
		{
			$res = $this->get('tg_user', ['*'], sprintf(" AND tg_mobile='%s'", $mobile), true);
			if(empty($res['data']))
			{
				$addUser = [
					'tg_mobile'=>$mobile,
					'tg_atime'=>time()
				];
				$res = $this->add('tg_user', $addUser);
				$uid = $res['data'];
			}
			else
			{
				$userData = $res['data'];
			}
		}
		
		if(empty($userData))
		{
			$res = $this->get('tg_user', ['*'], sprintf(" AND tg_uid=%d", $uid), true);
			if(empty($res['data'])) 
				return LibFc::ReturnData(false, '推广人ID不存在！');
			$userData = $res['data'];
		}
		$uid = $userData['tg_uid'];

		$where = sprintf(" and th_u_id=%d and th_style='%s' and th_status = 1 and th_tg_uid != %d", $dataArr['th_u_id'], $dataArr['th_style'], $uid);
		$res = $this->get('tg_history', ['th_id', 'th_tg_uid', 'th_status'], $where, true);
		if(!empty($res['data']))
			return LibFc::ReturnData(false, '此用户已被别人推荐，不可以重复推荐！');

		//关闭此用户其它推广人
		$where = sprintf(" and th_u_id=%d and th_style='%s' and th_tg_uid !=%d", $dataArr['th_u_id'], $dataArr['th_style'], $uid);
		$this->set('tg_history', ['th_status'=>-1], $where);

		$where = sprintf(" and th_u_id=%d and th_style='%s' and th_tg_uid=%d order by th_status desc", $dataArr['th_u_id'], $dataArr['th_style'], $uid);
		$res = $this->get('tg_history', ['th_id', 'th_tg_uid', 'th_status'], $where, true);
		if(!empty($res['data']))
		{
			$this->PjlDb->End();
			return LibFc::ReturnData(true, $res['data']['th_id']);
		}
	
		$addArr = [
			'th_status'=>0,	
			'th_tg_uid'=>$userData['tg_uid'],	
			'th_style'=>$dataArr['th_style'],	
			'th_txt'=>$dataArr['th_txt'],	
			'th_data_id'=>$dataArr['th_data_id'],
			'th_u_id'=>$dataArr['th_u_id'],
			'th_money'=>$userData['tg_price_'.$dataArr['th_style']],
			'th_atime'=>empty($dataArr['th_atime']) ? time(): $dataArr['th_atime']
		];
		$res = $this->add('tg_history', $addArr);
		$this->PjlDb->End();
		return LibFc::ReturnData(true, $res['data']);
	}


	/**
	  * @method 推广审核
	  * @param  $th_id 推广ID
	  * @author $status -1审核不通过 1审核通过
	  * @copyright 2018/07/23
	  * @return ["status"=> bool(true), "data"=> "", "code"=>""]
	  */
	public function checkTgStatus($th_id, $status)
	{
		if(!in_array($status, [-1,1])) return LibFc::ReturnData(false, '推广状态不正确！');
		$res = $this->get('tg_history join tg_user on tg_uid=th_tg_uid', ['*'], sprintf(" AND th_id=%d", $th_id), true);
		if(empty($res['data'])) 
			return LibFc::ReturnData(false, '提供的推广th_id参数不正确！');
		$tgArr = $res['data'];
		
		if($tgArr['th_status'] == $status)
			return LibFc::ReturnData(true, true);

		if($tgArr['th_status'] == 1 && $status == -1)
			return LibFc::ReturnData(false, '此推广单已完成，不允许关闭！');
		if($status == -1)
		{
			$this->set('tg_history', ['th_status'=>-1, 'th_close_time'=>time()], sprintf(' and th_id = %d', $tgArr['th_id']));
		}
		else if($status == 1)
		{
			//此配件商已被别人推荐
			$where = sprintf(" and th_u_id=%d and th_style='%s' and th_status = 1", $tgArr['th_u_id'], $tgArr['th_style']);
			$res = $this->get('tg_history', ['th_id'], $where, true);
			if(!empty($res['data']))
			{
				$where = sprintf(" and th_u_id=%d and th_style='%s' and th_status = 0", $tgArr['th_u_id'], $tgArr['th_style']);
				$this->set('tg_history', ['th_status'=>-1, 'th_close_time'=>time(), 'th_remark'=>'已被他人推广！'], $where);
				return LibFc::ReturnData(true, true);
			}
			else
			{
				$this->PjlDb->Begin();
				$where = sprintf(" and th_u_id=%d and th_style='%s' and th_status = 0 and th_id != %d", $tgArr['th_u_id'], $tgArr['th_style'], $tgArr['th_id']);
				$this->set('tg_history', ['th_status'=>-1, 'th_close_time'=>time(), 'th_remark'=>'已被他人推广！'], $where);
				
				$this->set('tg_history', ['th_status'=>1, 'th_ok_time'=>time(), 'th_close_time'=>0], sprintf(" and th_id=%d", $tgArr['th_id']));
				
				//此用户是否成为有效推广用户
				$this->set('tg_user', ['tg_istg'=>1], " and tg_istg=0 and tg_uid IN (SELECT th_tg_uid FROM `tg_history` WHERE th_status =1 GROUP BY th_tg_uid HAVING COUNT(DISTINCT th_u_id) > 1)");

				$tgmArr = [
					'tml_style'=>'pjlshop',	
					'tml_txt'=>$tgArr['th_txt'],	
					'tml_data_id'=>$tgArr['th_id'],
					'tml_change'=>$tgArr['th_money']
				];
				$this->changeMoney($tgArr['th_tg_uid'], $tgmArr);
				$this->PjlDb->End();
			}
		}
		return LibFc::ReturnData(true, true);	
	}



	/**
	  * @method 添加金额变化记录
	  * @param  $uid 推广人ID
	  * @param  $dataArr =[
				'tml_style'=>跑街令商家：pjlshop 跑街令PC版会员：pjlvip 提现：tixian...
				'tml_change'=>发生余额
				'tml_txt'=>简要说明
				'tml_data_id'=>推广ID、提现ID...
			]
	  * @author soul
	  * @copyright 2019/5/8
	  * @return {"status":true,"data": ,"code":0}
	  */
	public function changeMoney($uid, $dataArr)
	{
		$uid = (int) $uid;
		if(empty($dataArr['tml_style']) || empty($this->styleArr[$dataArr['tml_style']]))
			return LibFc::ReturnData(false, '提供的tml_style参数不正确！');

		if(empty($dataArr['tml_txt'])) 
			return LibFc::ReturnData(false, '提供的简要说明tml_txt参数不正确！');

		$res = $this->get('tg_user', ['*'], sprintf(" AND tg_uid=%d", $uid), true);
		if(empty($res['data'])) 
			return LibFc::ReturnData(false, '推广人ID不存在！');
		$userData = $res['data'];

		$this->PjlDb->Begin();

		$dataArr['tml_change'] = sprintf("%.2f", $dataArr['tml_change']);
		if(empty($dataArr['tml_change'])) 
			return LibFc::ReturnData(false, '提供的变化金额tml_change参数不正确！');
		
		$newMoney = $userData['tg_money'] + $dataArr['tml_change'];

		if($newMoney < 0) 
			return LibFc::ReturnData(false, '账户余额不足，操作失败！');
		
		$dataArr['tml_data_id'] = (int) $dataArr['tml_data_id'];

		$addArr = [
			'tml_tg_uid'=>$userData['tg_uid'],	
			'tml_style'=>$dataArr['tml_style'],	
			'tml_txt'=>$dataArr['tml_txt'],	
			'tml_change'=>$dataArr['tml_change'],	
			'tml_data_id'=>$dataArr['tml_data_id'],
			'tml_old_money'=>$userData['tg_money'],
			'tml_new_money'=>$newMoney,
			'tml_atime'=>time()
		];
		$res = $this->add('tg_money_list', $addArr);
		$this->set('tg_user', ['tg_money'=>$newMoney], sprintf(" AND tg_uid=%d", $userData['tg_uid']));
		$this->PjlDb->End();
		return LibFc::ReturnData(true, $res['data']);
	}



	/**
	  * @method 提现
	  * @param  $tg_uid [必填] 推广人ID
	  * @param  $money [选填] 为空则全额提现
	  * @author soul
	  * @copyright 2019/5/8
	  * @return {"status":true,"data": ,"code":0}
	  */
	public function tixian($uid, $money)
	{
		$res = $this->get('tg_user', ['*'], sprintf(" AND tg_uid=%d", $uid), true);
		if(empty($res['data'])) 
			return LibFc::ReturnData(false, '推广人ID不存在！');
		$userData = $res['data'];

		if(empty($userData['tg_istg']))
			return LibFc::ReturnData(false, '您不是有效的推广用户，不能提现！');

		if(empty($userData['tg_weixin']))
			return LibFc::ReturnData(false, '请先完善收款人的微信账号！');

		if($userData['tg_money'] < 10)
			return LibFc::ReturnData(false, '提现金额不能低于10元！');

		$money = !empty($money) ? sprintf("%.2f", $money) : $userData['tg_money'];
		if($money > 0 && ($userData['tg_money'] - $money) >= 0)
		{}
		else
		{
			return LibFc::ReturnData(false, '账户余额不足！');
		}

		$this->PjlDb->Begin();
		$addTx = [
			'tmt_tg_uid'=>$userData['tg_uid'],
			'tmt_status'=>0,
			'tmt_moeny'=>$money,
			'tmt_tg_weixin'=>$userData['tg_weixin'],
			'tmt_atime'=>time()
		];
		$res = $this->add('tg_money_tixian', $addTx);
		$tmt_id = $res['data'];
		$addArr = [
			'tml_style'=>'tixian',	
			'tml_txt'=>'余额提现',	
			'tml_change'=>-$money,	
			'tml_data_id'=>$tmt_id
		];
		$this->changeMoney($uid, $addArr);
		$this->PjlDb->End();
		return LibFc::ReturnData(true, $tmt_id);
	}



	/**
	  * @method 审核提现/关闭提现
	  * @param  $tmt_id [必填] 提现ID
	 * @param  $dataArr =[
				'tmt_status'=> -1已取消 0待审核 1已转账
				'tmt_txt'=>转账凭证
				'tmt_remark'=>管理备注
				'tmt_check'=>审核人
			]
	  * @author soul
	  * @copyright 2019/5/8
	  * @return {"status":true,"data": ,"code":0}
	  */
	public function checkTiXian($id, $dataArr)
	{
		$res = $this->get('tg_money_tixian', ['*'], sprintf(" AND tmt_id=%d", $id), true);
		if(empty($res['data'])) 
			return LibFc::ReturnData(false, '提现ID不存在！');
		$txArr = $res['data'];
		$dataArr['tmt_status'] = (int) $dataArr['tmt_status'];
		$this->PjlDb->Begin();
		
		$dataArr['tmt_etime'] =time();
		if($dataArr['tmt_status'] != $txArr['tmt_status'])
		{
			if($dataArr['tmt_status'] == -1)
			{
				$addArr = [
					'tml_style'=>'tixian',	
					'tml_txt'=>'取消提现',	
					'tml_change'=>$txArr['tmt_moeny'],	
					'tml_data_id'=>$txArr['tmt_id']
				];
				
				$this->changeMoney($txArr['tmt_tg_uid'], $addArr);
				$dataArr['tmt_close_time'] = time();
			}
			else if($dataArr['tmt_status'] == 1)
			{
				$dataArr['tmt_pay_time'] = empty($dataArr['tmt_pay_time']) ? time(): $dataArr['tmt_pay_time'];
			}
		}
		unset($dataArr['tmt_moeny']);
		unset($dataArr['tmt_tg_uid']);
		unset($dataArr['tmt_tg_weixin']);
		$this->set('tg_money_tixian', $dataArr, sprintf(" AND tmt_id=%d", $id));
		$this->PjlDb->End();
		return LibFc::ReturnData(true, $id);
	}
}