Selaa lähdekoodia

客户信息表增加归属组别筛选功能

cgp 4 päivää sitten
vanhempi
commit
4af702a852

+ 66 - 24
fs-company/src/main/java/com/fs/company/controller/qw/FsCompanyCustomerController.java

@@ -28,18 +28,23 @@ import com.fs.hisStore.service.IFsUserInformationCollectionService;
 import com.fs.hisStore.vo.FsStoreOrderVO;
 import com.fs.hisStore.vo.FsUserInformationCollectionOverviewVo;
 import com.fs.qw.domain.FsCompanyCustomer;
+import com.fs.qw.dto.ImportCustomerDTO;
 import com.fs.qw.service.IFsCompanyCustomerService;
 import com.fs.qw.vo.CompanyUserAndDoctorVO;
+import com.fs.qw.vo.ImportResult;
 import com.fs.system.service.ISysConfigService;
 import com.github.pagehelper.PageHelper;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -155,7 +160,68 @@ public class FsCompanyCustomerController extends BaseController {
             log.error("导出客户信息数据异常",e);
             throw new CustomException("客户信息数据异常");
         }
+    }
+
+    /**
+     * 导入客户数据(Excel)
+     * @param file 上传的 Excel 文件
+     * @return 导入结果
+     */
+    @PostMapping("/import")
+    public AjaxResult importCustomers(@RequestParam("file") MultipartFile file) {
+        try {
+            // 1. 解析 Excel
+            List<ImportCustomerDTO> list = parseExcel(file);
+            if (list.isEmpty()) {
+                return AjaxResult.error("未解析到有效数据,请检查文件格式(第3行起 B=姓名,C=手机,D=地址)");
+            }
+
+            // 2. 获取当前登录销售
+            LoginUser loginUser = SecurityUtils.getLoginUser();
+            CompanyUser companyUser = loginUser.getUser();
+
+            // 3. 执行导入
+            ImportResult result = fsCompanyCustomerService.importCustomers(list, companyUser);
+
+            // 4. 返回结果
+            String msg = String.format("成功导入 %d 条,失败 %d 条", result.getSuccess(), result.getErrors().size());
+            return AjaxResult.success(msg, result);
+        } catch (Exception e) {
+            log.error("导入客户数据异常", e);
+            return AjaxResult.error("导入失败:" + e.getMessage());
+        }
+    }
+
+    /**
+     * 解析 Excel 文件(从第3行开始读取 A、B、C 列)
+     */
+    private List<ImportCustomerDTO> parseExcel(MultipartFile file) throws Exception {
+        List<ImportCustomerDTO> list = new ArrayList<>();
+        Workbook workbook = WorkbookFactory.create(file.getInputStream());
+        Sheet sheet = workbook.getSheetAt(0);
+        DataFormatter formatter = new DataFormatter();
+        int startRow = 2; // 第3行(0-based)
+
+        for (int i = startRow; i <= sheet.getLastRowNum(); i++) {
+            Row row = sheet.getRow(i);
+            if (row == null) continue;
+
+            String name = formatter.formatCellValue(row.getCell(0));  // A列
+            String phone = formatter.formatCellValue(row.getCell(1)); // B列
+            String address = formatter.formatCellValue(row.getCell(2)); // C列
+
+            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(address)) {
+                continue; // 跳过空行
+            }
 
+            ImportCustomerDTO dto = new ImportCustomerDTO();
+            dto.setCustomerName(name);
+            dto.setPhone(phone);
+            dto.setAddress(address);
+            list.add(dto);
+        }
+        workbook.close();
+        return list;
     }
 
     /**
@@ -359,30 +425,6 @@ public class FsCompanyCustomerController extends BaseController {
         return toAjax( result);
     }
 
-    /**
-     * 修改商城处方表的制单状态
-     * */
-    @PostMapping("/updateScrmPrescriptionDocumentSuccess")
-    public AjaxResult updateScrmPrescriptionDocumentSuccess(@RequestBody FsCompanyCustomer fsCompanyCustomer){
-        LoginUser loginUser = SecurityUtils.getLoginUser();
-        Long companyUserId = loginUser.getUser().getUserId();
-        if (companyUserId == null){
-            throw new CustomException("登录信息已过期,请重新登录");
-        }
-        FsCompanyCustomer companyCustomer = fsCompanyCustomerService.selectFsCompanyCustomerById(fsCompanyCustomer.getId());
-        if (companyCustomer == null){
-            return AjaxResult.error("未找到客户信息");
-        }
-        //重置客户信息表的流程状态为"待完善"
-        companyCustomer.setProcessStatus(0);
-        fsCompanyCustomerService.updateFsCompanyCustomer(companyCustomer);
-        FsPrescribeDataScrm fsPrescribeDataScrm=new FsPrescribeDataScrm();
-        fsPrescribeDataScrm.setPrescribeId(companyCustomer.getPrescribeId());
-        fsPrescribeDataScrm.setIsDocument(1);//1-销售已制单
-        int result=prescribeDataScrmService.updateFsPrescribeDataScrm(fsPrescribeDataScrm);
-        return toAjax( result);
-    }
-
     /**
      * 生成客户信息表制单二维码
      * */

