Common.php 8.7 KB

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