123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- <?php
- /**
- * 订单管理
- *
- *
- *
- *
-
- */
- defined('InShopNC') or exit('Access Invalid!');
- class orderModel extends Model
- {
- /**
- * 取单条订单信息
- *
- * @param unknown_type $condition
- * @param array $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
- * @return unknown
- */
- public function getOrderInfo($condition = array(), $extend = array(), $fields = '*', $order = '',$group = '') {
- $order_info = $this->table('order')->field($fields)->where($condition)->group($group)->order($order)->find();
- if (empty($order_info)) {
- return array();
- }
- if (isset($order_info['order_state'])) {
- $order_info['state_desc'] = orderState($order_info);
- }
- if (isset($order_info['payment_code'])) {
- $order_info['payment_name'] = orderPaymentName($order_info['payment_code']);
- }
- //追加返回订单扩展表信息
- if (in_array('order_common',$extend)) {
- $order_info['extend_order_common'] = $this->getOrderCommonInfo(array('order_id'=>$order_info['order_id']));
- $order_info['extend_order_common']['reciver_info'] = unserialize($order_info['extend_order_common']['reciver_info']);
- $order_info['extend_order_common']['invoice_info'] = unserialize($order_info['extend_order_common']['invoice_info']);
- }
- //追加返回店铺信息
- if (in_array('store',$extend)) {
- $order_info['extend_store'] = Model('store')->getStoreInfo(array('store_id'=>$order_info['store_id']));
- }
- //返回买家信息
- if (in_array('member',$extend)) {
- $order_info['extend_member'] = Model('member')->getMemberInfoByID($order_info['buyer_id']);
- }
- //追加返回商品信息
- if (in_array('order_goods',$extend)) {
- //取商品列表
- $order_goods_list = $this->getOrderGoodsList(array('order_id'=>$order_info['order_id']));
- $order_info['extend_order_goods'] = $order_goods_list;
- }
- return $order_info;
- }
- public function getOrderCommonInfo($condition = array(), $field = '*') {
- return $this->table('order_common')->where($condition)->find();
- }
- public function getOrderPayInfo($condition = array(), $master = false) {
- return $this->table('order_pay')->where($condition)->master($master)->find();
- }
- /**
- * 取得支付单列表
- *
- * @param unknown_type $condition
- * @param unknown_type $pagesize
- * @param unknown_type $filed
- * @param unknown_type $order
- * @param string $key 以哪个字段作为下标,这里一般指pay_id
- * @return unknown
- */
- public function getOrderPayList($condition, $pagesize = '', $filed = '*', $order = '', $key = '') {
- return $this->table('order_pay')->field($filed)->where($condition)->order($order)->page($pagesize)->key($key)->select();
- }
- /**
- * 取得订单列表(未被删除)
- * @param unknown $condition
- * @param string $pagesize
- * @param string $field
- * @param string $order
- * @param string $limit
- * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
- * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
- */
- public function getNormalOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '', $extend = array(),$master = false){
- $condition['delete_state'] = 0;
- return $this->getOrderList($condition, $pagesize, $field, $order, $limit, $extend,$master);
- }
- public function getNormalOrderListEx($condition, $master = false){
- $condition['delete_state'] = 0;
- return $this->getOrderList($condition, '', '*', 'order_id desc', '', [],$master);
- }
- /**
- * 取得订单列表(所有)
- * @param unknown $condition
- * @param string $pagesize
- * @param string $field
- * @param string $order
- * @param string $limit
- * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
- * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
- */
- public function getOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '', $extend = array(), $master = false){
- $list = $this->table('order')->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
- if (empty($list)) return array();
- $order_list = array();
- foreach ($list as $order) {
- if (isset($order['order_state'])) {
- $order['state_desc'] = orderState($order);
- }
- if (isset($order['payment_code'])) {
- $order['payment_name'] = orderPaymentName($order['payment_code']);
- }
- if (!empty($extend)) $order_list[$order['order_id']] = $order;
- }
- if (empty($order_list)) $order_list = $list;
- //追加返回订单扩展表信息
- if (in_array('order_common',$extend)) {
- $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
- foreach ($order_common_list as $value) {
- $order_list[$value['order_id']]['extend_order_common'] = $value;
- $order_list[$value['order_id']]['extend_order_common']['reciver_info'] = @unserialize($value['reciver_info']);
- $order_list[$value['order_id']]['extend_order_common']['invoice_info'] = @unserialize($value['invoice_info']);
- }
- }
- //追加返回店铺信息
- if (in_array('store',$extend)) {
- $store_id_array = array();
- foreach ($order_list as $value) {
- if (!in_array($value['store_id'],$store_id_array)) $store_id_array[] = $value['store_id'];
- }
- $store_list = Model('store')->getStoreList(array('store_id'=>array('in',$store_id_array)));
- $store_new_list = array();
- foreach ($store_list as $store) {
- $store_new_list[$store['store_id']] = $store;
- }
- foreach ($order_list as $order_id => $order) {
- $order_list[$order_id]['extend_store'] = $store_new_list[$order['store_id']];
- }
- }
- //追加返回买家信息
- if (in_array('member',$extend)) {
- foreach ($order_list as $order_id => $order) {
- $order_list[$order_id]['extend_member'] = Model('member')->getMemberInfoByID($order['buyer_id']);
- }
- }
- //追加返回商品信息
- if (in_array('order_goods',$extend)) {
- //取商品列表
- $order_goods_list = $this->getOrderGoodsList(array('order_id'=>array('in',array_keys($order_list))));
- if (!empty($order_goods_list)) {
- foreach ($order_goods_list as $value) {
- $order_list[$value['order_id']]['extend_order_goods'][] = $value;
- }
- } else {
- $order_list[$value['order_id']]['extend_order_goods'] = array();
- }
- }
- if (in_array('order_address',$extend)) {
- $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
- foreach ($order_common_list as $value) {
- $order_list[$value['order_id']]['reciver_info'] = @unserialize($value['reciver_info']);
- $order_list[$value['order_id']]['reciver_name'] = $value['reciver_name'];
- }
- }
- return $order_list;
- }
- /**
- * 取得(买/卖家)订单某个数量缓存
- * @param string $type 买/卖家标志,允许传入 buyer、store
- * @param int $id 买家ID、店铺ID
- * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
- * @return array
- */
- public function getOrderCountCache($type, $id, $key) {
- if (!C('cache_open')) return array();
- $type = 'ordercount'.$type;
- $ins = Cache::getInstance('cacheredis');
- $order_info = $ins->hget($id,$type,$key);
- return !is_array($order_info) ? array($key => $order_info) : $order_info;
- }
- /**
- * 设置(买/卖家)订单某个数量缓存
- * @param string $type 买/卖家标志,允许传入 buyer、store
- * @param int $id 买家ID、店铺ID
- * @param array $data
- */
- public function editOrderCountCache($type, $id, $data) {
- if (!C('cache_open') || empty($type) || !intval($id) || !is_array($data)) return ;
- $ins = Cache::getInstance('cacheredis');
- $type = 'ordercount'.$type;
- $ins->hset($id,$type,$data);
- }
- /**
- * 取得买卖家订单数量某个缓存
- * @param string $type $type 买/卖家标志,允许传入 buyer、store
- * @param int $id 买家ID、店铺ID
- * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
- * @return int
- */
- public function getOrderCountByID($type, $id, $key) {
- $cache_info = $this->getOrderCountCache($type, $id, $key);
-
- if (is_string($cache_info[$key])) {
- //从缓存中取得
- $count = $cache_info[$key];
- } else {
- //从数据库中取得
- $field = $type == 'buyer' ? 'buyer_id' : 'store_id';
- $condition = array($field => $id);
- $func = 'getOrderState'.$key;
- $count = $this->$func($condition);
- $this->editOrderCountCache($type,$id,array($key => $count));
- }
- return $count;
- }
- /**
- * 删除(买/卖家)订单全部数量缓存
- * @param string $type 买/卖家标志,允许传入 buyer、store
- * @param int $id 买家ID、店铺ID
- * @return bool
- */
- public function delOrderCountCache($type, $id) {
- if (!C('cache_open')) return true;
- $ins = Cache::getInstance('cacheredis');
- $type = 'ordercount'.$type;
- return $ins->hdel($id,$type);
- }
- /**
- * 待付款订单数量
- * @param unknown $condition
- */
- public function getOrderStateNewCount($condition = array()) {
- $condition['order_state'] = ORDER_STATE_NEW;
- return $this->getOrderCount($condition);
- }
- /**
- * 待发货订单数量
- * @param unknown $condition
- */
- public function getOrderStatePayCount($condition = array()) {
- $condition['order_state'] = ORDER_STATE_PAY;
- return $this->getOrderCount($condition);
- }
- /**
- * 待收货订单数量
- * @param unknown $condition
- */
- public function getOrderStateSendCount($condition = array()) {
- $condition['order_state'] = ORDER_STATE_SEND;
- return $this->getOrderCount($condition);
- }
- /**
- * 待评价订单数量
- * @param unknown $condition
- */
- public function getOrderStateEvalCount($condition = array()) {
- $condition['order_state'] = ORDER_STATE_SUCCESS;
- $condition['evaluation_state'] = 0;
- return $this->getOrderCount($condition);
- }
- /**
- * 交易中的订单数量
- * @param unknown $condition
- */
- public function getOrderStateTradeCount($condition = array()) {
- $condition['order_state'] = array(array('neq',ORDER_STATE_CANCEL),array('neq',ORDER_STATE_SUCCESS),'and');
- return $this->getOrderCount($condition);
- }
- /**
- * 取得订单数量
- * @param unknown $condition
- */
- public function getOrderCount($condition) {
- return $this->table('order')->where($condition)->count();
- }
- /**
- * 取得订单商品表详细信息
- * @param unknown $condition
- * @param string $fields
- * @param string $order
- */
- public function getOrderGoodsInfo($condition = array(), $fields = '*', $order = '') {
- return $this->table('order_goods')->where($condition)->field($fields)->order($order)->find();
- }
- /**
- * 取得订单商品表列表
- * @param unknown $condition
- * @param string $fields
- * @param string $limit
- * @param string $page
- * @param string $order
- * @param string $group
- * @param string $key
- */
- public function getOrderGoodsList($condition = array(), $fields = '*', $limit = null, $page = null, $order = 'rec_id desc', $group = null, $key = null) {
- return $this->table('order_goods')->field($fields)->where($condition)->limit($limit)->order($order)->group($group)->key($key)->page($page)->select();
- }
- /**
- * 取得订单扩展表列表
- * @param unknown $condition
- * @param string $fields
- * @param string $limit
- */
- public function getOrderCommonList($condition = array(), $fields = '*', $order = '', $limit = null) {
- return $this->table('order_common')->field($fields)->where($condition)->order($order)->limit($limit)->select();
- }
- /**
- * 插入订单支付表信息
- * @param array $data
- * @return int 返回 insert_id
- */
- public function addOrderPay($data) {
- return $this->table('order_pay')->insert($data);
- }
- /**
- * 插入订单表信息
- * @param array $data
- * @return int 返回 insert_id
- */
- public function addOrder($data) {
- $insert = $this->table('order')->insert($data);
- if ($insert) {
- //更新缓存
- QueueClient::push('delOrderCountCache',array('buyer_id'=>$data['buyer_id'],'store_id'=>$data['store_id']));
- }
- return $insert;
- }
- /**
- * 插入订单扩展表信息
- * @param array $data
- * @return int 返回 insert_id
- */
- public function addOrderCommon($data) {
- return $this->table('order_common')->insert($data);
- }
- /**
- * 插入订单扩展表信息
- * @param array $data
- * @return int 返回 insert_id
- */
- public function addOrderGoods($data) {
- return $this->table('order_goods')->insertAll($data);
- }
- /**
- * 添加订单日志
- */
- public function addOrderLog($data) {
- $data['log_role'] = str_replace(array('buyer','seller','system','admin'),array('买家','商家','系统','管理员'), $data['log_role']);
- $data['log_time'] = time();
- return $this->table('order_log')->insert($data);
- }
- /**
- * 更改订单信息
- *
- * @param unknown_type $data
- * @param unknown_type $condition
- */
- public function editOrder($data,$condition,$limit = '') {
- $update = $this->table('order')->where($condition)->limit($limit)->update($data);
- if ($update) {
- //更新缓存
- QueueClient::push('delOrderCountCache',$condition);
- }
- return $update;
- }
- /**
- * 更改订单信息
- *
- * @param unknown_type $data
- * @param unknown_type $condition
- */
- public function editOrderCommon($data,$condition) {
- return $this->table('order_common')->where($condition)->update($data);
- }
- /**
- * 更改订单信息
- *
- * @param unknown_type $data
- * @param unknown_type $condition
- */
- public function editOrderGoods($data,$condition) {
- return $this->table('order_goods')->where($condition)->update($data);
- }
- /**
- * 更改订单支付信息
- *
- * @param unknown_type $data
- * @param unknown_type $condition
- */
- public function editOrderPay($data,$condition) {
- return $this->table('order_pay')->where($condition)->update($data);
- }
- /**
- * 订单操作历史列表
- * @param unknown $order_id
- * @return Ambigous <multitype:, unknown>
- */
- public function getOrderLogList($condition) {
- return $this->table('order_log')->where($condition)->select();
- }
- /**
- * 取得单条订单操作记录
- * @param unknown $condition
- * @param string $order
- */
- public function getOrderLogInfo($condition = array(), $order = '') {
- return $this->table('order_log')->where($condition)->order($order)->find();
- }
- /**
- * 返回是否允许某些操作
- * @param unknown $operate
- * @param unknown $order_info
- */
- public function getOrderOperateState($operate,$order_info){
- if (!is_array($order_info) || empty($order_info)) return false;
- switch ($operate)
- {
- //买家取消订单
- case 'buyer_cancel':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) || ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
- break;
- //申请退款
- case 'refund_cancel':
- $state = $order_info['refund'] == 1 && !intval($order_info['lock_state']);
- break;
- //商家取消订单
- case 'store_cancel':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
- ($order_info['payment_code'] == 'offline' &&
- in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)));
- break;
- //平台取消订单
- case 'system_cancel':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
- ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
- break;
- //平台收款
- case 'system_receive_pay':
- $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
- break;
- //买家投诉
- case 'complain':
- $state = in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)) ||
- intval($order_info['finnshed_time']) > (time() - C('complain_time_limit'));
- break;
- case 'payment':
- $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
- break;
- //调整运费
- case 'modify_price':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
- ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
- $state = floatval($order_info['shipping_fee']) > 0 && $state;
- break;
- //调整商品价格
- case 'spay_price':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
- ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
- $state = floatval($order_info['goods_amount']) > 0 && $state;
- break;
- //发货
- case 'send':
- $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_PAY;
- break;
- //收货
- case 'receive':
- $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_SEND;
- break;
- //评价
- case 'evaluation':
- $state = !$order_info['lock_state'] && !$order_info['evaluation_state'] && $order_info['order_state'] == ORDER_STATE_SUCCESS;
- break;
- //锁定
- case 'lock':
- $state = intval($order_info['lock_state']) ? true : false;
- break;
- //快递跟踪
- case 'deliver':
- $state = !empty($order_info['shipping_code']) && in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS));
- break;
- //放入回收站
- case 'delete':
- $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 0;
- break;
- //永久删除、从回收站还原
- case 'drop':
- case 'restore':
- $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 1;
- break;
- //分享
- case 'share':
- $state = true;
- break;
- }
- return $state;
- }
-
- /**
- * 联查订单表订单商品表
- *
- * @param array $condition
- * @param string $field
- * @param number $page
- * @param string $order
- * @return array
- */
- public function getOrderAndOrderGoodsList($condition, $field = '*', $page = 0, $order = 'rec_id desc') {
- return $this->table('order_goods,order')->join('inner')->on('order_goods.order_id=order.order_id')->where($condition)->field($field)->page($page)->order($order)->select();
- }
-
- /**
- * 订单销售记录 订单状态为20、30、40时
- * @param unknown $condition
- * @param string $field
- * @param number $page
- * @param string $order
- */
- public function getOrderAndOrderGoodsSalesRecordList($condition, $field="*", $page = 0, $order = 'rec_id desc') {
- $condition['order_state'] = array('in', array(ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS));
- return $this->getOrderAndOrderGoodsList($condition, $field, $page, $order);
- }
- public function setOrderDelivery($order_sn,$express_id,$shipper_code,$ship_time)
- {
- $items = $this->table('order')->field('order_id')->where(array('order_sn' => $order_sn))->select();
- if(empty($items) || count($items) == 0) {
- return false;
- } else {
- $order_id = $items[0]['order_id'];
- }
- $f1 = $this->table('order_common')->where(array('order_id' => $order_id))->update(array('shipping_express_id' => $express_id,'shipping_time' => $ship_time));
- $f2 = $this->table('order')->where(array('order_id' => $order_id))->update(array('order_state' => ORDER_STATE_SEND,'shipping_code' => $shipper_code,'delay_time' => $ship_time));
- return ($f1 && $f2);
- }
- }
|