|
|
@@ -0,0 +1,147 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.dto.ContactImportResultVO;
|
|
|
+import com.etotem.cfc.entity.Contact;
|
|
|
+import com.etotem.cfc.entity.ContactGroup;
|
|
|
+import com.etotem.cfc.mapper.ContactGroupMapper;
|
|
|
+import com.etotem.cfc.mapper.ContactMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 联系人批量导入业务(SC-P-62「建分组 → 导入」,能力键 {@code contact.group.import})。
|
|
|
+ *
|
|
|
+ * <p>CSV 文本逐行解析:格式错误行(缺姓名/手机号非法/重复号码)跳过并报告,不整批失败;
|
|
|
+ * 导入联系人按 groupId 归组,归属当前登录 userId(横向越权:groupId 非本人分组被拒)。
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ContactImportService {
|
|
|
+
|
|
|
+ /** 手机号合法正则:1 开头的 11 位数字 */
|
|
|
+ private static final String PHONE_PATTERN = "^1\\d{10}$";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContactMapper contactMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContactGroupMapper contactGroupMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 建分组(分组名称在参数 {@code name})。
|
|
|
+ *
|
|
|
+ * @param userId 当前登录用户
|
|
|
+ */
|
|
|
+ public Result<ContactGroup> createGroup(Long userId, Map<String, Object> params) {
|
|
|
+ Object nameObj = params == null ? null : params.get("name");
|
|
|
+ String name = nameObj == null ? null : nameObj.toString().trim();
|
|
|
+ if (name == null || name.isEmpty()) {
|
|
|
+ return Result.badRequest("分组名称不能为空");
|
|
|
+ }
|
|
|
+ ContactGroup group = new ContactGroup();
|
|
|
+ group.setUserId(userId);
|
|
|
+ group.setName(name);
|
|
|
+ group.setCreatedAt(new Date());
|
|
|
+ group.setUpdatedAt(new Date());
|
|
|
+ contactGroupMapper.insert(group);
|
|
|
+ return Result.success(group);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 分组列表(按 userId 过滤 + 降序排列) */
|
|
|
+ public Result<List<ContactGroup>> listGroups(Long userId) {
|
|
|
+ LambdaQueryWrapper<ContactGroup> query = new LambdaQueryWrapper<ContactGroup>()
|
|
|
+ .eq(ContactGroup::getUserId, userId)
|
|
|
+ .orderByDesc(ContactGroup::getId);
|
|
|
+ return Result.success(contactGroupMapper.selectList(query));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量导入(按 groupId 归组;格式错误行跳过并报告)。
|
|
|
+ *
|
|
|
+ * <p>参数承载归属(权限流):{@code csvContent} 为导入源(group 引用),{@code userId}
|
|
|
+ * 为登录用户;{@code groupId} 非空时校验其为当前用户分组,否则拒绝跨用户写入。</p>
|
|
|
+ *
|
|
|
+ * @param csvContent CSV 文本(每行「姓名,手机号」,首行可含表头跳过)
|
|
|
+ * @param groupId 目标分组 ID(可为 null,不归组则归「未分组」)
|
|
|
+ * @param userId 当前登录用户
|
|
|
+ */
|
|
|
+ public Result<ContactImportResultVO> importBatch(String csvContent, Long groupId, Long userId) {
|
|
|
+ if (csvContent == null || csvContent.trim().isEmpty()) {
|
|
|
+ return Result.badRequest("导入内容为空");
|
|
|
+ }
|
|
|
+ if (groupId != null) {
|
|
|
+ ContactGroup group = contactGroupMapper.selectById(groupId);
|
|
|
+ if (group == null || !group.getUserId().equals(userId)) {
|
|
|
+ return Result.forbidden("分组不存在或无权访问");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ContactImportResultVO result = new ContactImportResultVO();
|
|
|
+ List<Map<String, Object>> skippedRows = new ArrayList<>();
|
|
|
+ int success = 0;
|
|
|
+ int skipped = 0;
|
|
|
+
|
|
|
+ String[] lines = csvContent.replace("\r\n", "\n").split("\n");
|
|
|
+ for (int i = 0; i < lines.length; i++) {
|
|
|
+ String line = lines[i].trim();
|
|
|
+ if (line.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 跳过首行表头
|
|
|
+ if (i == 0 && line.contains("姓名") && line.contains("手机")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String[] cols = line.split("[,\\uFF0C]"); // 支持英文/中文逗号
|
|
|
+ String name = cols.length > 0 ? cols[0].trim() : "";
|
|
|
+ String phone = cols.length > 1 ? cols[1].trim() : "";
|
|
|
+ String reason = null;
|
|
|
+ if (name.isEmpty()) {
|
|
|
+ reason = "缺少姓名";
|
|
|
+ } else if (!phone.matches(PHONE_PATTERN)) {
|
|
|
+ reason = "手机号格式非法";
|
|
|
+ } else if (isDuplicate(phone, userId)) {
|
|
|
+ reason = "重复号码";
|
|
|
+ }
|
|
|
+ if (reason != null) {
|
|
|
+ skipped++;
|
|
|
+ Map<String, Object> row = new HashMap<>();
|
|
|
+ row.put("line", i + 1);
|
|
|
+ row.put("reason", reason);
|
|
|
+ row.put("raw", line);
|
|
|
+ skippedRows.add(row);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Contact contact = new Contact();
|
|
|
+ contact.setUserId(userId);
|
|
|
+ contact.setName(name);
|
|
|
+ contact.setPhone(phone);
|
|
|
+ contact.setGroupId(groupId);
|
|
|
+ contact.setContactSource("phone");
|
|
|
+ contact.setIntimacyLevel(30);
|
|
|
+ contact.setContactCount(0);
|
|
|
+ contact.setCreatedAt(new Date());
|
|
|
+ contact.setUpdatedAt(new Date());
|
|
|
+ contactMapper.insert(contact);
|
|
|
+ success++;
|
|
|
+ }
|
|
|
+ result.setSuccessCount(success);
|
|
|
+ result.setSkippedCount(skipped);
|
|
|
+ result.setSkippedRows(skippedRows);
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 重复号码检测(同 userId 下手机号已存在) */
|
|
|
+ private boolean isDuplicate(String phone, Long userId) {
|
|
|
+ LambdaQueryWrapper<Contact> query = new LambdaQueryWrapper<Contact>()
|
|
|
+ .eq(Contact::getUserId, userId)
|
|
|
+ .eq(Contact::getPhone, phone);
|
|
|
+ Long count = contactMapper.selectCount(query);
|
|
|
+ return count != null && count > 0;
|
|
|
+ }
|
|
|
+}
|