operator.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. private function try_lock($fcode,$pay_sn)
  143. {
  144. $cond = [];
  145. $cond['goods_commonid'] = $this->mCommonID;
  146. $cond['batch_code'] = $this->mBatchCode;
  147. $cond['user_key'] = $fcode['user_key'];
  148. $cond['fc_id'] = $fcode['fc_id'];
  149. $cond['fc_state'] = $fcode['fc_state'];
  150. $cond['grab_state'] = $fcode['grab_state'];
  151. $cond['grab_time'] = $fcode['grab_time'];
  152. $cond['session_id'] = $fcode['session_id'];
  153. $cond['mobile'] = $fcode['mobile'];
  154. $datas = array( 'grab_state' => 2,
  155. 'grab_time' => time(),
  156. 'session_id' => session_helper::session_id(),
  157. 'mobile' => session_helper::cur_mobile(),
  158. 'fc_state' => 3,
  159. 'pay_sn' => $pay_sn,
  160. 'user_key' => mt_rand(1000, 9999));
  161. try
  162. {
  163. $this->mod_fcode->beginTransaction();
  164. $ret = $this->mod_fcode->where($cond)->update($datas);
  165. $affect_rows = $this->mod_fcode->affected_rows();
  166. $this->mod_fcode->commit();
  167. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  168. if($ret != false && $affect_rows > 0) {
  169. $fcode = array_merge($fcode,$datas);
  170. return $fcode;
  171. } else {
  172. return false;
  173. }
  174. } catch (Exception $ex) {
  175. $this->mod_fcode->rollback();
  176. return false;
  177. }
  178. }
  179. private function try_grab($fcode)
  180. {
  181. $cond = [];
  182. $cond['goods_commonid'] = $this->mCommonID;
  183. $cond['batch_code'] = $this->mBatchCode;
  184. $cond['user_key'] = $fcode['user_key'];
  185. $cond['fc_id'] = $fcode['fc_id'];
  186. $cond['fc_state'] = $fcode['fc_state'];
  187. $cond['grab_state'] = $fcode['grab_state'];
  188. $cond['grab_time'] = $fcode['grab_time'];
  189. $cond['session_id'] = $fcode['session_id'];
  190. $cond['mobile'] = $fcode['mobile'];
  191. if(session_helper::logined() || session_helper::isVerfiyMobile()) { // 如果是登录状态直接绑定
  192. $datas = array( 'grab_state' => 2,
  193. 'grab_time' => time(),
  194. 'session_id' => session_helper::session_id(),
  195. 'mobile' => session_helper::cur_mobile(),
  196. 'user_key' => mt_rand(1000, 9999),
  197. 'usable_time' => time() + ($fcode['usable_days'] * 24 * 3600));
  198. }
  199. else {
  200. $datas = array( 'grab_state' => 1,
  201. 'grab_time' => time(),
  202. 'session_id' => session_helper::session_id());
  203. }
  204. try
  205. {
  206. $this->mod_fcode->beginTransaction();
  207. $ret = $this->mod_fcode->where($cond)->update($datas);
  208. $affect_rows = $this->mod_fcode->affected_rows();
  209. $this->mod_fcode->commit();
  210. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  211. if($ret != false && $affect_rows > 0) {
  212. $fcode = array_merge($fcode,$datas);
  213. return $fcode;
  214. } else {
  215. return false;
  216. }
  217. } catch (Exception $ex) {
  218. $this->mod_fcode->rollback();
  219. return false;
  220. }
  221. }
  222. }