Common.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/6/24
  7. *
  8. *
  9. */
  10. namespace app\controller\merchant;
  11. use app\common\repositories\merchant\MerchantRepository;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\order\StoreOrderProductRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use app\common\repositories\user\UserRelationRepository;
  17. use app\common\repositories\user\UserVisitRepository;
  18. use think\App;
  19. /**
  20. * Class Common
  21. * @package app\controller\merchant
  22. * @author xaboy
  23. * @day 2020/6/25
  24. */
  25. class Common extends BaseController
  26. {
  27. /**
  28. * @var int|null
  29. */
  30. protected $merId;
  31. /**
  32. * Common constructor.
  33. * @param App $app
  34. */
  35. public function __construct(App $app)
  36. {
  37. parent::__construct($app);
  38. $this->merId = $this->request->merId() ?: null;
  39. }
  40. /**
  41. * @param null $merId
  42. * @return mixed
  43. * @author xaboy
  44. * @day 2020/6/25
  45. */
  46. public function main($merId = null)
  47. {
  48. $today = $this->mainGroup('today', $merId ?? $this->merId);
  49. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  50. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  51. $lastWeekRate = [];
  52. foreach ($lastWeek as $k => $item) {
  53. if ($item == $today[$k])
  54. $lastWeekRate[$k] = 0;
  55. else if ($item == 0)
  56. $lastWeekRate[$k] = $today[$k];
  57. else if ($today[$k] == 0)
  58. $lastWeekRate[$k] = -$item;
  59. else
  60. $lastWeekRate[$k] = bcdiv(bcsub($today[$k], $item, 2), $item, 2);
  61. }
  62. $day = date('Y-m-d');
  63. return $merId ? compact('today', 'yesterday', 'lastWeekRate', 'day') : app('json')->success(compact('today', 'yesterday', 'lastWeekRate', 'day'));
  64. }
  65. /**
  66. * @param $date
  67. * @param $merId
  68. * @return array
  69. * @author xaboy
  70. * @day 2020/6/25
  71. */
  72. public function mainGroup($date, $merId)
  73. {
  74. $userVisitRepository = app()->make(UserVisitRepository::class);
  75. $repository = app()->make(StoreOrderRepository::class);
  76. $relationRepository = app()->make(UserRelationRepository::class);
  77. $merchRepository = app()->make(MerchantRepository::class);
  78. $orderNum = $repository->dayOrderNum($date, $merId);
  79. $payPrice = $repository->dayOrderPrice($date, $merId);
  80. $payUser = $repository->dayOrderUserNum($date, $merId);
  81. $visitNum = $userVisitRepository->dateVisitUserNum($date, $merId);
  82. $likeStore = $relationRepository->dayLikeStore($date, $merId);
  83. $lockUserNum = $merchRepository->get_lock_num($merId);
  84. return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore','lockUserNum');
  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. * @return mixed
  113. * @author xaboy
  114. * @day 2020/6/25
  115. */
  116. public function user(UserRelationRepository $repository, StoreOrderRepository $orderRepository)
  117. {
  118. $date = $this->request->param('date', 'today') ?: 'today';
  119. $visitUser = $repository->dateVisitStore($date, $this->merId);
  120. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  121. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  122. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  123. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  124. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  125. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  126. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  127. return app('json')->success(compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate'));
  128. }
  129. /**
  130. * @param StoreOrderRepository $repository
  131. * @return mixed
  132. * @author xaboy
  133. * @day 2020/6/25
  134. */
  135. public function userRate(StoreOrderRepository $repository)
  136. {
  137. $date = $this->request->param('date') ?: 'today';
  138. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  139. $oldUids = $repository->oldUserIds(array_column($uids, 'uid'), $this->merId);
  140. $user = count($uids);
  141. $oldUser = count($oldUids);
  142. $totalPrice = 0;
  143. $oldTotalPrice = 0;
  144. foreach ($uids as $uid) {
  145. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  146. if (in_array($uid['uid'], $oldUids))
  147. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  148. }
  149. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
  150. $newUser = $user - $oldUser;
  151. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  152. }
  153. /**
  154. * @param StoreOrderProductRepository $repository
  155. * @return mixed
  156. * @author xaboy
  157. * @day 2020/6/25
  158. */
  159. public function product(StoreOrderProductRepository $repository)
  160. {
  161. $date = $this->request->param('date', 'today') ?: 'today';
  162. return app('json')->success($repository->orderProductGroup($date, $this->merId));
  163. }
  164. public function productVisit(UserVisitRepository $repository)
  165. {
  166. $date = $this->request->param('date', 'today') ?: 'today';
  167. return app('json')->success($repository->dateVisitProductNum($date, $this->merId));
  168. }
  169. /**
  170. * @param ProductRepository $repository
  171. * @return mixed
  172. * @author xaboy
  173. * @day 2020/6/25
  174. */
  175. public function productCart(ProductRepository $repository)
  176. {
  177. $date = $this->request->param('date', 'today') ?: 'today';
  178. return app('json')->success($repository->cartProductGroup($date, $this->merId));
  179. }
  180. }