OrderController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. class OrderController extends AdminController{
  3. public function actionCount() {
  4. header('Content-Type: application/json; charset=utf-8');
  5. $mongo = new MongoClient(DB_CONNETC);
  6. $db = $mongo->fuwu;
  7. $collection = $db->selectCollection('orders');
  8. $pipleline = array(
  9. array(
  10. '$match' => array(
  11. 'user' => array('$nin' => array()),
  12. ),
  13. ),
  14. array(
  15. '$group' => array(
  16. '_id' => '$user',
  17. 'count' => array('$sum' => 1)
  18. ),
  19. ),
  20. array(
  21. '$sort' => array(
  22. 'count' => -1
  23. ),
  24. ),
  25. array(
  26. '$limit' => 60
  27. ),
  28. );
  29. $a = $collection->aggregate($pipleline);
  30. $result = $a['result'];
  31. foreach ($result as $key => &$row) {
  32. $user = RUser::get($row['_id']);
  33. $row['user_name'] = $user->user_name;
  34. $row['No'] = $key + 1;
  35. }
  36. echo json_encode($result);
  37. }
  38. public function actionIndex()
  39. {
  40. $status_option = Order::$status_option;
  41. $status = CommonFn::getComboboxData($status_option, 100, true, 100);
  42. $this->render('index', array(
  43. 'status' => $status
  44. ));
  45. }
  46. public function actionList(){
  47. $params = CommonFn::getPageParams();
  48. $search = Yii::app()->request->getParam('search', '');
  49. $status = intval(Yii::app()->request->getParam('status', 100));
  50. $search = trim($search);
  51. if(preg_match('/\d{13,14}/',$search)){
  52. $res = Order::getOrderByOrderNu($search);
  53. if($res){
  54. $rows[] = $res;
  55. $parsedRows = Order::model()->parse($rows);
  56. echo CommonFn::composeDatagridData($parsedRows,1);
  57. }
  58. die();
  59. }
  60. $criteria = new EMongoCriteria($params);
  61. if($status!=100){
  62. $criteria->status('==', $status);
  63. }
  64. if ($search != ''){
  65. if (strlen($search) == 24){
  66. $criteria->addCond('user','==',new MongoId($search));
  67. }else{
  68. $user = RUser::getUserByName($search);
  69. if($user){
  70. $criteria->addCond('user','==',$user->_id);
  71. }
  72. }
  73. }
  74. $criteria->sort('time',EMongoCriteria::SORT_DESC);
  75. $cursor = Order::model()->findAll($criteria);
  76. $total = $cursor->count();
  77. $rows = CommonFn::getRowsFromCursor($cursor);
  78. $parsedRows = Order::model()->parse($rows);
  79. echo CommonFn::composeDatagridData($parsedRows, $total);
  80. }
  81. public function actionEdit(){
  82. $id = Yii::app()->request->getParam('id', '');
  83. $express_company = Yii::app()->request->getParam('express_company', '');
  84. $express_number = Yii::app()->request->getParam('express_number', '');
  85. $memo = Yii::app()->request->getParam('memo', '');
  86. $reason = Yii::app()->request->getParam('reason','');
  87. $status = intval(Yii::app()->request->getParam('status', 1));
  88. if($status == 100){
  89. CommonFn::requestAjax(false, '必须指定订单状态!');
  90. }
  91. if(!$id){
  92. CommonFn::requestAjax(false, '订单不存在');
  93. }else{
  94. $criteria = new EMongoCriteria();
  95. $criteria->_id = new MongoId($id);
  96. $order = Order::model()->find($criteria);
  97. if (empty($order)){
  98. CommonFn::requestAjax(false, '订单不存在');
  99. }
  100. }
  101. $order->express_company = $express_company;
  102. $order->express_number = $express_number;
  103. $order->memo = $memo;
  104. if(($order->status==0 || $order->status==3)&&$status == -2){
  105. $return_count = true;
  106. }elseif ($order->status == -2 && ($status == 0 || $status == 1 || $status==3) ) {
  107. $cut_count = true;
  108. }
  109. if(isset($cut_count)){
  110. foreach ($order->goods as $key => $value) {
  111. $goods = CommonFn::getObJ($value['goods_id'],"ZGoods");
  112. if($goods && $goods->count>=1){
  113. $goods->count -= 1;
  114. $goods->update(array('count',true));
  115. }else{
  116. CommonFn::requestAjax(false, '商品库存不足');
  117. }
  118. }
  119. }
  120. $order->status = $status;
  121. $success = $order->update(array('express_number','express_company','memo','status'),true);
  122. if($success&&isset($order->address['mobile'])){
  123. $order_info = Order::model()->parseRow($order);
  124. if($order->status == -2){
  125. //已取消
  126. CommonSMS::send('shop_order_cancel',array('order_num'=>$order_info['order_num'],'mobile'=>$order_info['address']['mobile']));
  127. }elseif ($order->status == 1){
  128. //已发货
  129. CommonSMS::send('shop_order_ship',array('express_company'=>$order_info['express_company'],'express_number'=>$order_info['express_number'],'mobile'=>$order_info['address']['mobile']));
  130. }
  131. }
  132. if($success&&isset($return_count)){
  133. foreach ($order->goods as $key => $value) {
  134. $goods = CommonFn::getObJ($value['goods_id'],"ZGoods");
  135. if($goods){
  136. $goods->count += 1;
  137. $goods->update(array('count',true));
  138. }
  139. }
  140. }
  141. CommonFn::requestAjax($success, '', array());
  142. }
  143. }