Index.php 36 KB

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