fetch_order.model.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, $amount, $count)
  19. {
  20. try {
  21. $tran = new trans_wapper($this,__METHOD__);
  22. $items = $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'card_type' => $card_type, 'amount' => $amount, '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. $tran->commit();
  31. return $items;
  32. }
  33. catch (Exception $ex) {
  34. Log::record(__METHOD__ . " fetch order trans err:" . $ex->getMessage() ,Log::ERR);
  35. return [];
  36. }
  37. }
  38. //退回订单,抢完后,但不想处理的单子.
  39. public function reput($fetch_id)
  40. {
  41. 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]);
  42. }
  43. //处理完成的单子
  44. public function processed($fetch_id,$official_sn = '')
  45. {
  46. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])
  47. ->update(['fetch_status' => self::FETCH_ORDER_PROCESSING,'finsh_time' => time(),'official_sn' => $official_sn]);
  48. }
  49. //超时未处理订单,不可以再接单.
  50. public function timeout($fetch_id)
  51. {
  52. return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_AVALIABLE])
  53. ->update(['fetch_status' => self::FETCH_ORDER_UNAVALIABLE]);
  54. }
  55. //用户已经抢到,尚未处理完成的单子列表
  56. public function fetch_order_list($store_id, $member_id)
  57. {
  58. return $this->field('*')->where(['store_id' => $store_id,'member_id' => $member_id])->select();
  59. }
  60. //
  61. public function getFetchOrderList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '')
  62. {
  63. $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
  64. if (empty($list)) return [];
  65. return $list;
  66. }
  67. //
  68. public function editFetchOrder($update,$condition)
  69. {
  70. return $this->where($condition)->update($update);
  71. }
  72. }