Index.php 35 KB

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