Index.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\store\StoreProduct;
  4. use app\admin\model\system\SystemConfig;
  5. use app\admin\model\system\SystemMenus;
  6. use app\admin\model\system\SystemRole;
  7. use app\admin\model\order\StoreOrder as StoreOrderModel;//订单
  8. use app\admin\model\user\UserExtract as UserExtractModel;//分销
  9. use app\admin\model\user\User as UserModel;//用户
  10. use app\admin\model\store\StoreProductReply as StoreProductReplyModel;//评论
  11. use app\admin\model\store\StoreProduct as ProductModel;//产品
  12. use FormBuilder\Json;
  13. use think\DB;
  14. /**
  15. * 首页控制器
  16. * Class Index
  17. * @package app\admin\controller
  18. *
  19. */
  20. class Index extends AuthController
  21. {
  22. public function index()
  23. {
  24. //获取当前登录后台的管理员信息
  25. $adminInfo = $this->adminInfo->toArray();
  26. $roles = explode(',',$adminInfo['roles']);
  27. $site_logo = SystemConfig::getOneConfig('menu_name','site_logo')->toArray();
  28. // dump(SystemMenus::menuList());
  29. // exit();
  30. $this->assign([
  31. 'menuList'=>SystemMenus::menuList(),
  32. 'site_logo'=>json_decode($site_logo['value'],true),
  33. 'role_name'=>SystemRole::where('id',$roles[0])->field('role_name')->find()
  34. ]);
  35. return $this->fetch();
  36. }
  37. //后台首页内容
  38. public function main()
  39. {
  40. /*首页第一行统计*/
  41. $now_month = strtotime(date('Y-m'));//本月
  42. $pre_month = strtotime(date('Y-m',strtotime('-1 month')));//上月
  43. $now_day = strtotime(date('Y-m-d'));//今日
  44. $pre_day = strtotime(date('Y-m-d',strtotime('-1 day')));//昨天时间戳
  45. $beforyester_day = strtotime(date('Y-m-d',strtotime('-2 day')));//前天时间戳
  46. //待发货数量
  47. $topData['orderDeliveryNum'] = StoreOrderModel::where('status',0)
  48. ->where('paid',1)
  49. ->where('refund_status',0)
  50. ->count();
  51. //退换货订单数
  52. $topData['orderRefundNum'] = StoreOrderModel::where('paid',1)
  53. ->where('refund_status','IN','1')
  54. ->count();
  55. //库存预警
  56. $replenishment_num = SystemConfig::getValue('store_stock') > 0 ? SystemConfig::getValue('store_stock') : 20;//库存预警界限
  57. $topData['stockProduct'] = StoreProduct::where('stock','<=',$replenishment_num)->where('is_show',1)->where('is_del',0)->count();
  58. //待处理提现
  59. $topData['treatedExtract'] = UserExtractModel::where('status',0)->count();
  60. //订单数->日
  61. $now_day_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$now_day)->count();
  62. $pre_day_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$pre_day)->where('pay_time','lt',$now_day)->count();
  63. $first_line['d_num'] = [
  64. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  65. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  66. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  67. ];
  68. //交易额->昨天
  69. $now_month_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$pre_day)->value('sum(pay_price)');
  70. $pre_month_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$beforyester_day)->where('pay_time','lt',$pre_day)->value('sum(pay_price)');
  71. $first_line['d_price'] = [
  72. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  73. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  74. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  75. ];
  76. //交易额->月
  77. $now_month_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$now_month)->value('sum(pay_price)');
  78. $pre_month_order_p = StoreOrderModel::where('paid',1)->where('pay_time','gt',$pre_month)->where('pay_time','lt',$now_month)->value('sum(pay_price)');
  79. $first_line['m_price'] = [
  80. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  81. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  82. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  83. ];
  84. //新粉丝->日
  85. $now_day_user = DB::name('User')->where('add_time','gt',$now_day)->count();
  86. $pre_day_user = DB::name('User')->where('add_time','gt',$pre_day)->where('add_time','lt',$now_day)->count();
  87. $pre_day_user = $pre_day_user ? $pre_day_user : 0;
  88. $first_line['day'] = [
  89. 'data' => $now_day_user ? $now_day_user : 0,
  90. 'percent' => abs($now_day_user - $pre_day_user),
  91. 'is_plus' => $now_day_user - $pre_day_user > 0 ? 1 : ($now_day_user - $pre_day_user == 0 ? -1 : 0)
  92. ];
  93. //新粉丝->月
  94. $now_month_user = DB::name('User')->where('add_time','gt',$now_month)->count();
  95. $pre_month_user = DB::name('User')->where('add_time','gt',$pre_month)->where('add_time','lt',$now_month)->count();
  96. $first_line['month'] = [
  97. 'data' => $now_month_user ? $now_month_user : 0,
  98. 'percent' => abs($now_month_user - $pre_month_user),
  99. 'is_plus' => $now_month_user - $pre_month_user > 0 ? 1 : ($now_month_user - $pre_month_user == 0 ? -1 : 0)
  100. ];
  101. //本月订单总数
  102. $now_order_info_c = StoreOrderModel::where('add_time','gt',$now_month)->count();
  103. $pre_order_info_c = StoreOrderModel::where('add_time','gt',$pre_month)->where('add_time','lt',$now_month)->count();
  104. $order_info['first'] = [
  105. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  106. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  107. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  108. ];
  109. //上月订单总数
  110. $second_now_month = strtotime(date('Y-m',strtotime('-1 month')));
  111. $second_pre_month = strtotime(date('Y-m',strtotime('-2 month')));
  112. $now_order_info_c = StoreOrderModel::where('add_time','gt',$pre_month)->where('add_time','lt',$now_month)->count();
  113. $pre_order_info_c = StoreOrderModel::where('add_time','gt',$second_pre_month)->where('add_time','lt',$second_now_month)->count();
  114. $order_info["second"] = [
  115. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  116. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  117. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  118. ];
  119. $second_line['order_info'] = $order_info;
  120. $this->assign([
  121. 'first_line' => $first_line,
  122. 'second_line' => $second_line,
  123. 'topData' => $topData,
  124. ]);
  125. return $this->fetch();
  126. }
  127. /**
  128. * 订单图表
  129. */
  130. public function orderchart(){
  131. header('Content-type:text/json');
  132. $cycle = $this->request->param('cycle')?:'thirtyday';//默认30天
  133. $datalist = [];
  134. switch ($cycle){
  135. case 'thirtyday':
  136. $datebefor = date('Y-m-d',strtotime('-30 day'));
  137. $dateafter = date('Y-m-d');
  138. //上期
  139. $pre_datebefor = date('Y-m-d',strtotime('-60 day'));
  140. $pre_dateafter = date('Y-m-d',strtotime('-30 day'));
  141. for($i=-30;$i < 0;$i++){
  142. $datalist[date('m-d',strtotime($i.' day'))] = date('m-d',strtotime($i.' day'));
  143. }
  144. $order_list = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  145. ->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price")
  146. ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
  147. ->order('add_time asc')
  148. ->select()->toArray();
  149. if(empty($order_list)) return Json::fail('无数据');
  150. foreach ($order_list as $k=>&$v){
  151. $order_list[$v['day']] = $v;
  152. }
  153. $cycle_list = [];
  154. foreach ($datalist as $dk=>$dd){
  155. if(!empty($order_list[$dd])){
  156. $cycle_list[$dd] = $order_list[$dd];
  157. }else{
  158. $cycle_list[$dd] = ['count'=>0,'day'=>$dd,'price'=>''];
  159. }
  160. }
  161. $chartdata = [];
  162. $data = [];//临时
  163. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  164. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  165. foreach ($cycle_list as $k=>$v){
  166. $data['day'][] = $v['day'];
  167. $data['count'][] = $v['count'];
  168. $data['price'][] = round($v['price'],2);
  169. if($chartdata['yAxis']['maxnum'] < $v['count'])
  170. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  171. if($chartdata['yAxis']['maxprice'] < $v['price'])
  172. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  173. }
  174. $chartdata['legend'] = ['订单金额','订单数'];//分类
  175. $chartdata['xAxis'] = $data['day'];//X轴值
  176. //,'itemStyle'=>$series
  177. $series= ['normal'=>['label'=>['show'=>true,'position'=>'top']]];
  178. $chartdata['series'][] = ['name'=>$chartdata['legend'][0],'type'=>'bar','itemStyle'=>$series,'data'=>$data['price']];//分类1值
  179. $chartdata['series'][] = ['name'=>$chartdata['legend'][1],'type'=>'bar','itemStyle'=>$series,'data'=>$data['count']];//分类2值
  180. //统计总数上期
  181. $pre_total = StoreOrderModel::where('add_time','between time',[$pre_datebefor,$pre_dateafter])
  182. ->field("count(*) as count,sum(pay_price) as price")
  183. ->find();
  184. if($pre_total){
  185. $chartdata['pre_cycle']['count'] = [
  186. 'data' => $pre_total['count']? : 0
  187. ];
  188. $chartdata['pre_cycle']['price'] = [
  189. 'data' => $pre_total['price']? : 0
  190. ];
  191. }
  192. //统计总数
  193. $total = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  194. ->field("count(*) as count,sum(pay_price) as price")
  195. ->find();
  196. if($total){
  197. $cha_count = intval($pre_total['count']) - intval($total['count']);
  198. $pre_total['count'] = $pre_total['count']==0 ? 1 : $pre_total['count'];
  199. $chartdata['cycle']['count'] = [
  200. 'data' => $total['count']? : 0,
  201. 'percent' => round((abs($cha_count)/intval($pre_total['count'])*100),2),
  202. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  203. ];
  204. $cha_price = round($pre_total['price'],2) - round($total['price'],2);
  205. $pre_total['price'] = $pre_total['price']==0 ? 1 : $pre_total['price'];
  206. $chartdata['cycle']['price'] = [
  207. 'data' => $total['price']? : 0,
  208. 'percent' => round(abs($cha_price)/$pre_total['price']*100,2),
  209. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  210. ];
  211. }
  212. return Json::succ('ok',$chartdata);
  213. break;
  214. case 'week':
  215. $weekarray=array(['周日'],['周一'],['周二'],['周三'],['周四'],['周五'],['周六']);
  216. $datebefor = date('Y-m-d',strtotime('-1 week Monday'));
  217. $dateafter = date('Y-m-d',strtotime('-1 week Sunday'));
  218. $order_list = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  219. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  220. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  221. ->order('add_time asc')
  222. ->select()->toArray();
  223. //数据查询重新处理
  224. $new_order_list = [];
  225. foreach ($order_list as $k=>$v){
  226. $new_order_list[$v['day']] = $v;
  227. }
  228. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  229. $now_dateafter = date('Y-m-d',strtotime("+1 day"));
  230. $now_order_list = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_dateafter])
  231. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  232. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  233. ->order('add_time asc')
  234. ->select()->toArray();
  235. //数据查询重新处理 key 变为当前值
  236. $new_now_order_list = [];
  237. foreach ($now_order_list as $k=>$v){
  238. $new_now_order_list[$v['day']] = $v;
  239. }
  240. foreach ($weekarray as $dk=>$dd){
  241. if(!empty($new_order_list[$dk])){
  242. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  243. }else{
  244. $weekarray[$dk]['pre'] = ['count'=>0,'day'=>$weekarray[$dk][0],'price'=>'0'];
  245. }
  246. if(!empty($new_now_order_list[$dk])){
  247. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  248. }else{
  249. $weekarray[$dk]['now'] = ['count'=>0,'day'=>$weekarray[$dk][0],'price'=>'0'];
  250. }
  251. }
  252. $chartdata = [];
  253. $data = [];//临时
  254. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  255. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  256. foreach ($weekarray as $k=>$v){
  257. $data['day'][] = $v[0];
  258. $data['pre']['count'][] = $v['pre']['count'];
  259. $data['pre']['price'][] = round($v['pre']['price'],2);
  260. $data['now']['count'][] = $v['now']['count'];
  261. $data['now']['price'][] = round($v['now']['price'],2);
  262. if($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']){
  263. $chartdata['yAxis']['maxnum'] = $v['pre']['count']>$v['now']['count']?$v['pre']['count']:$v['now']['count'];//日最大订单数
  264. }
  265. if($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']){
  266. $chartdata['yAxis']['maxprice'] = $v['pre']['price']>$v['now']['price']?$v['pre']['price']:$v['now']['price'];//日最大金额
  267. }
  268. }
  269. $chartdata['legend'] = ['上周金额','本周金额','上周订单数','本周订单数'];//分类
  270. $chartdata['xAxis'] = $data['day'];//X轴值
  271. //,'itemStyle'=>$series
  272. $series= ['normal'=>['label'=>['show'=>true,'position'=>'top']]];
  273. $chartdata['series'][] = ['name'=>$chartdata['legend'][0],'type'=>'bar','itemStyle'=>$series,'data'=>$data['pre']['price']];//分类1值
  274. $chartdata['series'][] = ['name'=>$chartdata['legend'][1],'type'=>'bar','itemStyle'=>$series,'data'=>$data['now']['price']];//分类1值
  275. $chartdata['series'][] = ['name'=>$chartdata['legend'][2],'type'=>'line','itemStyle'=>$series,'data'=>$data['pre']['count']];//分类2值
  276. $chartdata['series'][] = ['name'=>$chartdata['legend'][3],'type'=>'line','itemStyle'=>$series,'data'=>$data['now']['count']];//分类2值
  277. //统计总数上期
  278. $pre_total = StoreOrderModel::where('add_time','between time',[$datebefor,$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 = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_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 Json::succ('ok',$chartdata);
  310. break;
  311. case 'month':
  312. $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']);
  313. $datebefor = date('Y-m-01',strtotime('-1 month'));
  314. $dateafter = date('Y-m-d',strtotime(date('Y-m-01')));
  315. $order_list = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  316. ->field("FROM_UNIXTIME(add_time,'%d') 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-01');
  326. $now_dateafter = date('Y-m-d',strtotime("+1 day"));
  327. $now_order_list = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_dateafter])
  328. ->field("FROM_UNIXTIME(add_time,'%d') 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. $chartdata['series'][] = ['name'=>$chartdata['legend'][0],'type'=>'bar','itemStyle'=>$series,'data'=>$data['pre']['price']];//分类1值
  371. $chartdata['series'][] = ['name'=>$chartdata['legend'][1],'type'=>'bar','itemStyle'=>$series,'data'=>$data['now']['price']];//分类1值
  372. $chartdata['series'][] = ['name'=>$chartdata['legend'][2],'type'=>'line','itemStyle'=>$series,'data'=>$data['pre']['count']];//分类2值
  373. $chartdata['series'][] = ['name'=>$chartdata['legend'][3],'type'=>'line','itemStyle'=>$series,'data'=>$data['now']['count']];//分类2值
  374. //统计总数上期
  375. $pre_total = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  376. ->field("count(*) as count,sum(pay_price) as price")
  377. ->find();
  378. if($pre_total){
  379. $chartdata['pre_cycle']['count'] = [
  380. 'data' => $pre_total['count']? : 0
  381. ];
  382. $chartdata['pre_cycle']['price'] = [
  383. 'data' => $pre_total['price']? : 0
  384. ];
  385. }
  386. //统计总数
  387. $total = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_dateafter])
  388. ->field("count(*) as count,sum(pay_price) as price")
  389. ->find();
  390. if($total){
  391. $cha_count = intval($pre_total['count']) - intval($total['count']);
  392. $pre_total['count'] = $pre_total['count']==0 ? 1 : $pre_total['count'];
  393. $chartdata['cycle']['count'] = [
  394. 'data' => $total['count']? : 0,
  395. 'percent' => round((abs($cha_count)/intval($pre_total['count'])*100),2),
  396. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  397. ];
  398. $cha_price = round($pre_total['price'],2) - round($total['price'],2);
  399. $pre_total['price'] = $pre_total['price']==0 ? 1 : $pre_total['price'];
  400. $chartdata['cycle']['price'] = [
  401. 'data' => $total['price']? : 0,
  402. 'percent' => round(abs($cha_price)/$pre_total['price']*100,2),
  403. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  404. ];
  405. }
  406. return Json::succ('ok',$chartdata);
  407. break;
  408. case 'year':
  409. $weekarray=array('01'=>['一月'],'02'=>['二月'],'03'=>['三月'],'04'=>['四月'],'05'=>['五月'],'06'=>['六月'],'07'=>['七月'],'08'=>['八月'],'09'=>['九月'],'10'=>['十月'],'11'=>['十一月'],'12'=>['十二月']);
  410. $datebefor = date('Y-01-01',strtotime('-1 year'));
  411. $dateafter = date('Y-12-31',strtotime('-1 year'));
  412. $order_list = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  413. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  414. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  415. ->order('add_time asc')
  416. ->select()->toArray();
  417. //数据查询重新处理
  418. $new_order_list = [];
  419. foreach ($order_list as $k=>$v){
  420. $new_order_list[$v['day']] = $v;
  421. }
  422. $now_datebefor = date('Y-01-01');
  423. $now_dateafter = date('Y-m-d');
  424. $now_order_list = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_dateafter])
  425. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  426. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  427. ->order('add_time asc')
  428. ->select()->toArray();
  429. //数据查询重新处理 key 变为当前值
  430. $new_now_order_list = [];
  431. foreach ($now_order_list as $k=>$v){
  432. $new_now_order_list[$v['day']] = $v;
  433. }
  434. foreach ($weekarray as $dk=>$dd){
  435. if(!empty($new_order_list[$dk])){
  436. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  437. }else{
  438. $weekarray[$dk]['pre'] = ['count'=>0,'day'=>$weekarray[$dk][0],'price'=>'0'];
  439. }
  440. if(!empty($new_now_order_list[$dk])){
  441. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  442. }else{
  443. $weekarray[$dk]['now'] = ['count'=>0,'day'=>$weekarray[$dk][0],'price'=>'0'];
  444. }
  445. }
  446. $chartdata = [];
  447. $data = [];//临时
  448. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  449. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  450. foreach ($weekarray as $k=>$v){
  451. $data['day'][] = $v[0];
  452. $data['pre']['count'][] = $v['pre']['count'];
  453. $data['pre']['price'][] = round($v['pre']['price'],2);
  454. $data['now']['count'][] = $v['now']['count'];
  455. $data['now']['price'][] = round($v['now']['price'],2);
  456. if($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']){
  457. $chartdata['yAxis']['maxnum'] = $v['pre']['count']>$v['now']['count']?$v['pre']['count']:$v['now']['count'];//日最大订单数
  458. }
  459. if($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']){
  460. $chartdata['yAxis']['maxprice'] = $v['pre']['price']>$v['now']['price']?$v['pre']['price']:$v['now']['price'];//日最大金额
  461. }
  462. }
  463. $chartdata['legend'] = ['去年金额','今年金额','去年订单数','今年订单数'];//分类
  464. $chartdata['xAxis'] = $data['day'];//X轴值
  465. //,'itemStyle'=>$series
  466. $series= ['normal'=>['label'=>['show'=>true,'position'=>'top']]];
  467. $chartdata['series'][] = ['name'=>$chartdata['legend'][0],'type'=>'bar','itemStyle'=>$series,'data'=>$data['pre']['price']];//分类1值
  468. $chartdata['series'][] = ['name'=>$chartdata['legend'][1],'type'=>'bar','itemStyle'=>$series,'data'=>$data['now']['price']];//分类1值
  469. $chartdata['series'][] = ['name'=>$chartdata['legend'][2],'type'=>'line','itemStyle'=>$series,'data'=>$data['pre']['count']];//分类2值
  470. $chartdata['series'][] = ['name'=>$chartdata['legend'][3],'type'=>'line','itemStyle'=>$series,'data'=>$data['now']['count']];//分类2值
  471. //统计总数上期
  472. $pre_total = StoreOrderModel::where('add_time','between time',[$datebefor,$dateafter])
  473. ->field("count(*) as count,sum(pay_price) as price")
  474. ->find();
  475. if($pre_total){
  476. $chartdata['pre_cycle']['count'] = [
  477. 'data' => $pre_total['count']? : 0
  478. ];
  479. $chartdata['pre_cycle']['price'] = [
  480. 'data' => $pre_total['price']? : 0
  481. ];
  482. }
  483. //统计总数
  484. $total = StoreOrderModel::where('add_time','between time',[$now_datebefor,$now_dateafter])
  485. ->field("count(*) as count,sum(pay_price) as price")
  486. ->find();
  487. if($total){
  488. $cha_count = intval($pre_total['count']) - intval($total['count']);
  489. $pre_total['count'] = $pre_total['count']==0 ? 1 : $pre_total['count'];
  490. $chartdata['cycle']['count'] = [
  491. 'data' => $total['count']? : 0,
  492. 'percent' => round((abs($cha_count)/intval($pre_total['count'])*100),2),
  493. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  494. ];
  495. $cha_price = round($pre_total['price'],2) - round($total['price'],2);
  496. $pre_total['price'] = $pre_total['price']==0 ? 1 : $pre_total['price'];
  497. $chartdata['cycle']['price'] = [
  498. 'data' => $total['price']? : 0,
  499. 'percent' => round(abs($cha_price)/$pre_total['price']*100,2),
  500. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  501. ];
  502. }
  503. return Json::succ('ok',$chartdata);
  504. break;
  505. default:
  506. break;
  507. }
  508. }
  509. /**
  510. * 用户图表
  511. */
  512. public function userchart(){
  513. header('Content-type:text/json');
  514. $starday = date('Y-m-d',strtotime('-30 day'));
  515. $yesterday = date('Y-m-d');
  516. $user_list = UserModel::where('add_time','between time',[$starday,$yesterday])
  517. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  518. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  519. ->order('add_time asc')
  520. ->select()->toArray();
  521. $chartdata = [];
  522. $data = [];
  523. $chartdata['legend'] = ['用户数'];//分类
  524. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  525. if(empty($user_list))return Json::fail('无数据');
  526. foreach ($user_list as $k=>$v){
  527. $data['day'][] = $v['day'];
  528. $data['count'][] = $v['count'];
  529. if($chartdata['yAxis']['maxnum'] < $v['count'])
  530. $chartdata['yAxis']['maxnum'] = $v['count'];
  531. }
  532. $chartdata['xAxis'] = $data['day'];//X轴值
  533. $chartdata['series'] = $data['count'];//分类1值
  534. return Json::succ('ok',$chartdata);
  535. }
  536. /**待办事统计
  537. * @param Request|null $request
  538. */
  539. public function Jnotice()
  540. {
  541. header('Content-type:text/json');
  542. $data = [];
  543. $data['ordernum'] = StoreOrderModel::statusByWhere(1)->count();//待发货
  544. $replenishment_num = SystemConfig::getValue('store_stock') > 0 ? SystemConfig::getValue('store_stock') : 2;//库存预警界限
  545. $data['inventory'] = ProductModel::where('stock','<=',$replenishment_num)->where('is_show',1)->where('is_del',0)->count();//库存
  546. $data['commentnum'] = StoreProductReplyModel::where('is_reply',0)->count();//评论
  547. $data['reflectnum'] = UserExtractModel::where('status',0)->count();;//提现
  548. $data['msgcount'] = intval($data['ordernum'])+intval($data['inventory'])+intval($data['commentnum'])+intval($data['reflectnum']);
  549. return Json::succ('ok',$data);
  550. }
  551. }