Index.php 34 KB

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