customerSave.php 40 KB

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