VoucherOrderController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace addons\Happiness\client\controllers;
  3. use addons\Happiness\common\enums\HappinessPensionEnum;
  4. use addons\Happiness\common\enums\HappinessVoucherEnum;
  5. use addons\Happiness\common\models\HappinessPension;
  6. use addons\Happiness\common\models\HappinessStore;
  7. use addons\Happiness\common\models\HappinessVoucherExpress;
  8. use addons\Happiness\common\models\HappinessVoucherExpressGoods;
  9. use addons\Happiness\common\models\HappinessVoucherGoods;
  10. use addons\Happiness\common\models\HappinessVoucherOrder;
  11. use addons\Happiness\common\service\VoucherService;
  12. use common\enums\StatusEnum;
  13. use common\models\goods\Goods;
  14. use common\models\goods\GoodsAttr;
  15. use Yii;
  16. class VoucherOrderController extends BaseController
  17. {
  18. public function actionIndex()
  19. {
  20. if(Yii::$app->request->isAjax)
  21. {
  22. if (\Yii::$app->request->isGet)
  23. {
  24. $page = \Yii::$app->request->get('page', 1);
  25. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  26. $start_time=\Yii::$app->request->get('start_time');
  27. $end_time=\Yii::$app->request->get('end_time');
  28. $status=\Yii::$app->request->get('status');
  29. $wheres = [];
  30. $wheres[]=['=', 'status', StatusEnum::ENABLED];
  31. $wheres[]=['=', 'mall_id', $this->mall_id];
  32. $wheres[]=['=', 'store_id', $this->store_id];
  33. if(!empty($start_time))
  34. {
  35. $wheres[] = ['>=','created_at', strtotime($start_time)];
  36. }
  37. if(!empty($end_time))
  38. {
  39. $wheres[] = ['<=','created_at', strtotime($end_time)+86400];
  40. }
  41. if(!empty($status))
  42. {
  43. $wheres[] = ['=','order_status', $status];
  44. }
  45. $params = [
  46. 'select' => '*',
  47. 'where' => $wheres,
  48. 'order' => 'created_at desc',
  49. 'page' => $page,
  50. 'limit' => $limit,
  51. 'with' => [
  52. 'store'=>function ($query) {
  53. $query->select('id,store_name');
  54. }
  55. ],
  56. ];
  57. $list = HappinessVoucherOrder::listPage($params);
  58. if (false === $list) {
  59. return $this->error(HappinessVoucherOrder::getStaticError());
  60. }
  61. if(!empty($list['list']))
  62. {
  63. foreach ($list['list'] as &$item)
  64. {
  65. $item['order_status_text'] = HappinessVoucherEnum::getOrderStatusMap()[$item['order_status']];
  66. $item['order_no']=$item['order_no'].' ';
  67. }
  68. }
  69. return $this->success('操作成功',$list);
  70. }
  71. }
  72. return $this->render('index');
  73. }
  74. public function actionConfirm()
  75. {
  76. if(Yii::$app->request->isAjax)
  77. {
  78. $id = \Yii::$app->request->post('id');
  79. $model = HappinessVoucherOrder::find()->where(['id'=>$id,'mall_id'=>$this->mall_id,'store_id'=>$this->store_id,'status'=>StatusEnum::ENABLED])->one();
  80. if(!$model)
  81. {
  82. return $this->error('订单不存在');
  83. }
  84. $service=new VoucherService(['mall_id'=>$this->mall_id,'store_id'=>$this->store_id]);
  85. $result = $service->confirm($model);
  86. if($result==false)
  87. {
  88. return $this->error($service->getError());
  89. }
  90. return $this->success('操作成功');
  91. }
  92. }
  93. public function actionExpress()
  94. {
  95. if(Yii::$app->request->isAjax)
  96. {
  97. if (\Yii::$app->request->isGet)
  98. {
  99. $id = Yii::$app->request->get('id');
  100. $model = HappinessVoucherOrder::find()->where(['id'=>$id,'mall_id'=>$this->mall_id,'store_id'=>$this->store_id,'status'=>StatusEnum::ENABLED])->one();
  101. if(!$model)
  102. return $this->error('订单不存在');
  103. $list = HappinessVoucherExpress::find()->with(['goods'])->where(['order_id' => $id,'mall_id' =>
  104. $this->mall_id])
  105. ->asArray()->all();
  106. $goods_ids=[];
  107. $goods_attr_ids=[];
  108. foreach ($list as &$item)
  109. {
  110. foreach ($item['goods'] as &$goods)
  111. {
  112. $goods_ids[]=$goods['goods_id'];
  113. $goods_attr_ids[]=$goods['attr_id'];
  114. }
  115. }
  116. $goods_list=Goods::find()->where(['id' => $goods_ids,'mall_id' =>
  117. $this->mall_id])
  118. ->asArray()
  119. ->indexBy('id')
  120. ->all();
  121. $goods_attr_list=GoodsAttr::find()->where(['id' => $goods_attr_ids])
  122. ->asArray()
  123. ->indexBy('id')
  124. ->all();
  125. $voucherGoods=HappinessVoucherGoods::find()->where(['order_id' => $id,'mall_id' =>
  126. $this->mall_id])
  127. ->asArray()
  128. ->indexBy('attr_id')
  129. ->all();
  130. foreach ($list as &$item)
  131. {
  132. foreach ($item['goods'] as &$goods)
  133. {
  134. $goods['buy_num']=$voucherGoods[$goods['attr_id']]['num'];
  135. $goods['goods_name']=$goods_list[$goods['goods_id']]['goods_name'];
  136. $goods['goods_attr_name']=$goods_attr_list[$goods['attr_id']]['name'];
  137. $goods['cover_pic']=empty($goods_attr_list[$goods['attr_id']]['pic_url'])?$goods_list[$goods['goods_id']]['cover_pic']:$goods_attr_list[$goods['attr_id']]['pic_url'];
  138. }
  139. }
  140. return $this->success('操作成功',$list);
  141. }
  142. }
  143. }
  144. public function actionDetail()
  145. {
  146. if(Yii::$app->request->isAjax)
  147. {
  148. if (\Yii::$app->request->isGet)
  149. {
  150. $id = Yii::$app->request->get('id');
  151. $model = HappinessVoucherOrder::find()->with(['goods'])->where(['id' => $id,'mall_id' =>
  152. $this->mall_id,'store_id'=>$this->store_id])
  153. ->asArray()
  154. ->one();
  155. if(!$model)
  156. {
  157. return $this->error('数据不存在');
  158. }
  159. $goods_attr_ids=array_column($model['goods'],'attr_id');
  160. $goods_ids=array_column($model['goods'],'goods_id');
  161. $goods_list=Goods::find()->where(['id' => $goods_ids,'mall_id' =>
  162. $this->mall_id])
  163. ->asArray()
  164. ->indexBy('id')
  165. ->all();
  166. $goods_attr_list=GoodsAttr::find()->where(['id' => $goods_attr_ids])
  167. ->asArray()
  168. ->indexBy('id')
  169. ->all();
  170. foreach ($model['goods'] as &$item)
  171. {
  172. $item['goods_name']=$goods_list[$item['goods_id']]['goods_name'];
  173. $item['goods_attr_name']=$goods_attr_list[$item['attr_id']]['name'];
  174. $item['cover_pic']=empty($goods_attr_list[$item['attr_id']]['pic_url'])?$goods_list[$item['goods_id']]['cover_pic']:$goods_attr_list[$item['attr_id']]['pic_url'];
  175. }
  176. return $this->success('操作成功',$model);
  177. }
  178. }
  179. }
  180. }