From 0578c6c76f13e367b5dc7d0882efe3c69ca4cb0e Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期五, 15 五月 2026 16:29:30 +0800
Subject: [PATCH] feat(customer): 添加客户银行代码字段并实现批量删除功能

---
 src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java |  188 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 178 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
index 52d01ec..c429623 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -7,13 +7,12 @@
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.dto.CustomerContactDto;
 import com.ruoyi.basic.dto.CustomerDto;
 import com.ruoyi.basic.dto.CustomerFollowUpDto;
+import com.ruoyi.basic.mapper.CustomerContactMapper;
 import com.ruoyi.basic.mapper.CustomerMapper;
-import com.ruoyi.basic.pojo.Customer;
-import com.ruoyi.basic.pojo.CustomerFollowUp;
-import com.ruoyi.basic.pojo.CustomerFollowUpFile;
-import com.ruoyi.basic.pojo.CustomerUser;
+import com.ruoyi.basic.pojo.*;
 import com.ruoyi.basic.service.*;
 import com.ruoyi.basic.vo.CustomerVo;
 import com.ruoyi.common.utils.SecurityUtils;
@@ -60,6 +59,8 @@
     private CustomerReturnVisitService customerReturnVisitService;
     @Autowired
     private CustomerUserService customerUserService;
+    @Autowired
+    private CustomerContactMapper customerContactMapper;
 
     /**
      * 鏌ヨ瀹㈡埛妗f
@@ -105,6 +106,19 @@
             }).collect(Collectors.toList());
 
             customerVo.setFollowUpList(followUpDtoList);
+        }
+        //鏌ヨ鑱旂郴浜轰俊鎭�
+        List<CustomerContact> customerContactList = customerContactMapper.selectList(
+                new QueryWrapper<CustomerContact>().lambda()
+                        .apply("FIND_IN_SET({0}, customer_id)", id)
+        );
+        if (!CollectionUtils.isEmpty(customerContactList)) {
+            List<CustomerContactDto> contactDtoList = customerContactList.stream().map(contact -> {
+                CustomerContactDto contactDto = new CustomerContactDto();
+                BeanUtils.copyProperties(contact, contactDto);
+                return contactDto;
+            }).collect(Collectors.toList());
+            customerVo.setContactList(contactDtoList);
         }
 
         return customerVo;
@@ -185,11 +199,14 @@
      * @return 缁撴灉
      */
     @Override
