customerSave.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <title>管理区域</title>
  10. <link rel="stylesheet" href="system/css/common.css" type="text/css" />
  11. <script src="system/js/jquery-1.7.2.min.js"></script>
  12. <script src="js/js.js"></script>
  13. </head>
  14. <body class="clear">
  15. <?php
  16. // Get query parameters
  17. $page = $_GET['Page'] ?? '';
  18. $keys = urlencode($_GET['Keys'] ?? '');
  19. $fliterBusiness = $_GET['fliterBusiness'] ?? '';
  20. $fliterDeal = $_GET['Deal'] ?? '';
  21. // Get form data
  22. $id = $_POST['id'] ?? '';
  23. $cs_code = textEncode($_POST['cs_code'] ?? '');
  24. $cs_company = textEncode($_POST['cs_company'] ?? '');
  25. $cs_country = $_POST['cs_country'] ?? '';
  26. $cs_from = $_POST['cs_from'] ?? '';
  27. $cs_address = textEncode($_POST['cs_address'] ?? '');
  28. $cs_type = $_POST['cs_type'] ?? []; // Changed to array for multi-select
  29. $cs_belongclient = $_POST['cs_belongclient'] ?? '';
  30. $cs_addtime = $_POST['cs_addtime'] ?? '';
  31. $cs_updatetime = date('Y-m-d H:i:s');
  32. $cs_belong = $_SESSION['employee_id'];
  33. $cs_state = 1;
  34. $cs_deal = $_POST['cs_deal'] ?? '';
  35. $allowedit = $_POST['Permissions'] ?? '0';
  36. // Get contact information from the form
  37. $contacts = $_POST['contact'] ?? [];
  38. // Validate numeric values
  39. $allowedit = is_numeric($allowedit) ? $allowedit : 0;
  40. $cs_country = (is_numeric($cs_country) && $cs_country !== '') ? $cs_country : 0;
  41. $cs_from = (is_numeric($cs_from) && $cs_from !== '') ? $cs_from : 0;
  42. $cs_deal = (is_numeric($cs_deal) && $cs_deal !== '') ? $cs_deal : 1;
  43. $cs_belongClient = (is_numeric($cs_belongclient) && $cs_belongclient !== '') ? $cs_belongclient : 0;
  44. $cs_note = htmlEncode($_POST['cs_note'] ?? '');
  45. $mytag = textEncode($_POST['mytag'] ?? '');
  46. $mytag = str_replace(['&#60;&#47;span&#62;&#60;span&#62;', '&#60;&#47;span&#62;', '&#60;span&#62;'], [',', '', ''], $mytag);
  47. $mytag = explode(',', $mytag);
  48. // Auto-detect source from code
  49. if (strpos($cs_code, ';1688') !== false) {
  50. $cs_from = 1; // 1688
  51. }
  52. if (strpos($cs_code, ';阿里') !== false) {
  53. $cs_from = 2; // International station
  54. }
  55. // Get the first contact for validation (if any)
  56. $primary_contact = !empty($contacts) ? current($contacts) : [];
  57. // Check contact-specific validation requirements based on source
  58. if ($allowedit != 1) {
  59. // Get the first contact's information for validation
  60. $contact_name = textEncode($primary_contact['contact_name'] ?? '');
  61. $tel_1 = textEncode($primary_contact['tel_1'] ?? '');
  62. $wechat_1 = textEncode($primary_contact['wechat_1'] ?? '');
  63. $whatsapp_1 = textEncode($primary_contact['whatsapp_1'] ?? '');
  64. $email_1 = textEncode($primary_contact['email_1'] ?? '');
  65. $alibaba_1 = textEncode($primary_contact['alibaba_1'] ?? '');
  66. $facebook_1 = textEncode($primary_contact['facebook_1'] ?? '');
  67. // Alibaba validation
  68. if (($cs_from == 1 || $cs_from == 2) && empty($alibaba_1)) {
  69. echo "<script>alert('阿里旺旺为必填项');history.back();</script>";
  70. exit;
  71. }
  72. // Website source validation
  73. if (strpos($cs_code, '官网') !== false) {
  74. $cs_from = 3;
  75. }
  76. if ($cs_from == 3 && empty($tel_1) && empty($whatsapp_1) && empty($wechat_1)) {
  77. echo "<script>alert('电话和WhatsApp为必填项');history.back();</script>";
  78. exit;
  79. }
  80. // Market customer validation
  81. if ($cs_from == 8 && empty($wechat_1)) {
  82. echo "<script>alert('微信为必填项');history.back();</script>";
  83. exit;
  84. }
  85. // Facebook validation
  86. if ($cs_from == 12 && empty($facebook_1)) {
  87. echo "<script>alert('Facebook为必填项');history.back();</script>";
  88. exit;
  89. }
  90. }
  91. // Determine action type
  92. $act = empty($id) || !is_numeric($id) ? 'addSave' : 'editSave';
  93. if ($act === 'editSave') {
  94. // Verify customer ownership - 将bind_param改为SQL拼接
  95. $id = intval($id); // 确保ID是整数,防止SQL注入
  96. $sql = "SELECT cs_belong FROM customer WHERE id = " . $id;
  97. $result = $conn->query($sql);
  98. if ($row = $result->fetch_assoc()) {
  99. if ($row['cs_belong'] != $cs_belong) {
  100. echo "<script>alert('抱歉,该客户属于另一业务,你没有权限修改');history.back();</script>";
  101. exit;
  102. }
  103. } else {
  104. echo "<script>alert('没有此客户!');history.back();</script>";
  105. exit;
  106. }
  107. }
  108. // Validate customer code
  109. if (empty($cs_code)) {
  110. echo "<script>alert('客户编码不能为空');history.back();</script>";
  111. exit;
  112. }
  113. $customer_id = is_numeric($id) ? intval($id) : 0;
  114. // Check for duplicate customer information
  115. $checkStr = "SELECT c.*, cc.*
  116. FROM customer c
  117. LEFT JOIN customer_contact cc ON c.id = cc.customer_id
  118. WHERE c.id != " . $customer_id." AND ( 1=2 ";
  119. $Dupli = "";
  120. /* 暂时不需要这些检查 START */
  121. // Company name similarity check
  122. //if (!empty($cs_company)) {
  123. // $companyWords = explode(' ', strtolower($cs_company));
  124. // foreach ($companyWords as $word) {
  125. // if (strlen($word) > 3) { // Only check words longer than 3 characters
  126. // $checkStr .= " OR LOWER(c.cs_company) LIKE '%" . $conn->real_escape_string($word) . "%'";
  127. // }
  128. // }
  129. //}
  130. // Address similarity check
  131. //if (!empty($cs_address)) {
  132. // $addressWords = explode(' ', strtolower($cs_address));
  133. // foreach ($addressWords as $word) {
  134. // if (strlen($word) > 3) { // Only check words longer than 3 characters
  135. // $checkStr .= " OR LOWER(c.cs_address) LIKE '%" . $conn->real_escape_string($word) . "%'";
  136. // }
  137. // }
  138. //}
  139. /* 暂时不需要这些检查 END */
  140. // Check all contacts for duplicates
  141. foreach ($contacts as $contact) {
  142. //遍历处理空格
  143. foreach ($contact as $key => $value) {
  144. $contact[$key] = trim($value);
  145. }
  146. // Check all phone numbers
  147. for ($i = 1; $i <= 3; $i++) {
  148. $tel_field = 'tel_' . $i;
  149. if (!empty($contact[$tel_field])) {
  150. $tel_format = numFormat($contact[$tel_field]);
  151. // Remove country code and spaces for better matching
  152. $tel_clean = preg_replace('/[^0-9]/', '', $tel_format);
  153. if(!empty($tel_clean))
  154. {
  155. $checkStr .= " OR cc.tel_1_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  156. " OR cc.tel_2_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  157. " OR cc.tel_3_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  158. " OR cc.wechat_1 LIKE '%" . substr($tel_clean, -9) . "%'" .
  159. " OR cc.wechat_2 LIKE '%" . substr($tel_clean, -9) . "%'" .
  160. " OR cc.wechat_3 LIKE '%" . substr($tel_clean, -9) . "%'";
  161. $Dupli .= "电话" . $i . ":" . $contact[$tel_field] . " ";
  162. }
  163. }
  164. }
  165. // Check all email addresses
  166. for ($i = 1; $i <= 3; $i++) {
  167. $email_field = 'email_' . $i;
  168. if (!empty($contact[$email_field])) {
  169. $contact[$email_field]=trim($contact[$email_field]);
  170. $email = strtolower($contact[$email_field]);
  171. $checkStr .= " OR LOWER(cc.email_1) = '" . $conn->real_escape_string($email) . "'" .
  172. " OR LOWER(cc.email_2) = '" . $conn->real_escape_string($email) . "'" .
  173. " OR LOWER(cc.email_3) = '" . $conn->real_escape_string($email) . "'";
  174. $Dupli .= "邮箱" . $i . ":" . $contact[$email_field] . " ";
  175. }
  176. }
  177. // Check all WhatsApp numbers
  178. for ($i = 1; $i <= 3; $i++) {
  179. $whatsapp_field = 'whatsapp_' . $i;
  180. if (!empty($contact[$whatsapp_field])) {
  181. $whatsapp_format = numFormat($contact[$whatsapp_field]);
  182. $whatsapp_clean = preg_replace('/[^0-9]/', '', $whatsapp_format);
  183. if(!empty($whatsapp_clean))
  184. {
  185. $checkStr .= " OR cc.whatsapp_1_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  186. " OR cc.whatsapp_2_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  187. " OR cc.whatsapp_3_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  188. " OR cc.tel_1_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  189. " OR cc.tel_2_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  190. " OR cc.tel_3_format LIKE '%" . substr($whatsapp_clean, -9) . "%'";
  191. $Dupli .= "WhatsApp" . $i . ":" . $contact[$whatsapp_field] . " ";
  192. }
  193. }
  194. }
  195. // Check all WeChat accounts
  196. for ($i = 1; $i <= 3; $i++) {
  197. $wechat_field = 'wechat_' . $i;
  198. if (!empty($contact[$wechat_field])) {
  199. $wechat = $contact[$wechat_field];
  200. $checkStr .= " OR cc.wechat_1 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  201. " OR cc.wechat_2 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  202. " OR cc.wechat_3 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  203. " OR cc.tel_1_format LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  204. " OR cc.tel_2_format LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  205. " OR cc.tel_3_format LIKE '%" . $conn->real_escape_string($wechat) . "%'";
  206. $Dupli .= "微信" . $i . ":" . $contact[$wechat_field] . " ";
  207. }
  208. }
  209. // Check all LinkedIn accounts
  210. for ($i = 1; $i <= 3; $i++) {
  211. $linkedin_field = 'linkedin_' . $i;
  212. if (!empty($contact[$linkedin_field])) {
  213. $linkedin = strtolower($contact[$linkedin_field]);
  214. $checkStr .= " OR LOWER(cc.linkedin_1) LIKE '%" . $conn->real_escape_string($linkedin) . "%'" .
  215. " OR LOWER(cc.linkedin_2) LIKE '%" . $conn->real_escape_string($linkedin) . "%'" .
  216. " OR LOWER(cc.linkedin_3) LIKE '%" . $conn->real_escape_string($linkedin) . "%'";
  217. $Dupli .= "LinkedIn" . $i . ":" . $contact[$linkedin_field] . " ";
  218. }
  219. }
  220. // Check all Facebook accounts
  221. for ($i = 1; $i <= 3; $i++) {
  222. $facebook_field = 'facebook_' . $i;
  223. if (!empty($contact[$facebook_field])) {
  224. $facebook = strtolower($contact[$facebook_field]);
  225. $checkStr .= " OR LOWER(cc.facebook_1) LIKE '%" . $conn->real_escape_string($facebook) . "%'" .
  226. " OR LOWER(cc.facebook_2) LIKE '%" . $conn->real_escape_string($facebook) . "%'" .
  227. " OR LOWER(cc.facebook_3) LIKE '%" . $conn->real_escape_string($facebook) . "%'";
  228. $Dupli .= "Facebook" . $i . ":" . $contact[$facebook_field] . " ";
  229. }
  230. }
  231. // Check all Alibaba accounts
  232. for ($i = 1; $i <= 3; $i++) {
  233. $alibaba_field = 'alibaba_' . $i;
  234. if (!empty($contact[$alibaba_field])) {
  235. $alibaba = strtolower($contact[$alibaba_field]);
  236. if (strlen($alibaba) < 10) {
  237. $checkStr .= " OR LOWER(cc.alibaba_1) LIKE '" . $conn->real_escape_string($alibaba) . "'" .
  238. " OR LOWER(cc.alibaba_2) LIKE '" . $conn->real_escape_string($alibaba) . "'" .
  239. " OR LOWER(cc.alibaba_3) LIKE '" . $conn->real_escape_string($alibaba) . "'";
  240. } else {
  241. $alibaba_clean = preg_replace('/[^0-9a-zA-Z]/', '', $alibaba);
  242. $checkStr .= " OR LOWER(cc.alibaba_1) LIKE '%" . substr($alibaba_clean, -9) . "%'" .
  243. " OR LOWER(cc.alibaba_2) LIKE '%" . substr($alibaba_clean, -9) . "%'" .
  244. " OR LOWER(cc.alibaba_3) LIKE '%" . substr($alibaba_clean, -9) . "%'";
  245. }
  246. $Dupli .= "阿里旺旺" . $i . ":" . $contact[$alibaba_field] . " ";
  247. }
  248. }
  249. }
  250. $checkStr .= " ) ORDER BY c.id ASC";
  251. if ($allowedit != 1) {
  252. $result = $conn->query($checkStr);
  253. if ($result && $result->num_rows > 0) {
  254. $row = $result->fetch_assoc();
  255. // Get owner name
  256. $ownerResult = $conn->query("SELECT em_user FROM employee WHERE id = " . $row['cs_belong']);
  257. $ownerRow = $ownerResult->fetch_assoc();
  258. $owner = textUncode($ownerRow['em_user']);
  259. // Determine who entered first
  260. if (strtotime($cs_addtime) > strtotime($row['cs_addtime'])) {
  261. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  262. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  263. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  264. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  265. $_SESSION['employee_name'] . $cs_addtime . "首次录入')";
  266. } else {
  267. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  268. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  269. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  270. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  271. $owner . $row['cs_addtime'] . "首次录入')";
  272. }
  273. $conn->query($tstr);
  274. echo "<script>alert('warning.1.录入信息\\n与" . $owner . "客户编号:" . textUncode($row['cs_code']) .
  275. "\\n高度类似,未能保存,请联系管理员核实!');history.back();</script>";
  276. exit;
  277. }
  278. // else
  279. // {
  280. // //全文检索再检查一次
  281. // //先去掉联系方式中的特殊字符
  282. // //用联系方式进行全文fullindex检索,用相关性分数大于百分70 的进行检查
  283. //
  284. // // 根据不同联系方式类型进行专门检查
  285. // $duplicateFound = false;
  286. // $matchDetails = '';
  287. // $matchScore = 0;
  288. // $matchCustomerId = 0;
  289. // $matchCustomerCode = '';
  290. // $matchOwner = '';
  291. // $matchAddTime = '';
  292. //
  293. // // 编辑模式下需要排除自己的ID
  294. // $excludeId = ($act === 'editSave' && is_numeric($id)) ? intval($id) : 0;
  295. //
  296. // // 1. 优先检查邮箱 - 邮箱是最精确的匹配
  297. // $emailTerms = [];
  298. // foreach ($contacts as $contact) {
  299. // for ($i = 1; $i <= 3; $i++) {
  300. // $field = 'email_' . $i;
  301. // if (!empty($contact[$field])) {
  302. // $email = strtolower(trim($contact[$field]));
  303. // if (strpos($email, '@') !== false) {
  304. // $emailTerms[] = $email;
  305. // }
  306. // }
  307. // }
  308. // }
  309. //
  310. // if (!empty($emailTerms)) {
  311. // $emailQuery = "SELECT c.id, c.cs_code, c.cs_belong, c.cs_addtime, cc.email_1, cc.email_2, cc.email_3
  312. // FROM customer c
  313. // JOIN customer_contact cc ON c.id = cc.customer_id
  314. // WHERE c.cs_belong != " . $_SESSION['employee_id'] . "
  315. // AND c.id != " . $excludeId . " AND (";
  316. //
  317. // $emailConditions = [];
  318. // foreach ($emailTerms as $email) {
  319. // $emailConditions[] = "cc.email_1 = '" . $conn->real_escape_string($email) . "'";
  320. // $emailConditions[] = "cc.email_2 = '" . $conn->real_escape_string($email) . "'";
  321. // $emailConditions[] = "cc.email_3 = '" . $conn->real_escape_string($email) . "'";
  322. // }
  323. //
  324. // $emailQuery .= implode(" OR ", $emailConditions) . ") LIMIT 1";
  325. // $emailResult = $conn->query($emailQuery);
  326. //
  327. // if ($emailResult && $emailResult->num_rows > 0) {
  328. // $row = $emailResult->fetch_assoc();
  329. // $matchCustomerId = $row['id'];
  330. // $matchCustomerCode = $row['cs_code'];
  331. // $matchOwner = $row['cs_belong'];
  332. // $matchAddTime = $row['cs_addtime'];
  333. // $matchDetails = "邮箱完全匹配";
  334. // $matchScore = 0.95; // 邮箱精确匹配,高可信度
  335. // $duplicateFound = true;
  336. // }
  337. // }
  338. //
  339. // // 2. 检查电话号码与WhatsApp - 清理后进行后缀匹配
  340. // if (!$duplicateFound) {
  341. // $phoneTerms = [];
  342. // foreach ($contacts as $contact) {
  343. // // 收集所有电话号码
  344. // for ($i = 1; $i <= 3; $i++) {
  345. // $fields = ['tel_' . $i, 'whatsapp_' . $i];
  346. // foreach ($fields as $field) {
  347. // if (!empty($contact[$field])) {
  348. // $cleaned = preg_replace('/[^0-9]/', '', $contact[$field]);
  349. // if (strlen($cleaned) > 7) { // 至少8位有效数字
  350. // $phoneTerms[] = $cleaned;
  351. // }
  352. // }
  353. // }
  354. // }
  355. // }
  356. //
  357. // if (!empty($phoneTerms)) {
  358. // $phoneQuery = "SELECT c.id, c.cs_code, c.cs_belong, c.cs_addtime,
  359. // cc.tel_1_format, cc.tel_2_format, cc.tel_3_format,
  360. // cc.whatsapp_1_format, cc.whatsapp_2_format, cc.whatsapp_3_format
  361. // FROM customer c
  362. // JOIN customer_contact cc ON c.id = cc.customer_id
  363. // WHERE c.cs_belong != " . $_SESSION['employee_id'] . "
  364. // AND c.id != " . $excludeId . " AND (";
  365. //
  366. // $phoneConditions = [];
  367. // foreach ($phoneTerms as $phone) {
  368. // // 使用后8位进行匹配,避免国家代码等差异
  369. // $suffix = substr($phone, -8);
  370. // if (strlen($suffix) == 8) {
  371. // $phoneConditions[] = "cc.tel_1_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  372. // $phoneConditions[] = "cc.tel_2_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  373. // $phoneConditions[] = "cc.tel_3_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  374. // $phoneConditions[] = "cc.whatsapp_1_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  375. // $phoneConditions[] = "cc.whatsapp_2_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  376. // $phoneConditions[] = "cc.whatsapp_3_format LIKE '%" . $conn->real_escape_string($suffix) . "'";
  377. // }
  378. // }
  379. //
  380. // if (!empty($phoneConditions)) {
  381. // $phoneQuery .= implode(" OR ", $phoneConditions) . ") LIMIT 1";
  382. // $phoneResult = $conn->query($phoneQuery);
  383. //
  384. // if ($phoneResult && $phoneResult->num_rows > 0) {
  385. // $row = $phoneResult->fetch_assoc();
  386. // $matchCustomerId = $row['id'];
  387. // $matchCustomerCode = $row['cs_code'];
  388. // $matchOwner = $row['cs_belong'];
  389. // $matchAddTime = $row['cs_addtime'];
  390. // $matchDetails = "电话号码后8位匹配";
  391. // $matchScore = 0.90; // 电话匹配,高可信度
  392. // $duplicateFound = true;
  393. // }
  394. // }
  395. // }
  396. // }
  397. //
  398. // // 3. 检查社交媒体账号 (alibaba/wechat/facebook/linkedin)
  399. // if (!$duplicateFound) {
  400. // $socialTerms = [];
  401. // $socialFields = [
  402. // 'alibaba' => '阿里旺旺',
  403. // 'wechat' => '微信',
  404. // 'facebook' => 'Facebook',
  405. // 'linkedin' => 'LinkedIn'
  406. // ];
  407. //
  408. // foreach ($contacts as $contact) {
  409. // foreach ($socialFields as $field => $label) {
  410. // for ($i = 1; $i <= 3; $i++) {
  411. // $fieldName = $field . '_' . $i;
  412. // if (!empty($contact[$fieldName])) {
  413. // $value = trim($contact[$fieldName]);
  414. // if (strlen($value) > 3) { // 至少4个字符
  415. // $socialTerms[] = [
  416. // 'type' => $field,
  417. // 'label' => $label,
  418. // 'value' => $value
  419. // ];
  420. // }
  421. // }
  422. // }
  423. // }
  424. // }
  425. //
  426. // if (!empty($socialTerms)) {
  427. // foreach ($socialTerms as $term) {
  428. // $field = $term['type'];
  429. // $value = $term['value'];
  430. // $label = $term['label'];
  431. //
  432. // // 根据社交媒体类型构建不同的查询
  433. // $socialQuery = "SELECT c.id, c.cs_code, c.cs_belong, c.cs_addtime FROM customer c
  434. // JOIN customer_contact cc ON c.id = cc.customer_id
  435. // WHERE c.cs_belong != " . $_SESSION['employee_id'] . "
  436. // AND c.id != " . $excludeId . " AND (";
  437. //
  438. // // 根据社交账号类型确定匹配方式
  439. // if ($field == 'alibaba' || $field == 'wechat') {
  440. // // 阿里旺旺和微信用精确匹配
  441. // $socialQuery .=
  442. // "cc.{$field}_1 = '" . $conn->real_escape_string($value) . "' OR " .
  443. // "cc.{$field}_2 = '" . $conn->real_escape_string($value) . "' OR " .
  444. // "cc.{$field}_3 = '" . $conn->real_escape_string($value) . "'";
  445. // } else {
  446. // // Facebook和LinkedIn用模糊匹配
  447. // $socialQuery .=
  448. // "cc.{$field}_1 LIKE '%" . $conn->real_escape_string($value) . "%' OR " .
  449. // "cc.{$field}_2 LIKE '%" . $conn->real_escape_string($value) . "%' OR " .
  450. // "cc.{$field}_3 LIKE '%" . $conn->real_escape_string($value) . "%'";
  451. // }
  452. //
  453. // $socialQuery .= ") LIMIT 1";
  454. // $socialResult = $conn->query($socialQuery);
  455. //
  456. // if ($socialResult && $socialResult->num_rows > 0) {
  457. // $row = $socialResult->fetch_assoc();
  458. // $matchCustomerId = $row['id'];
  459. // $matchCustomerCode = $row['cs_code'];
  460. // $matchOwner = $row['cs_belong'];
  461. // $matchAddTime = $row['cs_addtime'];
  462. // $matchDetails = $label . "账号匹配";
  463. //
  464. // // 不同社交媒体账号的可信度
  465. // switch ($field) {
  466. // case 'alibaba':
  467. // $matchScore = 0.85;
  468. // break;
  469. // case 'wechat':
  470. // $matchScore = 0.85;
  471. // break;
  472. // case 'facebook':
  473. // $matchScore = 0.80;
  474. // break;
  475. // case 'linkedin':
  476. // $matchScore = 0.80;
  477. // break;
  478. // default:
  479. // $matchScore = 0.75;
  480. // }
  481. //
  482. // $duplicateFound = true;
  483. // break; // 找到匹配就退出循环
  484. // }
  485. // }
  486. // }
  487. // }
  488. //
  489. // // 4. 最后尝试全文检索 - 作为补充检测手段
  490. // if (!$duplicateFound) {
  491. // // 准备全文检索字符串
  492. // $searchTerms = [];
  493. //
  494. // // 处理所有联系人信息用于检索
  495. // foreach ($contacts as $contact) {
  496. // // 添加联系人名称
  497. // if (!empty($contact['contact_name'])) {
  498. // $searchTerms[] = textUncode($contact['contact_name']);
  499. // }
  500. //
  501. // // 所有联系方式的组合检索
  502. // $contactFields = [
  503. // 'tel', 'email', 'whatsapp', 'wechat',
  504. // 'linkedin', 'facebook', 'alibaba'
  505. // ];
  506. //
  507. // foreach ($contactFields as $fieldType) {
  508. // for ($i = 1; $i <= 3; $i++) {
  509. // $field = $fieldType . '_' . $i;
  510. // if (!empty($contact[$field])) {
  511. // // 针对不同类型的联系方式进行不同清理
  512. // if ($fieldType == 'tel' || $fieldType == 'whatsapp') {
  513. // $cleaned = preg_replace('/[^0-9]/', '', $contact[$field]);
  514. // if (strlen($cleaned) > 5) {
  515. // $searchTerms[] = $cleaned;
  516. // }
  517. // } else if ($fieldType == 'email') {
  518. // $cleaned = strtolower(trim($contact[$field]));
  519. // if (strpos($cleaned, '@') !== false) {
  520. // $searchTerms[] = $cleaned;
  521. // }
  522. // } else {
  523. // $searchTerms[] = trim($contact[$field]);
  524. // }
  525. // }
  526. // }
  527. // }
  528. // }
  529. //
  530. // // 如果有搜索条件
  531. // if (!empty($searchTerms)) {
  532. // // 创建MATCH AGAINST语句的词条
  533. // $searchStr = implode(' ', array_unique(array_filter($searchTerms)));
  534. //
  535. // // 确保搜索字符串不为空
  536. // if (!empty($searchStr)) {
  537. // // 构建全文检索SQL
  538. // $ftQuery = "SELECT c.id, c.cs_code, c.cs_belong, c.cs_addtime,
  539. // MATCH( cc.tel_1, cc.tel_2, cc.tel_3,
  540. // cc.email_1, cc.email_2, cc.email_3,
  541. // cc.whatsapp_1, cc.whatsapp_2, cc.whatsapp_3,
  542. // cc.wechat_1, cc.wechat_2, cc.wechat_3
  543. // )
  544. // AGAINST('" . $conn->real_escape_string($searchStr) . "' IN NATURAL LANGUAGE MODE) AS score
  545. // FROM customer c
  546. // JOIN customer_contact cc ON c.id = cc.customer_id
  547. // WHERE c.id != " . $excludeId . "
  548. // HAVING score > 0.7
  549. // ORDER BY score DESC
  550. // LIMIT 1";
  551. // $ftResult = $conn->query($ftQuery);
  552. //
  553. // if(!($ftResult && $ftResult->num_rows > 0 ))
  554. // {
  555. //
  556. // $ftQuery = "SELECT c.id, c.cs_code, c.cs_belong, c.cs_addtime,
  557. // MATCH( cc.alibaba_1, cc.alibaba_2, cc.alibaba_3,
  558. // cc.facebook_1, cc.facebook_2, cc.facebook_3,
  559. // cc.linkedin_1, cc.linkedin_2, cc.linkedin_3)
  560. // AGAINST('" . $conn->real_escape_string($searchStr) . "' IN NATURAL LANGUAGE MODE) AS score
  561. // FROM customer c
  562. // JOIN customer_contact cc ON c.id = cc.customer_id
  563. // WHERE c.id != " . $excludeId . "
  564. // HAVING score > 0.7
  565. // ORDER BY score DESC
  566. // LIMIT 1";
  567. // $ftResult = $conn->query($ftQuery);
  568. // }
  569. //
  570. //
  571. //
  572. // if ($ftResult && $ftResult->num_rows > 0) {
  573. // $row = $ftResult->fetch_assoc();
  574. // $matchCustomerId = $row['id'];
  575. // $matchCustomerCode = $row['cs_code'];
  576. // $matchOwner = $row['cs_belong'];
  577. // $matchAddTime = $row['cs_addtime'];
  578. // $matchDetails = "全文检索相似度" . number_format($row['score'] * 100, 1) . "%";
  579. // $matchScore = $row['score'];
  580. // $duplicateFound = true;
  581. // }
  582. // }
  583. // }
  584. // }
  585. //
  586. // // 如果找到重复客户,记录并提示
  587. // if ($duplicateFound) {
  588. // // 获取客户所有者姓名
  589. // $ownerResult = $conn->query("SELECT em_user FROM employee WHERE id = " . $matchOwner);
  590. // $ownerRow = $ownerResult->fetch_assoc();
  591. // $owner = textUncode($ownerRow['em_user']);
  592. //
  593. // // 确定谁先录入
  594. // if (strtotime($cs_addtime) > strtotime($matchAddTime)) {
  595. // $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  596. // $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  597. // $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  598. // textUncode($matchCustomerCode) . "\"存在重复,<br>匹配类型:" . $matchDetails .
  599. // "<br>客户由:" . $owner . $matchAddTime . "首次录入')";
  600. // } else {
  601. // $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  602. // $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  603. // $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  604. // textUncode($matchCustomerCode) . "\"存在重复,<br>匹配类型:" . $matchDetails .
  605. // "<br>客户由:" . $_SESSION['employee_name'] . $cs_addtime . "首次录入')";
  606. // }
  607. //
  608. // $conn->query($tstr);
  609. // echo "<script>alert('warning.2.录入信息\\n与" . $owner . "客户编号:" . textUncode($matchCustomerCode) .
  610. // "\\存在重复(" . $matchDetails . "),未能保存\\n请联系管理员核实!');history.back();</script>";
  611. // exit;
  612. // }
  613. // }
  614. }
  615. // Save or update customer data
  616. if ($act == "editSave" || $allowedit == 1) {
  617. $hrefstr = "/customers.php?Keys=" . $keys . "&fliterBusiness=" . $fliterBusiness .
  618. "&fliterDeal=" . $fliterDeal . "&Page=" . $page;
  619. // 更新客户基本信息
  620. $updateSql = "UPDATE customer SET
  621. cs_code='" . $conn->real_escape_string($cs_code) . "',
  622. cs_company='" . $conn->real_escape_string($cs_company) . "',
  623. cs_country=" . $cs_country . ",
  624. cs_from=" . $cs_from . ",
  625. cs_address='" . $conn->real_escape_string($cs_address) . "',
  626. cs_updatetime='" . $cs_updatetime . "',
  627. cs_belong=" . $cs_belong . ",
  628. cs_belongclient=" . $cs_belongClient . ",
  629. cs_state=" . $cs_state . ",
  630. cs_deal=" . $cs_deal . ",
  631. cs_note='" . $conn->real_escape_string($cs_note) . "'";
  632. // 处理cs_dealdate
  633. if ($cs_deal == 3) {
  634. $updateSql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
  635. }
  636. $updateSql .= " WHERE id=" . intval($id);
  637. $conn->query($updateSql);
  638. // 处理业务类型 - 先删除已有的业务类型
  639. $conn->query("DELETE FROM customer_business_type WHERE customer_id = " . intval($id));
  640. // 添加新的业务类型
  641. if (!empty($cs_type) && is_array($cs_type)) {
  642. foreach ($cs_type as $type_id) {
  643. $type_id = intval($type_id);
  644. if ($type_id > 0) {
  645. $conn->query("INSERT INTO customer_business_type (customer_id, business_type_id)
  646. VALUES (" . intval($id) . ", " . $type_id . ")");
  647. }
  648. }
  649. }
  650. // 处理联系人信息 - 首先删除已有的不在提交列表中的联系人
  651. $existingContactIds = [];
  652. foreach ($contacts as $contact) {
  653. if (!empty($contact['id'])) {
  654. $existingContactIds[] = (int)$contact['id'];
  655. }
  656. }
  657. if (!empty($existingContactIds)) {
  658. $idsToKeep = implode(',', $existingContactIds);
  659. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id) .
  660. " AND id NOT IN (" . $idsToKeep . ")";
  661. } else {
  662. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id);
  663. }
  664. $conn->query($deleteContactsSql);
  665. // 处理联系人信息 - 更新或添加联系人
  666. foreach ($contacts as $contact) {
  667. $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
  668. $contact_name = textEncode($contact['contact_name'] ?? '');
  669. // 准备SQL字段和值
  670. $fields = ['contact_name'];
  671. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  672. $updates = ["contact_name = '" . $conn->real_escape_string($contact_name) . "'"];
  673. // 处理所有联系方式类型
  674. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  675. foreach ($methodTypes as $type) {
  676. for ($i = 1; $i <= 3; $i++) {
  677. $field = $type . '_' . $i;
  678. $format_field = $field . '_format';
  679. $bu_field = $field . '_bu';
  680. $value = textEncode($contact[$field] ?? '');
  681. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  682. $bu_value = textEncode($contact[$bu_field] ?? $value);
  683. // 添加字段名
  684. $fields[] = $field;
  685. $fields[] = $bu_field;
  686. if ($type == 'tel' || $type == 'whatsapp') {
  687. $fields[] = $format_field;
  688. }
  689. // 添加值
  690. $values[] = "'" . $conn->real_escape_string($value) . "'";
  691. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  692. if ($type == 'tel' || $type == 'whatsapp') {
  693. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  694. }
  695. // 添加更新语句
  696. $updates[] = $field . " = '" . $conn->real_escape_string($value) . "'";
  697. $updates[] = $bu_field . " = '" . $conn->real_escape_string($bu_value) . "'";
  698. if ($type == 'tel' || $type == 'whatsapp') {
  699. $updates[] = $format_field . " = '" . $conn->real_escape_string($format_value) . "'";
  700. }
  701. }
  702. }
  703. if ($contact_id > 0) {
  704. // 更新已有联系人
  705. $updateContactSql = "UPDATE customer_contact SET " .
  706. implode(", ", $updates) . ", updated_at = NOW() " .
  707. "WHERE id = " . $contact_id . " AND customer_id = " . intval($id);
  708. $conn->query($updateContactSql);
  709. } else {
  710. // 添加新联系人
  711. $insertContactSql = "INSERT INTO customer_contact (" .
  712. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  713. implode(", ", $values) . ", " . intval($id) . ", NOW(), NOW())";
  714. $conn->query($insertContactSql);
  715. }
  716. }
  717. // Update tags
  718. $conn->query("DELETE FROM tagtable WHERE customerId = " . intval($id));
  719. foreach ($mytag as $tag) {
  720. if (!empty(trim($tag))) {
  721. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  722. $conn->real_escape_string($tag) . "', " .
  723. intval($_SESSION['employee_id']) . ", " .
  724. intval($id) . ")";
  725. $conn->query($tagSql);
  726. }
  727. }
  728. echo "<script>location.href='$hrefstr';</script>";
  729. } else {
  730. // Insert new customer record
  731. $insertSql = "INSERT INTO customer (
  732. cs_code, cs_company, cs_country, cs_from, cs_address,
  733. cs_addtime, cs_updatetime, cs_belong, cs_belongClient,
  734. cs_state, cs_deal, cs_note, cs_chain, is_silent, cs_dealdate
  735. ) VALUES (
  736. '" . $conn->real_escape_string($cs_code) . "',
  737. '" . $conn->real_escape_string($cs_company) . "',
  738. " . $cs_country . ",
  739. " . $cs_from . ",
  740. '" . $conn->real_escape_string($cs_address) . "',
  741. NOW(),
  742. NOW(),
  743. " . $cs_belong . ",
  744. " . $cs_belongClient . ",
  745. " . $cs_state . ",
  746. " . $cs_deal . ",
  747. '" . $conn->real_escape_string($cs_note) . "',
  748. " . $cs_belong . ",
  749. 0,
  750. " . ($cs_deal == 3 ? "NOW()" : "NULL") . "
  751. )";
  752. $conn->query($insertSql);
  753. $new_customer_id = $conn->insert_id;
  754. // Insert business types for new customer
  755. if ($new_customer_id > 0 && !empty($cs_type) && is_array($cs_type)) {
  756. foreach ($cs_type as $type_id) {
  757. $type_id = intval($type_id);
  758. if ($type_id > 0) {
  759. $conn->query("INSERT INTO customer_business_type (customer_id, business_type_id)
  760. VALUES (" . $new_customer_id . ", " . $type_id . ")");
  761. }
  762. }
  763. }
  764. // Insert contact information for all contacts
  765. if ($new_customer_id > 0) {
  766. foreach ($contacts as $contact) {
  767. $contact_name = textEncode($contact['contact_name'] ?? '');
  768. // 准备SQL字段和值
  769. $fields = ['contact_name'];
  770. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  771. // 处理所有联系方式类型
  772. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  773. foreach ($methodTypes as $type) {
  774. for ($i = 1; $i <= 3; $i++) {
  775. $field = $type . '_' . $i;
  776. $format_field = $field . '_format';
  777. $bu_field = $field . '_bu';
  778. $value = textEncode($contact[$field] ?? '');
  779. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  780. $bu_value = textEncode($contact[$bu_field] ?? $value);
  781. // 添加字段名
  782. $fields[] = $field;
  783. $fields[] = $bu_field;
  784. if ($type == 'tel' || $type == 'whatsapp') {
  785. $fields[] = $format_field;
  786. }
  787. // 添加值
  788. $values[] = "'" . $conn->real_escape_string($value) . "'";
  789. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  790. if ($type == 'tel' || $type == 'whatsapp') {
  791. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  792. }
  793. }
  794. }
  795. // 添加新联系人
  796. $insertContactSql = "INSERT INTO customer_contact (" .
  797. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  798. implode(", ", $values) . ", " . $new_customer_id . ", NOW(), NOW())";
  799. $conn->query($insertContactSql);
  800. }
  801. // Save tags for new customer
  802. foreach ($mytag as $tag) {
  803. if (!empty(trim($tag))) {
  804. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  805. $conn->real_escape_string($tag) . "', " .
  806. intval($_SESSION['employee_id']) . ", " .
  807. intval($new_customer_id) . ")";
  808. $conn->query($tagSql);
  809. }
  810. }
  811. }
  812. echo "<script>location.href='customerAdd.php';</script>";
  813. }
  814. ?>
  815. </body>
  816. </html>