Index.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. namespace app\admin\controller;
  3. use app\models\store\StoreGp;
  4. use app\models\store\StoreOrder;
  5. use FormBuilder\Json;
  6. use think\facade\Config;
  7. use app\admin\model\order\StoreOrder as StoreOrderModel;//订单
  8. use app\admin\model\system\{SystemConfig, SystemMenus, SystemRole};
  9. use app\admin\model\user\{User, UserExtract as UserExtractModel, User as UserModel};
  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. //订单数->昨日
  60. $now_day_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'yesterday')->count();
  61. $pre_day_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $pre_day)->where('pay_time', '<', $now_day)->count();
  62. $first_line['d_num'] = [
  63. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  64. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  65. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  66. ];
  67. //交易额->昨天
  68. $now_month_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'yesterday')->sum('pay_price');
  69. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $beforyester_day)->where('pay_time', '<', $pre_day)->sum('pay_price');
  70. $first_line['d_price'] = [
  71. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  72. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  73. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  74. ];
  75. //交易额->月
  76. $now_month_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'month')->sum('pay_price');
  77. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $pre_month)->where('pay_time', '<', $now_month)->value('sum(pay_price)');
  78. $first_line['m_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_day_user = User::where('add_time', '>', $now_day)->count();
  85. $pre_day_user = User::where('add_time', '>', $pre_day)->where('add_time', '<', $now_day)->count();
  86. $pre_day_user = $pre_day_user ? $pre_day_user : 0;
  87. $first_line['day'] = [
  88. 'data' => $now_day_user ? $now_day_user : 0,
  89. 'percent' => abs($now_day_user - $pre_day_user),
  90. 'is_plus' => $now_day_user - $pre_day_user > 0 ? 1 : ($now_day_user - $pre_day_user == 0 ? -1 : 0)
  91. ];
  92. //新粉丝->月
  93. $now_month_user = User::where('add_time', '>', $now_month)->count();
  94. $pre_month_user = User::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  95. $first_line['month'] = [
  96. 'data' => $now_month_user ? $now_month_user : 0,
  97. 'percent' => abs($now_month_user - $pre_month_user),
  98. 'is_plus' => $now_month_user - $pre_month_user > 0 ? 1 : ($now_month_user - $pre_month_user == 0 ? -1 : 0)
  99. ];
  100. //本月订单总数
  101. $now_order_info_c = StoreOrderModel::where('add_time', '>', $now_month)->count();
  102. $pre_order_info_c = StoreOrderModel::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  103. $order_info['first'] = [
  104. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  105. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  106. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  107. ];
  108. //上月订单总数
  109. $second_now_month = strtotime(date('Y-m', strtotime('-1 month')));
  110. $second_pre_month = strtotime(date('Y-m', strtotime('-2 month')));
  111. $now_order_info_c = StoreOrderModel::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  112. $pre_order_info_c = StoreOrderModel::where('add_time', '>', $second_pre_month)->where('add_time', '<', $second_now_month)->count();
  113. $order_info["second"] = [
  114. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  115. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  116. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  117. ];
  118. $second_line['order_info'] = $order_info;
  119. $this->assign([
  120. 'first_line' => $first_line,
  121. 'second_line' => $second_line,
  122. 'topData' => $topData,
  123. ]);
  124. return $this->fetch();
  125. }
  126. /**
  127. * 订单图表
  128. */
  129. public function orderchart()
  130. {
  131. header('Content-type:text/json');
  132. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  133. $datalist = [];
  134. $where = ['is_del' => 0, 'paid' => 1, 'refund_status' => 0];
  135. switch ($cycle) {
  136. case 'thirtyday':
  137. $datebefor = date('Y-m-d 00:00:00', strtotime('-30 day'));
  138. $dateafter = date('Y-m-d 23:59:59');
  139. //上期
  140. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  141. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  142. for ($i = -30; $i < 1; $i++) {
  143. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  144. }
  145. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  146. ->where($where)
  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. ->where($where)
  185. ->field("count(*) as count,sum(pay_price) as price")
  186. ->find();
  187. if ($pre_total) {
  188. $chartdata['pre_cycle']['count'] = [
  189. 'data' => $pre_total['count'] ?: 0
  190. ];
  191. $chartdata['pre_cycle']['price'] = [
  192. 'data' => $pre_total['price'] ?: 0
  193. ];
  194. }
  195. //统计总数
  196. $total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  197. ->where($where)
  198. ->field("count(*) as count,sum(pay_price) as price")
  199. ->find();
  200. if ($total) {
  201. $cha_count = intval($pre_total['count']) - intval($total['count']);
  202. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  203. $chartdata['cycle']['count'] = [
  204. 'data' => $total['count'] ?: 0,
  205. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  206. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  207. ];
  208. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  209. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  210. $chartdata['cycle']['price'] = [
  211. 'data' => $total['price'] ?: 0,
  212. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  213. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  214. ];
  215. }
  216. return app('json')->success('ok', $chartdata);
  217. break;
  218. case 'week':
  219. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  220. $datebefor = date('Y-m-d 00:00:00)', strtotime('-1 week Monday'));
  221. $dateafter = date('Y-m-d 23:59:59', strtotime('-1 week Sunday'));
  222. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  223. ->where($where)
  224. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  225. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  226. ->order('add_time asc')
  227. ->select()->toArray();
  228. //数据查询重新处理
  229. $new_order_list = [];
  230. foreach ($order_list as $k => $v) {
  231. $new_order_list[$v['day']] = $v;
  232. }
  233. $now_datebefor = date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  234. $now_dateafter = date('Y-m-d 23:59:59', strtotime("+1 day"));
  235. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  236. ->where($where)
  237. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  238. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  239. ->order('add_time asc')
  240. ->select()->toArray();
  241. //数据查询重新处理 key 变为当前值
  242. $new_now_order_list = [];
  243. foreach ($now_order_list as $k => $v) {
  244. $new_now_order_list[$v['day']] = $v;
  245. }
  246. foreach ($weekarray as $dk => $dd) {
  247. if (!empty($new_order_list[$dk])) {
  248. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  249. } else {
  250. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  251. }
  252. if (!empty($new_now_order_list[$dk])) {
  253. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  254. } else {
  255. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  256. }
  257. }
  258. $chartdata = [];
  259. $data = [];//临时
  260. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  261. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  262. foreach ($weekarray as $k => $v) {
  263. $data['day'][] = $v[0];
  264. $data['pre']['count'][] = $v['pre']['count'];
  265. $data['pre']['price'][] = round($v['pre']['price'], 2);
  266. $data['now']['count'][] = $v['now']['count'];
  267. $data['now']['price'][] = round($v['now']['price'], 2);
  268. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  269. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  270. }
  271. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  272. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  273. }
  274. }
  275. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  276. $chartdata['xAxis'] = $data['day'];//X轴值
  277. //,'itemStyle'=>$series
  278. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  279. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  280. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  281. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  282. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  283. //统计总数上期
  284. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  285. ->where($where)
  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. ->where($where)
  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 00:00:00', strtotime('-1 month'));
  322. $dateafter = date('Y-m-d 23:59:59', strtotime(date('Y-m-01')));
  323. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  324. ->where($where)
  325. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  326. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  327. ->order('add_time asc')
  328. ->select()->toArray();
  329. //数据查询重新处理
  330. $new_order_list = [];
  331. foreach ($order_list as $k => $v) {
  332. $new_order_list[$v['day']] = $v;
  333. }
  334. $now_datebefor = date('Y-m-01 00:00:00');
  335. $now_dateafter = date('Y-m-d 23:59:59', strtotime("+1 day"));
  336. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  337. ->where($where)
  338. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  339. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  340. ->order('add_time asc')
  341. ->select()->toArray();
  342. //数据查询重新处理 key 变为当前值
  343. $new_now_order_list = [];
  344. foreach ($now_order_list as $k => $v) {
  345. $new_now_order_list[$v['day']] = $v;
  346. }
  347. foreach ($weekarray as $dk => $dd) {
  348. if (!empty($new_order_list[$dk])) {
  349. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  350. } else {
  351. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  352. }
  353. if (!empty($new_now_order_list[$dk])) {
  354. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  355. } else {
  356. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  357. }
  358. }
  359. $chartdata = [];
  360. $data = [];//临时
  361. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  362. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  363. foreach ($weekarray as $k => $v) {
  364. $data['day'][] = $v[0];
  365. $data['pre']['count'][] = $v['pre']['count'];
  366. $data['pre']['price'][] = round($v['pre']['price'], 2);
  367. $data['now']['count'][] = $v['now']['count'];
  368. $data['now']['price'][] = round($v['now']['price'], 2);
  369. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  370. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  371. }
  372. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  373. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  374. }
  375. }
  376. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  377. $chartdata['xAxis'] = $data['day'];//X轴值
  378. //,'itemStyle'=>$series
  379. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  380. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  381. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  382. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  383. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  384. //统计总数上期
  385. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  386. ->where($where)
  387. ->field("count(*) as count,sum(pay_price) as price")
  388. ->find();
  389. if ($pre_total) {
  390. $chartdata['pre_cycle']['count'] = [
  391. 'data' => $pre_total['count'] ?: 0
  392. ];
  393. $chartdata['pre_cycle']['price'] = [
  394. 'data' => $pre_total['price'] ?: 0
  395. ];
  396. }
  397. //统计总数
  398. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  399. ->where($where)
  400. ->field("count(*) as count,sum(pay_price) as price")
  401. ->find();
  402. if ($total) {
  403. $cha_count = intval($pre_total['count']) - intval($total['count']);
  404. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  405. $chartdata['cycle']['count'] = [
  406. 'data' => $total['count'] ?: 0,
  407. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  408. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  409. ];
  410. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  411. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  412. $chartdata['cycle']['price'] = [
  413. 'data' => $total['price'] ?: 0,
  414. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  415. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  416. ];
  417. }
  418. return app('json')->success('ok', $chartdata);
  419. break;
  420. case 'year':
  421. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  422. $datebefor = date('Y-01-01 00:00:00', strtotime('-1 year'));
  423. $dateafter = date('Y-12-31 23:59:59', strtotime('-1 year'));
  424. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  425. ->where($where)
  426. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  427. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  428. ->order('add_time asc')
  429. ->select()->toArray();
  430. //数据查询重新处理
  431. $new_order_list = [];
  432. foreach ($order_list as $k => $v) {
  433. $new_order_list[$v['day']] = $v;
  434. }
  435. $now_datebefor = date('Y-01-01 00:00:00');
  436. $now_dateafter = date('Y-m-d 23:59:59');
  437. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  438. ->where($where)
  439. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  440. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  441. ->order('add_time asc')
  442. ->select()->toArray();
  443. //数据查询重新处理 key 变为当前值
  444. $new_now_order_list = [];
  445. foreach ($now_order_list as $k => $v) {
  446. $new_now_order_list[$v['day']] = $v;
  447. }
  448. foreach ($weekarray as $dk => $dd) {
  449. if (!empty($new_order_list[$dk])) {
  450. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  451. } else {
  452. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  453. }
  454. if (!empty($new_now_order_list[$dk])) {
  455. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  456. } else {
  457. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  458. }
  459. }
  460. $chartdata = [];
  461. $data = [];//临时
  462. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  463. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  464. foreach ($weekarray as $k => $v) {
  465. $data['day'][] = $v[0];
  466. $data['pre']['count'][] = $v['pre']['count'];
  467. $data['pre']['price'][] = round($v['pre']['price'], 2);
  468. $data['now']['count'][] = $v['now']['count'];
  469. $data['now']['price'][] = round($v['now']['price'], 2);
  470. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  471. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  472. }
  473. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  474. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  475. }
  476. }
  477. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  478. $chartdata['xAxis'] = $data['day'];//X轴值
  479. //,'itemStyle'=>$series
  480. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  481. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  482. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  483. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  484. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  485. //统计总数上期
  486. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  487. ->where($where)
  488. ->field("count(*) as count,sum(pay_price) as price")
  489. ->find();
  490. if ($pre_total) {
  491. $chartdata['pre_cycle']['count'] = [
  492. 'data' => $pre_total['count'] ?: 0
  493. ];
  494. $chartdata['pre_cycle']['price'] = [
  495. 'data' => $pre_total['price'] ?: 0
  496. ];
  497. }
  498. //统计总数
  499. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  500. ->where($where)
  501. ->field("count(*) as count,sum(pay_price) as price")
  502. ->find();
  503. if ($total) {
  504. $cha_count = intval($pre_total['count']) - intval($total['count']);
  505. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  506. $chartdata['cycle']['count'] = [
  507. 'data' => $total['count'] ?: 0,
  508. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  509. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  510. ];
  511. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  512. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  513. $chartdata['cycle']['price'] = [
  514. 'data' => $total['price'] ?: 0,
  515. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  516. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  517. ];
  518. }
  519. return app('json')->success('ok', $chartdata);
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. /**
  526. * 用户图表
  527. */
  528. public function userchart()
  529. {
  530. header('Content-type:text/json');
  531. $starday = date('Y-m-d', strtotime('-30 day'));
  532. $yesterday = date('Y-m-d');
  533. $user_list = UserModel::where('add_time', 'between time', [$starday, $yesterday])
  534. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  535. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  536. ->order('add_time asc')
  537. ->select()->toArray();
  538. $chartdata = [];
  539. $data = [];
  540. $chartdata['legend'] = ['用户数'];//分类
  541. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  542. $chartdata['xAxis'] = [date('m-d')];//X轴值
  543. $chartdata['series'] = [0];//分类1值
  544. if (!empty($user_list)) {
  545. foreach ($user_list as $k => $v) {
  546. $data['day'][] = $v['day'];
  547. $data['count'][] = $v['count'];
  548. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  549. $chartdata['yAxis']['maxnum'] = $v['count'];
  550. }
  551. $chartdata['xAxis'] = $data['day'];//X轴值
  552. $chartdata['series'] = $data['count'];//分类1值
  553. }
  554. return app('json')->success('ok', $chartdata);
  555. }
  556. /**
  557. * 待办事统计
  558. * @param int $newTime
  559. * @return false|string
  560. */
  561. public function Jnotice($newTime = 30)
  562. {
  563. header('Content-type:text/json');
  564. $data = [];
  565. $data['ordernum'] = StoreOrderModel::statusByWhere(1)->count();//待发货
  566. $replenishment_num = sys_config('store_stock') > 0 ? sys_config('store_stock') : 2;//库存预警界限
  567. $data['inventory'] = ProductModel::where('stock', '<=', $replenishment_num)->where('is_show', 1)->where('is_del', 0)->count();//库存
  568. $data['commentnum'] = StoreProductReplyModel::where('is_reply', 0)->count();//评论
  569. $data['reflectnum'] = UserExtractModel::where('status', 0)->count();;//提现
  570. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  571. //新订单提醒
  572. $data['newOrderId'] = StoreOrderModel::statusByWhere(1)->where('is_remind', 0)->column('order_id', 'id');
  573. if (count($data['newOrderId'])) StoreOrderModel::where('order_id', 'in', $data['newOrderId'])->update(['is_remind' => 1]);
  574. return app('json')->success('ok', $data);
  575. }
  576. public function test()
  577. {
  578. $order = StoreOrder::find(2);
  579. StoreGp::createGp($order);
  580. }
  581. }