Common.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\controller\merchant;
  3. use app\common\repositories\user\UserRepository;
  4. use ln\basic\BaseController;
  5. use app\common\repositories\store\order\StoreOrderProductRepository;
  6. use app\common\repositories\store\order\StoreOrderRepository;
  7. use app\common\repositories\store\product\ProductRepository;
  8. use app\common\repositories\user\UserRelationRepository;
  9. use app\common\repositories\user\UserVisitRepository;
  10. use ln\services\ImageWaterMarkService;
  11. use ln\services\UploadService;
  12. use think\App;
  13. use think\facade\Cache;
  14. use think\facade\Db;
  15. /**
  16. * Class Common
  17. * @package app\controller\merchant
  18. * @author zfy
  19. * @day 2020/6/25
  20. */
  21. class Common extends BaseController
  22. {
  23. /**
  24. * @var int|null
  25. */
  26. protected $merId;
  27. /**
  28. * Common constructor.
  29. * @param App $app
  30. */
  31. public function __construct(App $app)
  32. {
  33. parent::__construct($app);
  34. $this->merId = $this->request->merId() ?: null;
  35. }
  36. /**
  37. * @param null $merId
  38. * @return mixed
  39. * @author zfy
  40. * @day 2020/6/25
  41. */
  42. public function main($merId = null)
  43. {
  44. $today = $this->mainGroup('today', $merId ?? $this->merId);
  45. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  46. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  47. $lastWeekRate = [];
  48. foreach ($lastWeek as $k => $item) {
  49. if ($item == $today[$k])
  50. $lastWeekRate[$k] = 0;
  51. else if ($item == 0)
  52. $lastWeekRate[$k] = $today[$k];
  53. else if ($today[$k] == 0)
  54. $lastWeekRate[$k] = -$item;
  55. else
  56. $lastWeekRate[$k] = (float)bcdiv(bcsub($today[$k], $item, 4), $item, 4);
  57. }
  58. $day = date('Y-m-d');
  59. return $merId ? compact('today', 'yesterday', 'lastWeekRate', 'day') : app('json')->success(compact('today', 'yesterday', 'lastWeekRate', 'day'));
  60. }
  61. /**
  62. * @param $date
  63. * @param $merId
  64. * @return array
  65. * @author zfy
  66. * @day 2020/6/25
  67. */
  68. public function mainGroup($date, $merId)
  69. {
  70. $userVisitRepository = app()->make(UserVisitRepository::class);
  71. $repository = app()->make(StoreOrderRepository::class);
  72. $relationRepository = app()->make(UserRelationRepository::class);
  73. $orderNum = (float)$repository->dayOrderNum($date, $merId);
  74. $payPrice = (float)$repository->dayOrderPrice($date, $merId);
  75. $payUser = (float)$repository->dayOrderUserNum($date, $merId);
  76. $visitNum = (float)$userVisitRepository->dateVisitUserNum($date, $merId);
  77. $likeStore = (float)$relationRepository->dayLikeStore($date, $merId);
  78. return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore');
  79. }
  80. /**
  81. * @param StoreOrderRepository $repository
  82. * @return mixed
  83. * @author zfy
  84. * @day 2020/6/25
  85. */
  86. public function order(StoreOrderRepository $repository)
  87. {
  88. $date = $this->request->param('date') ?: 'lately7';
  89. $res = Cache::store('file')->remember(self::class . '@order' . $date, function () use ($repository, $date) {
  90. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  91. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  92. $list = array_combine(array_column($list, 'day'), $list);
  93. $data = [];
  94. foreach ($time as $item) {
  95. $data[] = [
  96. 'day' => $item,
  97. 'total' => $list[$item]['total'] ?? 0,
  98. 'user' => $list[$item]['user'] ?? 0,
  99. 'pay_price' => $list[$item]['pay_price'] ?? 0
  100. ];
  101. }
  102. return $data;
  103. }, 2000 + random_int(600, 1200));
  104. return app('json')->success($res);
  105. }
  106. /**
  107. * @param UserRelationRepository $repository
  108. * @param StoreOrderRepository $orderRepository
  109. * @param UserVisitRepository $userVisitRepository
  110. * @return \think\response\Json
  111. * @author zfy
  112. * @day 2020/9/24
  113. */
  114. public function user(StoreOrderRepository $orderRepository, UserVisitRepository $userVisitRepository)
  115. {
  116. $date = $this->request->param('date', 'today') ?: 'today';
  117. $res = Cache::store('file')->remember(self::class . '@user' . $date, function () use ($orderRepository, $userVisitRepository, $date) {
  118. $visitUser = $userVisitRepository->dateVisitUserNum($date, $this->merId);
  119. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  120. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  121. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  122. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  123. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  124. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  125. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  126. return compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate');
  127. }, 2000 + random_int(600, 1200));
  128. return app('json')->success($res);
  129. }
  130. /**
  131. * @param StoreOrderRepository $repository
  132. * @return mixed
  133. * @author zfy
  134. * @day 2020/6/25
  135. */
  136. public function userRate(StoreOrderRepository $repository, UserRepository $userRepository)
  137. {
  138. $date = $this->request->param('date') ?: 'today';
  139. $res = Cache::store('file')->remember(self::class . '@userRate' . $date, function () use ($userRepository, $repository, $date) {
  140. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  141. $userPayCount = $userRepository->idsByPayCount(array_column($uids, 'uid'));
  142. $user = count($uids);
  143. $oldUser = 0;
  144. $totalPrice = 0;
  145. $oldTotalPrice = 0;
  146. foreach ($uids as $uid) {
  147. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  148. if (($userPayCount[$uid['uid']] ?? 0) > $uid['total']) {
  149. $oldUser++;
  150. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  151. }
  152. }
  153. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice, 2);
  154. $newUser = $user - $oldUser;
  155. return compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user');
  156. }, 2000 + random_int(600, 1200));
  157. return app('json')->success($res);
  158. }
  159. /**
  160. * @param StoreOrderProductRepository $repository
  161. * @return mixed
  162. * @author zfy
  163. * @day 2020/6/25
  164. */
  165. public function product(StoreOrderProductRepository $repository)
  166. {
  167. $date = $this->request->param('date', 'today') ?: 'today';
  168. $res = Cache::store('file')->remember(self::class . '@product' . $date, function () use ($repository, $date) {
  169. return $repository->orderProductGroup($date, $this->merId)->toArray();
  170. }, 2000 + random_int(600, 1200));
  171. return app('json')->success($res);
  172. }
  173. public function productVisit(UserVisitRepository $repository)
  174. {
  175. $date = $this->request->param('date', 'today') ?: 'today';
  176. $res = Cache::store('file')->remember(self::class . '@productVisit' . $date, function () use ($repository, $date) {
  177. return $repository->dateVisitProductNum($date, $this->merId);
  178. }, 2000 + random_int(600, 1200));
  179. return app('json')->success($res);
  180. }
  181. /**
  182. * @param ProductRepository $repository
  183. * @return mixed
  184. * @author zfy
  185. * @day 2020/6/25
  186. */
  187. public function productCart(ProductRepository $repository)
  188. {
  189. $date = $this->request->param('date', 'today') ?: 'today';
  190. $res = Cache::store('file')->remember(self::class . '@productCart' . $date, function () use ($repository, $date) {
  191. return $repository->cartProductGroup($date, $this->merId);
  192. }, 2000 + random_int(600, 1200));
  193. return app('json')->success($res);
  194. }
  195. public function uploadCertificate()
  196. {
  197. $file = $this->request->file('file');
  198. if (!$file)
  199. return app('json')->fail('请上传证书');
  200. validate(["file|图片" => [
  201. 'fileSize' => config('upload.filesize'),
  202. 'fileExt' => 'jpg,jpeg,png,bmp',
  203. 'fileMime' => 'image/jpeg,image/png',
  204. ]])->check(['file' => $file]);
  205. $upload = UploadService::create(1);
  206. $data = $upload->to('attach')->move('file');
  207. if ($data === false) {
  208. return app('json')->fail($upload->getError());
  209. }
  210. app()->make(ImageWaterMarkService::class)->run('./public' . $upload->getFileInfo()->filePath);
  211. return app('json')->success(['src' => tidy_url($upload->getFileInfo()->filePath)]);
  212. }
  213. public function uploadVideo()
  214. {
  215. $file = $this->request->file('file');
  216. if (!$file)
  217. return app('json')->fail('请上传视频');
  218. validate(["file|视频" => [
  219. 'fileSize' => config('upload.filesize'),
  220. 'fileExt' => 'mp4',
  221. 'fileMime' => 'video/mp4',
  222. ]])->check(['file' => $file]);
  223. $upload = UploadService::create(1);
  224. $data = $upload->to('media')->validate([])->move('file');
  225. if ($data === false) {
  226. return app('json')->fail($upload->getError());
  227. }
  228. return app('json')->success(['src' => tidy_url($upload->getFileInfo()->filePath)]);
  229. }
  230. }