Index.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. namespace app\admin\controller;
  3. use app\models\store\{StoreGp,StoreMgp};
  4. use app\models\store\StoreOrder;
  5. use app\models\user\UserBill;
  6. use FormBuilder\Json;
  7. use think\facade\Config;
  8. use app\admin\model\order\StoreOrder as StoreOrderModel;//订单
  9. use app\admin\model\system\{SystemConfig, SystemMenus, SystemRole};
  10. use app\admin\model\user\{User, UserExtract as UserExtractModel, User as UserModel};
  11. use app\admin\model\store\{StoreProduct, StoreProductReply as StoreProductReplyModel, StoreProduct as ProductModel};
  12. /**
  13. * 首页控制器
  14. * Class Index
  15. * @package app\admin\controller
  16. *
  17. */
  18. class Index extends AuthController
  19. {
  20. public function index()
  21. {
  22. //获取当前登录后台的管理员信息
  23. $adminInfo = $this->adminInfo->toArray();
  24. $roles = explode(',', $adminInfo['roles']);
  25. $site_logo = SystemConfig::getOneConfig('menu_name', 'site_logo')->toArray();
  26. $this->assign([
  27. 'menuList' => SystemMenus::menuList(),
  28. 'site_logo' => json_decode($site_logo['value'], true),
  29. 'new_order_audio_link' => sys_config('new_order_audio_link'),
  30. 'role_name' => SystemRole::where('id', $roles[0])->field('role_name')->find(),
  31. 'workermanPort' => Config::get('workerman.admin.port')
  32. ]);
  33. return $this->fetch();
  34. }
  35. //后台首页内容
  36. public function main()
  37. {
  38. /*首页第一行统计*/
  39. $now_month = strtotime(date('Y-m'));//本月
  40. $pre_month = strtotime(date('Y-m', strtotime('-1 month')));//上月
  41. $now_day = strtotime(date('Y-m-d'));//今日
  42. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨天时间戳
  43. $beforyester_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天时间戳
  44. //待发货数量
  45. $topData['orderDeliveryNum'] = StoreOrderModel::where('status', 0)
  46. ->where('paid', 1)
  47. ->where('refund_status', 0)
  48. ->where('shipping_type', 1)
  49. ->where('is_del', 0)
  50. ->count();
  51. //退换货订单数
  52. $topData['orderRefundNum'] = StoreOrderModel::where('paid', 1)
  53. ->where('refund_status', 'IN', '1')
  54. ->count();
  55. //库存预警
  56. $replenishment_num = sys_config('store_stock') > 0 ? sys_config('store_stock') : 20;//库存预警界限
  57. $topData['stockProduct'] = StoreProduct::where('stock', '<=', $replenishment_num)->where('is_show', 1)->where('is_del', 0)->count();
  58. //待处理提现
  59. $topData['treatedExtract'] = UserExtractModel::where('status', 0)->count();
  60. //订单数->昨日
  61. $now_day_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'yesterday')->count();
  62. $pre_day_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $pre_day)->where('pay_time', '<', $now_day)->count();
  63. $first_line['d_num'] = [
  64. 'data' => $now_day_order_p ? $now_day_order_p : 0,
  65. 'percent' => abs($now_day_order_p - $pre_day_order_p),
  66. 'is_plus' => $now_day_order_p - $pre_day_order_p > 0 ? 1 : ($now_day_order_p - $pre_day_order_p == 0 ? -1 : 0)
  67. ];
  68. //交易额->昨天
  69. $now_month_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'yesterday')->sum('pay_price');
  70. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $beforyester_day)->where('pay_time', '<', $pre_day)->sum('pay_price');
  71. $first_line['d_price'] = [
  72. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  73. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  74. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  75. ];
  76. //交易额->月
  77. $now_month_order_p = StoreOrderModel::where('paid', 1)->whereTime('pay_time', 'month')->sum('pay_price');
  78. $pre_month_order_p = StoreOrderModel::where('paid', 1)->where('pay_time', '>', $pre_month)->where('pay_time', '<', $now_month)->value('sum(pay_price)');
  79. $first_line['m_price'] = [
  80. 'data' => $now_month_order_p > 0 ? $now_month_order_p : 0,
  81. 'percent' => abs($now_month_order_p - $pre_month_order_p),
  82. 'is_plus' => $now_month_order_p - $pre_month_order_p > 0 ? 1 : ($now_month_order_p - $pre_month_order_p == 0 ? -1 : 0)
  83. ];
  84. //新粉丝->日
  85. $now_day_user = User::where('add_time', '>', $now_day)->count();
  86. $pre_day_user = User::where('add_time', '>', $pre_day)->where('add_time', '<', $now_day)->count();
  87. $pre_day_user = $pre_day_user ? $pre_day_user : 0;
  88. $first_line['day'] = [
  89. 'data' => $now_day_user ? $now_day_user : 0,
  90. 'percent' => abs($now_day_user - $pre_day_user),
  91. 'is_plus' => $now_day_user - $pre_day_user > 0 ? 1 : ($now_day_user - $pre_day_user == 0 ? -1 : 0)
  92. ];
  93. //新粉丝->月
  94. $now_month_user = User::where('add_time', '>', $now_month)->count();
  95. $pre_month_user = User::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  96. $first_line['month'] = [
  97. 'data' => $now_month_user ? $now_month_user : 0,
  98. 'percent' => abs($now_month_user - $pre_month_user),
  99. 'is_plus' => $now_month_user - $pre_month_user > 0 ? 1 : ($now_month_user - $pre_month_user == 0 ? -1 : 0)
  100. ];
  101. //本月订单总数
  102. $now_order_info_c = StoreOrderModel::where('add_time', '>', $now_month)->count();
  103. $pre_order_info_c = StoreOrderModel::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  104. $order_info['first'] = [
  105. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  106. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  107. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  108. ];
  109. //上月订单总数
  110. $second_now_month = strtotime(date('Y-m', strtotime('-1 month')));
  111. $second_pre_month = strtotime(date('Y-m', strtotime('-2 month')));
  112. $now_order_info_c = StoreOrderModel::where('add_time', '>', $pre_month)->where('add_time', '<', $now_month)->count();
  113. $pre_order_info_c = StoreOrderModel::where('add_time', '>', $second_pre_month)->where('add_time', '<', $second_now_month)->count();
  114. $order_info["second"] = [
  115. 'data' => $now_order_info_c ? $now_order_info_c : 0,
  116. 'percent' => abs($now_order_info_c - $pre_order_info_c),
  117. 'is_plus' => $now_order_info_c - $pre_order_info_c > 0 ? 1 : ($now_order_info_c - $pre_order_info_c == 0 ? -1 : 0)
  118. ];
  119. $second_line['order_info'] = $order_info;
  120. $this->assign([
  121. 'first_line' => $first_line,
  122. 'second_line' => $second_line,
  123. 'topData' => $topData,
  124. ]);
  125. return $this->fetch();
  126. }
  127. /**
  128. * 订单图表
  129. */
  130. public function orderchart()
  131. {
  132. header('Content-type:text/json');
  133. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  134. $datalist = [];
  135. $where = ['is_del' => 0, 'paid' => 1, 'refund_status' => 0];
  136. switch ($cycle) {
  137. case 'thirtyday':
  138. $datebefor = date('Y-m-d 00:00:00', strtotime('-30 day'));
  139. $dateafter = date('Y-m-d 23:59:59');
  140. //上期
  141. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  142. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  143. for ($i = -30; $i < 1; $i++) {
  144. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  145. }
  146. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  147. ->where($where)
  148. ->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price")
  149. ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
  150. ->order('add_time asc')
  151. ->select()->toArray();
  152. if (empty($order_list)) return Json::fail('无数据');
  153. foreach ($order_list as $k => &$v) {
  154. $order_list[$v['day']] = $v;
  155. }
  156. $cycle_list = [];
  157. foreach ($datalist as $dk => $dd) {
  158. if (!empty($order_list[$dd])) {
  159. $cycle_list[$dd] = $order_list[$dd];
  160. } else {
  161. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  162. }
  163. }
  164. $chartdata = [];
  165. $data = [];//临时
  166. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  167. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  168. foreach ($cycle_list as $k => $v) {
  169. $data['day'][] = $v['day'];
  170. $data['count'][] = $v['count'];
  171. $data['price'][] = round($v['price'], 2);
  172. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  173. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  174. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  175. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  176. }
  177. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  178. $chartdata['xAxis'] = $data['day'];//X轴值
  179. //,'itemStyle'=>$series
  180. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  181. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['price']];//分类1值
  182. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['count']];//分类2值
  183. //统计总数上期
  184. $pre_total = StoreOrderModel::where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  185. ->where($where)
  186. ->field("count(*) as count,sum(pay_price) as price")
  187. ->find();
  188. if ($pre_total) {
  189. $chartdata['pre_cycle']['count'] = [
  190. 'data' => $pre_total['count'] ?: 0
  191. ];
  192. $chartdata['pre_cycle']['price'] = [
  193. 'data' => $pre_total['price'] ?: 0
  194. ];
  195. }
  196. //统计总数
  197. $total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  198. ->where($where)
  199. ->field("count(*) as count,sum(pay_price) as price")
  200. ->find();
  201. if ($total) {
  202. $cha_count = intval($pre_total['count']) - intval($total['count']);
  203. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  204. $chartdata['cycle']['count'] = [
  205. 'data' => $total['count'] ?: 0,
  206. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  207. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  208. ];
  209. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  210. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  211. $chartdata['cycle']['price'] = [
  212. 'data' => $total['price'] ?: 0,
  213. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  214. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  215. ];
  216. }
  217. return app('json')->success('ok', $chartdata);
  218. break;
  219. case 'week':
  220. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  221. $datebefor = date('Y-m-d 00:00:00)', strtotime('-1 week Monday'));
  222. $dateafter = date('Y-m-d 23:59:59', strtotime('-1 week Sunday'));
  223. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  224. ->where($where)
  225. ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price")
  226. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  227. ->order('add_time asc')
  228. ->select()->toArray();
  229. //数据查询重新处理
  230. $new_order_list = [];
  231. foreach ($order_list as $k => $v) {
  232. $new_order_list[$v['day']] = $v;
  233. }
  234. $now_datebefor = date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  235. $now_dateafter = date('Y-m-d 23:59:59', strtotime("+1 day"));
  236. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  237. ->where($where)
  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. ->where($where)
  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. ->where($where)
  300. ->field("count(*) as count,sum(pay_price) as price")
  301. ->find();
  302. if ($total) {
  303. $cha_count = intval($pre_total['count']) - intval($total['count']);
  304. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  305. $chartdata['cycle']['count'] = [
  306. 'data' => $total['count'] ?: 0,
  307. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  308. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  309. ];
  310. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  311. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  312. $chartdata['cycle']['price'] = [
  313. 'data' => $total['price'] ?: 0,
  314. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  315. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  316. ];
  317. }
  318. return app('json')->success('ok', $chartdata);
  319. break;
  320. case 'month':
  321. $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']);
  322. $datebefor = date('Y-m-01 00:00:00', strtotime('-1 month'));
  323. $dateafter = date('Y-m-d 23:59:59', strtotime(date('Y-m-01')));
  324. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  325. ->where($where)
  326. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  327. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  328. ->order('add_time asc')
  329. ->select()->toArray();
  330. //数据查询重新处理
  331. $new_order_list = [];
  332. foreach ($order_list as $k => $v) {
  333. $new_order_list[$v['day']] = $v;
  334. }
  335. $now_datebefor = date('Y-m-01 00:00:00');
  336. $now_dateafter = date('Y-m-d 23:59:59', strtotime("+1 day"));
  337. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  338. ->where($where)
  339. ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price")
  340. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  341. ->order('add_time asc')
  342. ->select()->toArray();
  343. //数据查询重新处理 key 变为当前值
  344. $new_now_order_list = [];
  345. foreach ($now_order_list as $k => $v) {
  346. $new_now_order_list[$v['day']] = $v;
  347. }
  348. foreach ($weekarray as $dk => $dd) {
  349. if (!empty($new_order_list[$dk])) {
  350. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  351. } else {
  352. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  353. }
  354. if (!empty($new_now_order_list[$dk])) {
  355. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  356. } else {
  357. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  358. }
  359. }
  360. $chartdata = [];
  361. $data = [];//临时
  362. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  363. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  364. foreach ($weekarray as $k => $v) {
  365. $data['day'][] = $v[0];
  366. $data['pre']['count'][] = $v['pre']['count'];
  367. $data['pre']['price'][] = round($v['pre']['price'], 2);
  368. $data['now']['count'][] = $v['now']['count'];
  369. $data['now']['price'][] = round($v['now']['price'], 2);
  370. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  371. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  372. }
  373. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  374. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  375. }
  376. }
  377. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  378. $chartdata['xAxis'] = $data['day'];//X轴值
  379. //,'itemStyle'=>$series
  380. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  381. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  382. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  383. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  384. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  385. //统计总数上期
  386. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  387. ->where($where)
  388. ->field("count(*) as count,sum(pay_price) as price")
  389. ->find();
  390. if ($pre_total) {
  391. $chartdata['pre_cycle']['count'] = [
  392. 'data' => $pre_total['count'] ?: 0
  393. ];
  394. $chartdata['pre_cycle']['price'] = [
  395. 'data' => $pre_total['price'] ?: 0
  396. ];
  397. }
  398. //统计总数
  399. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  400. ->where($where)
  401. ->field("count(*) as count,sum(pay_price) as price")
  402. ->find();
  403. if ($total) {
  404. $cha_count = intval($pre_total['count']) - intval($total['count']);
  405. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  406. $chartdata['cycle']['count'] = [
  407. 'data' => $total['count'] ?: 0,
  408. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  409. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  410. ];
  411. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  412. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  413. $chartdata['cycle']['price'] = [
  414. 'data' => $total['price'] ?: 0,
  415. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  416. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  417. ];
  418. }
  419. return app('json')->success('ok', $chartdata);
  420. break;
  421. case 'year':
  422. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  423. $datebefor = date('Y-01-01 00:00:00', strtotime('-1 year'));
  424. $dateafter = date('Y-12-31 23:59:59', strtotime('-1 year'));
  425. $order_list = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  426. ->where($where)
  427. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  428. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  429. ->order('add_time asc')
  430. ->select()->toArray();
  431. //数据查询重新处理
  432. $new_order_list = [];
  433. foreach ($order_list as $k => $v) {
  434. $new_order_list[$v['day']] = $v;
  435. }
  436. $now_datebefor = date('Y-01-01 00:00:00');
  437. $now_dateafter = date('Y-m-d 23:59:59');
  438. $now_order_list = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  439. ->where($where)
  440. ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price")
  441. ->group("FROM_UNIXTIME(add_time, '%Y%m')")
  442. ->order('add_time asc')
  443. ->select()->toArray();
  444. //数据查询重新处理 key 变为当前值
  445. $new_now_order_list = [];
  446. foreach ($now_order_list as $k => $v) {
  447. $new_now_order_list[$v['day']] = $v;
  448. }
  449. foreach ($weekarray as $dk => $dd) {
  450. if (!empty($new_order_list[$dk])) {
  451. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  452. } else {
  453. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  454. }
  455. if (!empty($new_now_order_list[$dk])) {
  456. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  457. } else {
  458. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  459. }
  460. }
  461. $chartdata = [];
  462. $data = [];//临时
  463. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  464. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  465. foreach ($weekarray as $k => $v) {
  466. $data['day'][] = $v[0];
  467. $data['pre']['count'][] = $v['pre']['count'];
  468. $data['pre']['price'][] = round($v['pre']['price'], 2);
  469. $data['now']['count'][] = $v['now']['count'];
  470. $data['now']['price'][] = round($v['now']['price'], 2);
  471. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  472. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  473. }
  474. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  475. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  476. }
  477. }
  478. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  479. $chartdata['xAxis'] = $data['day'];//X轴值
  480. //,'itemStyle'=>$series
  481. $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]];
  482. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['pre']['price']];//分类1值
  483. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series, 'data' => $data['now']['price']];//分类1值
  484. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['pre']['count']];//分类2值
  485. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series, 'data' => $data['now']['count']];//分类2值
  486. //统计总数上期
  487. $pre_total = StoreOrderModel::where('add_time', 'between time', [$datebefor, $dateafter])
  488. ->where($where)
  489. ->field("count(*) as count,sum(pay_price) as price")
  490. ->find();
  491. if ($pre_total) {
  492. $chartdata['pre_cycle']['count'] = [
  493. 'data' => $pre_total['count'] ?: 0
  494. ];
  495. $chartdata['pre_cycle']['price'] = [
  496. 'data' => $pre_total['price'] ?: 0
  497. ];
  498. }
  499. //统计总数
  500. $total = StoreOrderModel::where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  501. ->where($where)
  502. ->field("count(*) as count,sum(pay_price) as price")
  503. ->find();
  504. if ($total) {
  505. $cha_count = intval($pre_total['count']) - intval($total['count']);
  506. //$pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  507. $chartdata['cycle']['count'] = [
  508. 'data' => $total['count'] ?: 0,
  509. 'percent' => intval($pre_total['count']) == 0 ? 100 : round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  510. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  511. ];
  512. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  513. //$pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  514. $chartdata['cycle']['price'] = [
  515. 'data' => $total['price'] ?: 0,
  516. 'percent' => (intval($pre_total['price']) == 0 || !$pre_total['price'] || $pre_total['price'] == 0.00) ? 100 : round(abs($cha_price) / $pre_total['price'] * 100, 2),
  517. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  518. ];
  519. }
  520. return app('json')->success('ok', $chartdata);
  521. break;
  522. default:
  523. break;
  524. }
  525. }
  526. /**
  527. * 用户图表
  528. */
  529. public function userchart()
  530. {
  531. header('Content-type:text/json');
  532. $starday = date('Y-m-d', strtotime('-30 day'));
  533. $yesterday = date('Y-m-d');
  534. $user_list = UserModel::where('add_time', 'between time', [$starday, $yesterday])
  535. ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
  536. ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
  537. ->order('add_time asc')
  538. ->select()->toArray();
  539. $chartdata = [];
  540. $data = [];
  541. $chartdata['legend'] = ['用户数'];//分类
  542. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  543. $chartdata['xAxis'] = [date('m-d')];//X轴值
  544. $chartdata['series'] = [0];//分类1值
  545. if (!empty($user_list)) {
  546. foreach ($user_list as $k => $v) {
  547. $data['day'][] = $v['day'];
  548. $data['count'][] = $v['count'];
  549. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  550. $chartdata['yAxis']['maxnum'] = $v['count'];
  551. }
  552. $chartdata['xAxis'] = $data['day'];//X轴值
  553. $chartdata['series'] = $data['count'];//分类1值
  554. }
  555. return app('json')->success('ok', $chartdata);
  556. }
  557. /**
  558. * 待办事统计
  559. * @param int $newTime
  560. * @return false|string
  561. */
  562. public function Jnotice($newTime = 30)
  563. {
  564. header('Content-type:text/json');
  565. $data = [];
  566. $data['ordernum'] = StoreOrderModel::statusByWhere(1)->count();//待发货
  567. $replenishment_num = sys_config('store_stock') > 0 ? sys_config('store_stock') : 2;//库存预警界限
  568. $data['inventory'] = ProductModel::where('stock', '<=', $replenishment_num)->where('is_show', 1)->where('is_del', 0)->count();//库存
  569. $data['commentnum'] = StoreProductReplyModel::where('is_reply', 0)->count();//评论
  570. $data['reflectnum'] = UserExtractModel::where('status', 0)->count();;//提现
  571. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  572. //新订单提醒
  573. $data['newOrderId'] = StoreOrderModel::statusByWhere(1)->where('is_remind', 0)->column('order_id', 'id');
  574. if (count($data['newOrderId'])) StoreOrderModel::where('order_id', 'in', $data['newOrderId'])->update(['is_remind' => 1]);
  575. return app('json')->success('ok', $data);
  576. }
  577. public function test()
  578. {
  579. /*
  580. $order = StoreOrder::find(423);
  581. StoreGp::createGp(['data'=>$order]);
  582. $list = StoreGp::select()->toArray();
  583. foreach ($list as $v)
  584. {
  585. $user = UserModel::where('uid',$v['uid'])->find();
  586. $spuser = User::where('uid',$user['spread_uid'])->find();
  587. $sp_uid = $spuser['uid']>0?$spuser['uid']:0;
  588. $order_id = $v['order_id'];
  589. $price = $v['price'];
  590. $uid = $v['uid'];
  591. $add_time = time();
  592. $status = 0;
  593. if(!StoreMgp::be(['uid'=>$user['uid']]))
  594. {
  595. dump($sp_uid);
  596. $rs = StoreMgp::getpath($v['uid'],$sp_uid);
  597. dump($rs);
  598. list($p_no,$no,$no_path,$user_path,$level) = [$rs['p_no'],$rs['no'],$rs['no_path'],$rs['user_path'],$rs['level']];
  599. StoreMgp::create(compact('no','p_no','order_id','no_path','user_path','level','price','uid','add_time','status'));
  600. StoreMgp::where('no',$p_no)->inc('cts',1)->update();
  601. }
  602. }
  603. $list = StoreMgp::where('id','>',453)->select()->toArray();
  604. StoreMgp::beginTrans();
  605. try {
  606. foreach ($list as $v)
  607. {
  608. $no = $v['no'];
  609. $order = StoreOrder::find($v['order_id']);
  610. $no_path = explode(",",$v['user_path']);
  611. if (sizeof($no_path) > 1) {
  612. $path = array_reverse($no_path);
  613. $n = 0;
  614. foreach ($path as $v) {
  615. $n++;
  616. if ($n <=1) continue;
  617. if($n>8) break;
  618. $sp_brokeragePrice = StoreGp::getbrokeragePrice($v);
  619. if($sp_brokeragePrice==0) continue;
  620. $user = User::find($v);
  621. $spuser = User::find($user['spread_uid']);
  622. if($user['cycle_time']>time() && !UserBill::where('title','团队奖')->where('link_id',$order['id'])->where('uid',$user['uid'])->find()) {
  623. $balance = bcadd($user['brokerage_price'], $sp_brokeragePrice, 2);
  624. $mark = "公排号:" . $no . '团队奖' . floatval($sp_brokeragePrice) . "元,订单号:" . $order['order_id'];
  625. $res1 = UserBill::income('团队奖', $user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  626. echo UserBill::getlastsql()."<br>";
  627. User::where('uid', $user['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  628. echo User::getlastsql()."<br>";
  629. }
  630. if($spuser && $user['spread_uid']>0 && $spuser['cycle_time']>time())
  631. {
  632. $balance = bcadd($spuser['brokerage_price'], $sp_brokeragePrice, 2);
  633. $mark = "公排号:" . $no . '推荐奖' . $order['$sp_brokeragePrice'] . "元,订单号:" . $order['order_id'];
  634. $res = UserBill::income('推荐奖', $spuser['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  635. echo UserBill::getlastsql()."<br>";
  636. User::where('uid', $spuser['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  637. echo User::getlastsql()."<br>";
  638. }
  639. }
  640. }
  641. }
  642. echo 'ok';
  643. StoreMgp::commitTrans();
  644. }catch (Exception $e)
  645. {
  646. StoreMgp::rollbackTrans();
  647. echo 'error';
  648. return false;
  649. }*/
  650. }
  651. }