123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- <?php
- namespace app\common\model;
- use think\facade\Db;
- class Order extends BaseModel {
- public $page_info;
- public $lock=false;
-
- public function getOrderInfo($condition = array(), $extend = array(), $fields = '*', $order = '', $group = '') {
- $order_info = Db::name('order')->field($fields)->where($condition)->lock($this->lock)->group($group)->order($order)->find();
- if (empty($order_info)) {
- return array();
- }
- if (isset($order_info['order_state'])) {
- $order_info['state_desc'] = get_order_state($order_info);
- }
- if (isset($order_info['payment_code'])) {
- $order_info['payment_name'] = get_order_payment_name($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;
- }
-
-
- if (in_array('ppintuanorder', $extend)) {
-
- $pintuanorder_list = model('ppintuanorder')->getPpintuanorderList(array('ppintuanorder.order_id' => $order_info['order_id'],'pintuanorder_type'=>0));
- if (!empty($pintuanorder_list)) {
- foreach ($pintuanorder_list as $value) {
- $order_info['pintuan_id'] = $value['pintuan_id'];
- $order_info['pintuangroup_id'] = $value['pintuangroup_id'];
- $order_info['pintuanorder_state'] = $value['pintuanorder_state'];
- $order_info['pintuanorder_state_text'] = $value['pintuanorder_state_text'];
- }
- }
- }
- return $order_info;
- }
-
-
- public function getOrdercommonInfo($condition = array(), $field = '*') {
- return Db::name('ordercommon')->where($condition)->find();
- }
-
-
- public function getOrderpayInfo($condition = array()) {
- return Db::name('orderpay')->where($condition)->find();
- }
-
- public function getOrderpayList($condition, $field = '*', $order = '', $key = '') {
- $pay_list = Db::name('orderpay')->field($field)->where($condition)->order($order)->select()->toArray();
- if($key){
- $pay_list = ds_change_arraykey($pay_list, $key);
- }
- return $pay_list;
- }
-
- public function getNormalOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = 0, $extend = array()) {
- $condition[]=array('delete_state','=',0);
- return $this->getOrderList($condition, $pagesize, $field, $order, $limit, $extend);
- }
-
- public function getOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = 0, $extend = array()) {
- if($pagesize){
- $list_paginate = Db::name('order')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
- $this->page_info = $list_paginate;
- $list = $list_paginate->items();
- }else{
- $list = Db::name('order')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
- }
- if (empty($list))
- return array();
- $order_list = array();
- foreach ($list as $order) {
- if (isset($order['order_state'])) {
- $order['state_desc'] = get_order_state($order);
- }
- if (isset($order['payment_code'])) {
- $order['payment_name'] = get_order_payment_name($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(array('order_id','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 = Db::name('store')->where('store_id', 'in', array_values($store_id_array))->select()->toArray();
- $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'] = isset($store_new_list[$order['store_id']])?$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 = Db::name('ordergoods')->where('order_id', 'in', array_keys($order_list))->select()->toArray();
- 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('ppintuanorder', $extend)) {
-
- $condition = array();
- $condition[] = array('ppintuanorder.order_id','in', array_keys($order_list));
- $condition[] = array('pintuanorder_type','=',0);
- $pintuanorder_list = model('ppintuanorder')->getPpintuanorderList($condition);
- if (!empty($pintuanorder_list)) {
- foreach ($pintuanorder_list as $value) {
- $order_list[$value['order_id']]['pintuan_id'] = $value['pintuan_id'];
- $order_list[$value['order_id']]['pintuangroup_id'] = $value['pintuangroup_id'];
- $order_list[$value['order_id']]['pintuanorder_state'] = $value['pintuanorder_state'];
- $order_list[$value['order_id']]['pintuanorder_state_text'] = $value['pintuanorder_state_text'];
- }
- }
- }
- return $order_list;
- }
-
- public function getOrderCountCache($type, $id, $key) {
- if (!config('ds_config.cache_open')) return ;
- $types = $id.'_ordercount' .'_'.$type.'_'. $key;
- $count = rcache($types);
- return $count;
- }
-
- public function editOrderCountCache($type, $id, $key,$count) {
- if (!config('ds_config.cache_open') || empty($type) || !intval($id))
- return;
- $types = $id.'_ordercount'.'_'.$type .'_'. $key;
- wkcache($types, $count);
- }
-
- public function getOrderCountByID($type, $id, $key) {
- $cache_info = $this->getOrderCountCache($type, $id, $key);
- if (config('ds_config.cache_open') && is_numeric($cache_info)) {
-
- $count = $cache_info;
- } else {
-
- $field = $type == 'buyer' ? 'buyer_id' : 'store_id';
- $condition = array();
- $condition[] = array($field , '=' ,$id);
- $func = 'getOrderState' . $key;
- $count = $this->$func($condition);
- $this->editOrderCountCache($type, $id, $key,$count);
- }
- return $count;
- }
-
- public function delOrderCountCache($type, $id) {
- $type_NewCount = $id.'_ordercount' . '_'.$type.'_NewCount';
- $type_PayCount = $id.'_ordercount' . '_'.$type.'_PayCount';
- $type_SendCount = $id.'_ordercount' . '_'.$type.'_SendCount';
- $type_EvalCount = $id.'_ordercount' . '_'.$type.'_EvalCount';
- $type_TradeCount = $id.'_ordercount' . '_'.$type.'_TradeCount';
- dcache($type_NewCount);
- dcache($type_PayCount);
- dcache($type_SendCount);
- dcache($type_EvalCount);
- dcache($type_TradeCount);
- }
-
- public function getOrderStateNewCount($condition = array()) {
- $condition[]=array('order_state','=',ORDER_STATE_NEW);
- return $this->getOrderCount($condition);
- }
-
- public function getOrderStatePayCount($condition = array()) {
- $condition[]=array('order_state','=',ORDER_STATE_PAY);
- return $this->getOrderCount($condition);
- }
-
- public function getOrderStateSendCount($condition = array()) {
- $condition[]=array('order_state','=',ORDER_STATE_SEND);
- return $this->getOrderCount($condition);
- }
-
- public function getOrderStateEvalCount($condition = array()) {
- $condition[]=array('order_state','=',ORDER_STATE_SUCCESS);
- $condition[]=array('evaluation_state','=',0);
- $condition[]=array('refund_state','=',0);
- return $this->getOrderCount($condition);
- }
-
- public function getOrderStateTradeCount($condition = array()) {
- $condition[]=array('order_state','not in',array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS));
- return $this->getOrderCount($condition);
- }
-
- public function getOrderCount($condition) {
- return Db::name('order')->where($condition)->count();
- }
-
- public function getOrdergoodsInfo($condition = array(), $fields = '*', $order = '') {
- return Db::name('ordergoods')->where($condition)->field($fields)->order($order)->find();
- }
-
- public function getOrdergoodsList($condition = array(), $fields = '*', $limit = 0, $pagesize = null, $order = 'rec_id desc', $group = null, $key = null) {
- if ($pagesize) {
- $res= Db::name('ordergoods')->field($fields)->where($condition)->order($order)->group($group)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
- $this->page_info=$res;
- $ordergoods = $res->items();
- if(!empty($key)){
- $ordergoods = ds_change_arraykey($ordergoods, $key);
- }
- return $ordergoods;
- } else {
- $ordergoods = Db::name('ordergoods')->field($fields)->where($condition)->limit($limit)->order($order)->group($group)->select()->toArray();
- if(!empty($key)){
- $ordergoods = ds_change_arraykey($ordergoods, $key);
- }
- return $ordergoods;
- }
- }
-
- public function getOrdercommonList($condition = array(), $fields = '*', $order = '', $limit = 0) {
- return Db::name('ordercommon')->field($fields)->where($condition)->order($order)->limit($limit)->select()->toArray();
- }
-
- public function addOrderpay($data) {
- return Db::name('orderpay')->insertGetId($data);
- }
-
- public function addOrder($data) {
- $result = Db::name('order')->insertGetId($data);
- if ($result) {
-
- model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'delOrderCountCache','cron_value'=>serialize(array('buyer_id'=>$data['buyer_id'],'store_id'=>$data['store_id']))));
- }
- return $result;
- }
-
- public function addOrdercommon($data) {
- return Db::name('ordercommon')->insertGetId($data);
- }
-
- public function addOrdergoods($data) {
- return Db::name('ordergoods')->insertAll($data);
- }
-
- public function addOrderlog($data) {
- $data['log_role'] = str_replace(array('buyer', 'seller', 'system', 'admin'), array('买家', '商家', '系统', '管理员'), $data['log_role']);
- $data['log_time'] = TIMESTAMP;
- return Db::name('orderlog')->insertGetId($data);
- }
-
- public function editOrder($data, $condition, $limit = 0) {
- $update = Db::name('order')->where($condition)->limit($limit)->update($data);
- if ($update) {
-
- $order_list = Db::name('order')->where($condition)->select()->toArray();
- foreach ($order_list as $key => $order) {
- model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'delOrderCountCache','cron_value'=>serialize(array('buyer_id'=>$order['buyer_id'],'store_id'=>$order['store_id']))));
- }
- }
- return $update;
- }
-
- public function editOrdercommon($data, $condition) {
- return Db::name('ordercommon')->where($condition)->update($data);
- }
-
- public function editOrdergoods($data, $condition) {
- return Db::name('ordergoods')->where($condition)->update($data);
- }
-
- public function editOrderpay($data, $condition) {
- return Db::name('orderpay')->where($condition)->update($data);
- }
-
- public function getOrderlogList($condition) {
- return Db::name('orderlog')->where($condition)->select()->toArray();
- }
-
- public function getOrderlogInfo($condition = array(), $order = '') {
- return Db::name('orderlog')->where($condition)->order($order)->find();
- }
-
- 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['order_state'] == ORDER_STATE_DEPOSIT) ||
- ($order_info['order_state'] == ORDER_STATE_REST) ||
- ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
- break;
-
- case 'refund_cancel':
- if (isset($order_info['refund'])) {
- $state = $order_info['refund'] == 1 && !intval($order_info['lock_state']);
- if($order_info['ob_no']){
- $state = FALSE;
- }
- } else {
- $state = FALSE;
- }
- break;
-
- case 'store_cancel':
- $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
- ($order_info['order_state'] == ORDER_STATE_DEPOSIT) ||
- ($order_info['order_state'] == ORDER_STATE_REST) ||
- ($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['order_state'] == ORDER_STATE_DEPOSIT) ||
- ($order_info['order_state'] == ORDER_STATE_REST) ||
- ($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['order_state'] == ORDER_STATE_DEPOSIT || $order_info['order_state'] == ORDER_STATE_REST) && $order_info['payment_code'] != 'offline';
- break;
-
- case 'complain':
- $state = in_array($order_info['order_state'], array(ORDER_STATE_PAY, ORDER_STATE_SEND)) ||
- intval($order_info['finnshed_time']) > (TIMESTAMP - config('ds_config.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['refund_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;
- }
- return $state;
- }
-
- public function getOrderAndOrderGoodsList($condition, $field = '*', $pagesize = 0, $order = 'rec_id desc') {
- if($pagesize){
- $list = Db::name('ordergoods')->alias('order_goods')->where($condition)->field($field)->join('order order','order_goods.order_id=order.order_id','LEFT')->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
- $this->page_info = $list;
- return $list->items();
- }else{
- $list = Db::name('ordergoods')->alias('order_goods')->where($condition)->field($field)->join('order order','order_goods.order_id=order.order_id','LEFT')->select()->toArray();
- return $list;
- }
- }
-
- public function getOrderAndOrderGoodsSalesRecordList($condition, $field = "*", $pagesize = 0, $order = 'rec_id desc') {
- $condition[] = array('order_state','in', array(ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS));
- return $this->getOrderAndOrderGoodsList($condition, $field, $pagesize, $order);
- }
- }
|