operator.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/6
  6. * Time: 下午3:00
  7. */
  8. namespace fcode;
  9. require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
  10. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  11. use session_helper;
  12. use Log;
  13. use util;
  14. class operator
  15. {
  16. private $mCommonID;
  17. private $mod_fcode;
  18. private $mBatchCode;
  19. private $mMobile;
  20. private $mSessionID;
  21. private $mFUserMode;
  22. const time_out = 300;
  23. public function __construct($common_id, $batch_code, $mobile, $session_id)
  24. {
  25. $this->mCommonID = $common_id;
  26. $this->mBatchCode = $batch_code;
  27. $this->mod_fcode = Model('goods_fcode');
  28. $this->mMobile = $mobile;
  29. $this->mSessionID = $session_id;
  30. $this->mFUserMode = !empty($session_id);
  31. }
  32. public function grabed()
  33. {
  34. $cond = [];
  35. $cond['goods_commonid'] = $this->mCommonID;
  36. $cond['batch_code'] = $this->mBatchCode;
  37. if(empty($this->mMobile) && empty($this->mSessionID)) {
  38. return false;
  39. }
  40. elseif (empty($this->mMobile)) {
  41. $cond['session_id'] = $this->mSessionID;
  42. }
  43. elseif(empty($this->mSessionID)) {
  44. $cond['mobile'] = $this->mMobile;
  45. }
  46. else {
  47. $cond['mobile|session_id'] = ['_multi'=>true,$this->mMobile,$this->mSessionID];
  48. }
  49. $fcode = $this->mod_fcode->getGoodsFCode($cond,true);
  50. if(empty($fcode)) {
  51. return false;
  52. }
  53. else {
  54. return $fcode;
  55. }
  56. }
  57. public function grab()
  58. {
  59. $fcode = $this->grabed();
  60. if($fcode != false) return $fcode;
  61. while(true)
  62. {
  63. $cond = array('goods_commonid' => $this->mCommonID,
  64. 'batch_code' => $this->mBatchCode,
  65. 'fc_state' => '0',
  66. 'grab_state' => array('in', array(0,1)),
  67. 'grab_time' => array('lt',time() - self::time_out));
  68. $fcodes = $this->mod_fcode->where($cond)->field('*')->order('fc_id asc')->limit(1)->select(array('master' => true));
  69. if(empty($fcodes) || empty($fcodes[0])) return false;
  70. $fcode = $this->try_grab($fcodes[0]);
  71. if($fcode != false) {
  72. return $fcode;
  73. }
  74. }
  75. }
  76. public function bind($fcode)
  77. {
  78. $cond = [];
  79. $cond['goods_commonid'] = $this->mCommonID;
  80. $cond['batch_code'] = $this->mBatchCode;
  81. $cond['user_key'] = $fcode['user_key'];
  82. $cond['fc_id'] = $fcode['fc_id'];
  83. $cond['fc_state'] = $fcode['fc_state'];
  84. $cond['grab_state'] = $fcode['grab_state'];
  85. $cond['grab_time'] = $fcode['grab_time'];
  86. $cond['session_id'] = $fcode['session_id'];
  87. $cond['mobile'] = $fcode['mobile'];
  88. $datas = ['grab_state' => 2,
  89. 'user_key' => mt_rand(1000, 9999),
  90. 'usable_time' => util::last_day_secs(time() + $fcode['usable_days'] * 86400)];
  91. if(!empty($this->mSessionID)) {
  92. $datas['session_id'] = $this->mSessionID;
  93. }
  94. if(!empty($this->mMobile)) {
  95. $datas['mobile'] = $this->mMobile;
  96. }
  97. $ret = $this->mod_fcode->where($cond)->update($datas);
  98. $affect_rows = $this->mod_fcode->affected_rows();
  99. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  100. if($ret != false && $affect_rows > 0) {
  101. $fcode = array_merge($fcode,$datas);
  102. return $fcode;
  103. } else {
  104. return false;
  105. }
  106. }
  107. public function change($fcode)
  108. {
  109. return $this->bind($fcode);
  110. }
  111. public function lock($pay_sn)
  112. {
  113. $fcode = $this->grabed();
  114. if($fcode != false) return $fcode;
  115. while(true)
  116. {
  117. $cond = array('goods_commonid' => $this->mCommonID,
  118. 'batch_code' => $this->mBatchCode,
  119. 'fc_state' => '0',
  120. 'grab_state' => array('in', array(0,1)),
  121. 'grab_time' => array('lt',time() - self::time_out));
  122. $fcodes = $this->mod_fcode->where($cond)->field('*')->order('fc_id asc')->limit(1)->select(array('master' => true));
  123. if(empty($fcodes) || empty($fcodes[0])) return false;
  124. $fcode = $this->try_lock($fcodes[0],$pay_sn);
  125. if($fcode != false) {
  126. return $fcode;
  127. }
  128. }
  129. }
  130. public static function unlock($pay_sn)
  131. {
  132. $mod_fcode = Model('goods_fcode');
  133. $cur_time = util::last_day_secs(time());
  134. $ret = $mod_fcode->where(['pay_sn' => $pay_sn,'fc_state' => 3])
  135. ->update(['fc_state' => 0,'usable_time' => array('exp', "usable_days * 86400 + {$cur_time}")]);
  136. $affect_rows = $mod_fcode->affected_rows();
  137. if($ret != false && $affect_rows > 0) {
  138. return $affect_rows;
  139. } else {
  140. return 0;
  141. }
  142. }
  143. public static function reset($pay_sn)
  144. {
  145. $mod_fcode = Model('goods_fcode');
  146. $ret = $mod_fcode->where(['pay_sn' => $pay_sn,'fc_state' => 3])
  147. ->update(['fc_state' => 0,
  148. 'usable_time' => 0,
  149. 'mobile' => '',
  150. 'session_id' => '',
  151. 'grab_state' => 0,
  152. 'grab_time' => 0,
  153. 'pay_sn' => 0]);
  154. $affect_rows = $mod_fcode->affected_rows();
  155. if($ret != false && $affect_rows > 0) {
  156. return $affect_rows;
  157. } else {
  158. return 0;
  159. }
  160. }
  161. private function try_lock($fcode,$pay_sn)
  162. {
  163. $cond = [];
  164. $cond['goods_commonid'] = $this->mCommonID;
  165. $cond['batch_code'] = $this->mBatchCode;
  166. $cond['user_key'] = $fcode['user_key'];
  167. $cond['fc_id'] = $fcode['fc_id'];
  168. $cond['fc_state'] = $fcode['fc_state'];
  169. $cond['grab_state'] = $fcode['grab_state'];
  170. $cond['grab_time'] = $fcode['grab_time'];
  171. $cond['session_id'] = $fcode['session_id'];
  172. $cond['mobile'] = $fcode['mobile'];
  173. $datas = ['grab_state' => 2,
  174. 'grab_time' => time(),
  175. 'fc_state' => 3,
  176. 'pay_sn' => $pay_sn,
  177. 'user_key' => mt_rand(1000, 9999)];
  178. if(!empty($this->mSessionID)) {
  179. $datas['session_id'] = $this->mSessionID;
  180. }
  181. if(!empty($this->mMobile)) {
  182. $datas['mobile'] = $this->mMobile;
  183. }
  184. $ret = $this->mod_fcode->where($cond)->update($datas);
  185. $affect_rows = $this->mod_fcode->affected_rows();
  186. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  187. if($ret != false && $affect_rows > 0) {
  188. $fcode = array_merge($fcode,$datas);
  189. return $fcode;
  190. } else {
  191. return false;
  192. }
  193. }
  194. private function try_grab($fcode)
  195. {
  196. $cond = [];
  197. $cond['goods_commonid'] = $this->mCommonID;
  198. $cond['batch_code'] = $this->mBatchCode;
  199. $cond['user_key'] = $fcode['user_key'];
  200. $cond['fc_id'] = $fcode['fc_id'];
  201. $cond['fc_state'] = $fcode['fc_state'];
  202. $cond['grab_state'] = $fcode['grab_state'];
  203. $cond['grab_time'] = $fcode['grab_time'];
  204. $cond['session_id'] = $fcode['session_id'];
  205. $cond['mobile'] = $fcode['mobile'];
  206. if($this->mFUserMode && session_helper::logined())
  207. {
  208. $datas = ['grab_state' => 2,
  209. 'grab_time' => time(),
  210. 'user_key' => mt_rand(1000, 9999),
  211. 'usable_time' => util::last_day_secs(time() + $fcode['usable_days'] * 86400)];
  212. }
  213. else {
  214. $datas = ['grab_state' => 1,
  215. 'grab_time' => time()];
  216. }
  217. if(!empty($this->mSessionID)) {
  218. $datas['session_id'] = $this->mSessionID;
  219. }
  220. if(!empty($this->mMobile)) {
  221. $datas['mobile'] = $this->mMobile;
  222. }
  223. $ret = $this->mod_fcode->where($cond)->update($datas);
  224. $affect_rows = $this->mod_fcode->affected_rows();
  225. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  226. if($ret != false && $affect_rows > 0) {
  227. $fcode = array_merge($fcode,$datas);
  228. return $fcode;
  229. } else {
  230. return false;
  231. }
  232. }
  233. }