Index.php 33 KB

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