| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 | 
							- <?php
 
- /*
 
- |--------------------------------------------------------------------------
 
- | 获取优惠券
 
- |--------------------------------------------------------------------------
 
- */
 
- namespace Youzan\Src;
 
- use Dever;
 
- use Main\Lib\Wechat;
 
- use Main\Lib\Core;
 
- class Card
 
- {
 
- 	public $lock = 'cardv2';
 
- 	public function len($id = false)
 
- 	{
 
- 		$id = Dever::input('id', $id);
 
- 		$len = Dever::queue($this->lock . '_' . $id, 'len');
 
- 		return $len;
 
- 	}
 
- 	public function total()
 
- 	{
 
- 		$info = Dever::db('youzan/card')->state();
 
- 		if ($info) {
 
- 			foreach ($info as $k => $v) {
 
- 				$total = Dever::db('youzan/card_list')->total(array('option_state' => 1, 'option_status' => 1, 'option_card_id' => $v['id']));
 
- 				print_r($total);
 
- 				echo '<br />';
 
- 				Dever::lock($this->lock . '_' . $v['id'], $total);
 
- 			}
 
- 		}
 
- 		
 
- 		return true;
 
- 	}
 
- 	# 分配优惠券,此处需要做高并发处理
 
- 	public function get()
 
- 	{
 
- 		$key = Dever::input('key');
 
- 		$user = Dever::input('user');
 
- 		$msg_page = Dever::input('msg_page');
 
- 		$msg_project_id = Dever::input('msg_project_id');
 
- 		$project_id = Dever::input('project_id');
 
- 		$info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
 
- 		if ($info) {
 
- 			$one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
 
- 			if ($one) {
 
- 				return $one;
 
- 			} else {
 
- 				//$state = Dever::lock($this->lock . '_' . $info['id']);
 
- 				$state = true;
 
- 				if (!$state) {
 
- 					Dever::alert('error');
 
- 				} else {
 
- 					$list = Dever::db('youzan/card_list')->getAll(array('option_card_id' => $info['id'], 'option_status' => 1));
 
- 					print_r($list);die;
 
- 					$key = array_rand($list);
 
- 					if (isset($list[$key]) && $list[$key]) {
 
- 						$card = $list[$key];
 
- 					}
 
- 				}
 
- 				if (isset($card) && $card) {
 
- 					$one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
 
- 					if ($one) {
 
- 						return $one;
 
- 					}
 
- 					Dever::db('youzan/card_list')->update(array('where_id' => $card['id'], 'user' => $user, 'status' => 2));
 
- 					# 检查info,有无msg_key,有则发送模板消息
 
- 					if ($msg_project_id && $info['msg_key'] && $info['msg_page'] && $info['msg_content']) {
 
- 						if ($msg_page) {
 
- 							$info['msg_page'] = $msg_page;
 
- 						}
 
- 						$send = Dever::load('applet/msg')->getData($info['msg_content']);
 
- 						Dever::load('applet/msg')->sendOne($info['msg_key'], $msg_project_id, $user, $info['msg_page'], $send);
 
- 					}
 
- 					return $card;
 
- 				}
 
- 			}
 
- 		}
 
- 		Dever::alert('error');
 
- 	}
 
- 	public function create()
 
- 	{
 
- 		$id = Dever::input('id');
 
- 		$info = Dever::db('youzan/card')->one($id);
 
- 		if ($info) {
 
- 			Core::run($info['project_id'], 'card_create', 'card.createStart', 'card.createEnd', 'youzan', $id, false);
 
- 		}
 
- 		Dever::alert('提交成功,请到优惠券列表中查看');
 
- 		return;
 
- 	}
 
- 	public function createStart($id)
 
- 	{
 
- 		$info = Dever::db('youzan/card')->one($id);
 
- 		if ($info) {
 
- 			$info['title'] = $info['name'];
 
- 			if ($info['at_least'] > 0) {
 
- 				$info['is_at_least'] = 1;
 
- 			} else {
 
- 				$info['is_at_least'] = 0;
 
- 			}
 
- 			$info['can_give_friend'] = 0;
 
- 			$info['date_type'] = 1;
 
- 			$info['expire_notice'] = $info['expire_notice'] - 1;
 
- 			$info['fixed_begin_term'] = 0;
 
- 			//$info['fixed_term'] = 0;
 
- 			if ($info['fixed_term']) {
 
- 				$info['date_type'] = 2;
 
- 			}
 
- 			if (!$info['discount']) {
 
- 				$info['discount'] = 0;
 
- 			}
 
- 			if ($info['value_random_to']) {
 
- 				$info['is_random'] = 1;
 
- 			} else {
 
- 				$info['is_random'] = 0;
 
- 			}
 
- 			if ($info['need_user_level'] <= 0) {
 
- 				$info['need_user_level'] = 0;
 
- 			}
 
- 			if ($info['quota'] <= 0) {
 
- 				$info['quota'] = 0;
 
- 			}
 
- 			if ($info['specify_item_ids']) {
 
- 				$info['range_type'] = 'PART';
 
- 			} else {
 
- 				$info['range_type'] = 'ALL';
 
- 			}
 
- 			$info['preferential_type'] = 0;
 
- 			
 
- 			$info['is_sync_weixin'] = $info['is_sync_weixin'] - 1;
 
- 			$info['is_share'] = $info['is_share'] - 1;
 
- 			$info['is_forbid_preference'] = $info['is_forbid_preference'] - 1;
 
- 			$info['at_least'] = $info['at_least'];
 
- 			$info['start_at'] = date('Y-m-d H:i:s', $info['sdate']);
 
- 			$info['end_at'] = date('Y-m-d H:i:s', $info['edate']);
 
- 			if ($info['create_num'] > 1) {
 
- 				$info['run_num'] = $info['create_num'];
 
- 			} else {
 
- 				$info['run_num'] = 1;
 
- 			}
 
- 			return $info;
 
- 		} else {
 
- 			die;
 
- 		}
 
- 	}
 
- 	public function createEnd($project_id, $data, $id = false, $end = false)
 
- 	{
 
- 		if ($end) {
 
- 			# 如果执行结束,则加到库存里
 
- 			$this->total();
 
- 		} else {
 
- 			$info = Dever::db('youzan/card')->one($id);
 
- 			//$data['response']['promocard']['group_id'] = 1;
 
- 			//$data['response']['promocard']['fetch_url'] = rand(1,100);
 
- 			if ($info && isset($data['response']['promocard']['group_id'])) {
 
- 				$update['card_id'] = $info['id'];
 
- 				$update['project_id'] = $info['project_id'];
 
- 				$update['user'] = '';
 
- 				$update['status'] = 1;
 
- 				$update['group_id'] = $data['response']['promocard']['group_id'];
 
- 				$update['code_url'] = $data['response']['promocard']['fetch_url'];
 
- 				$update['cdate'] = time();
 
- 				$update['result'] = json_encode($data);
 
- 				Dever::db('youzan/card_list')->insert($update);
 
- 			}
 
- 		}
 
- 	}
 
- 	/*
 
- 	# 发送优惠券
 
- 	public function send()
 
- 	{
 
- 		$key = Dever::input('key');
 
- 		$project_id = Dever::input('project_id');
 
- 		$touser = Dever::input('touser');
 
- 		return $this->sendOne($key, $project_id, $touser);
 
- 	}
 
- 	# 发送优惠券
 
- 	public function sendOne($key, $project_id, $touser, $state = true)
 
- 	{
 
- 		if ($project_id > 0 && $key && $touser) {
 
- 			$info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
 
- 			if ($info) {
 
- 				Dever::setInput('mobile', $touser);
 
- 				Dever::load('youzan/user.reg');
 
- 				$update = array();
 
- 				$update['project_id'] = $info['project_id'];
 
- 				$update['card_id'] = $info['id'];
 
- 				$update['touser'] = $touser;
 
- 				$where = array();
 
- 				$where['option_card_id'] = $info['id'];
 
- 				//$where['option_project_id'] = $info['project_id'];
 
- 				$where['option_touser'] = $update['touser'];
 
- 				$id = Dever::upinto('youzan/card_log', $where, $update);
 
- 				return Core::run($info['project_id'], 'card', 'card.sendAction', 'card.sendLog', 'youzan', $id, $state);
 
- 			}
 
- 		}
 
- 		return false;
 
- 	}
 
- 	public function sendAction($id)
 
- 	{
 
- 		$info = Dever::db('youzan/card_log')->one($id);
 
- 		if ($info) {
 
- 			$msg = Dever::db('youzan/card')->one($info['card_id']);
 
- 			$info['mobile'] = $info['touser'];
 
- 			$info['coupon_group_id'] = $msg['card_id'];
 
- 			return $info;
 
- 		} else {
 
- 			die;
 
- 		}
 
- 	}
 
- 	public function sendLog($project_id, $data, $id = false)
 
- 	{
 
- 		$info = Dever::db('youzan/card_log')->one($id);
 
- 		if ($info) {
 
- 			$update['where_id'] = $info['id'];
 
- 			$update['result'] = json_encode($data);
 
- 			if (!isset($data['error_response'])) {
 
- 				$update['status'] = 2;
 
- 			} else {
 
- 				$update['status'] = 3;
 
- 			}
 
- 			$update['num'] = $info['num'] + 1;
 
- 			Dever::db('youzan/card_log')->update($update);
 
- 		}
 
- 	}
 
- 	*/
 
- }
 
 
  |