operator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class operator
  13. {
  14. private $mCommonID;
  15. private $mod_fcode;
  16. private $mBatchCode;
  17. const time_out = 300;
  18. public function __construct($common_id, $batch_code)
  19. {
  20. $this->mCommonID = $common_id;
  21. $this->mBatchCode = $batch_code;
  22. $this->mod_fcode = Model('goods_fcode');
  23. }
  24. public function grabed($mobile = null)
  25. {
  26. $cond = [];
  27. $cond['goods_commonid'] = $this->mCommonID;
  28. $cond['batch_code'] = $this->mBatchCode;
  29. if($mobile == null)
  30. {
  31. if(session_helper::logined()) {
  32. $cond['mobile|session_id'] = array('_multi'=>true,session_helper::cur_mobile(),session_helper::session_id());
  33. }
  34. else {
  35. $cond['session_id'] = session_helper::session_id();
  36. }
  37. }
  38. else {
  39. $cond['mobile|session_id'] = array('_multi'=>true,$mobile,session_helper::session_id());
  40. }
  41. $fcode = $this->mod_fcode->getGoodsFCode($cond);
  42. if(empty($fcode)) {
  43. return false;
  44. }
  45. else {
  46. return $fcode;
  47. }
  48. }
  49. public function grab()
  50. {
  51. $fcode = $this->grabed();
  52. if($fcode != false) return $fcode;
  53. while(true)
  54. {
  55. $cond = array('goods_commonid' => $this->mCommonID,
  56. 'batch_code' => $this->mBatchCode,
  57. 'fc_state' => '0',
  58. 'grab_state' => array('in', array(0,1)),
  59. 'grab_time' => array('lt',time() - self::time_out));
  60. $fcodes = $this->mod_fcode->where($cond)->field('*')->order('fc_id asc')->limit(1)->select(array('master' => true));
  61. if(empty($fcodes) || empty($fcodes[0])) return false;
  62. $fcode = $this->try_grab($fcodes[0]);
  63. if($fcode != false) {
  64. return $fcode;
  65. }
  66. }
  67. }
  68. public function bind($fcode,$mobile)
  69. {
  70. $cond = [];
  71. $cond['goods_commonid'] = $this->mCommonID;
  72. $cond['batch_code'] = $this->mBatchCode;
  73. $cond['user_key'] = $fcode['user_key'];
  74. $cond['fc_id'] = $fcode['fc_id'];
  75. $cond['fc_state'] = $fcode['fc_state'];
  76. $cond['grab_state'] = $fcode['grab_state'];
  77. $cond['grab_time'] = $fcode['grab_time'];
  78. $cond['session_id'] = $fcode['session_id'];
  79. $cond['mobile'] = $fcode['mobile'];
  80. $datas = array( 'grab_state' => 2,
  81. 'mobile' => $mobile,
  82. 'user_key' => mt_rand(1000, 9999));
  83. $this->mod_fcode->beginTransaction();
  84. $ret = $this->mod_fcode->where($cond)->update($datas);
  85. $affect_rows = $this->mod_fcode->affected_rows();
  86. $this->mod_fcode->commit();
  87. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  88. if($ret != false && $affect_rows > 0) {
  89. $fcode = array_merge($fcode,$datas);
  90. return $fcode;
  91. } else {
  92. return false;
  93. }
  94. }
  95. public function change($fcode,$mobile)
  96. {
  97. return $this->bind($fcode,$mobile);
  98. }
  99. private function try_grab($fcode)
  100. {
  101. $cond = [];
  102. $cond['goods_commonid'] = $this->mCommonID;
  103. $cond['batch_code'] = $this->mBatchCode;
  104. $cond['user_key'] = $fcode['user_key'];
  105. $cond['fc_id'] = $fcode['fc_id'];
  106. $cond['fc_state'] = $fcode['fc_state'];
  107. $cond['grab_state'] = $fcode['grab_state'];
  108. $cond['grab_time'] = $fcode['grab_time'];
  109. $cond['session_id'] = $fcode['session_id'];
  110. $cond['mobile'] = $fcode['mobile'];
  111. if(session_helper::logined() || session_helper::isVerfiyMobile()) { // 如果是登录状态直接绑定
  112. $datas = array( 'grab_state' => 2,
  113. 'grab_time' => time(),
  114. 'session_id' => session_helper::session_id(),
  115. 'mobile' => session_helper::cur_mobile(),
  116. 'user_key' => mt_rand(1000, 9999));
  117. }
  118. else {
  119. $datas = array( 'grab_state' => 1,
  120. 'grab_time' => time(),
  121. 'session_id' => session_helper::session_id());
  122. }
  123. $this->mod_fcode->beginTransaction();
  124. $ret = $this->mod_fcode->where($cond)->update($datas);
  125. $affect_rows = $this->mod_fcode->affected_rows();
  126. $this->mod_fcode->commit();
  127. Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  128. if($ret != false && $affect_rows > 0) {
  129. $fcode = array_merge($fcode,$datas);
  130. return $fcode;
  131. } else {
  132. return false;
  133. }
  134. }
  135. }