Index.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\store\StoreProduct;
  13. use app\admin\model\system\SystemConfig;
  14. use app\admin\model\system\SystemMenus;
  15. use app\admin\model\system\SystemRole;
  16. use app\admin\model\order\StoreOrder as StoreOrderModel;//订单
  17. use app\admin\model\user\UserExtract as UserExtractModel;//分销
  18. use app\admin\model\user\MemberRecord as MemberRecordModel;//会员购买记录
  19. use app\admin\model\user\User as UserModel;//用户
  20. use FormBuilder\Json;
  21. use service\JsonService;
  22. use think\Cache;
  23. use think\DB;
  24. /**
  25. * 首页控制器
  26. * Class Index
  27. * @package app\admin\controller
  28. *
  29. */
  30. class Index extends AuthController
  31. {
  32. public function index()
  33. {
  34. //获取当前登录后台的管理员信息
  35. $adminInfo = $this->adminInfo->toArray();
  36. $roles = explode(',', $adminInfo['roles']);
  37. $site_logo = SystemConfig::getOneConfig('menu_name', 'site_logo')->toArray();
  38. $this->assign([
  39. 'menuList' => SystemMenus::menuList(),
  40. 'site_logo' => json_decode($site_logo['value'], true),
  41. 'role_name' => SystemRole::where('id', $roles[0])->field('role_name')->find()
  42. ]);
  43. return $this->fetch();
  44. }
  45. //后台首页内容
  46. public function main()
  47. {
  48. /*首页第一行统计*/
  49. $now_day = strtotime(date('Y-m-d'));//今日
  50. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨天时间戳
  51. //新增学员->日
  52. $now_day_user = DB::name('User')->where('add_time', 'gt', $now_day)->count();
  53. $pre_day_user = DB::name('User')->where('add_time', 'gt', $pre_day)->where('add_time', 'lt', $now_day)->count();
  54. $pre_day_user = $pre_day_user ? $pre_day_user : 0;
  55. $first_line['day'] = [
  56. 'data' => $now_day_user ? $now_day_user : 0,
  57. 'percent' => abs($now_day_user - $pre_day_user),
  58. 'is_plus' => $now_day_user - $pre_day_user > 0 ? 1 : ($now_day_user - $pre_day_user == 0 ? -1 : 0)
  59. ];
  60. //学习次数->日
  61. $now_day_user = DB::name('learning_records')->where('add_time', 'gt', $now_day)->count();
  62. $pre_day_user = DB::name('learning_records')->where('add_time', 'gt', $pre_day)->where('add_time', 'lt', $now_day)->count();
  63. $pre_day_user = $pre_day_user ? $pre_day_user : 0;
  64. $first_line['records'] = [
  65. 'data' => $now_day_user ? $now_day_user : 0,
  66. 'percent' => abs($now_day_user - $pre_day_user),
  67. 'is_plus' => $now_day_user - $pre_day_user > 0 ? 1 : ($now_day_user - $pre_day_user == 0 ? -1 : 0)
  68. ];
  69. //课程订单数->今日
  70. $now_day_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>0])->where('pay_time', 'gt', $now_day)->count();
  71. $pre_day_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>0])->where('pay_time', 'gt', $pre_day)->where('pay_time', 'lt', $now_day)->count();
  72. $first_line['d_num'] = [
  73. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  74. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  75. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  76. ];
  77. //课程交易额->今天
  78. $now_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>0])->where('pay_time', 'gt', $now_day)->value('sum(pay_price)');
  79. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>0])->where('pay_time', 'gt', $pre_day)->where('pay_time', 'lt', $now_day)->value('sum(pay_price)');
  80. $first_line['d_price'] = [
  81. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  82. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  83. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  84. ];
  85. //商品订单数->今日
  86. $now_day_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>2])->where('pay_time', 'gt', $now_day)->count();
  87. $pre_day_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>2])->where('pay_time', 'gt', $pre_day)->where('pay_time', 'lt', $now_day)->count();
  88. $first_line['d_store_num'] = [
  89. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  90. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  91. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  92. ];
  93. //商品交易额->今天
  94. $now_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>2])->where('pay_time', 'gt', $now_day)->value('sum(pay_price)');
  95. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>2])->where('pay_time', 'gt', $pre_day)->where('pay_time', 'lt', $now_day)->value('sum(pay_price)');
  96. $first_line['d_store_price'] = [
  97. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  98. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  99. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  100. ];
  101. //新增会员->今日
  102. $now_day_order_p = DB::name('User')->where('level',1)->where('member_time', 'gt', $now_day)->count();
  103. $pre_day_order_p = DB::name('User')->where('level',1)->where('member_time', 'gt', $pre_day)->where('member_time', 'lt', $now_day)->count();
  104. $first_line['d_vip_num'] = [
  105. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  106. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  107. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  108. ];
  109. //会员充值->今天
  110. $now_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>1])->where('pay_time', 'gt', $now_day)->value('sum(pay_price)');
  111. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where(['is_del'=>0,'type'=>1])->where('pay_time', 'gt', $pre_day)->where('pay_time', 'lt', $now_day)->value('sum(pay_price)');
  112. $first_line['d_vip_price'] = [
  113. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  114. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  115. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  116. ];
  117. $this->assign([
  118. 'ip'=>get_server_ip(),
  119. 'first_line' => $first_line,
  120. ]);
  121. return $this->fetch();
  122. }
  123. /**
  124. * 订单图表
  125. */
  126. public function orderchart()
  127. {
  128. $cycle = $this->request->get('cycle', 'thirtyday');//默认30天
  129. $datalist = [];
  130. switch ($cycle) {
  131. case 'thirtyday':
  132. $datebefor = date('Y-m-d', strtotime('-30 day'));
  133. $dateafter = date('Y-m-d');
  134. //上期
  135. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  136. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  137. for ($i = -30; $i < 0; $i++) {
  138. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  139. }
  140. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  141. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price")
  142. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")->order('add_time asc')->select()->toArray();
  143. if (empty($order_list)) {
  144. return JsonService::successful([]);
  145. }
  146. foreach ($order_list as $k => &$v) {
  147. $order_list[$v['day']] = $v;
  148. }
  149. $cycle_list = [];
  150. foreach ($datalist as $dk => $dd) {
  151. if (!empty($order_list[$dd])) {
  152. $cycle_list[$dd] = $order_list[$dd];
  153. } else {
  154. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  155. }
  156. }
  157. $chartdata = [];
  158. $data = [];//临时
  159. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  160. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  161. foreach ($cycle_list as $k => $v) {
  162. $data['day'][] = $v['day'];
  163. $data['count'][] = $v['count'];
  164. $data['price'][] = round($v['price'], 2);
  165. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  166. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  167. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  168. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  169. }
  170. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  171. $chartdata['xAxis'] = $data['day'];//X轴值
  172. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  173. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['price']];//分类1值
  174. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['count']];//分类2值
  175. //统计总数上期
  176. $pre_total = StoreOrderModel::where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  177. ->field("count(*) as count,sum(pay_price) as price")->where('is_del',0)->where('paid',1)
  178. ->find();
  179. if ($pre_total) {
  180. $chartdata['pre_cycle']['count'] = [
  181. 'data' => $pre_total['count'] ?: 0
  182. ];
  183. $chartdata['pre_cycle']['price'] = [
  184. 'data' => $pre_total['price'] ?: 0
  185. ];
  186. }
  187. //统计总数
  188. $total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  189. ->field("count(*) as count,sum(pay_price) as price")->where('is_del',0)->where('paid',1)
  190. ->find();
  191. if ($total) {
  192. $cha_count = intval($pre_total['count']) - intval($total['count']);
  193. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  194. $chartdata['cycle']['count'] = [
  195. 'data' => $total['count'] ?: 0,
  196. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  197. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  198. ];
  199. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  200. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  201. $chartdata['cycle']['price'] = [
  202. 'data' => $total['price'] ?: 0,
  203. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  204. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  205. ];
  206. }
  207. return JsonService::successful('ok', $chartdata);
  208. break;
  209. case 'week':
  210. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  211. $datebefor = date('Y-m-d', strtotime('-2 monday', time()));
  212. $dateafter = date('Y-m-d', strtotime('-1 sunday', time()));
  213. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  214. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  215. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  216. ->order('add_time asc')
  217. ->select()->toArray();
  218. //数据查询重新处理
  219. $new_order_list = [];
  220. foreach ($order_list as $k => $v) {
  221. $new_order_list[$v['day']] = $v;
  222. }
  223. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  224. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  225. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  226. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  227. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  228. ->order('add_time asc')
  229. ->select()->toArray();
  230. //数据查询重新处理 key 变为当前值
  231. $new_now_order_list = [];
  232. foreach ($now_order_list as $k => $v) {
  233. $new_now_order_list[$v['day']] = $v;
  234. }
  235. foreach ($weekarray as $dk => $dd) {
  236. if (!empty($new_order_list[$dk])) {
  237. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  238. } else {
  239. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  240. }
  241. if (!empty($new_now_order_list[$dk])) {
  242. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  243. } else {
  244. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  245. }
  246. }
  247. $chartdata = [];
  248. $data = [];//临时
  249. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  250. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  251. foreach ($weekarray as $k => $v) {
  252. $data['day'][] = $v[0];
  253. $data['pre']['count'][] = $v['pre']['count'];
  254. $data['pre']['price'][] = round($v['pre']['price'], 2);
  255. $data['now']['count'][] = $v['now']['count'];
  256. $data['now']['price'][] = round($v['now']['price'], 2);
  257. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  258. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  259. }
  260. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  261. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  262. }
  263. }
  264. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  265. $chartdata['xAxis'] = $data['day'];//X轴值
  266. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  267. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  268. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  269. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  270. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  271. //统计总数上期
  272. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  273. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  274. ->find();
  275. if ($pre_total) {
  276. $chartdata['pre_cycle']['count'] = [
  277. 'data' => $pre_total['count'] ?: 0
  278. ];
  279. $chartdata['pre_cycle']['price'] = [
  280. 'data' => $pre_total['price'] ?: 0
  281. ];
  282. }
  283. //统计总数
  284. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  285. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  286. ->find();
  287. if ($total) {
  288. $cha_count = intval($pre_total['count']) - intval($total['count']);
  289. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  290. $chartdata['cycle']['count'] = [
  291. 'data' => $total['count'] ?: 0,
  292. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  293. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  294. ];
  295. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  296. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  297. $chartdata['cycle']['price'] = [
  298. 'data' => $total['price'] ?: 0,
  299. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  300. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  301. ];
  302. }
  303. return JsonService::successful('ok', $chartdata);
  304. break;
  305. case 'month':
  306. $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']);
  307. $datebefor = date('Y-m-01', strtotime('-1 month'));
  308. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  309. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  310. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  311. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  312. ->order('add_time asc')
  313. ->select()->toArray();
  314. //数据查询重新处理
  315. $new_order_list = [];
  316. foreach ($order_list as $k => $v) {
  317. $new_order_list[$v['day']] = $v;
  318. }
  319. $now_datebefor = date('Y-m-01');
  320. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  321. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  322. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  323. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  324. ->order('add_time asc')
  325. ->select()->toArray();
  326. //数据查询重新处理 key 变为当前值
  327. $new_now_order_list = [];
  328. foreach ($now_order_list as $k => $v) {
  329. $new_now_order_list[$v['day']] = $v;
  330. }
  331. foreach ($weekarray as $dk => $dd) {
  332. if (!empty($new_order_list[$dk])) {
  333. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  334. } else {
  335. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  336. }
  337. if (!empty($new_now_order_list[$dk])) {
  338. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  339. } else {
  340. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  341. }
  342. }
  343. $chartdata = [];
  344. $data = [];//临时
  345. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  346. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  347. foreach ($weekarray as $k => $v) {
  348. $data['day'][] = $v[0];
  349. $data['pre']['count'][] = $v['pre']['count'];
  350. $data['pre']['price'][] = round($v['pre']['price'], 2);
  351. $data['now']['count'][] = $v['now']['count'];
  352. $data['now']['price'][] = round($v['now']['price'], 2);
  353. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  354. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  355. }
  356. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  357. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  358. }
  359. }
  360. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  361. $chartdata['xAxis'] = $data['day'];//X轴值
  362. //,'itemStyle'=>$series
  363. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  364. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  365. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  366. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  367. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  368. //统计总数上期
  369. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  370. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  371. ->find();
  372. if ($pre_total) {
  373. $chartdata['pre_cycle']['count'] = [
  374. 'data' => $pre_total['count'] ?: 0
  375. ];
  376. $chartdata['pre_cycle']['price'] = [
  377. 'data' => $pre_total['price'] ?: 0
  378. ];
  379. }
  380. //统计总数
  381. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  382. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  383. ->find();
  384. if ($total) {
  385. $cha_count = intval($pre_total['count']) - intval($total['count']);
  386. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  387. $chartdata['cycle']['count'] = [
  388. 'data' => $total['count'] ?: 0,
  389. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  390. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  391. ];
  392. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  393. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  394. $chartdata['cycle']['price'] = [
  395. 'data' => $total['price'] ?: 0,
  396. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  397. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  398. ];
  399. }
  400. return JsonService::successful('ok', $chartdata);
  401. break;
  402. case 'year':
  403. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  404. $datebefor = date('Y-01-01', strtotime('-1 year'));
  405. $dateafter = date('Y-12-31', strtotime('-1 year'));
  406. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  407. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  408. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  409. ->order('add_time asc')
  410. ->select()->toArray();
  411. //数据查询重新处理
  412. $new_order_list = [];
  413. foreach ($order_list as $k => $v) {
  414. $new_order_list[$v['day']] = $v;
  415. }
  416. $now_datebefor = date('Y-01-01');
  417. $now_dateafter = date('Y-m-d');
  418. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  419. ->where('is_del',0)->where('paid',1)->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  420. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  421. ->order('add_time asc')
  422. ->select()->toArray();
  423. //数据查询重新处理 key 变为当前值
  424. $new_now_order_list = [];
  425. foreach ($now_order_list as $k => $v) {
  426. $new_now_order_list[$v['day']] = $v;
  427. }
  428. foreach ($weekarray as $dk => $dd) {
  429. if (!empty($new_order_list[$dk])) {
  430. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  431. } else {
  432. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  433. }
  434. if (!empty($new_now_order_list[$dk])) {
  435. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  436. } else {
  437. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  438. }
  439. }
  440. $chartdata = [];
  441. $data = [];//临时
  442. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  443. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  444. foreach ($weekarray as $k => $v) {
  445. $data['day'][] = $v[0];
  446. $data['pre']['count'][] = $v['pre']['count'];
  447. $data['pre']['price'][] = round($v['pre']['price'], 2);
  448. $data['now']['count'][] = $v['now']['count'];
  449. $data['now']['price'][] = round($v['now']['price'], 2);
  450. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  451. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  452. }
  453. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  454. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  455. }
  456. }
  457. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  458. $chartdata['xAxis'] = $data['day'];//X轴值
  459. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  460. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  461. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  462. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  463. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  464. //统计总数上期
  465. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  466. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  467. ->find();
  468. if ($pre_total) {
  469. $chartdata['pre_cycle']['count'] = [
  470. 'data' => $pre_total['count'] ?: 0
  471. ];
  472. $chartdata['pre_cycle']['price'] = [
  473. 'data' => $pre_total['price'] ?: 0
  474. ];
  475. }
  476. //统计总数
  477. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  478. ->where('is_del',0)->where('paid',1)->field("count(*) as count,sum(pay_price) as price")
  479. ->find();
  480. if ($total) {
  481. $cha_count = intval($pre_total['count']) - intval($total['count']);
  482. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  483. $chartdata['cycle']['count'] = [
  484. 'data' => $total['count'] ?: 0,
  485. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  486. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  487. ];
  488. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  489. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  490. $chartdata['cycle']['price'] = [
  491. 'data' => $total['price'] ?: 0,
  492. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  493. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  494. ];
  495. }
  496. return JsonService::successful($chartdata);
  497. break;
  498. default:
  499. return JsonService::successful([]);
  500. break;
  501. }
  502. }
  503. /**
  504. * 用户图表
  505. */
  506. public function userchart()
  507. {
  508. header('Content-type:text/json');
  509. $starday = date('Y-m-d', strtotime('-30 day'));
  510. $yesterday = date('Y-m-d');
  511. $user_list = UserModel::where('add_time', 'between time', [$starday, $yesterday])
  512. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  513. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  514. ->order('add_time asc')
  515. ->select()->toArray();
  516. $chartdata = [];
  517. $data = [];
  518. $chartdata['legend'] = ['用户数'];//分类
  519. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  520. if (empty($user_list)) return Json::fail('无数据');
  521. foreach ($user_list as $k => $v) {
  522. $data['day'][] = $v['day'];
  523. $data['count'][] = $v['count'];
  524. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  525. $chartdata['yAxis']['maxnum'] = $v['count'];
  526. }
  527. $chartdata['xAxis'] = $data['day'];//X轴值
  528. $chartdata['series'] = $data['count'];//分类1值
  529. return Json::succ('ok', $chartdata);
  530. }
  531. /**待办事统计
  532. * @param Request|null $request
  533. */
  534. public function Jnotice()
  535. {
  536. header('Content-type:text/json');
  537. $data = [];
  538. $data['reflectnum'] = UserExtractModel::where('status', 0)->count();;//提现
  539. $data['msgcount'] = intval(intval($data['reflectnum']));
  540. return Json::succ('ok', $data);
  541. }
  542. public function check_auth(){
  543. return Json::succ('ok', $this->checkAuthDecrypt());
  544. }
  545. /**
  546. * @return mixed
  547. */
  548. public function auth()
  549. {
  550. return $this->fetch();
  551. }
  552. public function auth_data()
  553. {
  554. return Json::succ('ok', $this->getAuth());
  555. }
  556. }