statistics_order_warnings.php 51 KB

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