IndexController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace addons\Happiness\client\controllers;
  3. use addons\Happiness\common\models\HappinessGoodsRanking;
  4. use addons\Happiness\common\models\HappinessOrder;
  5. use addons\Happiness\common\models\HappinessStore;
  6. use addons\Happiness\common\models\HappinessStoreCensus;
  7. use addons\Happiness\common\models\HappinessStoreGoods;
  8. use addons\Happiness\common\models\HappinessStoreUser;
  9. use common\enums\OrderStatusEnum;
  10. use common\models\goods\Goods;
  11. use common\models\order\Order;
  12. use common\models\order\OrderRefund;
  13. use common\models\user\User;
  14. use Yii;
  15. class IndexController extends BaseController
  16. {
  17. /**
  18. * 门店首页(概况)
  19. * @Author bing
  20. * @DateTime 2022-04-26 18:06:27
  21. * @return void
  22. * @copyright: Copyright (c) 2022 广东七件事集团
  23. */
  24. public function actionIndex()
  25. {
  26. if (Yii::$app->request->isAjax) {
  27. $goods_ranking_list = HappinessGoodsRanking::lists([
  28. 'where' => [
  29. ['store_id' => $this->store_id],
  30. ['mall_id' => $this->mall_id]
  31. ],
  32. 'order' => 'sales_money desc',
  33. 'select' => 'id,goods_id,sales_money',
  34. 'limit' => 5
  35. ]);
  36. $goods_ids = array_column($goods_ranking_list, 'goods_id');
  37. $store_goods_list = HappinessStoreGoods::lists([
  38. 'alias' => 'yg',
  39. 'where' => [['yg.goods_id' => $goods_ids], ['yg.mall_id' => $this->mall_id]],
  40. 'index' => 'goods_id',
  41. 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = yg.goods_id"]],
  42. 'select' => 'yg.id,g.goods_name,g.cover_pic,yg.goods_id'
  43. ]);
  44. foreach ($goods_ranking_list as &$item) {
  45. $item['goods_name'] = $item['cover_pic'] = '';
  46. if (isset($store_goods_list[$item['goods_id']])) {
  47. $item['goods_name'] = $store_goods_list[$item['goods_id']]['goods_name'];
  48. $item['cover_pic'] = $store_goods_list[$item['goods_id']]['cover_pic'];
  49. }
  50. }
  51. $user_ranking_list = HappinessStoreUser::lists([
  52. 'where' => [
  53. ['store_id' => $this->store_id],
  54. ['mall_id' => $this->mall_id],
  55. ],
  56. 'order' => 'pay_money desc',
  57. 'select' => 'id,user_id,pay_money',
  58. 'limit' => 5
  59. ]);
  60. $user_ids = array_column($user_ranking_list, 'user_id');
  61. $user_list = User::lists(['where' => [['id' => $user_ids]], 'index' => 'id', 'select' => 'id,nickname,avatar_url']);
  62. foreach ($user_ranking_list as &$item) {
  63. $item['nickname'] = $item['avatar_url'] = '';
  64. if (isset($user_list[$item['user_id']])) {
  65. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  66. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  67. }
  68. }
  69. $time = time();
  70. $today = date('Y-m-d', $time);
  71. $yesterday = date("Y-m-d", strtotime("-1 day"));
  72. $date_time_arr = [$today, $yesterday];
  73. for ($i = 2; $i <= 4; $i++) {
  74. $date_time_arr[] = date("Y-m-d", strtotime("-{$i} day"));
  75. }
  76. $today_data = $yesterday_data = [
  77. 'add_user_nums'=>'--',
  78. 'order_nums'=>'--',
  79. 'settlement_commission'=>'--',
  80. 'sales_money'=>'--',
  81. 'cashier_sales_money'=>'--',
  82. ];
  83. $store_census_list = HappinessStoreCensus::lists(['where' => [['store_id' => $this->store_id], ['date_time' => $date_time_arr]], 'order' => 'created_at asc']);
  84. $table_list = [];
  85. if ($store_census_list) {
  86. foreach ($store_census_list as $val) {
  87. $table_list[] = [
  88. 'created_at' => $val['date_time'],
  89. 'sales_money' => $val['sales_money'] ?? 0,
  90. ];
  91. switch ($val['date_time']) {
  92. case $today:
  93. $today_data = $val;
  94. break;
  95. case $yesterday:
  96. $yesterday_data = $val;
  97. break;
  98. }
  99. }
  100. }
  101. $store = HappinessStore::getOne([['id' => $this->store_id]], true, [], false, 'id,shop_owner,store_name,logo_img,expire_time,business_time_start,business_time_end');
  102. $store['business_status'] = '营业中';
  103. if ($time < $store['business_time_start'] || $time > $store['business_time_end']) {
  104. $store['business_status'] = '休息中';
  105. }
  106. $no_pay_counts = HappinessOrder::listCount([
  107. 'alias' => 'yo',
  108. 'where' => [
  109. ['yo.store_id' => $this->store_id],
  110. ['o.order_status' => 0],
  111. ],
  112. 'join' => [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"]],
  113. ]);
  114. // $no_pay_counts = HappinessOrder::find()->where([, ])->count();//待付款
  115. $no_delivery_counts = HappinessOrder::listCount([
  116. 'alias' => 'yo',
  117. 'where' => [
  118. ['yo.store_id' => $this->store_id],
  119. ['o.order_status' => 1],
  120. ],
  121. 'join' => [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"]],
  122. ]);
  123. //待自提订单
  124. $no_take_counts = HappinessOrder::listCount([
  125. 'alias' => 'yo',
  126. 'where' => [
  127. ['yo.store_id' => $this->store_id],
  128. ['o.order_status' => OrderStatusEnum::TAKE],
  129. ],
  130. 'join' => [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"]],
  131. ]);
  132. // $no_delivery_counts = HappinessOrder::find()->where(['store_id' => $this->store_id, 'order_status' => 1])->count();//待发货
  133. $after_sale_counts = OrderRefund::find()->where(['store_id' => $this->store_id, 'order_source' => 'happiness'])->andWhere(['!=','refund_status',1])->count();//售后
  134. $sell_out_counts = HappinessStoreGoods::find()->where(['store_id' => $this->store_id, 'stock' => 0])->count();//售罄
  135. $store['no_pay_counts'] = $no_pay_counts ?? 0;
  136. $store['no_delivery_counts'] = $no_delivery_counts ?? 0;
  137. $store['no_take_counts'] = $no_take_counts ?? 0;
  138. $store['after_sale_counts'] = $after_sale_counts ?? 0;
  139. $store['sell_out_counts'] = $sell_out_counts ?? 0;
  140. return $this->success('操作成功', compact('goods_ranking_list', 'user_ranking_list', 'today_data', 'yesterday_data', 'table_list', 'store'));
  141. }
  142. return $this->render($this->action->id);
  143. }
  144. //不用验证权限
  145. public function actionToPay()
  146. {
  147. if (Yii::$app->request->isAjax) {
  148. $params = Yii::$app->request->post();
  149. $limit = 1;
  150. switch ($params['activeDay']) {
  151. case "one":
  152. $limit = 1;
  153. break;
  154. case "seven":
  155. $limit = 6;
  156. break;
  157. case "thirty":
  158. $limit = 29;
  159. break;
  160. }
  161. $time = time();
  162. $today = date('Y-m-d', $time);
  163. $date_time_arr = [$today];
  164. for ($i = 1; $i <= $limit; $i++) {
  165. $date_time_arr[] = date("Y-m-d", strtotime("-{$i} day"));
  166. }
  167. $today_data = ['add_user_nums' => 0, 'order_nums' => 0, 'settlement_commission' => 0, 'sales_money' => 0, 'cashier_sales_money' => 0];
  168. $store_census_list = HappinessStoreCensus::lists(['where' => [['store_id' => $this->store_id], ['date_time' => $date_time_arr]], 'order' => 'created_at asc']);
  169. if ($store_census_list) {
  170. foreach ($store_census_list as $val) {
  171. $today_data['add_user_nums'] += $val['add_user_nums'];
  172. $today_data['order_nums'] += $val['order_nums'];
  173. $today_data['settlement_commission'] += $val['settlement_commission'];
  174. $today_data['sales_money'] += $val['sales_money'];
  175. $today_data['cashier_sales_money'] += $val['cashier_sales_money'];
  176. }
  177. }
  178. return $this->success('操作成功', compact('today_data'));
  179. }
  180. return $this->render($this->action->id);
  181. }
  182. public function actionHeader()
  183. {
  184. $admin = Yii::$app->store_user->identity;
  185. $user = array(
  186. 'id' => $admin->id,
  187. 'store_id' => $admin->store_id,
  188. 'username' => $admin->username,
  189. 'admin_type' => $admin->admin_type,
  190. );
  191. $mall = array(
  192. 'id' => $this->mall['id'],
  193. 'name' => $this->mall['name'],
  194. );
  195. $services = Yii::$app->services->setting->platform->get('system');
  196. $update_password_status = Yii::$app->services->setting->mall->get('basic.update_password_status');
  197. $navs = [];
  198. return $this->success('操作成功', compact('user', 'mall', 'services', 'update_password_status', 'navs'));
  199. }
  200. }