fetch_order.model.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. //抢单
  18. public function fetch($store_id, $member_id, $card_type, $count)
  19. {
  20. try {
  21. $trans = new trans_wapper($this,__METHOD__);
  22. $items = $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'card_type' => $card_type, 'store_id' => $store_id])
  23. ->limit($count)
  24. ->lock(true)
  25. ->master(true)->select();
  26. foreach ($items as $item) {
  27. $fetch_id = $item['fetch_id'];
  28. $this->where(['fetch_id' => $fetch_id])->update(['fetch_status' => self::FETCH_ORDER_PROCESSING,'member_id' => $member_id,'fetch_time' => time()]);
  29. }
  30. $trans->commit();
  31. return $items;
  32. }
  33. catch (Exception $ex) {
  34. $trans->rollback();
  35. Log::record(__METHOD__ . " fetch order trans err:" . $ex->getMessage() ,Log::ERR);
  36. return [];
  37. }
  38. }
  39. //退回订单,抢完后,但不想处理的单子,其它人可以继续处理.
  40. public function reput($fetch_id)
  41. {
  42. 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]);
  43. }
  44. //处理完成的单子
  45. public function processed($fetch_id,$notify_state,$official_sn = '')
  46. {
  47. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])
  48. ->update(['fetch_status' => self::FETCH_ORDER_PROCESSED,'finsh_time' => time(),'notify_state' => $notify_state,'official_sn' => $official_sn]);
  49. }
  50. //超时未处理订单,不可以再接单.
  51. public function timeout_orders($timeout)
  52. {
  53. return $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'add_time' => ['lt',time() - $timeout]])
  54. ->lock(true)
  55. ->master(true)->select();
  56. }
  57. public function getFetchOrder($condition = [], $fields = '*', $master = false, $lock = false)
  58. {
  59. $order_info = $this->field($fields)->where($condition)->order('')->master($master)->lock($lock)->find();
  60. if (empty($order_info)) {
  61. return [];
  62. }
  63. return $order_info;
  64. }
  65. public function unavaliable($fetch_id)
  66. {
  67. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_AVALIABLE])
  68. ->update(['fetch_status' => self::FETCH_ORDER_UNAVALIABLE]);
  69. }
  70. //用户已经抢到,尚未处理完成的单子列表
  71. public function fetch_order_list($store_id, $member_id)
  72. {
  73. return $this->field('*')->where(['store_id' => $store_id,'member_id' => $member_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])->select();
  74. }
  75. //
  76. public function getFetchOrderList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '')
  77. {
  78. $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
  79. if (empty($list)) return [];
  80. return $list;
  81. }
  82. //
  83. public function editFetchOrder($update,$condition)
  84. {
  85. return $this->where($condition)->update($update);
  86. }
  87. }