statistics_order_warnings.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. <?php
  2. /**
  3. * 订单预警系统 - 监控订单异常情况
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. // 检查登录状态
  8. if (!isset($_SESSION['employee_id'])) {
  9. checkLogin();
  10. }
  11. // 获取日期范围参数
  12. $date_params = getDateRangeParams();
  13. $current_start_date = $date_params['start_date_sql'];
  14. $current_end_date = $date_params['end_date_sql'];
  15. $date_range = $date_params['date_range'];
  16. // 获取选中的业务员ID
  17. $selected_employee = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : 0;
  18. // 获取所有业务员列表
  19. $sql_employees = "SELECT id, em_user FROM employee ORDER BY em_user";
  20. $employees_result = $conn->query($sql_employees);
  21. // 计算上一个时间段范围(用于比较)
  22. $previous_start_date = '';
  23. $previous_end_date = '';
  24. // 根据当前选择的日期范围,计算上一个对比时段
  25. if ($date_range == 'current_month') {
  26. // 上个月
  27. $previous_start_date = date('Y-m-01', strtotime('-1 month', strtotime($current_start_date)));
  28. $previous_end_date = date('Y-m-t', strtotime('-1 month', strtotime($current_end_date)));
  29. } elseif ($date_range == 'last_month') {
  30. // 上上个月
  31. $previous_start_date = date('Y-m-01', strtotime('-2 month', strtotime($current_start_date)));
  32. $previous_end_date = date('Y-m-t', strtotime('-2 month', strtotime($current_end_date)));
  33. } elseif ($date_range == 'current_year') {
  34. // 去年同期
  35. $previous_start_date = date('Y-01-01', strtotime('-1 year', strtotime($current_start_date)));
  36. $previous_end_date = date('Y-12-31', strtotime('-1 year', strtotime($current_end_date)));
  37. } elseif ($date_range == 'last_30_days' || $date_range == 'last_90_days' || $date_range == 'custom') {
  38. // 上一个同长度周期
  39. $date_diff = (strtotime($current_end_date) - strtotime($current_start_date)) / (60 * 60 * 24);
  40. $previous_end_date = date('Y-m-d', strtotime('-1 day', strtotime($current_start_date)));
  41. $previous_start_date = date('Y-m-d', strtotime("-{$date_diff} day", strtotime($previous_end_date)));
  42. }
  43. // 阈值设置(可以移到数据库或配置文件中)
  44. $order_amount_decrease_threshold = -15; // 订单金额下降超过15%触发预警
  45. $repurchase_cycle_threshold = 90; // 复购周期超过90天触发预警(3个月内未录入订单)
  46. $inactive_threshold = 90; // 90天未有客户信息修改视为不活跃客户(3个月)
  47. $churn_threshold = 365; // 365天未下单视为流失客户(1年)
  48. $normal_repurchase_days = 30; // 正常复购周期参考值(天)
  49. // 页面头部
  50. include('statistics_header.php');
  51. ?>
  52. <div class="page-header">
  53. <h1 class="page-title">订单预警系统</h1>
  54. <p class="page-description">监控订单异常情况,提前预警潜在问题</p>
  55. </div>
  56. <!-- 日期筛选 -->
  57. <div class="filter-form">
  58. <form method="get" class="filter-form-inline">
  59. <div class="form-group">
  60. <label for="date_range">分析周期</label>
  61. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  62. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  63. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  64. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  65. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  66. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  67. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  68. </select>
  69. </div>
  70. <div class="form-group custom-date-inputs" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  71. <label for="start_date">开始日期</label>
  72. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  73. </div>
  74. <div class="form-group custom-date-inputs" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  75. <label for="end_date">结束日期</label>
  76. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  77. </div>
  78. <div class="form-group">
  79. <label for="employee_id">业务员</label>
  80. <select class="form-control" id="employee_id" name="employee_id">
  81. <option value="0">全部业务员</option>
  82. <?php while ($emp = $employees_result->fetch_assoc()): ?>
  83. <option value="<?php echo $emp['id']; ?>" <?php echo $selected_employee == $emp['id'] ? 'selected' : ''; ?>><?php echo htmlspecialchars($emp['em_user']); ?></option>
  84. <?php endwhile; ?>
  85. </select>
  86. </div>
  87. <div class="form-group">
  88. <button type="submit" class="form-btn form-btn-primary">应用筛选</button>
  89. </div>
  90. </form>
  91. </div>
  92. <!-- 预警概览 -->
  93. <div class="warnings-overview">
  94. <div class="row">
  95. <?php
  96. // 获取总预警数
  97. $sql_total_warnings = "SELECT
  98. (SELECT COUNT(*) FROM customer WHERE cs_deal = 3) as total_customers,
  99. (SELECT COUNT(DISTINCT customer_id) FROM orders WHERE order_date BETWEEN ? AND ?) as active_customers";
  100. $stmt = $conn->prepare($sql_total_warnings);
  101. $stmt->bind_param("ss", $current_start_date, $current_end_date);
  102. $stmt->execute();
  103. $result = $stmt->get_result();
  104. $warning_count = $result->fetch_assoc();
  105. // 获取订单金额下降的客户数
  106. $decreasing_amount_count = getDecreasingOrderAmountCustomers($conn, $current_start_date, $current_end_date, $previous_start_date, $previous_end_date, $order_amount_decrease_threshold, true, $selected_employee);
  107. // 获取复购周期异常(3个月内未录入订单)的客户数
  108. $abnormal_cycle_count = getAbnormalRepurchaseCycleCustomers($conn, $current_start_date, $current_end_date, $repurchase_cycle_threshold, true, $selected_employee);
  109. // 获取长期不活跃(3个月内没有客户信息修改)客户数
  110. $inactive_customers_count = getInactiveCustomers($conn, $current_end_date, $inactive_threshold, true, 1, 10, $selected_employee);
  111. // 获取流失客户(1年内未录入订单)数
  112. $churn_customers_count = getChurnCustomers($conn, $current_end_date, $churn_threshold, true, 1, 10, $selected_employee);
  113. ?>
  114. <div class="col-md-3">
  115. <div class="stat-card warning">
  116. <h3>订单金额下降客户</h3>
  117. <div class="stat-value"><?php echo $decreasing_amount_count; ?></div>
  118. <div class="stat-desc">金额下降超过<?php echo abs($order_amount_decrease_threshold); ?>%</div>
  119. </div>
  120. </div>
  121. <div class="col-md-3">
  122. <div class="stat-card warning">
  123. <h3>复购周期异常客户</h3>
  124. <div class="stat-value"><?php echo $abnormal_cycle_count; ?></div>
  125. <div class="stat-desc">3个月内未录入订单</div>
  126. </div>
  127. </div>
  128. <div class="col-md-3">
  129. <div class="stat-card danger">
  130. <h3>流失客户</h3>
  131. <div class="stat-value"><?php echo $churn_customers_count; ?></div>
  132. <div class="stat-desc">1年内未录入订单</div>
  133. </div>
  134. </div>
  135. <div class="col-md-3">
  136. <div class="stat-card info">
  137. <h3>长期不活跃客户</h3>
  138. <div class="stat-value"><?php echo $inactive_customers_count; ?></div>
  139. <div class="stat-desc">3个月内无客户信息更新</div>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. <!-- 订单金额下降客户列表 -->
  145. <div class="warning-section">
  146. <div class="section-header">
  147. <h2>订单金额下降客户</h2>
  148. <p>与上一周期相比,订单金额显著下降的客户</p>
  149. </div>
  150. <table class="data-table">
  151. <thead>
  152. <tr>
  153. <th>客户名称</th>
  154. <th>本期订单金额</th>
  155. <th>上期订单金额</th>
  156. <th>变化百分比</th>
  157. <th>最近出货日期</th>
  158. <th>业务员</th>
  159. <th>操作</th>
  160. </tr>
  161. </thead>
  162. <tbody>
  163. <?php
  164. $decreasing_customers = getDecreasingOrderAmountCustomers(
  165. $conn,
  166. $current_start_date,
  167. $current_end_date,
  168. $previous_start_date,
  169. $previous_end_date,
  170. $order_amount_decrease_threshold,
  171. false,
  172. $selected_employee
  173. );
  174. while ($customer = $decreasing_customers->fetch_assoc()) {
  175. $change_percent = round((($customer['current_amount'] - $customer['previous_amount']) / $customer['previous_amount']) * 100, 1);
  176. $change_class = $change_percent < -20 ? 'text-danger' : 'text-warning';
  177. echo "<tr>";
  178. echo "<td>" . htmlspecialchars($customer['cs_company']) . "</td>";
  179. echo "<td>¥" . number_format($customer['current_amount'], 2) . "</td>";
  180. echo "<td>¥" . number_format($customer['previous_amount'], 2) . "</td>";
  181. echo "<td class='{$change_class}'>" . $change_percent . "%</td>";
  182. echo "<td>" . $customer['last_order_date'] . "</td>";
  183. echo "<td>" . htmlspecialchars($customer['em_user']) . "</td>";
  184. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  185. echo "</tr>";
  186. }
  187. if ($decreasing_customers->num_rows == 0) {
  188. echo "<tr><td colspan='7' class='text-center'>没有发现订单金额下降的客户</td></tr>";
  189. }
  190. ?>
  191. </tbody>
  192. </table>
  193. </div>
  194. <!-- 复购周期异常客户列表 -->
  195. <div class="warning-section" id="abnormal-customers">
  196. <div class="section-header">
  197. <h2>复购周期异常客户</h2>
  198. <p>3个月内未录入订单的客户</p>
  199. </div>
  200. <table class="data-table">
  201. <thead>
  202. <tr>
  203. <th>客户名称</th>
  204. <th>上次订单日期</th>
  205. <th>未订单天数</th>
  206. <th>历史订单总数</th>
  207. <th>历史订单总额</th>
  208. <th>业务员</th>
  209. <th>操作</th>
  210. </tr>
  211. </thead>
  212. <tbody>
  213. <?php
  214. // 获取分页参数
  215. $abnormal_page = isset($_GET['abnormal_page']) ? intval($_GET['abnormal_page']) : 1;
  216. $abnormal_page_size = 10; // 每页显示10条记录
  217. // 获取总记录数
  218. $total_abnormal = getAbnormalRepurchaseCycleCustomers($conn, $current_start_date, $current_end_date, $repurchase_cycle_threshold, true, $selected_employee);
  219. // 计算总页数
  220. $abnormal_total_pages = ceil($total_abnormal / $abnormal_page_size);
  221. // 确保页码合法
  222. if ($abnormal_page < 1) $abnormal_page = 1;
  223. if ($abnormal_page > $abnormal_total_pages && $abnormal_total_pages > 0) $abnormal_page = $abnormal_total_pages;
  224. // 获取当页数据
  225. $abnormal_customers = getAbnormalRepurchaseCycleCustomers(
  226. $conn,
  227. $current_start_date,
  228. $current_end_date,
  229. $repurchase_cycle_threshold,
  230. false,
  231. $selected_employee,
  232. $abnormal_page,
  233. $abnormal_page_size
  234. );
  235. while ($customer = $abnormal_customers->fetch_assoc()) {
  236. $inactive_days = $customer['inactive_days'];
  237. $inactive_class = $inactive_days > 60 ? 'text-danger' : 'text-warning';
  238. echo "<tr>";
  239. echo "<td>" . htmlspecialchars($customer['cs_company']) . "</td>";
  240. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  241. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  242. echo "<td>" . $customer['order_count'] . "</td>";
  243. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  244. echo "<td>" . htmlspecialchars($customer['em_user']) . "</td>";
  245. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  246. echo "</tr>";
  247. }
  248. if ($abnormal_customers->num_rows == 0) {
  249. echo "<tr><td colspan='7' class='text-center'>没有发现复购周期异常的客户</td></tr>";
  250. }
  251. ?>
  252. </tbody>
  253. </table>
  254. <!-- 分页控件 -->
  255. <?php if ($abnormal_total_pages > 1): ?>
  256. <div class="pagination-container">
  257. <ul class="pagination">
  258. <?php
  259. // 生成分页链接的基础URL
  260. $base_url = '?';
  261. foreach ($_GET as $key => $value) {
  262. if ($key != 'abnormal_page') {
  263. $base_url .= $key . '=' . urlencode($value) . '&';
  264. }
  265. }
  266. // 上一页链接
  267. if ($abnormal_page > 1) {
  268. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page - 1) . "#abnormal-customers'>上一页</a></li>";
  269. } else {
  270. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>上一页</a></li>";
  271. }
  272. // 页码链接
  273. $start_page = max(1, $abnormal_page - 2);
  274. $end_page = min($abnormal_total_pages, $abnormal_page + 2);
  275. if ($start_page > 1) {
  276. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=1#abnormal-customers'>1</a></li>";
  277. if ($start_page > 2) {
  278. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  279. }
  280. }
  281. for ($i = $start_page; $i <= $end_page; $i++) {
  282. if ($i == $abnormal_page) {
  283. echo "<li class='pager-item active'><a class='pager-link' href='#abnormal-customers'>{$i}</a></li>";
  284. } else {
  285. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$i}#abnormal-customers'>{$i}</a></li>";
  286. }
  287. }
  288. if ($end_page < $abnormal_total_pages) {
  289. if ($end_page < $abnormal_total_pages - 1) {
  290. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  291. }
  292. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$abnormal_total_pages}#abnormal-customers'>{$abnormal_total_pages}</a></li>";
  293. }
  294. // 下一页链接
  295. if ($abnormal_page < $abnormal_total_pages) {
  296. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page + 1) . "#abnormal-customers'>下一页</a></li>";
  297. } else {
  298. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>下一页</a></li>";
  299. }
  300. ?>
  301. </ul>
  302. <div class="pagination-info">
  303. 共 <?php echo $total_abnormal; ?> 条记录,当前显示第 <?php echo $abnormal_page; ?> 页,共 <?php echo $abnormal_total_pages; ?> 页
  304. </div>
  305. </div>
  306. <?php endif; ?>
  307. </div>
  308. <!-- 流失客户列表 -->
  309. <div class="warning-section">
  310. <div class="section-header">
  311. <h2>流失客户</h2>
  312. <p>1年内未录入订单的客户</p>
  313. </div>
  314. <table class="data-table">
  315. <thead>
  316. <tr>
  317. <th>客户编码</th>
  318. <th>客户名称</th>
  319. <th>最后出货日期</th>
  320. <th>未订单天数</th>
  321. <th>历史订单数</th>
  322. <th>历史订单总额</th>
  323. <th>业务员</th>
  324. <th>操作</th>
  325. </tr>
  326. </thead>
  327. <tbody>
  328. <?php
  329. // 获取分页参数
  330. $page = isset($_GET['churn_page']) ? intval($_GET['churn_page']) : 1;
  331. $page_size = 10; // 每页显示10条记录
  332. // 获取总记录数
  333. $total_churn = getChurnCustomers($conn, $current_end_date, $churn_threshold, true, 1, 10, $selected_employee);
  334. // 计算总页数
  335. $total_pages = ceil($total_churn / $page_size);
  336. // 确保页码合法
  337. if ($page < 1) $page = 1;
  338. if ($page > $total_pages && $total_pages > 0) $page = $total_pages;
  339. // 获取当页数据
  340. $churn_customers = getChurnCustomers($conn, $current_end_date, $churn_threshold, false, $page, $page_size, $selected_employee);
  341. while ($customer = $churn_customers->fetch_assoc()) {
  342. $inactive_days = $customer['inactive_days'];
  343. $inactive_class = $inactive_days > 365 ? 'text-danger' : 'text-warning';
  344. echo "<tr>";
  345. echo "<td title='{$customer['cs_code']}'>" . htmlspecialchars($customer['cs_code']) . "</td>";
  346. echo "<td>" . htmlspecialchars($customer['cs_company'] ?: '未填写') . "</td>";
  347. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  348. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  349. echo "<td>" . $customer['order_count'] . "</td>";
  350. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  351. echo "<td>" . htmlspecialchars($customer['em_user']) . "</td>";
  352. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  353. echo "</tr>";
  354. }
  355. if ($churn_customers->num_rows == 0) {
  356. echo "<tr><td colspan='8' class='text-center'>没有发现流失客户</td></tr>";
  357. }
  358. ?>
  359. </tbody>
  360. </table>
  361. <!-- 分页控件 -->
  362. <?php if ($total_pages > 1): ?>
  363. <div class="pagination-container">
  364. <ul class="pagination">
  365. <?php
  366. // 生成分页链接的基础URL
  367. $base_url = '?';
  368. foreach ($_GET as $key => $value) {
  369. if ($key != 'churn_page') {
  370. $base_url .= $key . '=' . urlencode($value) . '&';
  371. }
  372. }
  373. // 上一页链接
  374. if ($page > 1) {
  375. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page - 1) . "#churn-customers'>上一页</a></li>";
  376. } else {
  377. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>上一页</a></li>";
  378. }
  379. // 页码链接
  380. $start_page = max(1, $page - 2);
  381. $end_page = min($total_pages, $page + 2);
  382. if ($start_page > 1) {
  383. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=1#churn-customers'>1</a></li>";
  384. if ($start_page > 2) {
  385. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  386. }
  387. }
  388. for ($i = $start_page; $i <= $end_page; $i++) {
  389. if ($i == $page) {
  390. echo "<li class='pager-item active'><a class='pager-link' href='#churn-customers'>{$i}</a></li>";
  391. } else {
  392. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$i}#churn-customers'>{$i}</a></li>";
  393. }
  394. }
  395. if ($end_page < $total_pages) {
  396. if ($end_page < $total_pages - 1) {
  397. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  398. }
  399. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$total_pages}#churn-customers'>{$total_pages}</a></li>";
  400. }
  401. // 下一页链接
  402. if ($page < $total_pages) {
  403. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page + 1) . "#churn-customers'>下一页</a></li>";
  404. } else {
  405. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>下一页</a></li>";
  406. }
  407. ?>
  408. </ul>
  409. <div class="pagination-info">
  410. 共 <?php echo $total_churn; ?> 条记录,当前显示第 <?php echo $page; ?> 页,共 <?php echo $total_pages; ?> 页
  411. </div>
  412. </div>
  413. <?php endif; ?>
  414. </div>
  415. <!-- 总体订单趋势图 -->
  416. <div class="chart-section">
  417. <div class="section-header">
  418. <h2>总体订单趋势</h2>
  419. <p>最近12个月的订单数量和金额趋势</p>
  420. </div>
  421. <div class="chart-container">
  422. <canvas id="orderTrendChart"></canvas>
  423. </div>
  424. <?php
  425. // 获取最近12个月的订单趋势数据
  426. $sql_trend = "SELECT
  427. DATE_FORMAT(order_date, '%Y-%m') as month,
  428. COUNT(*) as order_count,
  429. SUM(total_amount) as total_amount
  430. FROM orders
  431. WHERE order_date >= DATE_SUB(?, INTERVAL 11 MONTH)
  432. GROUP BY DATE_FORMAT(order_date, '%Y-%m')
  433. ORDER BY month";
  434. $stmt = $conn->prepare($sql_trend);
  435. $stmt->bind_param("s", $current_end_date);
  436. $stmt->execute();
  437. $trend_result = $stmt->get_result();
  438. $months = [];
  439. $order_counts = [];
  440. $order_amounts = [];
  441. while ($row = $trend_result->fetch_assoc()) {
  442. $months[] = $row['month'];
  443. $order_counts[] = $row['order_count'];
  444. $order_amounts[] = $row['total_amount'];
  445. }
  446. // 转为JSON格式,用于JavaScript图表
  447. $months_json = json_encode($months);
  448. $order_counts_json = json_encode($order_counts);
  449. $order_amounts_json = json_encode($order_amounts);
  450. ?>
  451. </div>
  452. <style>
  453. .page-header {
  454. margin-bottom: 30px;
  455. }
  456. .page-title {
  457. font-size: 24px;
  458. margin-top: 0;
  459. margin-bottom: 5px;
  460. }
  461. .page-description {
  462. color: #666;
  463. margin-bottom: 0;
  464. }
  465. .filter-form {
  466. background-color: #f8f9fa;
  467. border-radius: 4px;
  468. padding: 20px;
  469. margin-bottom: 30px;
  470. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  471. }
  472. .filter-form-inline {
  473. display: flex;
  474. flex-wrap: wrap;
  475. gap: 15px;
  476. align-items: end;
  477. }
  478. .warnings-overview {
  479. margin-bottom: 30px;
  480. }
  481. .warnings-overview .row {
  482. display: flex;
  483. flex-wrap: wrap;
  484. margin: 0 -10px;
  485. }
  486. .warnings-overview .col-md-3 {
  487. padding: 0 10px;
  488. width: 25%;
  489. flex: 0 0 25%;
  490. box-sizing: border-box;
  491. }
  492. @media (max-width: 992px) {
  493. .warnings-overview .col-md-3 {
  494. width: 50%;
  495. flex: 0 0 50%;
  496. }
  497. }
  498. @media (max-width: 576px) {
  499. .warnings-overview .col-md-3 {
  500. width: 100%;
  501. flex: 0 0 100%;
  502. }
  503. }
  504. .stat-card {
  505. border-radius: 8px;
  506. padding: 20px;
  507. color: #fff;
  508. height: 100%;
  509. margin-bottom: 20px;
  510. box-shadow: 0 3px 6px rgba(0,0,0,0.16);
  511. text-align: center;
  512. transition: transform 0.3s ease;
  513. }
  514. .stat-card:hover {
  515. transform: translateY(-5px);
  516. }
  517. .stat-card h3 {
  518. margin-top: 0;
  519. font-size: 16px;
  520. font-weight: 500;
  521. margin-bottom: 10px;
  522. }
  523. .stat-value {
  524. font-size: 32px;
  525. font-weight: 600;
  526. margin-bottom: 10px;
  527. }
  528. .stat-desc {
  529. font-size: 15px;
  530. opacity: 0.8;
  531. }
  532. .stat-card.warning {
  533. background-color: #ff9800;
  534. }
  535. .stat-card.danger {
  536. background-color: #f44336;
  537. }
  538. .stat-card.info {
  539. background-color: #2196f3;
  540. }
  541. .stat-card.success {
  542. background-color: #4caf50;
  543. }
  544. .warning-section {
  545. background-color: white;
  546. border-radius: 8px;
  547. padding: 25px;
  548. margin-bottom: 35px;
  549. box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  550. }
  551. .section-header {
  552. margin-bottom: 20px;
  553. }
  554. .section-header h2 {
  555. font-size: 20px;
  556. margin-top: 0;
  557. margin-bottom: 5px;
  558. }
  559. .section-header p {
  560. color: #666;
  561. font-size: 15px;
  562. margin-bottom: 15px;
  563. }
  564. .chart-section {
  565. background-color: white;
  566. border-radius: 4px;
  567. padding: 20px;
  568. margin-bottom: 30px;
  569. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  570. }
  571. .chart-container {
  572. height: 350px;
  573. }
  574. .text-danger {
  575. color: #f44336;
  576. }
  577. .text-warning {
  578. color: #ff9800;
  579. }
  580. .text-info {
  581. color: #2196f3;
  582. }
  583. /* 调整表格和内容样式 */
  584. .data-table {
  585. margin-bottom: 20px;
  586. width: 100%;
  587. font-size: 14px; /* 增加表格字体大小 */
  588. border-collapse: separate;
  589. border-spacing: 0;
  590. table-layout: fixed; /* 固定表格布局 */
  591. }
  592. .data-table th {
  593. font-size: 15px; /* 表头字体大小 */
  594. padding: 12px 15px;
  595. background-color: #f5f5f5;
  596. font-weight: 600;
  597. }
  598. .data-table td {
  599. padding: 12px 15px;
  600. vertical-align: middle;
  601. white-space: nowrap; /* 防止文本换行 */
  602. overflow: hidden; /* 溢出隐藏 */
  603. text-overflow: ellipsis; /* 文本溢出显示省略号 */
  604. }
  605. /* 为长期不活跃客户表格设置列宽 */
  606. #inactive-customers .data-table th:nth-child(1), /* 客户编码 */
  607. #inactive-customers .data-table td:nth-child(1) {
  608. width: 12%;
  609. }
  610. #inactive-customers .data-table th:nth-child(2), /* 客户名称 */
  611. #inactive-customers .data-table td:nth-child(2) {
  612. width: 20%;
  613. }
  614. #inactive-customers .data-table th:nth-child(3), /* 最后出货日期 */
  615. #inactive-customers .data-table td:nth-child(3) {
  616. width: 12%;
  617. }
  618. #inactive-customers .data-table th:nth-child(4), /* 不活跃天数 */
  619. #inactive-customers .data-table td:nth-child(4) {
  620. width: 10%;
  621. }
  622. #inactive-customers .data-table th:nth-child(5), /* 历史订单数 */
  623. #inactive-customers .data-table td:nth-child(5) {
  624. width: 10%;
  625. }
  626. #inactive-customers .data-table th:nth-child(6), /* 历史订单总额 */
  627. #inactive-customers .data-table td:nth-child(6) {
  628. width: 12%;
  629. }
  630. #inactive-customers .data-table th:nth-child(7), /* 业务员 */
  631. #inactive-customers .data-table td:nth-child(7) {
  632. width: 12%;
  633. }
  634. #inactive-customers .data-table th:nth-child(8), /* 操作 */
  635. #inactive-customers .data-table td:nth-child(8) {
  636. width: 12%;
  637. }
  638. .data-table td:last-child {
  639. min-width: 80px; /* 确保操作列有足够宽度 */
  640. text-align: center;
  641. }
  642. .data-table tr:hover {
  643. background-color: #f9f9f9;
  644. }
  645. .action-btn {
  646. padding: 5px 10px;
  647. margin: 0 3px;
  648. min-width: 60px;
  649. display: inline-block;
  650. text-align: center;
  651. border-radius: 4px;
  652. font-size: 13px;
  653. transition: all 0.3s ease;
  654. }
  655. .action-btn-view {
  656. background-color: #2196f3;
  657. color: white;
  658. border: none;
  659. }
  660. .action-btn-view:hover {
  661. background-color: #0d8aee;
  662. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  663. }
  664. /* 为所有客户列表表格添加共同样式 */
  665. .warning-section .data-table {
  666. border: 1px solid #eee;
  667. border-radius: 4px;
  668. box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  669. }
  670. .warning-section .data-table th:first-child,
  671. .warning-section .data-table td:first-child {
  672. padding-left: 20px; /* 左侧留出更多间距 */
  673. }
  674. /* 表格内数字列的对齐方式 */
  675. .warning-section .data-table td:nth-child(3),
  676. .warning-section .data-table td:nth-child(4),
  677. .warning-section .data-table td:nth-child(5) {
  678. text-align: left;
  679. }
  680. /* 调整整体字体大小 */
  681. body {
  682. font-size: 14px;
  683. }
  684. /* 分页样式 */
  685. .pagination-container {
  686. margin-top: 25px;
  687. display: flex;
  688. flex-direction: column;
  689. align-items: center;
  690. }
  691. .pagination {
  692. display: flex;
  693. list-style: none;
  694. padding: 0;
  695. margin-bottom: 15px;
  696. }
  697. .pagination .pager-item {
  698. margin: 0 2px;
  699. }
  700. .pagination .pager-link {
  701. padding: 8px 16px;
  702. border-radius: 4px;
  703. margin: 0 3px;
  704. font-weight: 500;
  705. border: 1px solid #ddd;
  706. background-color: #fff;
  707. color: #2196f3;
  708. text-decoration: none;
  709. display: inline-block;
  710. }
  711. .pagination .pager-item.active .pager-link {
  712. background-color: #2196f3;
  713. color: white;
  714. border-color: #2196f3;
  715. }
  716. .pagination .pager-item.disabled .pager-link {
  717. color: #999;
  718. cursor: not-allowed;
  719. background-color: #f5f5f5;
  720. }
  721. .pagination-info {
  722. margin-top: 10px;
  723. font-size: 14px;
  724. color: #666;
  725. }
  726. .form-btn {
  727. padding: 8px 15px;
  728. border-radius: 4px;
  729. cursor: pointer;
  730. font-size: 14px;
  731. border: none;
  732. transition: all 0.3s ease;
  733. }
  734. .form-btn-primary {
  735. background-color: #2196f3;
  736. color: white;
  737. }
  738. .form-btn-primary:hover {
  739. background-color: #0d8aee;
  740. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  741. }
  742. /* 添加斑马纹和悬停效果 */
  743. .data-table tr:nth-child(even) {
  744. background-color: #f8f8f8;
  745. }
  746. .data-table tbody tr:hover {
  747. background-color: #f0f7ff;
  748. }
  749. </style>
  750. <script>
  751. function toggleCustomDates() {
  752. const dateRange = document.getElementById('date_range').value;
  753. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  754. if (dateRange === 'custom') {
  755. customDateInputs.forEach(el => el.style.display = 'inline-block');
  756. } else {
  757. customDateInputs.forEach(el => el.style.display = 'none');
  758. }
  759. }
  760. // 订单趋势图
  761. document.addEventListener('DOMContentLoaded', function() {
  762. const ctx = document.getElementById('orderTrendChart').getContext('2d');
  763. const months = <?php echo $months_json; ?>;
  764. const orderCounts = <?php echo $order_counts_json; ?>;
  765. const orderAmounts = <?php echo $order_amounts_json; ?>;
  766. const chart = new Chart(ctx, {
  767. type: 'line',
  768. data: {
  769. labels: months,
  770. datasets: [
  771. {
  772. label: '订单数量',
  773. data: orderCounts,
  774. backgroundColor: 'rgba(54, 162, 235, 0.2)',
  775. borderColor: 'rgba(54, 162, 235, 1)',
  776. borderWidth: 2,
  777. yAxisID: 'y-axis-1'
  778. },
  779. {
  780. label: '订单金额',
  781. data: orderAmounts,
  782. backgroundColor: 'rgba(255, 99, 132, 0.2)',
  783. borderColor: 'rgba(255, 99, 132, 1)',
  784. borderWidth: 2,
  785. yAxisID: 'y-axis-2'
  786. }
  787. ]
  788. },
  789. options: {
  790. responsive: true,
  791. maintainAspectRatio: false,
  792. scales: {
  793. 'y-axis-1': {
  794. type: 'linear',
  795. position: 'left',
  796. title: {
  797. display: true,
  798. text: '订单数量'
  799. }
  800. },
  801. 'y-axis-2': {
  802. type: 'linear',
  803. position: 'right',
  804. title: {
  805. display: true,
  806. text: '订单金额'
  807. },
  808. grid: {
  809. drawOnChartArea: false
  810. }
  811. }
  812. }
  813. }
  814. });
  815. });
  816. </script>
  817. <?php
  818. /**
  819. * 获取订单金额下降的客户
  820. */
  821. function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end, $previous_start, $previous_end, $threshold, $count_only = false, $selected_employee = 0) {
  822. // 构建业务员筛选条件
  823. $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
  824. // 如果只需要计数
  825. if ($count_only) {
  826. $sql = "SELECT COUNT(DISTINCT c.id) as count
  827. FROM customer c
  828. LEFT JOIN (
  829. SELECT customer_id, SUM(total_amount) as amount
  830. FROM orders
  831. WHERE order_date BETWEEN ? AND ?
  832. GROUP BY customer_id
  833. ) current_period ON c.id = current_period.customer_id
  834. LEFT JOIN (
  835. SELECT customer_id, SUM(total_amount) as amount
  836. FROM orders
  837. WHERE order_date BETWEEN ? AND ?
  838. GROUP BY customer_id
  839. ) previous_period ON c.id = previous_period.customer_id
  840. JOIN employee e ON c.cs_belong = e.id
  841. WHERE previous_period.amount > 0
  842. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= ?)
  843. AND c.cs_deal = 3" . $employee_filter;
  844. $stmt = $conn->prepare($sql);
  845. if ($selected_employee > 0) {
  846. $stmt->bind_param("ssssdi", $current_start, $current_end, $previous_start, $previous_end, $threshold, $selected_employee);
  847. } else {
  848. $stmt->bind_param("ssssd", $current_start, $current_end, $previous_start, $previous_end, $threshold);
  849. }
  850. $stmt->execute();
  851. $result = $stmt->get_result();
  852. $row = $result->fetch_assoc();
  853. return $row['count'];
  854. }
  855. // 如果需要详细数据
  856. $sql = "SELECT
  857. c.id,
  858. c.cs_company,
  859. IFNULL(current_period.amount, 0) as current_amount,
  860. previous_period.amount as previous_amount,
  861. e.em_user,
  862. IFNULL((SELECT MAX(order_date) FROM orders WHERE customer_id = c.id), '') as last_order_date
  863. FROM customer c
  864. LEFT JOIN (
  865. SELECT customer_id, SUM(total_amount) as amount
  866. FROM orders
  867. WHERE order_date BETWEEN ? AND ?
  868. GROUP BY customer_id
  869. ) current_period ON c.id = current_period.customer_id
  870. LEFT JOIN (
  871. SELECT customer_id, SUM(total_amount) as amount
  872. FROM orders
  873. WHERE order_date BETWEEN ? AND ?
  874. GROUP BY customer_id
  875. ) previous_period ON c.id = previous_period.customer_id
  876. JOIN employee e ON c.cs_belong = e.id
  877. WHERE previous_period.amount > 0
  878. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= ?)
  879. AND c.cs_deal = 3" . $employee_filter . "
  880. ORDER BY (current_period.amount / previous_period.amount) ASC";
  881. $stmt = $conn->prepare($sql);
  882. if ($selected_employee > 0) {
  883. $stmt->bind_param("ssssdi", $current_start, $current_end, $previous_start, $previous_end, $threshold, $selected_employee);
  884. } else {
  885. $stmt->bind_param("ssssd", $current_start, $current_end, $previous_start, $previous_end, $threshold);
  886. }
  887. $stmt->execute();
  888. return $stmt->get_result();
  889. }
  890. /**
  891. * 获取复购周期异常的客户(3个月内未录入订单)
  892. */
  893. function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end, $threshold, $count_only = false, $selected_employee = 0, $page = 1, $page_size = 10) {
  894. // 构建业务员筛选条件
  895. $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
  896. if ($count_only) {
  897. $sql = "SELECT COUNT(DISTINCT c.id) as count
  898. FROM customer c
  899. LEFT JOIN (
  900. SELECT customer_id, MAX(order_date) as last_order_date
  901. FROM orders
  902. GROUP BY customer_id
  903. ) last_orders ON c.id = last_orders.customer_id
  904. JOIN employee e ON c.cs_belong = e.id
  905. WHERE c.cs_deal = 3
  906. AND (
  907. last_orders.last_order_date IS NULL
  908. OR DATEDIFF(?, last_orders.last_order_date) > ?
  909. )" . $employee_filter;
  910. $stmt = $conn->prepare($sql);
  911. if ($selected_employee > 0) {
  912. $stmt->bind_param("sii", $current_end, $threshold, $selected_employee);
  913. } else {
  914. $stmt->bind_param("si", $current_end, $threshold);
  915. }
  916. $stmt->execute();
  917. $result = $stmt->get_result();
  918. $row = $result->fetch_assoc();
  919. return $row['count'];
  920. }
  921. $offset = ($page - 1) * $page_size;
  922. $sql = "SELECT
  923. c.id,
  924. c.cs_company,
  925. last_orders.last_order_date,
  926. CASE
  927. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF(?, c.cs_addtime)
  928. ELSE DATEDIFF(?, last_orders.last_order_date)
  929. END as inactive_days,
  930. IFNULL(order_stats.order_count, 0) as order_count,
  931. IFNULL(order_stats.total_amount, 0) as total_amount,
  932. e.em_user
  933. FROM customer c
  934. LEFT JOIN (
  935. SELECT customer_id, MAX(order_date) as last_order_date
  936. FROM orders
  937. GROUP BY customer_id
  938. ) last_orders ON c.id = last_orders.customer_id
  939. LEFT JOIN (
  940. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  941. FROM orders
  942. GROUP BY customer_id
  943. ) order_stats ON c.id = order_stats.customer_id
  944. JOIN employee e ON c.cs_belong = e.id
  945. WHERE c.cs_deal = 3
  946. AND (
  947. last_orders.last_order_date IS NULL
  948. OR DATEDIFF(?, last_orders.last_order_date) > ?
  949. )" . $employee_filter . "
  950. ORDER BY inactive_days DESC
  951. LIMIT ?, ?";
  952. $stmt = $conn->prepare($sql);
  953. if ($selected_employee > 0) {
  954. $stmt->bind_param("sssiii", $current_end, $current_end, $current_end, $threshold, $selected_employee, $offset, $page_size);
  955. } else {
  956. $stmt->bind_param("sssiii", $current_end, $current_end, $current_end, $threshold, $offset, $page_size);
  957. }
  958. $stmt->execute();
  959. return $stmt->get_result();
  960. }
  961. /**
  962. * 获取长期不活跃的客户(3个月内没有客户信息修改)
  963. */
  964. function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  965. // 构建业务员筛选条件
  966. $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
  967. if ($count_only) {
  968. $sql = "SELECT COUNT(*) as count
  969. FROM customer c
  970. JOIN employee e ON c.cs_belong = e.id
  971. WHERE c.cs_deal = 3
  972. AND DATEDIFF(?, c.cs_updatetime) > ?" . $employee_filter;
  973. $stmt = $conn->prepare($sql);
  974. if ($selected_employee > 0) {
  975. $stmt->bind_param("sii", $end_date, $inactive_days, $selected_employee);
  976. } else {
  977. $stmt->bind_param("si", $end_date, $inactive_days);
  978. }
  979. $stmt->execute();
  980. $result = $stmt->get_result();
  981. $row = $result->fetch_assoc();
  982. return $row['count'];
  983. }
  984. $sql = "SELECT
  985. c.id,
  986. c.cs_company,
  987. c.cs_code,
  988. last_orders.last_order_date,
  989. DATEDIFF(?, c.cs_updatetime) as inactive_days,
  990. c.cs_updatetime as last_update_time,
  991. IFNULL(order_stats.order_count, 0) as order_count,
  992. IFNULL(order_stats.total_amount, 0) as total_amount,
  993. e.em_user
  994. FROM customer c
  995. LEFT JOIN (
  996. SELECT customer_id, MAX(order_date) as last_order_date
  997. FROM orders
  998. GROUP BY customer_id
  999. ) last_orders ON c.id = last_orders.customer_id
  1000. LEFT JOIN (
  1001. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1002. FROM orders
  1003. GROUP BY customer_id
  1004. ) order_stats ON c.id = order_stats.customer_id
  1005. JOIN employee e ON c.cs_belong = e.id
  1006. WHERE c.cs_deal = 3
  1007. AND DATEDIFF(?, c.cs_updatetime) > ?" . $employee_filter . "
  1008. ORDER BY inactive_days DESC
  1009. LIMIT ?, ?";
  1010. $offset = ($page - 1) * $page_size;
  1011. $stmt = $conn->prepare($sql);
  1012. if ($selected_employee > 0) {
  1013. $stmt->bind_param("ssiiii", $end_date, $end_date, $inactive_days, $selected_employee, $offset, $page_size);
  1014. } else {
  1015. $stmt->bind_param("ssiii", $end_date, $end_date, $inactive_days, $offset, $page_size);
  1016. }
  1017. $stmt->execute();
  1018. return $stmt->get_result();
  1019. }
  1020. /**
  1021. * 获取流失客户(1年内未录入订单)
  1022. */
  1023. function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  1024. // 构建业务员筛选条件
  1025. $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
  1026. if ($count_only) {
  1027. $sql = "SELECT COUNT(*) as count
  1028. FROM customer c
  1029. LEFT JOIN (
  1030. SELECT customer_id, MAX(order_date) as last_order_date
  1031. FROM orders
  1032. GROUP BY customer_id
  1033. ) last_orders ON c.id = last_orders.customer_id
  1034. JOIN employee e ON c.cs_belong = e.id
  1035. WHERE c.cs_deal = 3
  1036. AND (
  1037. last_orders.last_order_date IS NULL
  1038. OR DATEDIFF(?, last_orders.last_order_date) > ?
  1039. )" . $employee_filter;
  1040. $stmt = $conn->prepare($sql);
  1041. if ($selected_employee > 0) {
  1042. $stmt->bind_param("sii", $end_date, $churn_days, $selected_employee);
  1043. } else {
  1044. $stmt->bind_param("si", $end_date, $churn_days);
  1045. }
  1046. $stmt->execute();
  1047. $result = $stmt->get_result();
  1048. $row = $result->fetch_assoc();
  1049. return $row['count'];
  1050. }
  1051. $sql = "SELECT
  1052. c.id,
  1053. c.cs_company,
  1054. c.cs_code,
  1055. last_orders.last_order_date,
  1056. CASE
  1057. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF(?, c.cs_addtime)
  1058. ELSE DATEDIFF(?, last_orders.last_order_date)
  1059. END as inactive_days,
  1060. IFNULL(order_stats.order_count, 0) as order_count,
  1061. IFNULL(order_stats.total_amount, 0) as total_amount,
  1062. e.em_user
  1063. FROM customer c
  1064. LEFT JOIN (
  1065. SELECT customer_id, MAX(order_date) as last_order_date
  1066. FROM orders
  1067. GROUP BY customer_id
  1068. ) last_orders ON c.id = last_orders.customer_id
  1069. LEFT JOIN (
  1070. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1071. FROM orders
  1072. GROUP BY customer_id
  1073. ) order_stats ON c.id = order_stats.customer_id
  1074. JOIN employee e ON c.cs_belong = e.id
  1075. WHERE c.cs_deal = 3
  1076. AND (
  1077. last_orders.last_order_date IS NULL
  1078. OR DATEDIFF(?, last_orders.last_order_date) > ?
  1079. )" . $employee_filter . "
  1080. ORDER BY inactive_days DESC
  1081. LIMIT ?, ?";
  1082. $offset = ($page - 1) * $page_size;
  1083. $stmt = $conn->prepare($sql);
  1084. if ($selected_employee > 0) {
  1085. $stmt->bind_param("sssiiiii", $end_date, $end_date, $end_date, $churn_days, $selected_employee, $offset, $page_size);
  1086. } else {
  1087. $stmt->bind_param("sssiii", $end_date, $end_date, $end_date, $churn_days, $offset, $page_size);
  1088. }
  1089. $stmt->execute();
  1090. return $stmt->get_result();
  1091. }
  1092. // 页面底部
  1093. include('statistics_footer.php');
  1094. ?>