Index.php 33 KB

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