+ 1 - 1
fs-service/src/main/java/com/fs/his/mapper/FsStoreOrderMapper.java

@@ -374,7 +374,7 @@ public interface FsStoreOrderMapper
             "LEFT JOIN company c on c.company_id =so.company_id " +
             "LEFT JOIN company_user cu on cu.user_id=so.company_user_id " +
             "LEFT JOIN fs_doctor fd on so.follow_doctor_id =fd.doctor_id " +
-            "LEFT JOIN fs_patient pat ON pat.patient_id=p.patient_id  WHERE so.is_del=0  and order_id= #{orderId}")
+            "LEFT JOIN fs_patient pat ON pat.patient_id=p.patient_id  WHERE so.is_del=0  and so.order_id= #{orderId}")
     FsStoreOrderVO selectFsStoreOrderByOrderIdVO(@Param("orderId") Long orderId);
 
     @Update("update fs_store_order set status=-3 where order_id=#{orderId}")

+ 10 - 0
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreOrderScrmServiceImpl.java

@@ -1674,6 +1674,16 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
             if (prescribeDataScrm.getStatus()!=1||prescribeDataScrm.getDoctorConfirm()!=1){
                 return R.error("关联处方还在开方中,请稍后刷新页面重试");
             }
+            //创建订单后,重置客户信息表的流程状态为"待完善"
+            FsCompanyCustomer updateCondition = new FsCompanyCustomer();
+            updateCondition.setId(fsCompanyCustomer.getId());
+            updateCondition.setProcessStatus(0);
+            fsCompanyCustomerMapper.updateFsCompanyCustomer(updateCondition);
+            //创建订单后,修改处方表的流程状态为"待完善"
+            FsPrescribeDataScrm fsPrescribeDataScrm=new FsPrescribeDataScrm();
+            fsPrescribeDataScrm.setPrescribeId(fsCompanyCustomer.getPrescribeId());
+            fsPrescribeDataScrm.setIsDocument(1);//更新处方表的制单状态,1-销售已制单
+            prescribeDataScrmService.updateFsPrescribeDataScrm(fsPrescribeDataScrm);
             param.setUserId(1L);
             //保存收货地址
             FsUserAddressScrm address = new FsUserAddressScrm();

+ 22 - 0
fs-service/src/main/java/com/fs/qw/dto/ImportCustomerDTO.java

@@ -0,0 +1,22 @@
+package com.fs.qw.dto;
+
+import lombok.Data;
+
+@Data
+/**
+ * 客户信息表导入DTO类
+ * */
+public class ImportCustomerDTO {
+    /**
+     * 客户姓名
+     * */
+    private String customerName;
+    /**
+     * 手机号
+     * */
+    private String phone;
+    /**
+     * 地址
+     * */
+    private String address;
+}

+ 5 - 0
fs-service/src/main/java/com/fs/qw/mapper/FsCompanyCustomerMapper.java

@@ -86,4 +86,9 @@ public interface FsCompanyCustomerMapper {
      * 根据关联处方id查询客户信息
      * */
     FsCompanyCustomer selectFsCompanyCustomerByPrescribeId(Long prescribeId);
+
+    /**
+     * 根据手机号查询客户信息表主键
+     * */
+    Long selectCompanyCustomerIdByPhone(String phone);
 }

+ 10 - 0
fs-service/src/main/java/com/fs/qw/service/IFsCompanyCustomerService.java

@@ -5,7 +5,9 @@ import com.fs.his.vo.CustomerInfoVO;
 import com.fs.hisStore.param.FsCompanyCustomerOrderParam;
 import com.fs.hisStore.vo.FsStoreOrderVO;
 import com.fs.qw.domain.FsCompanyCustomer;
+import com.fs.qw.dto.ImportCustomerDTO;
 import com.fs.qw.param.TransferCustomerParam;
+import com.fs.qw.vo.ImportResult;
 
 import java.util.List;
 
@@ -47,4 +49,12 @@ public interface IFsCompanyCustomerService {
 
     //查询客户信息+问答信息(根据客户信息表主键ID)
     CustomerInfoVO getCustomerInfoAndQuestionAnswer(Long companyCustomerId);
+
+    /**
+     * 批量导入客户数据
+     * @param list 导入数据列表
+     * @param companyUser 当前登录销售
+     * @return 导入结果(成功条数 + 错误信息)
+     */
+    ImportResult importCustomers(List<ImportCustomerDTO> list, CompanyUser companyUser);
 }

+ 58 - 0
fs-service/src/main/java/com/fs/qw/service/impl/FsCompanyCustomerServiceImpl.java

