fetch_order.model.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. defined('InShopNC') or exit('Access Invalid!');
  3. class fetch_orderModel extends Model
  4. {
  5. const FETCH_ORDER_UNAVALIABLE = 0;
  6. const FETCH_ORDER_AVALIABLE = 1;
  7. const FETCH_ORDER_PROCESSING = 2;
  8. const FETCH_ORDER_PROCESSED = 3;
  9. public function __construct()
  10. {
  11. parent::__construct('fetch_order');
  12. }
  13. public function add($fetch_datas)
  14. {
  15. return $this->insert($fetch_datas);
  16. }
  17. public function store_order_count($store_id)
  18. {
  19. return $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'store_id' => $store_id])->count();
  20. }
  21. public function member_order_count($store_id,$member_id)
  22. {
  23. return $this->where(['fetch_status' => self::FETCH_ORDER_PROCESSING, 'store_id' => $store_id,'member_id' => $member_id])->count();
  24. }
  25. //抢单
  26. public function fetch($store_id, $member_id, $card_type, $count, $spec)
  27. {
  28. try {
  29. $trans = new trans_wapper($this,__METHOD__);
  30. $items = $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'card_type' => $card_type, 'store_id' => $store_id, 'refill_amount' => $spec])
  31. ->limit($count)
  32. ->lock(true)
  33. ->master(true)
  34. ->order('add_time asc')
  35. ->select();
  36. foreach ($items as $item) {
  37. $fetch_id = $item['fetch_id'];
  38. $this->where(['fetch_id' => $fetch_id])->update(['fetch_status' => self::FETCH_ORDER_PROCESSING,'member_id' => $member_id,'fetch_time' => time()]);
  39. }
  40. $trans->commit();
  41. return $items;
  42. }
  43. catch (Exception $ex) {
  44. $trans->rollback();
  45. Log::record(__METHOD__ . " fetch order trans err:" . $ex->getMessage() ,Log::ERR);
  46. return [];
  47. }
  48. }
  49. //退回订单,抢完后,但不想处理的单子,其它人可以继续处理.
  50. public function reput($fetch_id)
  51. {
  52. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])->update(['member_id' => 0,'fetch_time' => 0,'fetch_status' => self::FETCH_ORDER_AVALIABLE]);
  53. }
  54. //处理完成的单子
  55. public function processed($fetch_id,$fetch_result,$official_sn = '',$voucher = '')
  56. {
  57. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])
  58. ->update([
  59. 'fetch_status' => self::FETCH_ORDER_PROCESSED,
  60. 'finsh_time' => time(),
  61. 'fetch_result' => $fetch_result,
  62. 'official_sn' => $official_sn,
  63. 'voucher' => $voucher
  64. ]);
  65. }
  66. //超时未处理订单,不可以再接单.
  67. public function timeout_orders($timeout)
  68. {
  69. return $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'add_time' => ['lt',time() - $timeout]])
  70. ->lock(true)
  71. ->master(true)->select();
  72. }
  73. public function order_info($condition = [], $fields = '*', $master = false)
  74. {
  75. return $this->field($fields)->where($condition)->master($master)->find();
  76. }
  77. public function unavaliable($fetch_id)
  78. {
  79. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_AVALIABLE])
  80. ->update(['fetch_status' => self::FETCH_ORDER_UNAVALIABLE]);
  81. }
  82. //用户已经抢到,尚未处理完成的单子列表
  83. public function fetch_order_list($store_id, $member_id)
  84. {
  85. return $this->field('*')->where(['store_id' => $store_id,'member_id' => $member_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])->select();
  86. }
  87. //
  88. public function getFetchOrderList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '')
  89. {
  90. $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
  91. if (empty($list)) return [];
  92. return $list;
  93. }
  94. //
  95. public function editFetchOrder($update,$condition)
  96. {
  97. return $this->where($condition)->update($update);
  98. }
  99. }