Common.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant;
  12. use app\common\repositories\user\UserRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\product\ProductRepository;
  17. use app\common\repositories\user\UserRelationRepository;
  18. use app\common\repositories\user\UserVisitRepository;
  19. use think\App;
  20. use think\facade\Db;
  21. /**
  22. * Class Common
  23. * @package app\controller\merchant
  24. * @author xaboy
  25. * @day 2020/6/25
  26. */
  27. class Common extends BaseController
  28. {
  29. /**
  30. * @var int|null
  31. */
  32. protected $merId;
  33. /**
  34. * Common constructor.
  35. * @param App $app
  36. */
  37. public function __construct(App $app)
  38. {
  39. parent::__construct($app);
  40. $this->merId = $this->request->merId() ?: null;
  41. }
  42. /**
  43. * @param null $merId
  44. * @return mixed
  45. * @author xaboy
  46. * @day 2020/6/25
  47. */
  48. public function main($merId = null)
  49. {
  50. $today = $this->mainGroup('today', $merId ?? $this->merId);
  51. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  52. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  53. $lastWeekRate = [];
  54. foreach ($lastWeek as $k => $item) {
  55. if ($item == $today[$k])
  56. $lastWeekRate[$k] = 0;
  57. else if ($item == 0)
  58. $lastWeekRate[$k] = $today[$k];
  59. else if ($today[$k] == 0)
  60. $lastWeekRate[$k] = -$item;
  61. else
  62. $lastWeekRate[$k] = (float)bcdiv(bcsub($today[$k], $item, 4), $item, 4);
  63. }
  64. $day = date('Y-m-d');
  65. return $merId ? compact('today', 'yesterday', 'lastWeekRate', 'day') : app('json')->success(compact('today', 'yesterday', 'lastWeekRate', 'day'));
  66. }
  67. /**
  68. * @param $date
  69. * @param $merId
  70. * @return array
  71. * @author xaboy
  72. * @day 2020/6/25
  73. */
  74. public function mainGroup($date, $merId)
  75. {
  76. $userVisitRepository = app()->make(UserVisitRepository::class);
  77. $repository = app()->make(StoreOrderRepository::class);
  78. $relationRepository = app()->make(UserRelationRepository::class);
  79. $orderNum = (float)$repository->dayOrderNum($date, $merId);
  80. $payPrice = (float)$repository->dayOrderPrice($date, $merId);
  81. $payUser = (float)$repository->dayOrderUserNum($date, $merId);
  82. $visitNum = (float)$userVisitRepository->dateVisitUserNum($date, $merId);
  83. $likeStore = (float)$relationRepository->dayLikeStore($date, $merId);
  84. return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore');
  85. }
  86. /**
  87. * @param StoreOrderRepository $repository
  88. * @return mixed
  89. * @author xaboy
  90. * @day 2020/6/25
  91. */
  92. public function order(StoreOrderRepository $repository)
  93. {
  94. $date = $this->request->param('date') ?: 'lately7';
  95. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  96. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  97. $list = array_combine(array_column($list, 'day'), $list);
  98. $data = [];
  99. foreach ($time as $item) {
  100. $data[] = [
  101. 'day' => $item,
  102. 'total' => $list[$item]['total'] ?? 0,
  103. 'user' => $list[$item]['user'] ?? 0,
  104. 'pay_price' => $list[$item]['pay_price'] ?? 0
  105. ];
  106. }
  107. return app('json')->success($data);
  108. }
  109. /**
  110. * @param UserRelationRepository $repository
  111. * @param StoreOrderRepository $orderRepository
  112. * @param UserVisitRepository $userVisitRepository
  113. * @return \think\response\Json
  114. * @author xaboy
  115. * @day 2020/9/24
  116. */
  117. public function user(StoreOrderRepository $orderRepository, UserVisitRepository $userVisitRepository)
  118. {
  119. $date = $this->request->param('date', 'today') ?: 'today';
  120. $visitUser = $userVisitRepository->dateVisitUserNum($date, $this->merId);
  121. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  122. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  123. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  124. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  125. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  126. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  127. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  128. return app('json')->success(compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate'));
  129. }
  130. /**
  131. * @param StoreOrderRepository $repository
  132. * @return mixed
  133. * @author xaboy
  134. * @day 2020/6/25
  135. */
  136. public function userRate(StoreOrderRepository $repository, UserRepository $userRepository)
  137. {
  138. $date = $this->request->param('date') ?: 'today';
  139. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  140. $userPayCount = $userRepository->idsByPayCount(array_column($uids, 'uid'));
  141. $user = count($uids);
  142. $oldUser = 0;
  143. $totalPrice = 0;
  144. $oldTotalPrice = 0;
  145. foreach ($uids as $uid) {
  146. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  147. if (($userPayCount[$uid['uid']] ?? 0) > $uid['total']) {
  148. $oldUser++;
  149. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  150. }
  151. }
  152. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice, 2);
  153. $newUser = $user - $oldUser;
  154. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  155. }
  156. /**
  157. * @param StoreOrderProductRepository $repository
  158. * @return mixed
  159. * @author xaboy
  160. * @day 2020/6/25
  161. */
  162. public function product(StoreOrderProductRepository $repository)
  163. {
  164. $date = $this->request->param('date', 'today') ?: 'today';
  165. return app('json')->success($repository->orderProductGroup($date, $this->merId));
  166. }
  167. public function productVisit(UserVisitRepository $repository)
  168. {
  169. $date = $this->request->param('date', 'today') ?: 'today';
  170. return app('json')->success($repository->dateVisitProductNum($date, $this->merId));
  171. }
  172. /**
  173. * @param ProductRepository $repository
  174. * @return mixed
  175. * @author xaboy
  176. * @day 2020/6/25
  177. */
  178. public function productCart(ProductRepository $repository)
  179. {
  180. $date = $this->request->param('date', 'today') ?: 'today';
  181. return app('json')->success($repository->cartProductGroup($date, $this->merId));
  182. }
  183. }