@@ -26,15 +26,18 @@ import com.fs.hisStore.vo.FsStoreOrderItemVO;
 import com.fs.hisStore.vo.FsStoreOrderVO;
 import com.fs.qw.domain.FsCompanyCustomer;
 import com.fs.qw.domain.FsCompanyCustomerLog;
+import com.fs.qw.dto.ImportCustomerDTO;
 import com.fs.qw.mapper.FsCompanyCustomerLogMapper;
 import com.fs.qw.mapper.FsCompanyCustomerMapper;
 import com.fs.qw.mapper.FsCompanyExternalPayReceiptMapper;
 import com.fs.qw.param.TransferCustomerParam;
 import com.fs.qw.service.IFsCompanyCustomerService;
+import com.fs.qw.vo.ImportResult;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -84,6 +87,15 @@ public class FsCompanyCustomerServiceImpl implements IFsCompanyCustomerService {
     private static final Set<Integer> ALLOWED_STATUSES =
             Collections.unmodifiableSet(new HashSet<>(Arrays.asList(0, 5, 6))); // 允许操作完善客户信息的状态
 
+
+
+    @Autowired
+    private ApplicationContext applicationContext;
+
+    private IFsCompanyCustomerService self() {
+        return applicationContext.getBean(IFsCompanyCustomerService.class);
+    }
+
     @Override
     public FsCompanyCustomer selectFsCompanyCustomerById(Long id) {
         return fsCompanyCustomerMapper.selectFsCompanyCustomerById(id);
@@ -396,4 +408,50 @@ public class FsCompanyCustomerServiceImpl implements IFsCompanyCustomerService {
         customerInfoVO.setCustomerQuestionAnswerVOList(result);
         return customerInfoVO;
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public ImportResult importCustomers(List<ImportCustomerDTO> list, CompanyUser companyUser) {
+        int success = 0;
+        List<String> errors = new ArrayList<>();
+
+        for (ImportCustomerDTO dto : list) {
+            try {
+                // 1. 基础校验
+                if (StringUtils.isEmpty(dto.getCustomerName()) ||
+                        StringUtils.isEmpty(dto.getPhone()) ||
+                        StringUtils.isEmpty(dto.getAddress())) {
+                    errors.add("手机号 " + dto.getPhone() + " 数据不完整,已跳过");
+                    continue;
+                }
+                if (!dto.getPhone().matches("\\d{11}")) {
+                    errors.add("手机号 " + dto.getPhone() + " 格式不正确(需11位数字),已跳过");
+                    continue;
+                }
+
+                // 2. 查询客户是否存在
+                Long existingId = fsCompanyCustomerMapper.selectCompanyCustomerIdByPhone(dto.getPhone());
+
+                if (existingId != null) {
+                    // 存在 → 购买次数 +1
+                    self().addBuyTimes(existingId);
+                } else {
+                    // 不存在 → 新增客户
+                    FsCompanyCustomer customer = new FsCompanyCustomer();
+                    customer.setCustomerName(dto.getCustomerName());
+                    customer.setPhone(dto.getPhone());
+                    customer.setAddress(dto.getAddress());
+                    customer.setBuyCount(1L);               // 首次购买次数 = 1
+                    customer.setCreateTime(new Date());     // 创建时间
+                    customer.setClaimStatus(0);  //未认领状态
+                    fsCompanyCustomerMapper.insertFsCompanyCustomer(customer);
+                }
+                success++;
+            } catch (Exception e) {
+                errors.add("手机号 " + dto.getPhone() + " 处理失败:" + e.getMessage());
+                log.error("导入客户失败,手机号:{}", dto.getPhone(), e);
+            }
+        }
+        return new ImportResult(success, errors);
+    }
 }

+ 12 - 0
fs-service/src/main/java/com/fs/qw/vo/ImportResult.java

@@ -0,0 +1,12 @@
+package com.fs.qw.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+public class ImportResult {
+    private int success;
+    private List<String> errors;
+}

+ 6 - 0
fs-service/src/main/resources/mapper/qw/FsCompanyCustomerMapper.xml

@@ -92,6 +92,9 @@
         <if test="maxBuyCount != null">
             and buy_count &lt;= #{maxBuyCount}
         </if>
+        <if test="deptId != null">
+            and dept_id = #{deptId}
+        </if>
         <if test="importMemberId != null">
             and import_member_id = #{importMemberId}
         </if>
@@ -205,6 +208,9 @@
         <include refid="selectFsCompanyCustomerVo"/>
         where del_flag = '0' and prescribe_id = #{prescribeId}
     </select>
+    <select id="selectCompanyCustomerIdByPhone" resultType="Long">
+        SELECT id FROM fs_company_customer WHERE phone = #{phone} and del_flag = '0' LIMIT 1
+    </select>
 
     <insert id="insertFsCompanyCustomer" parameterType="com.fs.qw.domain.FsCompanyCustomer" useGeneratedKeys="true" keyProperty="id">
         insert into fs_company_customer