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