Index.php 34 KB

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