123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- defined('InShopNC') or exit('Access Invalid!');
- class fetch_orderModel extends Model
- {
- const FETCH_ORDER_UNAVALIABLE = 0;
- const FETCH_ORDER_AVALIABLE = 1;
- const FETCH_ORDER_PROCESSING = 2;
- const FETCH_ORDER_PROCESSED = 3;
-
- public function __construct()
- {
- parent::__construct('fetch_order');
- }
- public function add($fetch_datas)
- {
- return $this->insert($fetch_datas);
- }
- //抢单
- public function fetch($store_id, $member_id, $card_type, $count)
- {
- try {
- $trans = new trans_wapper($this,__METHOD__);
- $items = $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'card_type' => $card_type, 'store_id' => $store_id])
- ->limit($count)
- ->lock(true)
- ->master(true)->select();
- foreach ($items as $item) {
- $fetch_id = $item['fetch_id'];
- $this->where(['fetch_id' => $fetch_id])->update(['fetch_status' => self::FETCH_ORDER_PROCESSING,'member_id' => $member_id,'fetch_time' => time()]);
- }
- $trans->commit();
- return $items;
- }
- catch (Exception $ex) {
- $trans->rollback();
- Log::record(__METHOD__ . " fetch order trans err:" . $ex->getMessage() ,Log::ERR);
- return [];
- }
- }
- //退回订单,抢完后,但不想处理的单子,其它人可以继续处理.
- public function reput($fetch_id)
- {
- 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]);
- }
- //处理完成的单子
- public function processed($fetch_id,$notify_state,$official_sn = '')
- {
- return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])
- ->update(['fetch_status' => self::FETCH_ORDER_PROCESSED,'finsh_time' => time(),'notify_state' => $notify_state,'official_sn' => $official_sn]);
- }
- //超时未处理订单,不可以再接单.
- public function timeout_orders($timeout)
- {
- return $this->where(['fetch_status' => self::FETCH_ORDER_AVALIABLE, 'add_time' => ['lt',time() - $timeout]])
- ->lock(true)
- ->master(true)->select();
- }
- public function getFetchOrder($condition = [], $fields = '*', $master = false, $lock = false)
- {
- $order_info = $this->field($fields)->where($condition)->order('')->master($master)->lock($lock)->find();
- if (empty($order_info)) {
- return [];
- }
- return $order_info;
- }
- public function unavaliable($fetch_id)
- {
- return $this->where(['fetch_id' => $fetch_id,'fetch_status' => self::FETCH_ORDER_AVALIABLE])
- ->update(['fetch_status' => self::FETCH_ORDER_UNAVALIABLE]);
- }
- //用户已经抢到,尚未处理完成的单子列表
- public function fetch_order_list($store_id, $member_id)
- {
- return $this->field('*')->where(['store_id' => $store_id,'member_id' => $member_id,'fetch_status' => self::FETCH_ORDER_PROCESSING])->select();
- }
- //
- public function getFetchOrderList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '')
- {
- $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
- if (empty($list)) return [];
- return $list;
- }
- //
- public function editFetchOrder($update,$condition)
- {
- return $this->where($condition)->update($update);
- }
- }
|