relationshipSave.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 获取操作类型
  5. $act = $_GET['act'] ?? '';
  6. // 获取表单数据
  7. $id = isset($_POST['id']) ? intval($_POST['id']) : (isset($_GET['id']) ? intval($_GET['id']) : 0);
  8. $source_customer_id = isset($_POST['source_customer_id']) ? intval($_POST['source_customer_id']) : 0;
  9. $target_customer_id = isset($_POST['target_customer_id']) ? intval($_POST['target_customer_id']) : 0;
  10. $relationship_type = isset($_POST['relationship_type']) ? intval($_POST['relationship_type']) : 0;
  11. $relationship_status = isset($_POST['relationship_status']) ? intval($_POST['relationship_status']) : 1;
  12. $description = isset($_POST['description']) ? textEncode($_POST['description']) : '';
  13. // 当前员工ID
  14. $employee_id = $_SESSION['employee_id'];
  15. // 删除操作
  16. if ($act == 'delete' && $id > 0) {
  17. // 验证关系记录是否存在
  18. $checkQuery = "SELECT * FROM customer_relationship WHERE id = $id";
  19. $result = $conn->query($checkQuery);
  20. if ($result->num_rows == 0) {
  21. echo "<script>alert('未找到指定的客户关系记录!'); window.location.href='relationships.php';</script>";
  22. exit;
  23. }
  24. // 记录删除操作到日志
  25. $row = $result->fetch_assoc();
  26. $source_company_query = "SELECT cs_company FROM customer WHERE id = ".$row['source_customer_id'];
  27. $target_company_query = "SELECT cs_company FROM customer WHERE id = ".$row['target_customer_id'];
  28. $source_result = $conn->query($source_company_query);
  29. $source_company = '';
  30. if ($source_row = $source_result->fetch_assoc()) {
  31. $source_company = textDecode($source_row['cs_company']);
  32. }
  33. $target_result = $conn->query($target_company_query);
  34. $target_company = '';
  35. if ($target_row = $target_result->fetch_assoc()) {
  36. $target_company = textDecode($target_row['cs_company']);
  37. }
  38. $log_message = $_SESSION['employee_name'] . " 删除了客户关系记录:" .
  39. $source_company . " 和 " . $target_company . " 之间的关系";
  40. $logQuery = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('".
  41. mysqli_real_escape_string($conn, $_SESSION['employee_name'])."', '".
  42. mysqli_real_escape_string($conn, getIp())."', NOW(), '".
  43. mysqli_real_escape_string($conn, $log_message)."')";
  44. $conn->query($logQuery);
  45. // 执行删除操作
  46. $deleteQuery = "DELETE FROM customer_relationship WHERE id = $id";
  47. $conn->query($deleteQuery);
  48. echo "<script>alert('客户关系记录已成功删除!'); window.location.href='relationships.php';</script>";
  49. exit;
  50. }
  51. // 表单数据验证
  52. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  53. // 验证源客户和目标客户
  54. if ($source_customer_id <= 0 || $target_customer_id <= 0) {
  55. echo "<script>alert('请选择有效的源客户和目标客户!'); history.back();</script>";
  56. exit;
  57. }
  58. if ($source_customer_id == $target_customer_id) {
  59. echo "<script>alert('源客户和目标客户不能是同一个客户!'); history.back();</script>";
  60. exit;
  61. }
  62. // 验证关系类型
  63. if ($relationship_type <= 0 || $relationship_type > 6) {
  64. echo "<script>alert('请选择有效的关系类型!'); history.back();</script>";
  65. exit;
  66. }
  67. // 检查相同的关系是否已存在
  68. $checkDuplicateQuery = "SELECT * FROM customer_relationship
  69. WHERE source_customer_id = $source_customer_id
  70. AND target_customer_id = $target_customer_id
  71. AND relationship_type = $relationship_type";
  72. if ($id > 0) {
  73. $checkDuplicateQuery .= " AND id != $id";
  74. }
  75. $result = $conn->query($checkDuplicateQuery);
  76. if ($result->num_rows > 0) {
  77. echo "<script>alert('相同的客户关系记录已存在!'); history.back();</script>";
  78. exit;
  79. }
  80. // 根据是否有ID决定是更新还是新增
  81. if ($id > 0) {
  82. // 更新操作
  83. $updateQuery = "UPDATE customer_relationship SET
  84. source_customer_id = $source_customer_id,
  85. target_customer_id = $target_customer_id,
  86. relationship_type = $relationship_type,
  87. relationship_status = $relationship_status,
  88. description = '$description',
  89. updated_by = $employee_id,
  90. updated_at = NOW()
  91. WHERE id = $id";
  92. if ($conn->query($updateQuery)) {
  93. // 获取源客户和目标客户名称
  94. $source_company_query = "SELECT cs_company FROM customer WHERE id = $source_customer_id";
  95. $target_company_query = "SELECT cs_company FROM customer WHERE id = $target_customer_id";
  96. $source_result = $conn->query($source_company_query);
  97. $source_company = '';
  98. if ($source_row = $source_result->fetch_assoc()) {
  99. $source_company = textDecode($source_row['cs_company']);
  100. }
  101. $target_result = $conn->query($target_company_query);
  102. $target_company = '';
  103. if ($target_row = $target_result->fetch_assoc()) {
  104. $target_company = textDecode($target_row['cs_company']);
  105. }
  106. // 记录日志
  107. $log_message = $_SESSION['employee_name'] . " 更新了客户关系记录:" .
  108. $source_company . " 和 " . $target_company . " 之间的关系";
  109. $logQuery = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('".
  110. mysqli_real_escape_string($conn, $_SESSION['employee_name'])."', '".
  111. mysqli_real_escape_string($conn, getIp())."', NOW(), '".
  112. mysqli_real_escape_string($conn, $log_message)."')";
  113. $conn->query($logQuery);
  114. echo "<script>alert('客户关系记录已成功更新!'); window.location.href='relationships.php';</script>";
  115. } else {
  116. echo "<script>alert('更新客户关系记录失败:" . $conn->error . "'); history.back();</script>";
  117. }
  118. } else {
  119. // 新增操作
  120. $insertQuery = "INSERT INTO customer_relationship
  121. (source_customer_id, target_customer_id, relationship_type, relationship_status,
  122. description, employee_id, updated_by, created_at, updated_at)
  123. VALUES ($source_customer_id, $target_customer_id, $relationship_type, $relationship_status,
  124. '$description', $employee_id, $employee_id, NOW(), NOW())";
  125. if ($conn->query($insertQuery)) {
  126. // 获取源客户和目标客户名称
  127. $source_company_query = "SELECT cs_company FROM customer WHERE id = $source_customer_id";
  128. $target_company_query = "SELECT cs_company FROM customer WHERE id = $target_customer_id";
  129. $source_result = $conn->query($source_company_query);
  130. $source_company = '';
  131. if ($source_row = $source_result->fetch_assoc()) {
  132. $source_company = textDecode($source_row['cs_company']);
  133. }
  134. $target_result = $conn->query($target_company_query);
  135. $target_company = '';
  136. if ($target_row = $target_result->fetch_assoc()) {
  137. $target_company = textDecode($target_row['cs_company']);
  138. }
  139. // 记录日志
  140. $log_message = $_SESSION['employee_name'] . " 新增了客户关系记录:" .
  141. $source_company . " 和 " . $target_company . " 之间的关系";
  142. $logQuery = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('".
  143. mysqli_real_escape_string($conn, $_SESSION['employee_name'])."', '".
  144. mysqli_real_escape_string($conn, getIp())."', NOW(), '".
  145. mysqli_real_escape_string($conn, $log_message)."')";
  146. $conn->query($logQuery);
  147. echo "<script>alert('客户关系记录已成功添加!'); window.location.href='relationships.php';</script>";
  148. } else {
  149. echo "<script>alert('添加客户关系记录失败:" . $conn->error . "'); history.back();</script>";
  150. }
  151. }
  152. } else {
  153. echo "<script>alert('无效的请求!'); window.location.href='relationships.php';</script>";
  154. }
  155. ?>