Index.php 33 KB

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