order.model.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. /**
  3. * 订单管理
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class orderModel extends Model {
  11. /**
  12. * 取单条订单信息
  13. *
  14. * @param unknown_type $condition
  15. * @param array $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  16. * @return unknown
  17. */
  18. public function getOrderInfo($condition = array(), $extend = array(), $fields = '*', $order = '',$group = '') {
  19. $order_info = $this->table('order')->field($fields)->where($condition)->group($group)->order($order)->find();
  20. if (empty($order_info)) {
  21. return array();
  22. }
  23. if (isset($order_info['order_state'])) {
  24. $order_info['state_desc'] = orderState($order_info);
  25. }
  26. if (isset($order_info['payment_code'])) {
  27. $order_info['payment_name'] = orderPaymentName($order_info['payment_code']);
  28. }
  29. //追加返回订单扩展表信息
  30. if (in_array('order_common',$extend)) {
  31. $order_info['extend_order_common'] = $this->getOrderCommonInfo(array('order_id'=>$order_info['order_id']));
  32. $order_info['extend_order_common']['reciver_info'] = unserialize($order_info['extend_order_common']['reciver_info']);
  33. $order_info['extend_order_common']['invoice_info'] = unserialize($order_info['extend_order_common']['invoice_info']);
  34. }
  35. //追加返回店铺信息
  36. if (in_array('store',$extend)) {
  37. $order_info['extend_store'] = Model('store')->getStoreInfo(array('store_id'=>$order_info['store_id']));
  38. }
  39. //返回买家信息
  40. if (in_array('member',$extend)) {
  41. $order_info['extend_member'] = Model('member')->getMemberInfoByID($order_info['buyer_id']);
  42. }
  43. //追加返回商品信息
  44. if (in_array('order_goods',$extend)) {
  45. //取商品列表
  46. $order_goods_list = $this->getOrderGoodsList(array('order_id'=>$order_info['order_id']));
  47. $order_info['extend_order_goods'] = $order_goods_list;
  48. }
  49. return $order_info;
  50. }
  51. public function getOrderCommonInfo($condition = array(), $field = '*') {
  52. return $this->table('order_common')->where($condition)->find();
  53. }
  54. public function getOrderPayInfo($condition = array(), $master = false) {
  55. return $this->table('order_pay')->where($condition)->master($master)->find();
  56. }
  57. /**
  58. * 取得支付单列表
  59. *
  60. * @param unknown_type $condition
  61. * @param unknown_type $pagesize
  62. * @param unknown_type $filed
  63. * @param unknown_type $order
  64. * @param string $key 以哪个字段作为下标,这里一般指pay_id
  65. * @return unknown
  66. */
  67. public function getOrderPayList($condition, $pagesize = '', $filed = '*', $order = '', $key = '') {
  68. return $this->table('order_pay')->field($filed)->where($condition)->order($order)->page($pagesize)->key($key)->select();
  69. }
  70. /**
  71. * 取得订单列表(未被删除)
  72. * @param unknown $condition
  73. * @param string $pagesize
  74. * @param string $field
  75. * @param string $order
  76. * @param string $limit
  77. * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  78. * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
  79. */
  80. public function getNormalOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '', $extend = array()){
  81. $condition['delete_state'] = 0;
  82. return $this->getOrderList($condition, $pagesize, $field, $order, $limit, $extend);
  83. }
  84. /**
  85. * 取得订单列表(所有)
  86. * @param unknown $condition
  87. * @param string $pagesize
  88. * @param string $field
  89. * @param string $order
  90. * @param string $limit
  91. * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  92. * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
  93. */
  94. public function getOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '', $extend = array(), $master = false){
  95. $list = $this->table('order')->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
  96. if (empty($list)) return array();
  97. $order_list = array();
  98. foreach ($list as $order) {
  99. if (isset($order['order_state'])) {
  100. $order['state_desc'] = orderState($order);
  101. }
  102. if (isset($order['payment_code'])) {
  103. $order['payment_name'] = orderPaymentName($order['payment_code']);
  104. }
  105. if (!empty($extend)) $order_list[$order['order_id']] = $order;
  106. }
  107. if (empty($order_list)) $order_list = $list;
  108. //追加返回订单扩展表信息
  109. if (in_array('order_common',$extend)) {
  110. $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
  111. foreach ($order_common_list as $value) {
  112. $order_list[$value['order_id']]['extend_order_common'] = $value;
  113. $order_list[$value['order_id']]['extend_order_common']['reciver_info'] = @unserialize($value['reciver_info']);
  114. $order_list[$value['order_id']]['extend_order_common']['invoice_info'] = @unserialize($value['invoice_info']);
  115. }
  116. }
  117. //追加返回店铺信息
  118. if (in_array('store',$extend)) {
  119. $store_id_array = array();
  120. foreach ($order_list as $value) {
  121. if (!in_array($value['store_id'],$store_id_array)) $store_id_array[] = $value['store_id'];
  122. }
  123. $store_list = Model('store')->getStoreList(array('store_id'=>array('in',$store_id_array)));
  124. $store_new_list = array();
  125. foreach ($store_list as $store) {
  126. $store_new_list[$store['store_id']] = $store;
  127. }
  128. foreach ($order_list as $order_id => $order) {
  129. $order_list[$order_id]['extend_store'] = $store_new_list[$order['store_id']];
  130. }
  131. }
  132. //追加返回买家信息
  133. if (in_array('member',$extend)) {
  134. foreach ($order_list as $order_id => $order) {
  135. $order_list[$order_id]['extend_member'] = Model('member')->getMemberInfoByID($order['buyer_id']);
  136. }
  137. }
  138. //追加返回商品信息
  139. if (in_array('order_goods',$extend)) {
  140. //取商品列表
  141. $order_goods_list = $this->getOrderGoodsList(array('order_id'=>array('in',array_keys($order_list))));
  142. if (!empty($order_goods_list)) {
  143. foreach ($order_goods_list as $value) {
  144. $order_list[$value['order_id']]['extend_order_goods'][] = $value;
  145. }
  146. } else {
  147. $order_list[$value['order_id']]['extend_order_goods'] = array();
  148. }
  149. }
  150. if (in_array('order_address',$extend)) {
  151. $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
  152. foreach ($order_common_list as $value) {
  153. $order_list[$value['order_id']]['reciver_info'] = @unserialize($value['reciver_info']);
  154. $order_list[$value['order_id']]['reciver_name'] = $value['reciver_name'];
  155. }
  156. }
  157. return $order_list;
  158. }
  159. /**
  160. * 取得(买/卖家)订单某个数量缓存
  161. * @param string $type 买/卖家标志,允许传入 buyer、store
  162. * @param int $id 买家ID、店铺ID
  163. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  164. * @return array
  165. */
  166. public function getOrderCountCache($type, $id, $key) {
  167. if (!C('cache_open')) return array();
  168. $type = 'ordercount'.$type;
  169. $ins = Cache::getInstance('cacheredis');
  170. $order_info = $ins->hget($id,$type,$key);
  171. return !is_array($order_info) ? array($key => $order_info) : $order_info;
  172. }
  173. /**
  174. * 设置(买/卖家)订单某个数量缓存
  175. * @param string $type 买/卖家标志,允许传入 buyer、store
  176. * @param int $id 买家ID、店铺ID
  177. * @param array $data
  178. */
  179. public function editOrderCountCache($type, $id, $data) {
  180. if (!C('cache_open') || empty($type) || !intval($id) || !is_array($data)) return ;
  181. $ins = Cache::getInstance('cacheredis');
  182. $type = 'ordercount'.$type;
  183. $ins->hset($id,$type,$data);
  184. }
  185. /**
  186. * 取得买卖家订单数量某个缓存
  187. * @param string $type $type 买/卖家标志,允许传入 buyer、store
  188. * @param int $id 买家ID、店铺ID
  189. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  190. * @return int
  191. */
  192. public function getOrderCountByID($type, $id, $key) {
  193. $cache_info = $this->getOrderCountCache($type, $id, $key);
  194. if (is_string($cache_info[$key])) {
  195. //从缓存中取得
  196. $count = $cache_info[$key];
  197. } else {
  198. //从数据库中取得
  199. $field = $type == 'buyer' ? 'buyer_id' : 'store_id';
  200. $condition = array($field => $id);
  201. $func = 'getOrderState'.$key;
  202. $count = $this->$func($condition);
  203. $this->editOrderCountCache($type,$id,array($key => $count));
  204. }
  205. return $count;
  206. }
  207. /**
  208. * 删除(买/卖家)订单全部数量缓存
  209. * @param string $type 买/卖家标志,允许传入 buyer、store
  210. * @param int $id 买家ID、店铺ID
  211. * @return bool
  212. */
  213. public function delOrderCountCache($type, $id) {
  214. if (!C('cache_open')) return true;
  215. $ins = Cache::getInstance('cacheredis');
  216. $type = 'ordercount'.$type;
  217. return $ins->hdel($id,$type);
  218. }
  219. /**
  220. * 待付款订单数量
  221. * @param unknown $condition
  222. */
  223. public function getOrderStateNewCount($condition = array()) {
  224. $condition['order_state'] = ORDER_STATE_NEW;
  225. return $this->getOrderCount($condition);
  226. }
  227. /**
  228. * 待发货订单数量
  229. * @param unknown $condition
  230. */
  231. public function getOrderStatePayCount($condition = array()) {
  232. $condition['order_state'] = ORDER_STATE_PAY;
  233. return $this->getOrderCount($condition);
  234. }
  235. /**
  236. * 待收货订单数量
  237. * @param unknown $condition
  238. */
  239. public function getOrderStateSendCount($condition = array()) {
  240. $condition['order_state'] = ORDER_STATE_SEND;
  241. return $this->getOrderCount($condition);
  242. }
  243. /**
  244. * 待评价订单数量
  245. * @param unknown $condition
  246. */
  247. public function getOrderStateEvalCount($condition = array()) {
  248. $condition['order_state'] = ORDER_STATE_SUCCESS;
  249. $condition['evaluation_state'] = 0;
  250. return $this->getOrderCount($condition);
  251. }
  252. /**
  253. * 交易中的订单数量
  254. * @param unknown $condition
  255. */
  256. public function getOrderStateTradeCount($condition = array()) {
  257. $condition['order_state'] = array(array('neq',ORDER_STATE_CANCEL),array('neq',ORDER_STATE_SUCCESS),'and');
  258. return $this->getOrderCount($condition);
  259. }
  260. /**
  261. * 取得订单数量
  262. * @param unknown $condition
  263. */
  264. public function getOrderCount($condition) {
  265. return $this->table('order')->where($condition)->count();
  266. }
  267. /**
  268. * 取得订单商品表详细信息
  269. * @param unknown $condition
  270. * @param string $fields
  271. * @param string $order
  272. */
  273. public function getOrderGoodsInfo($condition = array(), $fields = '*', $order = '') {
  274. return $this->table('order_goods')->where($condition)->field($fields)->order($order)->find();
  275. }
  276. /**
  277. * 取得订单商品表列表
  278. * @param unknown $condition
  279. * @param string $fields
  280. * @param string $limit
  281. * @param string $page
  282. * @param string $order
  283. * @param string $group
  284. * @param string $key
  285. */
  286. public function getOrderGoodsList($condition = array(), $fields = '*', $limit = null, $page = null, $order = 'rec_id desc', $group = null, $key = null) {
  287. return $this->table('order_goods')->field($fields)->where($condition)->limit($limit)->order($order)->group($group)->key($key)->page($page)->select();
  288. }
  289. /**
  290. * 取得订单扩展表列表
  291. * @param unknown $condition
  292. * @param string $fields
  293. * @param string $limit
  294. */
  295. public function getOrderCommonList($condition = array(), $fields = '*', $order = '', $limit = null) {
  296. return $this->table('order_common')->field($fields)->where($condition)->order($order)->limit($limit)->select();
  297. }
  298. /**
  299. * 插入订单支付表信息
  300. * @param array $data
  301. * @return int 返回 insert_id
  302. */
  303. public function addOrderPay($data) {
  304. return $this->table('order_pay')->insert($data);
  305. }
  306. /**
  307. * 插入订单表信息
  308. * @param array $data
  309. * @return int 返回 insert_id
  310. */
  311. public function addOrder($data) {
  312. $insert = $this->table('order')->insert($data);
  313. if ($insert) {
  314. //更新缓存
  315. QueueClient::push('delOrderCountCache',array('buyer_id'=>$data['buyer_id'],'store_id'=>$data['store_id']));
  316. }
  317. return $insert;
  318. }
  319. /**
  320. * 插入订单扩展表信息
  321. * @param array $data
  322. * @return int 返回 insert_id
  323. */
  324. public function addOrderCommon($data) {
  325. return $this->table('order_common')->insert($data);
  326. }
  327. /**
  328. * 插入订单扩展表信息
  329. * @param array $data
  330. * @return int 返回 insert_id
  331. */
  332. public function addOrderGoods($data) {
  333. return $this->table('order_goods')->insertAll($data);
  334. }
  335. /**
  336. * 添加订单日志
  337. */
  338. public function addOrderLog($data) {
  339. $data['log_role'] = str_replace(array('buyer','seller','system','admin'),array('买家','商家','系统','管理员'), $data['log_role']);
  340. $data['log_time'] = TIMESTAMP;
  341. return $this->table('order_log')->insert($data);
  342. }
  343. /**
  344. * 更改订单信息
  345. *
  346. * @param unknown_type $data
  347. * @param unknown_type $condition
  348. */
  349. public function editOrder($data,$condition,$limit = '') {
  350. $update = $this->table('order')->where($condition)->limit($limit)->update($data);
  351. if ($update) {
  352. //更新缓存
  353. QueueClient::push('delOrderCountCache',$condition);
  354. }
  355. return $update;
  356. }
  357. /**
  358. * 更改订单信息
  359. *
  360. * @param unknown_type $data
  361. * @param unknown_type $condition
  362. */
  363. public function editOrderCommon($data,$condition) {
  364. return $this->table('order_common')->where($condition)->update($data);
  365. }
  366. /**
  367. * 更改订单信息
  368. *
  369. * @param unknown_type $data
  370. * @param unknown_type $condition
  371. */
  372. public function editOrderGoods($data,$condition) {
  373. return $this->table('order_goods')->where($condition)->update($data);
  374. }
  375. /**
  376. * 更改订单支付信息
  377. *
  378. * @param unknown_type $data
  379. * @param unknown_type $condition
  380. */
  381. public function editOrderPay($data,$condition) {
  382. return $this->table('order_pay')->where($condition)->update($data);
  383. }
  384. /**
  385. * 订单操作历史列表
  386. * @param unknown $order_id
  387. * @return Ambigous <multitype:, unknown>
  388. */
  389. public function getOrderLogList($condition) {
  390. return $this->table('order_log')->where($condition)->select();
  391. }
  392. /**
  393. * 取得单条订单操作记录
  394. * @param unknown $condition
  395. * @param string $order
  396. */
  397. public function getOrderLogInfo($condition = array(), $order = '') {
  398. return $this->table('order_log')->where($condition)->order($order)->find();
  399. }
  400. /**
  401. * 返回是否允许某些操作
  402. * @param unknown $operate
  403. * @param unknown $order_info
  404. */
  405. public function getOrderOperateState($operate,$order_info){
  406. if (!is_array($order_info) || empty($order_info)) return false;
  407. switch ($operate) {
  408. //买家取消订单
  409. case 'buyer_cancel':
  410. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  411. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  412. break;
  413. //申请退款
  414. case 'refund_cancel':
  415. $state = $order_info['refund'] == 1 && !intval($order_info['lock_state']);
  416. break;
  417. //商家取消订单
  418. case 'store_cancel':
  419. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  420. ($order_info['payment_code'] == 'offline' &&
  421. in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)));
  422. break;
  423. //平台取消订单
  424. case 'system_cancel':
  425. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  426. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  427. break;
  428. //平台收款
  429. case 'system_receive_pay':
  430. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  431. break;
  432. //买家投诉
  433. case 'complain':
  434. $state = in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)) ||
  435. intval($order_info['finnshed_time']) > (TIMESTAMP - C('complain_time_limit'));
  436. break;
  437. case 'payment':
  438. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  439. break;
  440. //调整运费
  441. case 'modify_price':
  442. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  443. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  444. $state = floatval($order_info['shipping_fee']) > 0 && $state;
  445. break;
  446. //调整商品价格
  447. case 'spay_price':
  448. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  449. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  450. $state = floatval($order_info['goods_amount']) > 0 && $state;
  451. break;
  452. //发货
  453. case 'send':
  454. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_PAY;
  455. break;
  456. //收货
  457. case 'receive':
  458. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_SEND;
  459. break;
  460. //评价
  461. case 'evaluation':
  462. $state = !$order_info['lock_state'] && !$order_info['evaluation_state'] && $order_info['order_state'] == ORDER_STATE_SUCCESS;
  463. break;
  464. //锁定
  465. case 'lock':
  466. $state = intval($order_info['lock_state']) ? true : false;
  467. break;
  468. //快递跟踪
  469. case 'deliver':
  470. $state = !empty($order_info['shipping_code']) && in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS));
  471. break;
  472. //放入回收站
  473. case 'delete':
  474. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 0;
  475. break;
  476. //永久删除、从回收站还原
  477. case 'drop':
  478. case 'restore':
  479. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 1;
  480. break;
  481. //分享
  482. case 'share':
  483. $state = true;
  484. break;
  485. }
  486. return $state;
  487. }
  488. /**
  489. * 联查订单表订单商品表
  490. *
  491. * @param array $condition
  492. * @param string $field
  493. * @param number $page
  494. * @param string $order
  495. * @return array
  496. */
  497. public function getOrderAndOrderGoodsList($condition, $field = '*', $page = 0, $order = 'rec_id desc') {
  498. 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();
  499. }
  500. /**
  501. * 订单销售记录 订单状态为20、30、40时
  502. * @param unknown $condition
  503. * @param string $field
  504. * @param number $page
  505. * @param string $order
  506. */
  507. public function getOrderAndOrderGoodsSalesRecordList($condition, $field="*", $page = 0, $order = 'rec_id desc') {
  508. $condition['order_state'] = array('in', array(ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS));
  509. return $this->getOrderAndOrderGoodsList($condition, $field, $page, $order);
  510. }
  511. }