operator.php 9.8 KB

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