Common.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. namespace app\adminapi\controller;
  3. use app\models\merchant\MerchantMiniprogram;
  4. use app\models\store\StoreOrder;
  5. use app\models\store\StoreProduct;
  6. use app\models\store\StoreProductAttrValue;
  7. use app\models\user\User;
  8. use crmeb\repositories\NoticeRepositories;
  9. /**
  10. * 公共接口基类 主要存放公共接口
  11. * Class Common
  12. * @package app\adminapi\controller
  13. */
  14. class Common extends AuthController
  15. {
  16. /**
  17. * @return mixed
  18. */
  19. public function check_auth()
  20. {
  21. return $this->checkAuthDecrypt();
  22. }
  23. /**
  24. * @return mixed
  25. */
  26. public function auth()
  27. {
  28. return $this->getAuth();
  29. }
  30. /**
  31. * 首页头部统计数据
  32. * @return mixed
  33. */
  34. public function homeStatics()
  35. {
  36. $mer_id = $this->merId ?: '';
  37. //TODO 销售额
  38. //今日销售额
  39. $today_sales = StoreOrder::where('paid', 1)
  40. ->where('mer_id', $mer_id)
  41. ->where('is_del', 0)
  42. ->where('refund_status', 0)
  43. ->whereDay('pay_time')
  44. ->sum('pay_price');
  45. //昨日销售额
  46. $yesterday_sales = StoreOrder::where('paid', 1)
  47. ->where('mer_id', $mer_id)
  48. ->where('is_del', 0)
  49. ->where('refund_status', 0)
  50. ->whereDay('pay_time', 'yesterday')
  51. ->sum('pay_price');
  52. //日同比
  53. $sales_today_ratio = $this->growth($today_sales, $yesterday_sales);
  54. //周销售额
  55. //本周
  56. $this_week_sales = StoreOrder::where('paid', 1)
  57. ->where('mer_id', $mer_id)
  58. ->where('is_del', 0)
  59. ->where('refund_status', 0)
  60. ->whereWeek('pay_time')
  61. ->sum('pay_price');
  62. //上周
  63. $last_week_sales = StoreOrder::where('paid', 1)
  64. ->where('mer_id', $mer_id)
  65. ->where('is_del', 0)
  66. ->where('refund_status', 0)
  67. ->whereWeek('pay_time', 'last week')
  68. ->sum('pay_price');
  69. //周同比
  70. $sales_week_ratio = $this->growth($this_week_sales, $last_week_sales);
  71. //总销售额
  72. $total_sales = StoreOrder::where('paid', 1)
  73. ->where('mer_id', $mer_id)
  74. ->where('is_del', 0)
  75. ->where('refund_status', 0)
  76. ->sum('pay_price');
  77. $sales = [
  78. 'today' => $today_sales,
  79. 'yesterday' => $yesterday_sales,
  80. 'today_ratio' => $sales_today_ratio,
  81. 'week' => $this_week_sales,
  82. 'last_week' => $last_week_sales,
  83. 'week_ratio' => $sales_week_ratio,
  84. 'total' => $total_sales . '元',
  85. 'date' => '昨日'
  86. ];
  87. //TODO:用户访问量
  88. //今日访问量
  89. $today_visits = User::where('mer_id', $mer_id)->WhereDay('last_time')->count();
  90. //昨日访问量
  91. $yesterday_visits = User::where('mer_id', $mer_id)->whereDay('last_time', 'yesterday')->count();
  92. //日同比
  93. $visits_today_ratio = $this->growth($today_visits, $yesterday_visits);
  94. //本周访问量
  95. $this_week_visits = User::where('mer_id', $mer_id)->WhereWeek('last_time')->count();
  96. //上周访问量
  97. $last_week_visits = User::where('mer_id', $mer_id)->WhereWeek('last_time', 'last week')->count();
  98. //周同比
  99. $visits_week_ratio = $this->growth($this_week_visits, $last_week_visits);
  100. //总访问量
  101. $total_visits = User::where('mer_id', $mer_id)->count();
  102. $visits = [
  103. 'today' => $today_visits,
  104. 'yesterday' => $yesterday_visits,
  105. 'today_ratio' => $visits_today_ratio,
  106. 'week' => $this_week_visits,
  107. 'last_week' => $last_week_visits,
  108. 'week_ratio' => $visits_week_ratio,
  109. 'total' => $total_visits . 'Pv',
  110. 'date' => '昨日'
  111. ];
  112. //TODO 订单量
  113. //今日订单量
  114. $today_order = StoreOrder::where('mer_id', $mer_id)->whereDay('add_time')->count();
  115. //昨日订单量
  116. $yesterday_order = StoreOrder::where('mer_id', $mer_id)->whereDay('add_time', 'yesterday')->count();
  117. //订单日同比
  118. $order_today_ratio = $this->growth($today_order, $yesterday_order);
  119. //本周订单量
  120. $this_week_order = StoreOrder::where('mer_id', $mer_id)->whereWeek('add_time')->count();
  121. //上周订单量
  122. $last_week_order = StoreOrder::where('mer_id', $mer_id)->whereWeek('add_time', 'last week')->count();
  123. //订单周同比
  124. $order_week_ratio = $this->growth($this_week_order, $last_week_order);
  125. //总订单量
  126. $total_order = StoreOrder::where('mer_id', $mer_id)->where('is_system_del', 0)->count();
  127. $order = [
  128. 'today' => $today_order,
  129. 'yesterday' => $yesterday_order,
  130. 'today_ratio' => $order_today_ratio,
  131. 'week' => $this_week_order,
  132. 'last_week' => $last_week_order,
  133. 'week_ratio' => $order_week_ratio,
  134. 'total' => $total_order . '单',
  135. 'date' => '昨日'
  136. ];
  137. //TODO 用户
  138. //今日新增用户
  139. $today_user = User::where('mer_id', $mer_id)->whereDay('add_time')->count();
  140. //昨日新增用户
  141. $yesterday_user = User::where('mer_id', $mer_id)->whereDay('add_time', 'yesterday')->count();
  142. //新增用户日同比
  143. $user_today_ratio = $this->growth($today_user, $yesterday_user);
  144. //本周新增用户
  145. $this_week_user = User::where('mer_id', $mer_id)->whereWeek('add_time')->count();
  146. //上周新增用户
  147. $last_week_user = User::where('mer_id', $mer_id)->whereWeek('add_time', 'last week')->count();
  148. //新增用户周同比
  149. $user_week_ratio = $this->growth($this_week_user, $last_week_user);
  150. //所有用户
  151. $total_user = User::where('mer_id', $mer_id)->count();
  152. $user = [
  153. 'today' => $today_user,
  154. 'yesterday' => $yesterday_user,
  155. 'today_ratio' => $user_today_ratio,
  156. 'week' => $this_week_user,
  157. 'last_week' => $last_week_user,
  158. 'week_ratio' => $user_week_ratio,
  159. 'total' => $total_user . '人',
  160. 'date' => '昨日'
  161. ];
  162. // $qrcode = MerchantMiniprogram::vaildWhere()->where('mer_id', $this->merId)->field('test_qrcode_url,qrcode_url')->find();
  163. $info = array_values(compact('sales', 'visits', 'order', 'user'));
  164. $info[0]['title'] = '销售额';
  165. $info[1]['title'] = '用户访问量';
  166. $info[2]['title'] = '订单量';
  167. $info[3]['title'] = '新增用户';
  168. $info[0]['total_name'] = '总销售额';
  169. $info[1]['total_name'] = '总访问量';
  170. $info[2]['total_name'] = '总订单量';
  171. $info[3]['total_name'] = '总用户';
  172. return $this->success(compact('info'));
  173. }
  174. //增长率
  175. public function growth($left, $right)
  176. {
  177. if ($right)
  178. $ratio = bcmul(bcdiv(bcsub($left, $right, 2), $right, 4), 100, 2);
  179. else {
  180. if ($left)
  181. $ratio = 100;
  182. else
  183. $ratio = 0;
  184. }
  185. return $ratio;
  186. }
  187. /**
  188. * 订单图表
  189. */
  190. public function orderChart()
  191. {
  192. $mer_id = $this->merId ?: '';
  193. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  194. $datalist = [];
  195. switch ($cycle) {
  196. case 'thirtyday':
  197. $datebefor = date('Y-m-d', strtotime('-30 day'));
  198. $dateafter = date('Y-m-d');
  199. //上期
  200. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  201. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  202. for ($i = -30; $i < 0; $i++) {
  203. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  204. }
  205. $order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  206. ->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price")
  207. ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
  208. ->order('add_time asc')
  209. ->select()->toArray();
  210. if (empty($order_list)) return app('json')->success();
  211. foreach ($order_list as $k => &$v) {
  212. $order_list[$v['day']] = $v;
  213. }
  214. $cycle_list = [];
  215. foreach ($datalist as $dk => $dd) {
  216. if (!empty($order_list[$dd])) {
  217. $cycle_list[$dd] = $order_list[$dd];
  218. } else {
  219. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  220. }
  221. }
  222. $chartdata = [];
  223. $data = [];//临时
  224. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  225. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  226. foreach ($cycle_list as $k => $v) {
  227. $data['day'][] = $v['day'];
  228. $data['count'][] = $v['count'];
  229. $data['price'][] = round($v['price'], 2);
  230. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  231. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  232. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  233. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  234. }
  235. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  236. $chartdata['xAxis'] = $data['day'];//X轴值
  237. //,'itemStyle'=>$series
  238. // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  239. $series1 = ['normal' => ['color' => [
  240. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  241. 'colorStops' => [
  242. [
  243. 'offset' => 0,
  244. 'color' => '#69cdff'
  245. ],
  246. [
  247. 'offset' => 0.5,
  248. 'color' => '#3eb3f7'
  249. ],
  250. [
  251. 'offset' => 1,
  252. 'color' => '#1495eb'
  253. ]
  254. ]
  255. ]]
  256. ];
  257. $series2 = ['normal' => ['color' => [
  258. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  259. 'colorStops' => [
  260. [
  261. 'offset' => 0,
  262. 'color' => '#6fdeab'
  263. ],
  264. [
  265. 'offset' => 0.5,
  266. 'color' => '#44d693'
  267. ],
  268. [
  269. 'offset' => 1,
  270. 'color' => '#2cc981'
  271. ]
  272. ]
  273. ]]
  274. ];
  275. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['price']];//分类1值
  276. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['count']];//分类2值
  277. //统计总数上期
  278. $pre_total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  279. ->field("count(*) as count,sum(pay_price) as price")
  280. ->find();
  281. if ($pre_total) {
  282. $chartdata['pre_cycle']['count'] = [
  283. 'data' => $pre_total['count'] ?: 0
  284. ];
  285. $chartdata['pre_cycle']['price'] = [
  286. 'data' => $pre_total['price'] ?: 0
  287. ];
  288. }
  289. //统计总数
  290. $total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  291. ->field("count(*) as count,sum(pay_price) as price")
  292. ->find();
  293. if ($total) {
  294. $cha_count = intval($pre_total['count']) - intval($total['count']);
  295. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  296. $chartdata['cycle']['count'] = [
  297. 'data' => $total['count'] ?: 0,
  298. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  299. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  300. ];
  301. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  302. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  303. $chartdata['cycle']['price'] = [
  304. 'data' => $total['price'] ?: 0,
  305. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  306. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  307. ];
  308. }
  309. return $this->success($chartdata);
  310. break;
  311. case 'week':
  312. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  313. $datebefor = date('Y-m-d', strtotime('-1 week Monday'));
  314. $dateafter = date('Y-m-d', strtotime('-1 week Sunday'));
  315. $order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  316. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  317. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  318. ->order('add_time asc')
  319. ->select()->toArray();
  320. //数据查询重新处理
  321. $new_order_list = [];
  322. foreach ($order_list as $k => $v) {
  323. $new_order_list[$v['day']] = $v;
  324. }
  325. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  326. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  327. $now_order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  328. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  329. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  330. ->order('add_time asc')
  331. ->select()->toArray();
  332. //数据查询重新处理 key 变为当前值
  333. $new_now_order_list = [];
  334. foreach ($now_order_list as $k => $v) {
  335. $new_now_order_list[$v['day']] = $v;
  336. }
  337. foreach ($weekarray as $dk => $dd) {
  338. if (!empty($new_order_list[$dk])) {
  339. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  340. } else {
  341. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  342. }
  343. if (!empty($new_now_order_list[$dk])) {
  344. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  345. } else {
  346. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  347. }
  348. }
  349. $chartdata = [];
  350. $data = [];//临时
  351. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  352. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  353. foreach ($weekarray as $k => $v) {
  354. $data['day'][] = $v[0];
  355. $data['pre']['count'][] = $v['pre']['count'];
  356. $data['pre']['price'][] = round($v['pre']['price'], 2);
  357. $data['now']['count'][] = $v['now']['count'];
  358. $data['now']['price'][] = round($v['now']['price'], 2);
  359. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  360. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  361. }
  362. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  363. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  364. }
  365. }
  366. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  367. $chartdata['xAxis'] = $data['day'];//X轴值
  368. //,'itemStyle'=>$series
  369. // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  370. $series1 = ['normal' => ['color' => [
  371. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  372. 'colorStops' => [
  373. [
  374. 'offset' => 0,
  375. 'color' => '#69cdff'
  376. ],
  377. [
  378. 'offset' => 0.5,
  379. 'color' => '#3eb3f7'
  380. ],
  381. [
  382. 'offset' => 1,
  383. 'color' => '#1495eb'
  384. ]
  385. ]
  386. ]]
  387. ];
  388. $series2 = ['normal' => ['color' => [
  389. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  390. 'colorStops' => [
  391. [
  392. 'offset' => 0,
  393. 'color' => '#6fdeab'
  394. ],
  395. [
  396. 'offset' => 0.5,
  397. 'color' => '#44d693'
  398. ],
  399. [
  400. 'offset' => 1,
  401. 'color' => '#2cc981'
  402. ]
  403. ]
  404. ]]
  405. ];
  406. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  407. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  408. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值
  409. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值
  410. //统计总数上期
  411. $pre_total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  412. ->field("count(*) as count,sum(pay_price) as price")
  413. ->find();
  414. if ($pre_total) {
  415. $chartdata['pre_cycle']['count'] = [
  416. 'data' => $pre_total['count'] ?: 0
  417. ];
  418. $chartdata['pre_cycle']['price'] = [
  419. 'data' => $pre_total['price'] ?: 0
  420. ];
  421. }
  422. //统计总数
  423. $total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  424. ->field("count(*) as count,sum(pay_price) as price")
  425. ->find();
  426. if ($total) {
  427. $cha_count = intval($pre_total['count']) - intval($total['count']);
  428. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  429. $chartdata['cycle']['count'] = [
  430. 'data' => $total['count'] ?: 0,
  431. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  432. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  433. ];
  434. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  435. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  436. $chartdata['cycle']['price'] = [
  437. 'data' => $total['price'] ?: 0,
  438. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  439. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  440. ];
  441. }
  442. return $this->success($chartdata);
  443. break;
  444. case 'month':
  445. $weekarray = array('01' => ['1'], '02' => ['2'], '03' => ['3'], '04' => ['4'], '05' => ['5'], '06' => ['6'], '07' => ['7'], '08' => ['8'], '09' => ['9'], '10' => ['10'], '11' => ['11'], '12' => ['12'], '13' => ['13'], '14' => ['14'], '15' => ['15'], '16' => ['16'], '17' => ['17'], '18' => ['18'], '19' => ['19'], '20' => ['20'], '21' => ['21'], '22' => ['22'], '23' => ['23'], '24' => ['24'], '25' => ['25'], '26' => ['26'], '27' => ['27'], '28' => ['28'], '29' => ['29'], '30' => ['30'], '31' => ['31']);
  446. $datebefor = date('Y-m-01', strtotime('-1 month'));
  447. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  448. $order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  449. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  450. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  451. ->order('add_time asc')
  452. ->select()->toArray();
  453. //数据查询重新处理
  454. $new_order_list = [];
  455. foreach ($order_list as $k => $v) {
  456. $new_order_list[$v['day']] = $v;
  457. }
  458. $now_datebefor = date('Y-m-01');
  459. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  460. $now_order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  461. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  462. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  463. ->order('add_time asc')
  464. ->select()->toArray();
  465. //数据查询重新处理 key 变为当前值
  466. $new_now_order_list = [];
  467. foreach ($now_order_list as $k => $v) {
  468. $new_now_order_list[$v['day']] = $v;
  469. }
  470. foreach ($weekarray as $dk => $dd) {
  471. if (!empty($new_order_list[$dk])) {
  472. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  473. } else {
  474. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  475. }
  476. if (!empty($new_now_order_list[$dk])) {
  477. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  478. } else {
  479. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  480. }
  481. }
  482. $chartdata = [];
  483. $data = [];//临时
  484. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  485. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  486. foreach ($weekarray as $k => $v) {
  487. $data['day'][] = $v[0];
  488. $data['pre']['count'][] = $v['pre']['count'];
  489. $data['pre']['price'][] = round($v['pre']['price'], 2);
  490. $data['now']['count'][] = $v['now']['count'];
  491. $data['now']['price'][] = round($v['now']['price'], 2);
  492. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  493. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  494. }
  495. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  496. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  497. }
  498. }
  499. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  500. $chartdata['xAxis'] = $data['day'];//X轴值
  501. //,'itemStyle'=>$series
  502. // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  503. $series1 = ['normal' => ['color' => [
  504. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  505. 'colorStops' => [
  506. [
  507. 'offset' => 0,
  508. 'color' => '#69cdff'
  509. ],
  510. [
  511. 'offset' => 0.5,
  512. 'color' => '#3eb3f7'
  513. ],
  514. [
  515. 'offset' => 1,
  516. 'color' => '#1495eb'
  517. ]
  518. ]
  519. ]]
  520. ];
  521. $series2 = ['normal' => ['color' => [
  522. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  523. 'colorStops' => [
  524. [
  525. 'offset' => 0,
  526. 'color' => '#6fdeab'
  527. ],
  528. [
  529. 'offset' => 0.5,
  530. 'color' => '#44d693'
  531. ],
  532. [
  533. 'offset' => 1,
  534. 'color' => '#2cc981'
  535. ]
  536. ]
  537. ]]
  538. ];
  539. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  540. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  541. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值
  542. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值
  543. //统计总数上期
  544. $pre_total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  545. ->field("count(*) as count,sum(pay_price) as price")
  546. ->find();
  547. if ($pre_total) {
  548. $chartdata['pre_cycle']['count'] = [
  549. 'data' => $pre_total['count'] ?: 0
  550. ];
  551. $chartdata['pre_cycle']['price'] = [
  552. 'data' => $pre_total['price'] ?: 0
  553. ];
  554. }
  555. //统计总数
  556. $total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  557. ->field("count(*) as count,sum(pay_price) as price")
  558. ->find();
  559. if ($total) {
  560. $cha_count = intval($pre_total['count']) - intval($total['count']);
  561. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  562. $chartdata['cycle']['count'] = [
  563. 'data' => $total['count'] ?: 0,
  564. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  565. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  566. ];
  567. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  568. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  569. $chartdata['cycle']['price'] = [
  570. 'data' => $total['price'] ?: 0,
  571. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  572. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  573. ];
  574. }
  575. return $this->success($chartdata);
  576. break;
  577. case 'year':
  578. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  579. $datebefor = date('Y-01-01', strtotime('-1 year'));
  580. $dateafter = date('Y-12-31', strtotime('-1 year'));
  581. $order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  582. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  583. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  584. ->order('add_time asc')
  585. ->select()->toArray();
  586. //数据查询重新处理
  587. $new_order_list = [];
  588. foreach ($order_list as $k => $v) {
  589. $new_order_list[$v['day']] = $v;
  590. }
  591. $now_datebefor = date('Y-01-01');
  592. $now_dateafter = date('Y-m-d');
  593. $now_order_list = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  594. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  595. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  596. ->order('add_time asc')
  597. ->select()->toArray();
  598. //数据查询重新处理 key 变为当前值
  599. $new_now_order_list = [];
  600. foreach ($now_order_list as $k => $v) {
  601. $new_now_order_list[$v['day']] = $v;
  602. }
  603. foreach ($weekarray as $dk => $dd) {
  604. if (!empty($new_order_list[$dk])) {
  605. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  606. } else {
  607. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  608. }
  609. if (!empty($new_now_order_list[$dk])) {
  610. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  611. } else {
  612. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  613. }
  614. }
  615. $chartdata = [];
  616. $data = [];//临时
  617. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  618. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  619. foreach ($weekarray as $k => $v) {
  620. $data['day'][] = $v[0];
  621. $data['pre']['count'][] = $v['pre']['count'];
  622. $data['pre']['price'][] = round($v['pre']['price'], 2);
  623. $data['now']['count'][] = $v['now']['count'];
  624. $data['now']['price'][] = round($v['now']['price'], 2);
  625. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  626. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  627. }
  628. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  629. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  630. }
  631. }
  632. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  633. $chartdata['xAxis'] = $data['day'];//X轴值
  634. //,'itemStyle'=>$series
  635. // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  636. $series1 = ['normal' => ['color' => [
  637. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  638. 'colorStops' => [
  639. [
  640. 'offset' => 0,
  641. 'color' => '#69cdff'
  642. ],
  643. [
  644. 'offset' => 0.5,
  645. 'color' => '#3eb3f7'
  646. ],
  647. [
  648. 'offset' => 1,
  649. 'color' => '#1495eb'
  650. ]
  651. ]
  652. ]]
  653. ];
  654. $series2 = ['normal' => ['color' => [
  655. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  656. 'colorStops' => [
  657. [
  658. 'offset' => 0,
  659. 'color' => '#6fdeab'
  660. ],
  661. [
  662. 'offset' => 0.5,
  663. 'color' => '#44d693'
  664. ],
  665. [
  666. 'offset' => 1,
  667. 'color' => '#2cc981'
  668. ]
  669. ]
  670. ]]
  671. ];
  672. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  673. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  674. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值
  675. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值
  676. //统计总数上期
  677. $pre_total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$datebefor, $dateafter])
  678. ->field("count(*) as count,sum(pay_price) as price")
  679. ->find();
  680. if ($pre_total) {
  681. $chartdata['pre_cycle']['count'] = [
  682. 'data' => $pre_total['count'] ?: 0
  683. ];
  684. $chartdata['pre_cycle']['price'] = [
  685. 'data' => $pre_total['price'] ?: 0
  686. ];
  687. }
  688. //统计总数
  689. $total = StoreOrder::where('mer_id', $mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  690. ->field("count(*) as count,sum(pay_price) as price")
  691. ->find();
  692. if ($total) {
  693. $cha_count = intval($pre_total['count']) - intval($total['count']);
  694. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  695. $chartdata['cycle']['count'] = [
  696. 'data' => $total['count'] ?: 0,
  697. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  698. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  699. ];
  700. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  701. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  702. $chartdata['cycle']['price'] = [
  703. 'data' => $total['price'] ?: 0,
  704. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  705. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  706. ];
  707. }
  708. return $this->success($chartdata);
  709. break;
  710. default:
  711. break;
  712. }
  713. }
  714. /**
  715. * 用户图表
  716. */
  717. public function userChart()
  718. {
  719. $mer_id = $this->merId ?: '';
  720. $starday = date('Y-m-d', strtotime('-30 day'));
  721. $yesterday = date('Y-m-d');
  722. $user_list = User::where('mer_id', $mer_id)->where('add_time', 'between time', [$starday, $yesterday])
  723. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  724. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  725. ->order('add_time asc')
  726. ->select()->toArray();
  727. $chartdata = [];
  728. $data = [];
  729. $chartdata['legend'] = ['用户数'];//分类
  730. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  731. $chartdata['xAxis'] = [date('m-d')];//X轴值
  732. $chartdata['series'] = [0];//分类1值
  733. if (!empty($user_list)) {
  734. foreach ($user_list as $k => $v) {
  735. $data['day'][] = $v['day'];
  736. $data['count'][] = $v['count'];
  737. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  738. $chartdata['yAxis']['maxnum'] = $v['count'];
  739. }
  740. $chartdata['xAxis'] = $data['day'];//X轴值
  741. $chartdata['series'] = $data['count'];//分类1值
  742. }
  743. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  744. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  745. $pay[0] = User::where('mer_id', $mer_id)->where('pay_count', 0)->count();
  746. $pay[1] = User::where('mer_id', $mer_id)->where('pay_count', 1)->count();
  747. $pay[2] = User::where('mer_id', $mer_id)->where('pay_count', '>', 0)->where('pay_count', '<', 4)->count();
  748. $pay[3] = User::where('mer_id', $mer_id)->where('pay_count', '>', 4)->count();
  749. foreach ($pay as $key => $item) {
  750. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  751. }
  752. $chartdata['bing_data'] = $bing_data;
  753. return $this->success($chartdata);
  754. }
  755. /**
  756. * 交易额排行
  757. * @return mixed
  758. */
  759. public function purchaseRanking()
  760. {
  761. $mer_id = $this->merId ?: '';
  762. $dlist = StoreProductAttrValue::alias('v')
  763. ->where('p.mer_id', $mer_id)
  764. ->join('store_product p', 'v.product_id=p.id')
  765. ->field('v.product_id,p.store_name,sum(v.sales * v.price) as val')->group('v.product_id')->order('val', 'desc')->limit(20)->select()->toArray();
  766. $slist = StoreProduct::where('mer_id', $mer_id)->field('id as product_id,store_name,sales * price as val')->where('is_del', 0)->order('val', 'desc')->limit(20)->select()->toArray();
  767. $data = array_merge($dlist, $slist);
  768. $last_names = array_column($data, 'val');
  769. array_multisort($last_names, SORT_DESC, $data);
  770. $list = array_splice($data, 0, 20);
  771. return $this->success(compact('list'));
  772. }
  773. /**
  774. * 待办事统计
  775. * @return mixed
  776. */
  777. public function jnotice()
  778. {
  779. $mer_id = $this->merId ?: '';
  780. return $this->success(NoticeRepositories::jnotice($mer_id));
  781. }
  782. //获取待发货订单数量
  783. public function getOrderNum()
  784. {
  785. $mer_id = $this->merId ?: '';
  786. $ordernum = StoreOrder::where('mer_id', $mer_id)->where('paid', 1)->where('status', 0)
  787. ->where('shipping_type', 1)->where('refund_status', 0)
  788. ->where('is_del', 0)->count();
  789. return $this->success($ordernum);
  790. }
  791. }