order.model.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. return $order_list;
  151. }
  152. /**
  153. * 取得(买/卖家)订单某个数量缓存
  154. * @param string $type 买/卖家标志,允许传入 buyer、store
  155. * @param int $id 买家ID、店铺ID
  156. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  157. * @return array
  158. */
  159. public function getOrderCountCache($type, $id, $key) {
  160. if (!C('cache_open')) return array();
  161. $type = 'ordercount'.$type;
  162. $ins = Cache::getInstance('cacheredis');
  163. $order_info = $ins->hget($id,$type,$key);
  164. return !is_array($order_info) ? array($key => $order_info) : $order_info;
  165. }
  166. /**
  167. * 设置(买/卖家)订单某个数量缓存
  168. * @param string $type 买/卖家标志,允许传入 buyer、store
  169. * @param int $id 买家ID、店铺ID
  170. * @param array $data
  171. */
  172. public function editOrderCountCache($type, $id, $data) {
  173. if (!C('cache_open') || empty($type) || !intval($id) || !is_array($data)) return ;
  174. $ins = Cache::getInstance('cacheredis');
  175. $type = 'ordercount'.$type;
  176. $ins->hset($id,$type,$data);
  177. }
  178. /**
  179. * 取得买卖家订单数量某个缓存
  180. * @param string $type $type 买/卖家标志,允许传入 buyer、store
  181. * @param int $id 买家ID、店铺ID
  182. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  183. * @return int
  184. */
  185. public function getOrderCountByID($type, $id, $key) {
  186. $cache_info = $this->getOrderCountCache($type, $id, $key);
  187. if (is_string($cache_info[$key])) {
  188. //从缓存中取得
  189. $count = $cache_info[$key];
  190. } else {
  191. //从数据库中取得
  192. $field = $type == 'buyer' ? 'buyer_id' : 'store_id';
  193. $condition = array($field => $id);
  194. $func = 'getOrderState'.$key;
  195. $count = $this->$func($condition);
  196. $this->editOrderCountCache($type,$id,array($key => $count));
  197. }
  198. return $count;
  199. }
  200. /**
  201. * 删除(买/卖家)订单全部数量缓存
  202. * @param string $type 买/卖家标志,允许传入 buyer、store
  203. * @param int $id 买家ID、店铺ID
  204. * @return bool
  205. */
  206. public function delOrderCountCache($type, $id) {
  207. if (!C('cache_open')) return true;
  208. $ins = Cache::getInstance('cacheredis');
  209. $type = 'ordercount'.$type;
  210. return $ins->hdel($id,$type);
  211. }
  212. /**
  213. * 待付款订单数量
  214. * @param unknown $condition
  215. */
  216. public function getOrderStateNewCount($condition = array()) {
  217. $condition['order_state'] = ORDER_STATE_NEW;
  218. return $this->getOrderCount($condition);
  219. }
  220. /**
  221. * 待发货订单数量
  222. * @param unknown $condition
  223. */
  224. public function getOrderStatePayCount($condition = array()) {
  225. $condition['order_state'] = ORDER_STATE_PAY;
  226. return $this->getOrderCount($condition);
  227. }
  228. /**
  229. * 待收货订单数量
  230. * @param unknown $condition
  231. */
  232. public function getOrderStateSendCount($condition = array()) {
  233. $condition['order_state'] = ORDER_STATE_SEND;
  234. return $this->getOrderCount($condition);
  235. }
  236. /**
  237. * 待评价订单数量
  238. * @param unknown $condition
  239. */
  240. public function getOrderStateEvalCount($condition = array()) {
  241. $condition['order_state'] = ORDER_STATE_SUCCESS;
  242. $condition['evaluation_state'] = 0;
  243. return $this->getOrderCount($condition);
  244. }
  245. /**
  246. * 交易中的订单数量
  247. * @param unknown $condition
  248. */
  249. public function getOrderStateTradeCount($condition = array()) {
  250. $condition['order_state'] = array(array('neq',ORDER_STATE_CANCEL),array('neq',ORDER_STATE_SUCCESS),'and');
  251. return $this->getOrderCount($condition);
  252. }
  253. /**
  254. * 取得订单数量
  255. * @param unknown $condition
  256. */
  257. public function getOrderCount($condition) {
  258. return $this->table('order')->where($condition)->count();
  259. }
  260. /**
  261. * 取得订单商品表详细信息
  262. * @param unknown $condition
  263. * @param string $fields
  264. * @param string $order
  265. */
  266. public function getOrderGoodsInfo($condition = array(), $fields = '*', $order = '') {
  267. return $this->table('order_goods')->where($condition)->field($fields)->order($order)->find();
  268. }
  269. /**
  270. * 取得订单商品表列表
  271. * @param unknown $condition
  272. * @param string $fields
  273. * @param string $limit
  274. * @param string $page
  275. * @param string $order
  276. * @param string $group
  277. * @param string $key
  278. */
  279. public function getOrderGoodsList($condition = array(), $fields = '*', $limit = null, $page = null, $order = 'rec_id desc', $group = null, $key = null) {
  280. return $this->table('order_goods')->field($fields)->where($condition)->limit($limit)->order($order)->group($group)->key($key)->page($page)->select();
  281. }
  282. /**
  283. * 取得订单扩展表列表
  284. * @param unknown $condition
  285. * @param string $fields
  286. * @param string $limit
  287. */
  288. public function getOrderCommonList($condition = array(), $fields = '*', $order = '', $limit = null) {
  289. return $this->table('order_common')->field($fields)->where($condition)->order($order)->limit($limit)->select();
  290. }
  291. /**
  292. * 插入订单支付表信息
  293. * @param array $data
  294. * @return int 返回 insert_id
  295. */
  296. public function addOrderPay($data) {
  297. return $this->table('order_pay')->insert($data);
  298. }
  299. /**
  300. * 插入订单表信息
  301. * @param array $data
  302. * @return int 返回 insert_id
  303. */
  304. public function addOrder($data) {
  305. $insert = $this->table('order')->insert($data);
  306. if ($insert) {
  307. //更新缓存
  308. QueueClient::push('delOrderCountCache',array('buyer_id'=>$data['buyer_id'],'store_id'=>$data['store_id']));
  309. }
  310. return $insert;
  311. }
  312. /**
  313. * 插入订单扩展表信息
  314. * @param array $data
  315. * @return int 返回 insert_id
  316. */
  317. public function addOrderCommon($data) {
  318. return $this->table('order_common')->insert($data);
  319. }
  320. /**
  321. * 插入订单扩展表信息
  322. * @param array $data
  323. * @return int 返回 insert_id
  324. */
  325. public function addOrderGoods($data) {
  326. return $this->table('order_goods')->insertAll($data);
  327. }
  328. /**
  329. * 添加订单日志
  330. */
  331. public function addOrderLog($data) {
  332. $data['log_role'] = str_replace(array('buyer','seller','system','admin'),array('买家','商家','系统','管理员'), $data['log_role']);
  333. $data['log_time'] = TIMESTAMP;
  334. return $this->table('order_log')->insert($data);
  335. }
  336. /**
  337. * 更改订单信息
  338. *
  339. * @param unknown_type $data
  340. * @param unknown_type $condition
  341. */
  342. public function editOrder($data,$condition,$limit = '') {
  343. $update = $this->table('order')->where($condition)->limit($limit)->update($data);
  344. if ($update) {
  345. //更新缓存
  346. QueueClient::push('delOrderCountCache',$condition);
  347. }
  348. return $update;
  349. }
  350. /**
  351. * 更改订单信息
  352. *
  353. * @param unknown_type $data
  354. * @param unknown_type $condition
  355. */
  356. public function editOrderCommon($data,$condition) {
  357. return $this->table('order_common')->where($condition)->update($data);
  358. }
  359. /**
  360. * 更改订单信息
  361. *
  362. * @param unknown_type $data
  363. * @param unknown_type $condition
  364. */
  365. public function editOrderGoods($data,$condition) {
  366. return $this->table('order_goods')->where($condition)->update($data);
  367. }
  368. /**
  369. * 更改订单支付信息
  370. *
  371. * @param unknown_type $data
  372. * @param unknown_type $condition
  373. */
  374. public function editOrderPay($data,$condition) {
  375. return $this->table('order_pay')->where($condition)->update($data);
  376. }
  377. /**
  378. * 订单操作历史列表
  379. * @param unknown $order_id
  380. * @return Ambigous <multitype:, unknown>
  381. */
  382. public function getOrderLogList($condition) {
  383. return $this->table('order_log')->where($condition)->select();
  384. }
  385. /**
  386. * 取得单条订单操作记录
  387. * @param unknown $condition
  388. * @param string $order
  389. */
  390. public function getOrderLogInfo($condition = array(), $order = '') {
  391. return $this->table('order_log')->where($condition)->order($order)->find();
  392. }
  393. /**
  394. * 返回是否允许某些操作
  395. * @param unknown $operate
  396. * @param unknown $order_info
  397. */
  398. public function getOrderOperateState($operate,$order_info){
  399. if (!is_array($order_info) || empty($order_info)) return false;
  400. switch ($operate) {
  401. //买家取消订单
  402. case 'buyer_cancel':
  403. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  404. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  405. break;
  406. //申请退款
  407. case 'refund_cancel':
  408. $state = $order_info['refund'] == 1 && !intval($order_info['lock_state']);
  409. break;
  410. //商家取消订单
  411. case 'store_cancel':
  412. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  413. ($order_info['payment_code'] == 'offline' &&
  414. in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)));
  415. break;
  416. //平台取消订单
  417. case 'system_cancel':
  418. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  419. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  420. break;
  421. //平台收款
  422. case 'system_receive_pay':
  423. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  424. break;
  425. //买家投诉
  426. case 'complain':
  427. $state = in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)) ||
  428. intval($order_info['finnshed_time']) > (TIMESTAMP - C('complain_time_limit'));
  429. break;
  430. case 'payment':
  431. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  432. break;
  433. //调整运费
  434. case 'modify_price':
  435. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  436. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  437. $state = floatval($order_info['shipping_fee']) > 0 && $state;
  438. break;
  439. //调整商品价格
  440. case 'spay_price':
  441. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  442. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  443. $state = floatval($order_info['goods_amount']) > 0 && $state;
  444. break;
  445. //发货
  446. case 'send':
  447. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_PAY;
  448. break;
  449. //收货
  450. case 'receive':
  451. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_SEND;
  452. break;
  453. //评价
  454. case 'evaluation':
  455. $state = !$order_info['lock_state'] && !$order_info['evaluation_state'] && $order_info['order_state'] == ORDER_STATE_SUCCESS;
  456. break;
  457. //锁定
  458. case 'lock':
  459. $state = intval($order_info['lock_state']) ? true : false;
  460. break;
  461. //快递跟踪
  462. case 'deliver':
  463. $state = !empty($order_info['shipping_code']) && in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS));
  464. break;
  465. //放入回收站
  466. case 'delete':
  467. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 0;
  468. break;
  469. //永久删除、从回收站还原
  470. case 'drop':
  471. case 'restore':
  472. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 1;
  473. break;
  474. //分享
  475. case 'share':
  476. $state = true;
  477. break;
  478. }
  479. return $state;
  480. }
  481. /**
  482. * 联查订单表订单商品表
  483. *
  484. * @param array $condition
  485. * @param string $field
  486. * @param number $page
  487. * @param string $order
  488. * @return array
  489. */
  490. public function getOrderAndOrderGoodsList($condition, $field = '*', $page = 0, $order = 'rec_id desc') {
  491. 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();
  492. }
  493. /**
  494. * 订单销售记录 订单状态为20、30、40时
  495. * @param unknown $condition
  496. * @param string $field
  497. * @param number $page
  498. * @param string $order
  499. */
  500. public function getOrderAndOrderGoodsSalesRecordList($condition, $field="*", $page = 0, $order = 'rec_id desc') {
  501. $condition['order_state'] = array('in', array(ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS));
  502. return $this->getOrderAndOrderGoodsList($condition, $field, $page, $order);
  503. }
  504. }