Index.php 35 KB

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