Index.php 33 KB

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