Index.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. $fertilizer = round(User::sum('white_integral'), 2);
  119. $integral = round(User::sum('integral'), 2);
  120. $this->assign([
  121. 'first_line' => $first_line,
  122. 'second_line' => $second_line,
  123. 'topData' => $topData,
  124. 'fertilizer' => $fertilizer,
  125. 'integral' => $integral
  126. ]);
  127. return $this->fetch();
  128. }
  129. /**
  130. * 订单图表
  131. */
  132. public function orderchart()
  133. {
  134. header('Content-type:text/json');
  135. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  136. $datalist = [];
  137. switch ($cycle) {
  138. case 'thirtyday':
  139. $datebefor = date('Y-m-d', strtotime('-30 day'));
  140. $dateafter = date('Y-m-d');
  141. //上期
  142. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  143. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  144. for ($i = -30; $i < 0; $i++) {
  145. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  146. }
  147. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  148. ->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price")
  149. ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
  150. ->order('add_time asc')
  151. ->select()->toArray();
  152. if (empty($order_list)) return Json::fail('无数据');
  153. foreach ($order_list as $k => &$v) {
  154. $order_list[$v['day']] = $v;
  155. }
  156. $cycle_list = [];
  157. foreach ($datalist as $dk => $dd) {
  158. if (!empty($order_list[$dd])) {
  159. $cycle_list[$dd] = $order_list[$dd];
  160. } else {
  161. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  162. }
  163. }
  164. $chartdata = [];
  165. $data = [];//临时
  166. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  167. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  168. foreach ($cycle_list as $k => $v) {
  169. $data['day'][] = $v['day'];
  170. $data['count'][] = $v['count'];
  171. $data['price'][] = round($v['price'], 2);
  172. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  173. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  174. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  175. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  176. }
  177. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  178. $chartdata['xAxis'] = $data['day'];//X轴值
  179. //,'itemStyle'=>$series
  180. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  181. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['price']];//分类1值
  182. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['count']];//分类2值
  183. //统计总数上期
  184. $pre_total = StoreOrderModel::where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  185. ->field("count(*) as count,sum(pay_price) as price")
  186. ->find();
  187. if ($pre_total) {
  188. $chartdata['pre_cycle']['count'] = [
  189. 'data' => $pre_total['count'] ?: 0
  190. ];
  191. $chartdata['pre_cycle']['price'] = [
  192. 'data' => $pre_total['price'] ?: 0
  193. ];
  194. }
  195. //统计总数
  196. $total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  197. ->field("count(*) as count,sum(pay_price) as price")
  198. ->find();
  199. if ($total) {
  200. $cha_count = intval($pre_total['count']) - intval($total['count']);
  201. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  202. $chartdata['cycle']['count'] = [
  203. 'data' => $total['count'] ?: 0,
  204. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  205. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  206. ];
  207. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  208. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  209. $chartdata['cycle']['price'] = [
  210. 'data' => $total['price'] ?: 0,
  211. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  212. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  213. ];
  214. }
  215. return app('json')->success('ok', $chartdata);
  216. break;
  217. case 'week':
  218. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  219. $datebefor = date('Y-m-d', strtotime('-1 week Monday'));
  220. $dateafter = date('Y-m-d', strtotime('-1 week Sunday'));
  221. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  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', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  232. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  233. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  234. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  235. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  236. ->order('add_time asc')
  237. ->select()->toArray();
  238. //数据查询重新处理 key 变为当前值
  239. $new_now_order_list = [];
  240. foreach ($now_order_list as $k => $v) {
  241. $new_now_order_list[$v['day']] = $v;
  242. }
  243. foreach ($weekarray as $dk => $dd) {
  244. if (!empty($new_order_list[$dk])) {
  245. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  246. } else {
  247. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  248. }
  249. if (!empty($new_now_order_list[$dk])) {
  250. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  251. } else {
  252. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  253. }
  254. }
  255. $chartdata = [];
  256. $data = [];//临时
  257. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  258. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  259. foreach ($weekarray as $k => $v) {
  260. $data['day'][] = $v[0];
  261. $data['pre']['count'][] = $v['pre']['count'];
  262. $data['pre']['price'][] = round($v['pre']['price'], 2);
  263. $data['now']['count'][] = $v['now']['count'];
  264. $data['now']['price'][] = round($v['now']['price'], 2);
  265. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  266. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  267. }
  268. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  269. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  270. }
  271. }
  272. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  273. $chartdata['xAxis'] = $data['day'];//X轴值
  274. //,'itemStyle'=>$series
  275. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  276. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  277. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  278. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  279. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  280. //统计总数上期
  281. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  282. ->field("count(*) as count,sum(pay_price) as price")
  283. ->find();
  284. if ($pre_total) {
  285. $chartdata['pre_cycle']['count'] = [
  286. 'data' => $pre_total['count'] ?: 0
  287. ];
  288. $chartdata['pre_cycle']['price'] = [
  289. 'data' => $pre_total['price'] ?: 0
  290. ];
  291. }
  292. //统计总数
  293. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  294. ->field("count(*) as count,sum(pay_price) as price")
  295. ->find();
  296. if ($total) {
  297. $cha_count = intval($pre_total['count']) - intval($total['count']);
  298. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  299. $chartdata['cycle']['count'] = [
  300. 'data' => $total['count'] ?: 0,
  301. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  302. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  303. ];
  304. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  305. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  306. $chartdata['cycle']['price'] = [
  307. 'data' => $total['price'] ?: 0,
  308. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  309. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  310. ];
  311. }
  312. return app('json')->success('ok', $chartdata);
  313. break;
  314. case 'month':
  315. $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']);
  316. $datebefor = date('Y-m-01', strtotime('-1 month'));
  317. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  318. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  319. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  320. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  321. ->order('add_time asc')
  322. ->select()->toArray();
  323. //数据查询重新处理
  324. $new_order_list = [];
  325. foreach ($order_list as $k => $v) {
  326. $new_order_list[$v['day']] = $v;
  327. }
  328. $now_datebefor = date('Y-m-01');
  329. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  330. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  331. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  332. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  333. ->order('add_time asc')
  334. ->select()->toArray();
  335. //数据查询重新处理 key 变为当前值
  336. $new_now_order_list = [];
  337. foreach ($now_order_list as $k => $v) {
  338. $new_now_order_list[$v['day']] = $v;
  339. }
  340. foreach ($weekarray as $dk => $dd) {
  341. if (!empty($new_order_list[$dk])) {
  342. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  343. } else {
  344. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  345. }
  346. if (!empty($new_now_order_list[$dk])) {
  347. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  348. } else {
  349. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  350. }
  351. }
  352. $chartdata = [];
  353. $data = [];//临时
  354. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  355. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  356. foreach ($weekarray as $k => $v) {
  357. $data['day'][] = $v[0];
  358. $data['pre']['count'][] = $v['pre']['count'];
  359. $data['pre']['price'][] = round($v['pre']['price'], 2);
  360. $data['now']['count'][] = $v['now']['count'];
  361. $data['now']['price'][] = round($v['now']['price'], 2);
  362. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  363. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  364. }
  365. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  366. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  367. }
  368. }
  369. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  370. $chartdata['xAxis'] = $data['day'];//X轴值
  371. //,'itemStyle'=>$series
  372. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  373. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  374. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  375. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  376. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  377. //统计总数上期
  378. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  379. ->field("count(*) as count,sum(pay_price) as price")
  380. ->find();
  381. if ($pre_total) {
  382. $chartdata['pre_cycle']['count'] = [
  383. 'data' => $pre_total['count'] ?: 0
  384. ];
  385. $chartdata['pre_cycle']['price'] = [
  386. 'data' => $pre_total['price'] ?: 0
  387. ];
  388. }
  389. //统计总数
  390. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  391. ->field("count(*) as count,sum(pay_price) as price")
  392. ->find();
  393. if ($total) {
  394. $cha_count = intval($pre_total['count']) - intval($total['count']);
  395. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  396. $chartdata['cycle']['count'] = [
  397. 'data' => $total['count'] ?: 0,
  398. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  399. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  400. ];
  401. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  402. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  403. $chartdata['cycle']['price'] = [
  404. 'data' => $total['price'] ?: 0,
  405. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  406. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  407. ];
  408. }
  409. return app('json')->success('ok', $chartdata);
  410. break;
  411. case 'year':
  412. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  413. $datebefor = date('Y-01-01', strtotime('-1 year'));
  414. $dateafter = date('Y-12-31', strtotime('-1 year'));
  415. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  416. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  417. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  418. ->order('add_time asc')
  419. ->select()->toArray();
  420. //数据查询重新处理
  421. $new_order_list = [];
  422. foreach ($order_list as $k => $v) {
  423. $new_order_list[$v['day']] = $v;
  424. }
  425. $now_datebefor = date('Y-01-01');
  426. $now_dateafter = date('Y-m-d');
  427. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  428. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  429. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  430. ->order('add_time asc')
  431. ->select()->toArray();
  432. //数据查询重新处理 key 变为当前值
  433. $new_now_order_list = [];
  434. foreach ($now_order_list as $k => $v) {
  435. $new_now_order_list[$v['day']] = $v;
  436. }
  437. foreach ($weekarray as $dk => $dd) {
  438. if (!empty($new_order_list[$dk])) {
  439. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  440. } else {
  441. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  442. }
  443. if (!empty($new_now_order_list[$dk])) {
  444. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  445. } else {
  446. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  447. }
  448. }
  449. $chartdata = [];
  450. $data = [];//临时
  451. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  452. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  453. foreach ($weekarray as $k => $v) {
  454. $data['day'][] = $v[0];
  455. $data['pre']['count'][] = $v['pre']['count'];
  456. $data['pre']['price'][] = round($v['pre']['price'], 2);
  457. $data['now']['count'][] = $v['now']['count'];
  458. $data['now']['price'][] = round($v['now']['price'], 2);
  459. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  460. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  461. }
  462. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  463. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  464. }
  465. }
  466. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  467. $chartdata['xAxis'] = $data['day'];//X轴值
  468. //,'itemStyle'=>$series
  469. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  470. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  471. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  472. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  473. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  474. //统计总数上期
  475. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  476. ->field("count(*) as count,sum(pay_price) as price")
  477. ->find();
  478. if ($pre_total) {
  479. $chartdata['pre_cycle']['count'] = [
  480. 'data' => $pre_total['count'] ?: 0
  481. ];
  482. $chartdata['pre_cycle']['price'] = [
  483. 'data' => $pre_total['price'] ?: 0
  484. ];
  485. }
  486. //统计总数
  487. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  488. ->field("count(*) as count,sum(pay_price) as price")
  489. ->find();
  490. if ($total) {
  491. $cha_count = intval($pre_total['count']) - intval($total['count']);
  492. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  493. $chartdata['cycle']['count'] = [
  494. 'data' => $total['count'] ?: 0,
  495. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  496. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  497. ];
  498. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  499. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  500. $chartdata['cycle']['price'] = [
  501. 'data' => $total['price'] ?: 0,
  502. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  503. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  504. ];
  505. }
  506. return app('json')->success('ok', $chartdata);
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. /**
  513. * 用户图表
  514. */
  515. public function userchart()
  516. {
  517. header('Content-type:text/json');
  518. $starday = date('Y-m-d', strtotime('-30 day'));
  519. $yesterday = date('Y-m-d');
  520. $user_list = UserModel::where('add_time', 'between time', [$starday, $yesterday])
  521. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  522. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  523. ->order('add_time asc')
  524. ->select()->toArray();
  525. $chartdata = [];
  526. $data = [];
  527. $chartdata['legend'] = ['用户数'];//分类
  528. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  529. $chartdata['xAxis'] = [date('m-d')];//X轴值
  530. $chartdata['series'] = [0];//分类1值
  531. if (!empty($user_list)) {
  532. foreach ($user_list as $k => $v) {
  533. $data['day'][] = $v['day'];
  534. $data['count'][] = $v['count'];
  535. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  536. $chartdata['yAxis']['maxnum'] = $v['count'];
  537. }
  538. $chartdata['xAxis'] = $data['day'];//X轴值
  539. $chartdata['series'] = $data['count'];//分类1值
  540. }
  541. return app('json')->success('ok', $chartdata);
  542. }
  543. /**
  544. * 待办事统计
  545. * @param int $newTime
  546. * @return false|string
  547. */
  548. public function Jnotice($newTime = 30)
  549. {
  550. header('Content-type:text/json');
  551. $data = [];
  552. $data['ordernum'] = StoreOrderModel::statusByWhere(1)->count();//待发货
  553. $replenishment_num = sys_config('store_stock') > 0 ? sys_config('store_stock') : 2;//库存预警界限
  554. $data['inventory'] = ProductModel::where('stock', '<=', $replenishment_num)->where('is_show', 1)->where('is_del', 0)->count();//库存
  555. $data['commentnum'] = StoreProductReplyModel::where('is_reply', 0)->count();//评论
  556. $data['reflectnum'] = UserExtractModel::where('status', 0)->count();;//提现
  557. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  558. //新订单提醒
  559. $data['newOrderId'] = StoreOrderModel::statusByWhere(1)->where('is_remind', 0)->column('order_id', 'id');
  560. if (count($data['newOrderId'])) StoreOrderModel::where('order_id', 'in', $data['newOrderId'])->update(['is_remind' => 1]);
  561. return app('json')->success('ok', $data);
  562. }
  563. }