Common.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\facade\Db;
  19. use think\App;
  20. /**
  21. * Class Common
  22. * @package app\controller\merchant
  23. * @author xaboy
  24. * @day 2020/6/25
  25. */
  26. class Common extends BaseController
  27. {
  28. /**
  29. * @var int|null
  30. */
  31. protected $merId;
  32. /**
  33. * Common constructor.
  34. * @param App $app
  35. */
  36. public function __construct(App $app)
  37. {
  38. parent::__construct($app);
  39. $this->merId = $this->request->merId() ?: null;
  40. }
  41. /**今日昨日统计+总贡献值记录=总金豆记录
  42. * @param null $merId
  43. * @return mixed
  44. * @author xaboy
  45. * @day 2020/6/25
  46. */
  47. public function zuorijinri()
  48. {
  49. $merId = $this->merId;
  50. $list['gongxian'] = Db::name('user_sign_gongxian')->where('mer_id', $merId)->value('number');
  51. $list['jingdou'] = Db::name('user_sign_jingdou')->where('mer_id', $merId)->value('number');
  52. return app('json')->success(compact('merId', 'list'));
  53. }
  54. /**收银台最新记录接口
  55. * @param null $merId
  56. * @return mixed
  57. * @author xaboy
  58. * @day 2020/6/25
  59. */
  60. public function zuixinjilu()
  61. {
  62. $merId = $this->merId;
  63. // $merId = 476;
  64. $pageNum = $this->request->param('pageNum') ?: 1;
  65. $pageSize = $this->request->param('pageSize') ?: 10;
  66. $query = Db::name('order_merchant');
  67. $count = $query->where('mer_id', $merId)->count();
  68. $list = $query->page($pageNum, $pageSize)->order('id desc')->where('mer_id', $merId)->where('paid', 1)->select();
  69. $list = $list->toArray();
  70. foreach ($list as $k => $v) {
  71. $list[$k]['pv'] = Db::name('user_sign_gongxian')->where('mer_id', $v['mer_id'])->where('order_id', $v['id'])->value('pv') ?? 0;
  72. $list[$k]['gongxian'] = Db::name('user_sign_gongxian')->where('mer_id', $v['mer_id'])->where('order_id', $v['id'])->value('number') ?? 0;
  73. $list[$k]['jingdou'] = Db::name('user_sign_jingdou')->where('mer_id', $v['mer_id'])->where('order_id', $v['id'])->value('number') ?? 0;
  74. }
  75. return app('json')->success(compact('merId', 'list', 'count'));
  76. }
  77. /**
  78. * @param null $merId
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020/6/25
  82. */
  83. public function main($merId = null)
  84. {
  85. $today = $this->mainGroup('today', $merId ?? $this->merId);
  86. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  87. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  88. $lastWeekRate = [];
  89. foreach ($lastWeek as $k => $item) {
  90. if ($item == $today[$k])
  91. $lastWeekRate[$k] = 0;
  92. else if ($item == 0)
  93. $lastWeekRate[$k] = $today[$k];
  94. else if ($today[$k] == 0)
  95. $lastWeekRate[$k] = -$item;
  96. else
  97. $lastWeekRate[$k] = bcdiv(bcsub($today[$k], $item, 2), $item, 2);
  98. }
  99. $day = date('Y-m-d');
  100. return $merId ? compact('today', 'yesterday', 'lastWeekRate', 'day') : app('json')->success(compact('today', 'yesterday', 'lastWeekRate', 'day'));
  101. }
  102. /**
  103. * @param $date
  104. * @param $merId
  105. * @return array
  106. * @author xaboy
  107. * @day 2020/6/25
  108. */
  109. public function mainGroup($date, $merId)
  110. {
  111. $userVisitRepository = app()->make(UserVisitRepository::class);
  112. $repository = app()->make(StoreOrderRepository::class);
  113. $relationRepository = app()->make(UserRelationRepository::class);
  114. $merchRepository = app()->make(MerchantRepository::class);
  115. $orderNum = $repository->dayOrderNum($date, $merId);
  116. $payPrice = $repository->dayOrderPrice($date, $merId);
  117. $payUser = $repository->dayOrderUserNum($date, $merId);
  118. $visitNum = $userVisitRepository->dateVisitUserNum($date, $merId);
  119. $likeStore = $relationRepository->dayLikeStore($date, $merId);
  120. $lockUserNum = $merchRepository->get_lock_num($merId);
  121. return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore','lockUserNum');
  122. }
  123. /**
  124. * @param StoreOrderRepository $repository
  125. * @return mixed
  126. * @author xaboy
  127. * @day 2020/6/25
  128. */
  129. public function order(StoreOrderRepository $repository)
  130. {
  131. $date = $this->request->param('date') ?: 'lately7';
  132. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  133. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  134. $list = array_combine(array_column($list, 'day'), $list);
  135. $data = [];
  136. foreach ($time as $item) {
  137. $data[] = [
  138. 'day' => $item,
  139. 'total' => $list[$item]['total'] ?? 0,
  140. 'user' => $list[$item]['user'] ?? 0,
  141. 'pay_price' => $list[$item]['pay_price'] ?? 0
  142. ];
  143. }
  144. return app('json')->success($data);
  145. }
  146. /**
  147. * @param UserRelationRepository $repository
  148. * @param StoreOrderRepository $orderRepository
  149. * @return mixed
  150. * @author xaboy
  151. * @day 2020/6/25
  152. */
  153. public function user(UserRelationRepository $repository, StoreOrderRepository $orderRepository)
  154. {
  155. $date = $this->request->param('date', 'today') ?: 'today';
  156. $visitUser = $repository->dateVisitStore($date, $this->merId);
  157. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  158. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  159. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  160. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  161. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  162. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  163. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  164. return app('json')->success(compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate'));
  165. }
  166. /**
  167. * @param StoreOrderRepository $repository
  168. * @return mixed
  169. * @author xaboy
  170. * @day 2020/6/25
  171. */
  172. public function userRate(StoreOrderRepository $repository)
  173. {
  174. $date = $this->request->param('date') ?: 'today';
  175. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  176. $oldUids = $repository->oldUserIds(array_column($uids, 'uid'), $this->merId);
  177. $user = count($uids);
  178. $oldUser = count($oldUids);
  179. $totalPrice = 0;
  180. $oldTotalPrice = 0;
  181. foreach ($uids as $uid) {
  182. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  183. if (in_array($uid['uid'], $oldUids))
  184. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  185. }
  186. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
  187. $newUser = $user - $oldUser;
  188. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  189. }
  190. /**
  191. * @param StoreOrderProductRepository $repository
  192. * @return mixed
  193. * @author xaboy
  194. * @day 2020/6/25
  195. */
  196. public function product(StoreOrderProductRepository $repository)
  197. {
  198. $date = $this->request->param('date', 'today') ?: 'today';
  199. return app('json')->success($repository->orderProductGroup($date, $this->merId));
  200. }
  201. public function productVisit(UserVisitRepository $repository)
  202. {
  203. $date = $this->request->param('date', 'today') ?: 'today';
  204. return app('json')->success($repository->dateVisitProductNum($date, $this->merId));
  205. }
  206. /**
  207. * @param ProductRepository $repository
  208. * @return mixed
  209. * @author xaboy
  210. * @day 2020/6/25
  211. */
  212. public function productCart(ProductRepository $repository)
  213. {
  214. $date = $this->request->param('date', 'today') ?: 'today';
  215. return app('json')->success($repository->cartProductGroup($date, $this->merId));
  216. }
  217. }