Index.php 34 KB

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