Common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\merchant\order\OrderMerchantRepository;
  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\facade\Db;
  20. use think\App;
  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 zuorijinri()
  49. {
  50. $merId = $this->merId;
  51. // $list['gongxian'] = Db::name('user_sign_gongxian')->where('mer_id', $merId)->sum('number');
  52. $list['jingdou'] = Db::name('user_sign_jingdou')->where('mer_id', $merId)->where('status',1)->sum('number');
  53. $list['fenrunzhi'] = Db::name('order_merchant')->where('mer_id', $merId)->where('paid', 1)->sum('available');
  54. $list['xianxia'] = Db::name('order_merchant')->where('mer_id', $merId)->where('paid', 1)->count();
  55. $list['xianshang'] = Db::name('store_order')->where('mer_id', $merId)->where('paid', 1)->count();
  56. return app('json')->success(compact('merId', 'list'));
  57. }
  58. /**收银台最新记录接口
  59. * @param null $merId
  60. * @return mixed
  61. * @author xaboy
  62. * @day 2020/6/25
  63. */
  64. public function zuixinjilu()
  65. {
  66. $merId = $this->merId;
  67. // $merId = 476;
  68. $pageNum = $this->request->param('pageNum') ?: 1;
  69. $pageSize = $this->request->param('pageSize') ?: 10;
  70. $query = Db::name('order_merchant');
  71. $count = $query->where('mer_id', $merId)->count();
  72. $list = $query->page($pageNum, $pageSize)->order('id desc')->where('mer_id', $merId)->where('paid', 1)->select();
  73. $list = $list->toArray();
  74. foreach ($list as $k => $v) {
  75. $list[$k]['pv'] = Db::name('user_sign_gongxian')->where('order_id', $v['id'])->value('pv') ?? 0;
  76. // $list[$k]['gongxian'] = Db::name('user_sign_gongxian')->where('mer_id', $v['mer_id'])->where('order_id', $v['id'])->value('number') ?? 0;
  77. $list[$k]['fenrunzhi'] = $v['available'];
  78. $list[$k]['jingdou'] = Db::name('user_sign_jingdou')->where('mer_id', $v['mer_id'])->where('order_id', $v['id'])->value('number') ?? 0;
  79. }
  80. return app('json')->success(compact('merId', 'list', 'count'));
  81. }
  82. /**
  83. * @param null $merId
  84. * @return mixed
  85. * @author xaboy
  86. * @day 2020/6/25
  87. */
  88. public function main($merId = null)
  89. {
  90. $all = $this->mainGroup('', $merId ?? $this->merId);
  91. $today = $this->mainGroup('today', $merId ?? $this->merId);
  92. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  93. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  94. $lastWeekRate = [];
  95. foreach ($lastWeek as $k => $item) {
  96. if ($item == $today[$k])
  97. $lastWeekRate[$k] = 0;
  98. else if ($item == 0)
  99. $lastWeekRate[$k] = $today[$k];
  100. else if ($today[$k] == 0)
  101. $lastWeekRate[$k] = -$item;
  102. else
  103. $lastWeekRate[$k] = bcdiv(bcsub($today[$k], $item, 2), $item, 2);
  104. }
  105. $day = date('Y-m-d');
  106. return $merId ? compact('all', 'today', 'yesterday', 'lastWeekRate', 'day') : app('json')->success(compact( 'all', 'today', 'yesterday', 'lastWeekRate', 'day'));
  107. }
  108. /**
  109. * 商户后台顶部统计
  110. * @param $date
  111. * @param $merId
  112. * @return array
  113. * @author xaboy
  114. * @day 2020/6/25
  115. */
  116. public function mainGroup($date, $merId)
  117. {
  118. $userVisitRepository = app()->make(UserVisitRepository::class);
  119. /** @var StoreOrderRepository $repository */
  120. $repository = app()->make(StoreOrderRepository::class);
  121. $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
  122. $relationRepository = app()->make(UserRelationRepository::class);
  123. $merchRepository = app()->make(MerchantRepository::class);
  124. $orderNum = $repository->dayOrderNum($date, $merId);
  125. $payPrice = $repository->dayOrderPrice($date, $merId);
  126. $payMoney = $repository->dayOrderMoney($date, $merId);
  127. $payJingDou = $repository->dayOrderJingDou($date, $merId);
  128. $payVR = $repository->dayOrderVR($date, $merId);
  129. $payJiFen = $repository->dayOrderJiFen($date, $merId);
  130. $payUser = $repository->dayOrderUserNum($date, $merId);
  131. $orderNumNew = $orderMerchantRepository->dayOrderNum($date, $merId);
  132. $payPriceNew = $orderMerchantRepository->dayOrderPrice($date, $merId);
  133. $payUserNew = $orderMerchantRepository->dayOrderUserNum($date, $merId);
  134. $fenrunNum = $orderMerchantRepository->dayOrderAvailable($date, $merId);
  135. $jingdouNum = Db::name('user_sign_jingdou')->when($merId, function ($query, $merId) {
  136. $query->where('mer_id', $merId);
  137. })->when($date, function ($query, $day) {
  138. getModelTime($query, $day, 'addtime');
  139. })->sum('number');
  140. $visitNum = $userVisitRepository->dateVisitUserNum($date, $merId);
  141. $likeStore = $relationRepository->dayLikeStore($date, $merId);
  142. $lockUserNum = $merchRepository->get_lock_num($date, $merId);
  143. return compact(
  144. 'orderNum',
  145. 'payPrice',
  146. 'payUser',
  147. 'orderNumNew',
  148. 'payPriceNew',
  149. 'payUserNew',
  150. 'fenrunNum',
  151. 'jingdouNum',
  152. 'visitNum',
  153. 'likeStore',
  154. 'lockUserNum',
  155. 'payMoney',
  156. 'payJingDou',
  157. 'payVR',
  158. 'payJiFen'
  159. );
  160. }
  161. /**
  162. * @param StoreOrderRepository $repository
  163. * @return mixed
  164. * @author xaboy
  165. * @day 2020/6/25
  166. */
  167. public function order(StoreOrderRepository $repository)
  168. {
  169. $date = $this->request->param('date') ?: 'lately7';
  170. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  171. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  172. $list = array_combine(array_column($list, 'day'), $list);
  173. $data = [];
  174. foreach ($time as $item) {
  175. $data[] = [
  176. 'day' => $item,
  177. 'total' => $list[$item]['total'] ?? 0,
  178. 'user' => $list[$item]['user'] ?? 0,
  179. 'pay_price' => $list[$item]['pay_price'] ?? 0
  180. ];
  181. }
  182. return app('json')->success($data);
  183. }
  184. /**
  185. * @param UserRelationRepository $repository
  186. * @param StoreOrderRepository $orderRepository
  187. * @return mixed
  188. * @author xaboy
  189. * @day 2020/6/25
  190. */
  191. public function user(UserRelationRepository $repository, StoreOrderRepository $orderRepository)
  192. {
  193. $date = $this->request->param('date', 'today') ?: 'today';
  194. $visitUser = $repository->dateVisitStore($date, $this->merId);
  195. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  196. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  197. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  198. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  199. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  200. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  201. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  202. return app('json')->success(compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate'));
  203. }
  204. /**
  205. * @param StoreOrderRepository $repository
  206. * @return mixed
  207. * @author xaboy
  208. * @day 2020/6/25
  209. */
  210. public function userRate(StoreOrderRepository $repository)
  211. {
  212. $date = $this->request->param('date') ?: 'today';
  213. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  214. $oldUids = $repository->oldUserIds(array_column($uids, 'uid'), $this->merId);
  215. $user = count($uids);
  216. $oldUser = count($oldUids);
  217. $totalPrice = 0;
  218. $oldTotalPrice = 0;
  219. foreach ($uids as $uid) {
  220. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  221. if (in_array($uid['uid'], $oldUids))
  222. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  223. }
  224. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
  225. $newUser = $user - $oldUser;
  226. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  227. }
  228. /**
  229. * @param StoreOrderProductRepository $repository
  230. * @return mixed
  231. * @author xaboy
  232. * @day 2020/6/25
  233. */
  234. public function product(StoreOrderProductRepository $repository)
  235. {
  236. $date = $this->request->param('date', 'today') ?: 'today';
  237. return app('json')->success($repository->orderProductGroup($date, $this->merId));
  238. }
  239. public function productVisit(UserVisitRepository $repository)
  240. {
  241. $date = $this->request->param('date', 'today') ?: 'today';
  242. return app('json')->success($repository->dateVisitProductNum($date, $this->merId));
  243. }
  244. /**
  245. * @param ProductRepository $repository
  246. * @return mixed
  247. * @author xaboy
  248. * @day 2020/6/25
  249. */
  250. public function productCart(ProductRepository $repository)
  251. {
  252. $date = $this->request->param('date', 'today') ?: 'today';
  253. return app('json')->success($repository->cartProductGroup($date, $this->merId));
  254. }
  255. }