-    public int insertCustomer(Customer customer) {
+    @Transactional(rollbackFor = Exception.class)
+    public int insertCustomer(CustomerDto customer) {
         LoginUser loginUser = SecurityUtils.getLoginUser();
         Long tenantId = loginUser.getTenantId();
         customer.setTenantId(tenantId);
-        return customerMapper.insert(customer);
+        customerMapper.insert(customer);
+        syncCustomerContacts(customer.getId(), customer.getContactList());
+        return 1;
     }
 
     /**
@@ -199,11 +216,118 @@
      * @return 缁撴灉
      */
     @Override
-    public int updateCustomer(Customer customer) {
+    @Transactional(rollbackFor = Exception.class)
+    public int updateCustomer(CustomerDto customer) {
         LoginUser loginUser = SecurityUtils.getLoginUser();
         Long tenantId = loginUser.getTenantId();
         customer.setTenantId(tenantId);
-        return customerMapper.updateById(customer);
+        int rows = customerMapper.updateById(customer);
+        syncCustomerContacts(customer.getId(), customer.getContactList());
+        return rows;
+    }
+
+    private void syncCustomerContacts(Long customerId, List<? extends CustomerContact> contactList) {
+        List<CustomerContact> allContacts = customerContactMapper.selectList(new QueryWrapper<>());
+        Map<String, CustomerContact> submittedContactMap = new LinkedHashMap<>();
+        if (!CollectionUtils.isEmpty(contactList)) {
+            for (CustomerContact contact : contactList) {
+                if (contact == null) {
+                    continue;
+                }
+                submittedContactMap.putIfAbsent(buildContactKey(contact), contact);
+            }
+        }
+
+        // 鍏堝悓姝ユ湰娆℃彁浜ょ殑鑱旂郴浜猴紝鎸夆�滃鍚� + 鎵嬫満鍙封�濊仛鍚堝鎴稩D銆�
+        for (CustomerContact contact : submittedContactMap.values()) {
+            contact.setCustomerId(buildContactCustomerIds(allContacts, contact, customerId));
+            if (contact.getId() == null) {
+                customerContactMapper.insert(contact);
+                allContacts.add(contact);
+            } else {
+                customerContactMapper.updateById(contact);
+                replaceLocalContact(allContacts, contact);
+            }
+        }
+
+        Set<String> submittedContactKeys = submittedContactMap.keySet();
+        // 鍐嶅鐞嗘湰娆″凡鍒犻櫎鐨勮仈绯讳汉锛屾妸褰撳墠瀹㈡埛浠庡巻鍙插叧鑱旈噷绉婚櫎銆�
+        for (CustomerContact existingContact : new ArrayList<>(allContacts)) {
+            if (!containsCustomerId(existingContact.getCustomerId(), customerId)) {
+                continue;
+            }
+            if (submittedContactKeys.contains(buildContactKey(existingContact))) {
+                continue;
+            }
+            String updatedCustomerIds = removeCustomerId(existingContact.getCustomerId(), customerId);
+            if (StringUtils.isEmpty(updatedCustomerIds)) {
+                // 娌℃湁鍓╀綑瀹㈡埛鍏宠仈鏃讹紝鐩存帴鍒犻櫎鑱旂郴浜鸿褰曘��
+                customerContactMapper.deleteById(existingContact.getId());
+            } else {
+                existingContact.setCustomerId(updatedCustomerIds);
+                customerContactMapper.updateById(existingContact);
+            }
+        }
+    }
+
+    // 鎸夎仈绯讳汉濮撳悕鍜屾墜鏈哄彿姹囨�诲巻鍙插鎴稩D锛屽苟杩藉姞褰撳墠瀹㈡埛ID銆�
+    private String buildContactCustomerIds(List<CustomerContact> customerContacts, CustomerContact contact, Long customerId) {
+        LinkedHashSet<String> customerIdSet = new LinkedHashSet<>();
+        if (!CollectionUtils.isEmpty(customerContacts)) {
+            customerContacts.stream()
+                    .filter(Objects::nonNull)
+                    .filter(item -> Objects.equals(item.getContactPerson(), contact.getContactPerson())
+                            && Objects.equals(item.getContactPhone(), contact.getContactPhone()))
+                    .map(CustomerContact::getCustomerId)
+                    .filter(StringUtils::isNotEmpty)
+                    .forEach(ids -> Arrays.stream(ids.split(","))
+                            .map(String::trim)
+                            .filter(StringUtils::isNotEmpty)
+                            .forEach(customerIdSet::add));
+        }
+        customerIdSet.add(String.valueOf(customerId));
+        return String.join(",", customerIdSet);
+    }
+
+    // 鍒ゆ柇鑱旂郴浜哄叧鑱斿垪琛ㄩ噷鏄惁鍖呭惈褰撳墠瀹㈡埛ID銆�
+    private boolean containsCustomerId(String customerIds, Long customerId) {
+        if (StringUtils.isEmpty(customerIds) || customerId == null) {
+            return false;
+        }
+        String currentCustomerId = String.valueOf(customerId);
+        return Arrays.stream(customerIds.split(","))
+                .map(String::trim)
+                .anyMatch(currentCustomerId::equals);
+    }
+
+    // 浠庤仈绯讳汉鍏宠仈鍒楄〃閲岀Щ闄ゅ綋鍓嶅鎴稩D锛屽苟淇濇寔閫楀彿鎷兼帴鏍煎紡銆�
+    private String removeCustomerId(String customerIds, Long customerId) {
+        if (StringUtils.isEmpty(customerIds) || customerId == null) {
+            return customerIds;
+        }
+        String currentCustomerId = String.valueOf(customerId);
+        return Arrays.stream(customerIds.split(","))
+                .map(String::trim)
+                .filter(StringUtils::isNotEmpty)
+                .filter(id -> !currentCustomerId.equals(id))
+                .distinct()
+                .collect(Collectors.joining(","));
+    }
+
+    // 鐢熸垚鑱旂郴浜轰笟鍔″敮涓�閿紝鐢ㄤ簬瀵规湰娆℃彁浜ゆ暟鎹幓閲嶆瘮瀵广��
+    private String buildContactKey(CustomerContact contact) {
+        return StringUtils.defaultString(contact.getContactPerson()) + "_" + StringUtils.defaultString(contact.getContactPhone());
+    }
+
+    // 鏇存柊鍐呭瓨涓殑鑱旂郴浜哄揩鐓э紝閬垮厤鍚庣画澧炲垹鍒ゆ柇浣跨敤鏃ф暟鎹��
+    private void replaceLocalContact(List<CustomerContact> customerContacts, CustomerContact targetContact) {
+        for (int i = 0; i < customerContacts.size(); i++) {
+            CustomerContact customerContact = customerContacts.get(i);
+            if (Objects.equals(customerContact.getId(), targetContact.getId())) {
+                customerContacts.set(i, targetContact);
+                return;
+            }
+        }
     }
 
     /**
@@ -240,9 +364,53 @@
                     .eq(CustomerUser::getCustomerId, id)
             );
         }
+        // 鍒犻櫎瀹㈡埛瀵瑰簲鐨勮仈绯讳汉鍏宠仈
+        removeCustomerContactsByCustomerIds(idList);
 
         // 鍒犻櫎瀹㈡埛涓昏〃鏁版嵁
         return customerMapper.deleteBatchIds(idList);
+    }
+
+    private void removeCustomerContactsByCustomerIds(List<Long> customerIds) {
+        if (CollectionUtils.isEmpty(customerIds)) {
+            return;
+        }
+        List<CustomerContact> customerContacts = customerContactMapper.selectList(new QueryWrapper<>());
+        if (CollectionUtils.isEmpty(customerContacts)) {
+            return;
+        }
+        Set<Long> customerIdSet = customerIds.stream()
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+        for (CustomerContact customerContact : customerContacts) {
+            String contactCustomerIds = customerContact.getCustomerId();
+            if (StringUtils.isEmpty(contactCustomerIds)) {
+                continue;
+            }
+            String updatedCustomerIds = Arrays.stream(contactCustomerIds.split(","))
+                    .map(String::trim)
+                    .filter(StringUtils::isNotEmpty)
+                    .filter(id -> {
+                        Long parsedId = parseCustomerId(id);
+                        return parsedId == null || !customerIdSet.contains(parsedId);
+                    })
+                    .distinct()
+                    .collect(Collectors.joining(","));
+            if (StringUtils.isEmpty(updatedCustomerIds)) {
+                customerContactMapper.deleteById(customerContact.getId());
+            } else if (!updatedCustomerIds.equals(contactCustomerIds)) {
+                customerContact.setCustomerId(updatedCustomerIds);
+                customerContactMapper.updateById(customerContact);
+            }
+        }
+    }
+
+    private Long parseCustomerId(String customerId) {
+        try {
+            return Long.valueOf(customerId);
+        } catch (NumberFormatException e) {
+            return null;
+        }
     }
 
     @Override
@@ -303,7 +471,7 @@
     @Override
     public void assignCustomer(CustomerDto customerDto) {
         Customer customer = customerMapper.selectById(customerDto.getId());
-        if (customer.getType() == 1 && customer.getIsAssigned() == 0) {  // 鍏捣涓斿彲鍒嗛厤
+        if (customer.getType() == 1 ) {  // 鍏捣涓斿彲鍒嗛厤
             customer.setIsAssigned(1);
             customer.setUsageStatus(1L);
             customer.setUsageUser(customerDto.getUsageUser());
@@ -375,7 +543,7 @@
         //灏嗗鎴风殑type鏀逛负1 涓旂洿鎺ュ垎閰嶇粰褰撳墠鐢ㄦ埛
         Customer customer = customerMapper.selectById(id);
         customer.setType(1);
-        customer.setIsAssigned(1);
+        customer.setIsAssigned(0);
         return this.updateById(customer);
     }
 

--
Gitblit v1.9.3