Common.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. $repository = app()->make(StoreOrderRepository::class);
  120. $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
  121. $relationRepository = app()->make(UserRelationRepository::class);
  122. $merchRepository = app()->make(MerchantRepository::class);
  123. $orderNum = $repository->dayOrderNum($date, $merId);
  124. $payPrice = $repository->dayOrderPrice($date, $merId);
  125. $payUser = $repository->dayOrderUserNum($date, $merId);
  126. $orderNumNew = $orderMerchantRepository->dayOrderNum($date, $merId);
  127. $payPriceNew = $orderMerchantRepository->dayOrderPrice($date, $merId);
  128. $payUserNew = $orderMerchantRepository->dayOrderUserNum($date, $merId);
  129. $fenrunNum = $orderMerchantRepository->dayOrderAvailable($date, $merId);
  130. $jingdouNum = Db::name('user_sign_jingdou')->when($merId, function ($query, $merId) {
  131. $query->where('mer_id', $merId);
  132. })->when($date, function ($query, $day) {
  133. getModelTime($query, $day, 'addtime');
  134. })->sum('number');
  135. $visitNum = $userVisitRepository->dateVisitUserNum($date, $merId);
  136. $likeStore = $relationRepository->dayLikeStore($date, $merId);
  137. $lockUserNum = $merchRepository->get_lock_num($date, $merId);
  138. return compact('orderNum', 'payPrice', 'payUser', 'orderNumNew', 'payPriceNew', 'payUserNew', 'fenrunNum', 'jingdouNum', 'visitNum', 'likeStore','lockUserNum');
  139. }
  140. /**
  141. * @param StoreOrderRepository $repository
  142. * @return mixed
  143. * @author xaboy
  144. * @day 2020/6/25
  145. */
  146. public function order(StoreOrderRepository $repository)
  147. {
  148. $date = $this->request->param('date') ?: 'lately7';
  149. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  150. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  151. $list = array_combine(array_column($list, 'day'), $list);
  152. $data = [];
  153. foreach ($time as $item) {
  154. $data[] = [
  155. 'day' => $item,
  156. 'total' => $list[$item]['total'] ?? 0,
  157. 'user' => $list[$item]['user'] ?? 0,
  158. 'pay_price' => $list[$item]['pay_price'] ?? 0
  159. ];
  160. }
  161. return app('json')->success($data);
  162. }
  163. /**
  164. * @param UserRelationRepository $repository
  165. * @param StoreOrderRepository $orderRepository
  166. * @return mixed
  167. * @author xaboy
  168. * @day 2020/6/25
  169. */
  170. public function user(UserRelationRepository $repository, StoreOrderRepository $orderRepository)
  171. {
  172. $date = $this->request->param('date', 'today') ?: 'today';
  173. $visitUser = $repository->dateVisitStore($date, $this->merId);
  174. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  175. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  176. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  177. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  178. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  179. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  180. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  181. return app('json')->success(compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate'));
  182. }
  183. /**
  184. * @param StoreOrderRepository $repository
  185. * @return mixed
  186. * @author xaboy
  187. * @day 2020/6/25
  188. */
  189. public function userRate(StoreOrderRepository $repository)
  190. {
  191. $date = $this->request->param('date') ?: 'today';
  192. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  193. $oldUids = $repository->oldUserIds(array_column($uids, 'uid'), $this->merId);
  194. $user = count($uids);
  195. $oldUser = count($oldUids);
  196. $totalPrice = 0;
  197. $oldTotalPrice = 0;
  198. foreach ($uids as $uid) {
  199. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  200. if (in_array($uid['uid'], $oldUids))
  201. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  202. }
  203. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
  204. $newUser = $user - $oldUser;
  205. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  206. }
  207. /**
  208. * @param StoreOrderProductRepository $repository
  209. * @return mixed
  210. * @author xaboy
  211. * @day 2020/6/25
  212. */
  213. public function product(StoreOrderProductRepository $repository)
  214. {
  215. $date = $this->request->param('date', 'today') ?: 'today';
  216. return app('json')->success($repository->orderProductGroup($date, $this->merId));
  217. }
  218. public function productVisit(UserVisitRepository $repository)
  219. {
  220. $date = $this->request->param('date', 'today') ?: 'today';
  221. return app('json')->success($repository->dateVisitProductNum($date, $this->merId));
  222. }
  223. /**
  224. * @param ProductRepository $repository
  225. * @return mixed
  226. * @author xaboy
  227. * @day 2020/6/25
  228. */
  229. public function productCart(ProductRepository $repository)
  230. {
  231. $date = $this->request->param('date', 'today') ?: 'today';
  232. return app('json')->success($repository->cartProductGroup($date, $this->merId));
  233. }
  234. }