Index.php 33 KB

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