manager.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/17
  6. * Time: 下午11:34
  7. */
  8. namespace bonus;
  9. require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  11. use session_helper;
  12. use member_info;
  13. use algorithm;
  14. use Log;
  15. use Db;
  16. use Exception;
  17. class manager
  18. {
  19. //红包抢到不绑定手机的过期时间
  20. const grab_period_timeout = 10 * 60;
  21. //红包的四种状态
  22. const bonus_state_ungrab = 0; //未抢
  23. const bonus_state_grabed = 1; //已抢到
  24. const bonus_state_binded = 2; //已绑定手机
  25. const bonus_state_topuped= 3; //已经充值
  26. public function __construct() {
  27. }
  28. const type_prefix = 'bonus_type';
  29. private function avatars($user_ids)
  30. {
  31. $result = [];
  32. $mod_member = Model('member');
  33. $items = $mod_member->getMemberList(array('member_id' => array('in',$user_ids)));
  34. foreach ($items as $item) {
  35. $member = new member_info($item);
  36. $uid = $member->member_id();
  37. $avatar = $member->avatar();
  38. $result[$uid] = $avatar;
  39. }
  40. return $result;
  41. }
  42. public function get_typeinfo($type_sn)
  43. {
  44. $bonus_info = rcache($type_sn, self::type_prefix, '*');
  45. if (empty($bonus_info))
  46. {
  47. $type = type::create_by_sn($type_sn);
  48. $user_bonus = Model('user_bonus');
  49. $bonus_ex= $user_bonus->getTypeBinded(array('type_id' => $type->getType_id()));
  50. $type_info = $type->get_param();
  51. if($type->binded_over())
  52. {
  53. $lasted_time = Model('user_bonus')->lasted_binded_time(array('type_id' => $type->getType_id()));
  54. if($lasted_time > 0) {
  55. $period = $lasted_time - $type->get_start_time();
  56. $type_info['binded_period'] = $period == 0 ? 1 : $period;
  57. }
  58. $type_info['binded_over'] = 1;
  59. $type_info['grab_lastime'] = 0;
  60. }
  61. else
  62. {
  63. $type_info['binded_period'] = 0;
  64. $type_info['binded_over'] = 0;
  65. if($type->grabed_over()) {
  66. $lasted_time = Model('user_bonus')->lasted_grabed_time(array('type_id' => $type->getType_id(),'bonus_status' => 1));
  67. $type_info['grab_lastime'] = $lasted_time;
  68. } else {
  69. $type_info['grab_lastime'] = 0;
  70. }
  71. }
  72. $user_ids = [];
  73. $user_ids[] = $type->sender_id();
  74. foreach ($bonus_ex as $bonus) {
  75. $user_ids[] = intval($bonus['user_id']);
  76. }
  77. $user_avatars = $this->avatars($user_ids);
  78. $type_sn = $type->getType_sn();
  79. $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
  80. $ret['url'] = $url;
  81. $bonus_info = array('type_info' => $type_info,'binded_info' => $bonus_ex,'avatars' => $user_avatars);
  82. wcache($type_sn,array('infos' => serialize($bonus_info)),self::type_prefix);
  83. }
  84. else {
  85. $bonus_info = unserialize($bonus_info['infos']);
  86. }
  87. return $bonus_info;
  88. }
  89. public function get_mine_by_typesn($type_sn)
  90. {
  91. $bonus = $this->read_session($type_sn);
  92. if(empty($bonus))
  93. {
  94. $mod_bonus = Model('user_bonus');
  95. $session_id = $_SESSION['MPHPSESSID'];
  96. if(session_helper::isVerfiyMobile()) {
  97. $mobile = session_helper::cur_mobile();
  98. $sql = "select * from lrlz_user_bonus where type_sn = '{$type_sn}' and (session_id = '{$session_id}' or user_mobile = '{$mobile}')";
  99. } else {
  100. $sql = "select * from lrlz_user_bonus where type_sn = '{$type_sn}' and session_id = '{$session_id}'";
  101. }
  102. $bonusex = $mod_bonus->get_by_sql($sql);
  103. if(empty($bonusex)) {
  104. return false;
  105. }
  106. if(count($bonusex) > 1)
  107. {
  108. foreach($bonusex as $val)
  109. {
  110. if($val['user_mobile'] == $_SESSION['member_mobile']) {
  111. $this->write_session($val);
  112. return $val;
  113. }
  114. }
  115. }
  116. else {
  117. $this->write_session($bonusex[0]);
  118. return $bonusex[0];
  119. }
  120. }
  121. return $bonus;
  122. }
  123. public function get_mine_by_bonussn($bonus_sn)
  124. {
  125. $bonus = $this->read_session('',$bonus_sn);
  126. if(empty($bonus))
  127. {
  128. $mod_bonus = Model('user_bonus');
  129. $session_id = $_SESSION['MPHPSESSID'];
  130. if(session_helper::isVerfiyMobile()) {
  131. $mobile = session_helper::cur_mobile();
  132. $sql = "select * from lrlz_user_bonus where bonus_sn = '{$bonus_sn}' and (session_id = '{$session_id}' or user_mobile = '{$mobile}')";
  133. } else {
  134. $sql = "select * from lrlz_user_bonus where bonus_sn = '{$bonus_sn}' and session_id = '{$session_id}'";
  135. }
  136. $bonusex = $mod_bonus->get_by_sql($sql);
  137. if(empty($bonusex)) {
  138. return false;
  139. }
  140. if(count($bonusex) > 1)
  141. {
  142. foreach($bonusex as $val)
  143. {
  144. if($val['user_mobile'] == $_SESSION['member_mobile']) {
  145. $this->write_session($val);
  146. return $val;
  147. }
  148. }
  149. }
  150. else {
  151. $this->write_session($bonusex[0]);
  152. return $bonusex[0];
  153. }
  154. }
  155. return $bonus;
  156. }
  157. public function grab_bonus($type_sn)
  158. {
  159. $bonus = $this->read_session($type_sn);
  160. if($bonus === false)
  161. {
  162. $param = array( 'type_sn' => $type_sn,
  163. 'session_id' => $_SESSION['MPHPSESSID'],
  164. 'time_out' => self::grab_period_timeout,
  165. 'member_mobile' => $_SESSION['member_mobile']);
  166. $bonus_obj = factory::grab_bonus($param);
  167. if($bonus_obj == false) {
  168. return false;
  169. }
  170. else
  171. {
  172. $this->write_session($bonus_obj->get_param());
  173. dcache($bonus_obj->type_sn(),self::type_prefix);
  174. }
  175. $bonus = $bonus_obj->get_param();
  176. }
  177. return $bonus;
  178. }
  179. public function topup($type,$mod_user_bonus,&$bonus)
  180. {
  181. $usable_days = $type->usable_days();
  182. $expried_secs = $usable_days * 24 * 3600;
  183. $datas = array('bonus_status' => 3,
  184. 'user_id' => $_SESSION['member_id'],
  185. 'user_name' => session_helper::nickname(),
  186. 'usable_time' => time() + $expried_secs);
  187. $ret = $mod_user_bonus->where(array('bonus_id' => $bonus['bonus_id']))->update($datas);
  188. $affect_rows = $mod_user_bonus->affected_rows();
  189. if($ret == true && $affect_rows == 1) {
  190. array_merge($bonus,$datas);
  191. $this->write_session($bonus);
  192. return true;
  193. }
  194. else {
  195. return false;
  196. }
  197. }
  198. public function send($type_sn,$ids)
  199. {
  200. $member_ids = [];
  201. foreach ($ids as $id) {
  202. $member_ids[] = intval($id);
  203. }
  204. sort($member_ids);
  205. $mod_bonus = Model('user_bonus');
  206. $items = $mod_bonus->getBonusList(array('user_id' => array('in',$member_ids),'type_sn' => $type_sn));
  207. $ret = [];
  208. foreach ($items as $item)
  209. {
  210. $user_id = intval($item['user_id']);
  211. if(algorithm::binary_search($user_id,$member_ids) != false)
  212. {
  213. $pos = algorithm::lower_bonud($member_ids,$user_id);
  214. algorithm::array_erase($member_ids,$pos);
  215. $ret[] = $item;
  216. }
  217. }
  218. if(!empty($member_ids))
  219. {
  220. $count = count($member_ids);
  221. $items = $mod_bonus->getBonusList(array('bonus_status' => 0,'type_sn' => $type_sn),'*','',0,0,count($member_ids));
  222. if($count != count($items)) {
  223. return false;
  224. }
  225. $index = 0;
  226. $bonus_values = 0.00;
  227. foreach ($items as &$bonus) {
  228. $user_id = $member_ids[$index];
  229. $minfo = new member_info($user_id);
  230. $bonus['user_id'] = $user_id;
  231. $bonus['user_mobile'] = $minfo->mobile();
  232. $bonus['user_name'] = $minfo->nickname();
  233. $bonus['grab_time'] = time();
  234. $bonus['get_time'] = time();
  235. $bonus['bonus_status'] = 2;
  236. ++$index;
  237. $ret[] = $bonus;
  238. $bonus_values += $bonus['bonus_value'];
  239. }
  240. $mod_bonus->replaceAll($items);
  241. Model('bonus_type')->edit(array('type_sn' => $type_sn),
  242. array('binded_num' => array('exp', "binded_num+{$count}"),
  243. 'grabed_num' => array('exp', "grabed_num+{$count}"),
  244. 'remain_amount' => array('exp', "remain_amount-" . "{$bonus_values}")));
  245. dcache($type_sn,self::type_prefix);
  246. return $ret;
  247. } else {
  248. return false;
  249. }
  250. }
  251. public function bind_bonus($bonus_sn,$session_id,$mobile,&$new_bonus_sn)
  252. {
  253. $new_bonus_sn = $bonus_sn;
  254. $bonus_info = self::read_session('',$bonus_sn);
  255. if(!empty($bonus_info))
  256. {
  257. $user_bonus = user_bonus::create_by_param($bonus_info);
  258. if($user_bonus->isBinded()) {
  259. return true;
  260. } else {
  261. $bonus_id = $user_bonus->bonus_id();
  262. $type_id = $user_bonus->type_id();
  263. }
  264. }
  265. if(!isset($type_id))
  266. {
  267. $user_bonus = user_bonus::create_by_sn($bonus_sn);
  268. if($user_bonus->isBinded())
  269. {
  270. if($user_bonus->user_mobile() == $mobile) {
  271. return true;
  272. } else {
  273. return false;
  274. }
  275. }
  276. $bonus_id = $user_bonus->bonus_id();
  277. $type_id = $user_bonus->type_id();
  278. }
  279. $binded_bonus = array();
  280. $ret = Model('user_bonus')->bind($type_id,$bonus_id,$session_id,$mobile,$binded_bonus);
  281. Log::record("bind_bonus bind ret={$ret}",Log::DEBUG);
  282. if($ret == true)
  283. {
  284. try
  285. {
  286. //bind 一个新红包
  287. $bonus_val = $user_bonus->bonus_value();
  288. Db::beginTransaction();
  289. Model('bonus_type')->edit(array('type_id' => $type_id),
  290. array('binded_num' => array('exp', 'binded_num+1'),
  291. 'remain_amount' => array('exp', "remain_amount-" . "{$bonus_val}")));
  292. Db::commit();
  293. $this->clear_session($bonus_sn);
  294. dcache($user_bonus->type_sn(),self::type_prefix);
  295. } catch (Exception $ex) {
  296. Db::rollback();
  297. return false;
  298. }
  299. }
  300. else
  301. {
  302. if(!empty($binded_bonus)) { //返回已经绑定的红包
  303. $this->clear_session($bonus_sn);
  304. $this->write_session($binded_bonus);
  305. $new_bonus_sn = $binded_bonus['bonus_sn'];
  306. } else {
  307. return false;
  308. }
  309. }
  310. return true;
  311. }
  312. public function shake($bonus_sn,$strength,$direction)
  313. {
  314. $bonus_info = self::read_session('',$bonus_sn);
  315. if(!empty($bonus_info))
  316. {
  317. $user_bonus = user_bonus::create_by_param($bonus_info);
  318. $bonus_id = $user_bonus->bonus_id();
  319. $type_id = $user_bonus->type_id();
  320. $bonus_val = $user_bonus->bonus_value();
  321. $bonus_status = $user_bonus->bonus_status();
  322. }
  323. if(!isset($type_id)) {
  324. $user_bonus = user_bonus::create_by_sn($bonus_sn);
  325. $bonus_id = $user_bonus->bonus_id();
  326. $type_id = $user_bonus->type_id();
  327. $bonus_val = $user_bonus->bonus_value();
  328. $bonus_status = $user_bonus->bonus_status();
  329. }
  330. if($user_bonus->can_shake() == false) {
  331. return false;
  332. }
  333. $shaker = new \bonus\shaker();
  334. $ret = $shaker->reassign($type_id,$bonus_id,$bonus_status,$bonus_val,$strength,$direction);
  335. if($ret == true)
  336. {
  337. $bonus = $this->read_session('',$bonus_sn);
  338. if(!empty($bonus)) {
  339. $this->clear_session($bonus_sn);
  340. $bonus['bonus_value'] = $bonus_val;
  341. $this->write_session($bonus);
  342. }
  343. dcache($user_bonus->type_sn(),self::type_prefix);
  344. return true;
  345. } else {
  346. return false;
  347. }
  348. }
  349. public function comment($bonus_sn,$content)
  350. {
  351. if(empty($bonus_sn)) return false;
  352. $condition = array('user_mobile' => $_SESSION['member_mobile'],
  353. 'bonus_sn' => $bonus_sn,
  354. 'bonus_status' => array('in',array(2,3)));
  355. $ret = Model('user_bonus')->edit($condition,array('user_comment' => $content));
  356. if($ret == true) {
  357. $user_bonus = user_bonus::create_by_sn($bonus_sn);
  358. dcache($user_bonus->type_sn(),self::type_prefix);
  359. return true;
  360. } else {
  361. return false;
  362. }
  363. }
  364. //个人抢到的红包,放到个人的Session 里面.
  365. private function read_session($type_sn,$b_sn = '')
  366. {
  367. if(!isset($_SESSION['bonus'])) return false;
  368. $bonusex = $_SESSION['bonus'];
  369. foreach($bonusex as $bonus_sn => $val)
  370. {
  371. if($bonus_sn == $b_sn) return $val;
  372. if($val['type_sn'] == $type_sn)
  373. {
  374. $status = $val['bonus_status'];
  375. if ($status == self::bonus_state_binded) {
  376. return $val;
  377. }
  378. else if ($status == self::bonus_state_grabed)
  379. {
  380. $grab_time = $val['grab_time'];
  381. if ($grab_time > time() - self::grab_period_timeout) {
  382. return $val;
  383. } else {
  384. unset ($_SESSION['bonus'][$bonus_sn]);
  385. return false; // 需要重新抢
  386. }
  387. }
  388. else {
  389. return $val;
  390. }
  391. }
  392. }
  393. return false;
  394. }
  395. private function write_session($bonus)
  396. {
  397. if(!isset($_SESSION['bonus'])) {
  398. $_SESSION['bonus'] = array();
  399. }
  400. $bonus_sn = $bonus['bonus_sn'];
  401. $_SESSION['bonus'][$bonus_sn] = $bonus;
  402. }
  403. private function clear_session($bonus_sn = '')
  404. {
  405. if(!empty($bonus_sn))
  406. {
  407. if(isset($_SESSION['bonus']) && isset($_SESSION['bonus'][$bonus_sn])) {
  408. unset($_SESSION['bonus'][$bonus_sn]);
  409. }
  410. }
  411. else
  412. {
  413. if(!isset($_SESSION['bonus'])) {
  414. unset($_SESSION['bonus']);
  415. }
  416. }
  417. }
  418. }