package com.storlead.sales.service.impl.systemrule;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.storlead.framework.common.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.pojo.entity.customer.*;
import com.storlead.sales.pojo.vo.customer.CustomerAllVO;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.pojo.dto.systemrule.SystemRuleModuleConfigDTO;
import com.storlead.sales.pojo.entity.systemrule.SystemRuleModuleConfigEntity;
import com.storlead.sales.pojo.vo.systemrule.CustomerTypeRulesVO;
import com.storlead.sales.pojo.vo.systemrule.FieldSimilarityConfigVO;
import com.storlead.sales.pojo.vo.systemrule.SystemRuleModuleConfigDetileVO;
import com.storlead.sales.pojo.vo.systemrule.SystemRuleModuleConfigVO;
import com.storlead.sales.service.customer.*;
import com.storlead.sales.mapper.systemrule.SystemRuleModuleConfigMapper;
import com.storlead.sales.service.systemrule.SystemRuleModuleConfigService;

import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.service.IUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.time.LocalDate.now;

/**
 * <p>
 * 规则配置模块表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-04-03
 */
@Service
@Log4j2
public class SystemRuleModuleConfigServiceImpl extends MyBaseServiceImpl<SystemRuleModuleConfigMapper, SystemRuleModuleConfigEntity> implements SystemRuleModuleConfigService {

    @Resource
    private CustomerService customerService;
    @Resource
    private LiaisonService liaisonService;
    @Resource
    private CustomerOwnerRecordService customerOwnerService;
    @Resource
    private CustomerCompanyService customerCompanyService;
    @Resource
    private StorleadCalendarService storleadCalendarService;
    @Resource
    private MessageAssembleService assembleService;
    @Resource
    private IUserService userService;
    @Override
    public IPage<SystemRuleModuleConfigVO> listByKeywords(SystemRuleModuleConfigDTO systemRuleModuleConfigDTO) {
        Page<SystemRuleModuleConfigEntity> page = new Page(systemRuleModuleConfigDTO.getPageIndex(),systemRuleModuleConfigDTO.getPageSize());
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(systemRuleModuleConfigDTO.getBlurry())) {
            wrapper.and(w ->w.like(SystemRuleModuleConfigEntity::getUpdateExplain,systemRuleModuleConfigDTO.getBlurry()));
        }
        IPage<SystemRuleModuleConfigEntity> pageList = baseMapper.selectPage(page, wrapper);

        IPage<SystemRuleModuleConfigVO> pageVO = new Page<>();
        pageVO = SysBaseField.page(pageList, SystemRuleModuleConfigVO.class);
        SysBaseField.dictFileds(pageVO.getRecords());
        SysBaseField.orgFileds(pageVO.getRecords());
        return pageVO;
    }

    @Override
    public Result<?> getDetailById(Long id,String resourceType) {
        if (ObjectUtils.isEmpty(id) && ObjectUtils.isEmpty(resourceType)){
            return Result.error("参数错误");
        }
        SystemRuleModuleConfigEntity sysRuleModuleConfigEntity = new SystemRuleModuleConfigEntity();
        if (!ObjectUtils.isEmpty(id)){
            sysRuleModuleConfigEntity = getById(id);
        }
        if (!ObjectUtils.isEmpty(resourceType)){
            LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper();
            wrapper.eq(SystemRuleModuleConfigEntity::getResourceType,resourceType);
            wrapper.eq(SystemRuleModuleConfigEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
            wrapper.last("limit 1");
            sysRuleModuleConfigEntity = getOne(wrapper);
        }

        SystemRuleModuleConfigDetileVO systemRuleModuleConfigDetileVO = new SystemRuleModuleConfigDetileVO();
        BeanUtils.copyProperties(sysRuleModuleConfigEntity, systemRuleModuleConfigDetileVO);
        List<FieldSimilarityConfigVO> vos;
        if (StrUtil.isNotBlank(sysRuleModuleConfigEntity.getPlagiarismRules())) {
            vos = JSON.parseArray(sysRuleModuleConfigEntity.getPlagiarismRules(),FieldSimilarityConfigVO.class);
            systemRuleModuleConfigDetileVO.setFieldSimilarityls(vos);
        }
        List<CustomerTypeRulesVO> customerTypeRulesVOS;
        if (!ObjectUtils.isEmpty(sysRuleModuleConfigEntity.getCustomerTypeRules())){
            customerTypeRulesVOS = JSON.parseArray(sysRuleModuleConfigEntity.getCustomerTypeRules(),CustomerTypeRulesVO.class);
            systemRuleModuleConfigDetileVO.setCustomerTypeRules(customerTypeRulesVOS);
        }
        systemRuleModuleConfigDetileVO.dictFiled();
        systemRuleModuleConfigDetileVO.orgFiled();
        return Result.result(systemRuleModuleConfigDetileVO);
    }

    @Override
    public void useCustomerRules() {
        //查询冻结用户
        List<Long> freeUserIdList = new ArrayList<>();
        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
                .eq(UserEntity::getIsFreeze, 1));
        if (!CollectionUtils.isEmpty(userList)){
            freeUserIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }

        // 准备查询条件
//        wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);

        //查询客户管理规则
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CUSTOMER.code);
        wrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        SystemRuleModuleConfigEntity systemRuleModuleConfigEntity = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(systemRuleModuleConfigEntity)){
            return;
        }
        //是否开启客户类型区分
        if (systemRuleModuleConfigEntity.getHasDistinguishCustomerType() || systemRuleModuleConfigEntity.getHasDistinguishKeyRegular() || systemRuleModuleConfigEntity.getHasPublicClues()) {

            LambdaQueryWrapper<CustomerEntity> wrapperA = new LambdaQueryWrapper();
            wrapperA.eq(CustomerEntity::getHasPublicCustomer,false);
            wrapperA.eq(CustomerEntity::getHasOrderForm,0);
            wrapperA.eq(CustomerEntity::getCustomerForm,10);
            wrapperA.apply("DATE(public_date) <= DATE({0})", now());
            wrapperA.eq(CustomerEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
            wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListA = customerService.list(wrapperA);
            if (!CollectionUtils.isEmpty(customerListA)) {
                List<Long> customerIdListA = customerListA.stream().map(CustomerEntity::getId).collect(Collectors.toList());

                LambdaUpdateWrapper<CustomerEntity> wrapperUA = new LambdaUpdateWrapper<>();
                wrapperUA.in(CustomerEntity::getId, customerIdListA);
                wrapperUA.set(CustomerEntity::getHasPublicCustomer, true);
                wrapperUA.setSql("former_owner_by = owner_by");
                wrapperUA.set(CustomerEntity::getOwnerBy, null);
                wrapperUA.set(CustomerEntity::getReasonPublicCustomerDictValue, "3");
                wrapperUA.set(CustomerEntity::getGiveUpTime, LocalDateTime.now());
                customerService.update(wrapperUA);

                updateCustomerStatus(customerIdListA);

                LambdaUpdateWrapper<CustomerCompanyEntity> customerCompanyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                customerCompanyLambdaUpdateWrapper.in(CustomerCompanyEntity::getCustomerId,customerIdListA);
                customerCompanyLambdaUpdateWrapper.set(CustomerCompanyEntity::getOwnerBy, null);
                customerCompanyService.update(customerCompanyLambdaUpdateWrapper);

                LambdaUpdateWrapper<LiaisonEntity> liaisonEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                liaisonEntityLambdaUpdateWrapper.in(LiaisonEntity::getCustomerId, customerIdListA);
                liaisonEntityLambdaUpdateWrapper.set(LiaisonEntity::getOwnerBy, null);
                liaisonService.update(liaisonEntityLambdaUpdateWrapper);
            }
        }

        //公海客户变更垃圾客户设置
        if (systemRuleModuleConfigEntity.getHasGarbageClues()){
            LambdaUpdateWrapper<CustomerEntity> wrapperB = new LambdaUpdateWrapper();
            wrapperB.eq(CustomerEntity::getHasPublicCustomer,true);
            wrapperB.eq(CustomerEntity::getHasOrderForm,0);
            wrapperB.eq(CustomerEntity::getCustomerForm,10);
            wrapperB.apply("DATE(garbage_date) <= DATE({0})", now());
            wrapperB.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperB.eq(CustomerEntity::getHasGarbageCustomer, false);
            wrapperB.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            wrapperB.set(CustomerEntity::getHasGarbageCustomer,true);
            customerService.update(wrapperB);
        }
        //垃圾客户清空设置
        if (systemRuleModuleConfigEntity.getHasGarbageEmptyClues()){
            //查询出来需要清空的客户
            LambdaQueryWrapper<CustomerEntity> wrapperC = new LambdaQueryWrapper();
            wrapperC.eq(CustomerEntity::getHasGarbageCustomer,true);
            wrapperC.eq(CustomerEntity::getHasOrderForm,0);
            wrapperC.eq(CustomerEntity::getCustomerForm,10);
//            wrapperC.lt(CustomerEntity::getLastFollowUpTime,begainTime);
            wrapperC.apply("DATE(delete_date) <= DATE({0})", now());
            wrapperC.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperC.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerEntityList = customerService.list(wrapperC);
            List<Long> customerIdList = customerEntityList.stream().map(CustomerEntity::getId).collect(Collectors.toList());
            customerService.glAllListDelete(customerIdList);
        }

    }


    @Override
    public void useClueRules() {
        //查询冻结用户
        List<Long> freeUserIdList = new ArrayList<>();
        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
                .eq(UserEntity::getIsFreeze, 1));
        if (!CollectionUtils.isEmpty(userList)){
            freeUserIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }

        // 准备查询条件
//        wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);

        //查询客户管理规则
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CLUS.code);
        wrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        SystemRuleModuleConfigEntity systemRuleModuleConfigEntity = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(systemRuleModuleConfigEntity)){
            return;
        }
        //执行规则

        if (systemRuleModuleConfigEntity.getHasPublicClues()){
            //开启了线索置公

            LambdaQueryWrapper<CustomerEntity> wrapperA = new LambdaQueryWrapper();
            wrapperA.eq(CustomerEntity::getHasPublicCustomer,false);
            wrapperA.eq(CustomerEntity::getHasOrderForm,0);
            wrapperA.eq(CustomerEntity::getCustomerForm,11);
            wrapperA.apply("DATE(public_date) <= DATE({0})", now());
            wrapperA.eq(CustomerEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
            wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListA = customerService.list(wrapperA);
//            List<Long> customerIdListA = customerListA.stream().map(CustomerEntity::getId).collect(Collectors.toList());

            //置公时间
            //获取间隔maxPublicCluesNum的时间
            if (!CollectionUtils.isEmpty(customerListA)){
                List<Long> customerIdListA = customerListA.stream().map(CustomerEntity::getId).collect(Collectors.toList());
                LambdaUpdateWrapper<CustomerEntity> wrapperUA = new LambdaUpdateWrapper();
                wrapperUA.in(CustomerEntity::getId, customerIdListA);
                wrapperUA.set(CustomerEntity::getHasPublicCustomer,true);
                wrapperUA.setSql("former_owner_by =owner_by");
                wrapperUA.set(CustomerEntity::getOwnerBy,null);
                wrapperUA.set(CustomerEntity::getReasonPublicCustomerDictValue, "3");
                wrapperUA.set(CustomerEntity::getGiveUpTime,LocalDateTime.now());
                customerService.update(wrapperUA);

                updateCustomerStatus(customerIdListA);

                LambdaUpdateWrapper<CustomerCompanyEntity> customerCompanyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                customerCompanyLambdaUpdateWrapper.in(CustomerCompanyEntity::getCustomerId,customerIdListA);
                customerCompanyLambdaUpdateWrapper.set(CustomerCompanyEntity::getOwnerBy, null);
                customerCompanyService.update(customerCompanyLambdaUpdateWrapper);

                LambdaUpdateWrapper<LiaisonEntity> liaisonEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                liaisonEntityLambdaUpdateWrapper.in(LiaisonEntity::getCustomerId, customerIdListA);
                liaisonEntityLambdaUpdateWrapper.set(LiaisonEntity::getOwnerBy, null);
                liaisonService.update(liaisonEntityLambdaUpdateWrapper);
            }

        }

        //公共线索清空设置
        if (systemRuleModuleConfigEntity.getHasClearClues()){
            LambdaQueryWrapper<CustomerEntity> wrapperB = new LambdaQueryWrapper();
            wrapperB.eq(CustomerEntity::getHasPublicCustomer,true);
            wrapperB.eq(CustomerEntity::getHasOrderForm,0);
            wrapperB.eq(CustomerEntity::getCustomerForm,11);
//            wrapperB.lt(CustomerEntity::getLastFollowUpTime,begainTime);
            wrapperB.apply("DATE(delete_date) <= DATE({0})", now());
            wrapperB.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperB.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerList = customerService.list(wrapperB);
            List<Long> customerIdList = customerList.stream().map(CustomerEntity::getId).collect(Collectors.toList());
            customerService.glAllListDelete(customerIdList);
        }

    }

    public void customerPublic() {

        //查询冻结用户
        List<Long> freeUserIdList = new ArrayList<>();
        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
                .eq(UserEntity::getIsFreeze, 1));
        if (!CollectionUtils.isEmpty(userList)){
            freeUserIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }

        // 准备查询条件
//        wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);


        //查询客户管理规则
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CUSTOMER.code);
        wrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        SystemRuleModuleConfigEntity systemRuleModuleConfigEntity = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(systemRuleModuleConfigEntity)){
            return;
        }
        //是否开启客户类型区分
        if (systemRuleModuleConfigEntity.getHasDistinguishCustomerType() || systemRuleModuleConfigEntity.getHasDistinguishKeyRegular() || systemRuleModuleConfigEntity.getHasPublicClues()) {

            LambdaQueryWrapper<CustomerEntity> wrapperA = new LambdaQueryWrapper();
            wrapperA.eq(CustomerEntity::getHasPublicCustomer,false);
            wrapperA.eq(CustomerEntity::getHasOrderForm,0);
            wrapperA.eq(CustomerEntity::getCustomerForm,10);
            wrapperA.apply("DATE(public_remind_date) = DATE({0})", now());
            wrapperA.eq(CustomerEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
            wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListA = customerService.list(wrapperA);
            if (!CollectionUtils.isEmpty(customerListA)) {
                sendNotifications(customerListA, "PUBLIC_WARNING");
            }
        }

        //公海客户变更垃圾客户设置
        if (systemRuleModuleConfigEntity.getHasGarbageClues()){
            LambdaUpdateWrapper<CustomerEntity> wrapperB = new LambdaUpdateWrapper();
            wrapperB.eq(CustomerEntity::getHasPublicCustomer,true);
            wrapperB.eq(CustomerEntity::getHasOrderForm,0);
            wrapperB.eq(CustomerEntity::getCustomerForm,10);
            wrapperB.apply("DATE(garbage_remind_date) = DATE({0})", now());
            wrapperB.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperB.eq(CustomerEntity::getHasGarbageCustomer, false);
            wrapperB.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListB = customerService.list(wrapperB);
            sendNotifications(customerListB, "GARBAGE_CUSTOMER_WARNING",Set.of(1L));
//            wrapperB.set(CustomerEntity::getHasGarbageCustomer,true);
//            customerService.update(wrapperB);
        }
        //垃圾客户清空设置
        if (systemRuleModuleConfigEntity.getHasGarbageEmptyClues()){
            //查询出来需要清空的客户
            LambdaQueryWrapper<CustomerEntity> wrapperC = new LambdaQueryWrapper();
            wrapperC.eq(CustomerEntity::getHasGarbageCustomer,true);
            wrapperC.eq(CustomerEntity::getHasOrderForm,0);
            wrapperC.eq(CustomerEntity::getCustomerForm,10);
            wrapperC.eq(CustomerEntity::getHasPublicCustomer,true);
//            wrapperC.lt(CustomerEntity::getLastFollowUpTime,begainTime);
            wrapperC.apply("DATE(delete_remind_date) = DATE({0})", now());
            wrapperC.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperC.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListC = customerService.list(wrapperC);
            sendNotifications(customerListC, "CLEAR_WARNING",Set.of(1L));
        }

    }

    public void cluePublic() {

        //查询冻结用户
        List<Long> freeUserIdList = new ArrayList<>();
        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
                .eq(UserEntity::getIsFreeze, 1));
        if (!CollectionUtils.isEmpty(userList)){
            freeUserIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }

        // 准备查询条件
//        wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);

        //查询线索管理规则
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CLUS.code);
        wrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        SystemRuleModuleConfigEntity systemRuleModuleConfigEntity = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(systemRuleModuleConfigEntity)){
            return;
        }
        //执行规则

        if (systemRuleModuleConfigEntity.getHasPublicClues()){
            //开启了线索置公

            LambdaQueryWrapper<CustomerEntity> wrapperA = new LambdaQueryWrapper();
            wrapperA.eq(CustomerEntity::getHasPublicCustomer,false);
            wrapperA.eq(CustomerEntity::getHasOrderForm,0);
            wrapperA.eq(CustomerEntity::getCustomerForm,11);
            wrapperA.apply("DATE(public_remind_date) = DATE({0})", now());
            wrapperA.eq(CustomerEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
            wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
            List<CustomerEntity> customerListA = customerService.list(wrapperA);
            sendNotifications(customerListA, "PUBLIC_WARNING");
//            List<Long> customerIdListA = customerListA.stream().map(CustomerEntity::getId).collect(Collectors.toList());

            //置公时间
            //获取间隔maxPublicCluesNum的时间

        }

        //公共线索清空设置
        if (systemRuleModuleConfigEntity.getHasClearClues()){
            LambdaQueryWrapper<CustomerEntity> wrapperB = new LambdaQueryWrapper();
            wrapperB.eq(CustomerEntity::getHasPublicCustomer,true);
            wrapperB.eq(CustomerEntity::getHasOrderForm,0);
            wrapperB.eq(CustomerEntity::getCustomerForm,11);
//            wrapperB.lt(CustomerEntity::getLastFollowUpTime,begainTime);
            wrapperB.apply("DATE(delete_remind_date) = DATE({0})", now());
            wrapperB.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            wrapperB.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);

            List<CustomerEntity> customerList = customerService.list(wrapperB);
            sendNotifications(customerList, "CLEAR_WARNING",Set.of(1L));
//            List<Long> customerIdList = customerList.stream().map(CustomerEntity::getId).collect(Collectors.toList());
//            customerService.glAllListDelete(customerIdList);
        }

    }

    @Override
    public void clustomerPublicDateTask() {

        //查询冻结用户
        List<Long> freeUserIdList = new ArrayList<>();
        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
                .eq(UserEntity::getIsFreeze, 1));
        if (!CollectionUtils.isEmpty(userList)){
            freeUserIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }

        // 准备查询条件
//        wrapperA.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);


        long startTime = System.currentTimeMillis(); // 记录开始时间
        // 补充未跟进天数
        customerService.updateCustomerNoFollowUpDate(new ArrayList<>());

        //查询非冻结用户
//        List<UserEntity> userList = userService.list(new LambdaQueryWrapper<UserEntity>()
//                .eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
//                .eq(UserEntity::getIsFreeze, 0));
//        List<Long> userIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());

        // 准备查询条件
        LambdaQueryWrapper<CustomerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
//        wrapper.in(CustomerEntity::getOwnerBy,userIdList);
        wrapper.notIn(!CollectionUtils.isEmpty(freeUserIdList),CustomerEntity::getOwnerBy,freeUserIdList);
        wrapper.eq(CustomerEntity::getHasOrderForm, 0);

        // 查询配置信息（这些只需要查询一次）
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> customerWrapper = new LambdaQueryWrapper<>();
        customerWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CUSTOMER.code);
        customerWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        customerWrapper.last("limit 1");
        SystemRuleModuleConfigEntity customerConfig = this.getOne(customerWrapper);

        LambdaQueryWrapper<SystemRuleModuleConfigEntity> clusWrapper = new LambdaQueryWrapper<>();
        clusWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CLUS.code);
        clusWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        clusWrapper.last("limit 1");
        SystemRuleModuleConfigEntity clusConfig = this.getOne(clusWrapper);

        List<StorleadCalendarEntity> calendarList = storleadCalendarService.list();

        // 创建线程池
        int threadPoolSize = Runtime.getRuntime().availableProcessors() * 2; // 根据CPU核心数设置线程池大小
        ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);

        // 使用CountDownLatch等待所有任务完成
        int batchSize = 100;
        long total = customerService.count(wrapper);
        int totalPages = (int) ((total + batchSize - 1) / batchSize);
        CountDownLatch latch = new CountDownLatch(totalPages);

        // 提交分页处理任务
        for (int current = 1; current <= totalPages; current++) {
            final int pageNum = current;
//            try {
//                ThreadPoolUtil.execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        Page<CustomerEntity> page = new Page<>(pageNum, batchSize);
//                        IPage<CustomerEntity> result = customerService.page(page, wrapper);
//                        List<CustomerEntity> customerEntityList = result.getRecords();
//
//                        // 处理当前批次数据
//                        processBatch(customerEntityList, customerConfig, clusConfig, calendarList);
//                    }
//                });
//            }catch (Exception e) {
//                log.error("处理客户数据批次失败，页码: {}", pageNum, e);
////                return false;
//            }
            executorService.submit(() -> {
                try {
                    Page<CustomerEntity> page = new Page<>(pageNum, batchSize);
                    IPage<CustomerEntity> result = customerService.page(page, wrapper);
                    List<CustomerEntity> customerEntityList = result.getRecords();

                    // 处理当前批次数据
                    processBatch(customerEntityList, customerConfig, clusConfig, calendarList);
                } catch (Exception e) {
                    // 处理异常，可以根据需要记录日志
                    log.error("处理客户数据批次失败，页码: {}", pageNum, e);
                } finally {
                    latch.countDown();
                }
            });
        }

        // 等待所有任务完成
        try {
            latch.await();
            long processEndTime = System.currentTimeMillis();
            log.info("多线程处理完成，共处理 {} 页数据，耗时 {} 毫秒",
                    totalPages, (processEndTime - startTime));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.error("等待线程完成被中断", e);
        } finally {
            executorService.shutdown();
        }

        // 对有订单的客户，清除全部的置公时间等
        LambdaUpdateWrapper<CustomerEntity> clearWrapper = new LambdaUpdateWrapper<>();
        clearWrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        clearWrapper.eq(CustomerEntity::getHasOrderForm, 1);
        clearWrapper.set(CustomerEntity::getPublicDate, null);
        clearWrapper.set(CustomerEntity::getPublicRemindDate, null);
        clearWrapper.set(CustomerEntity::getGarbageDate, null);
        clearWrapper.set(CustomerEntity::getGarbageRemindDate, null);
        clearWrapper.set(CustomerEntity::getDeleteDate, null);
        clearWrapper.set(CustomerEntity::getDeleteRemindDate, null);
        customerService.update(clearWrapper);
        long endTime = System.currentTimeMillis();
        log.info("处理客户数据完成，耗时：{}ms", endTime - startTime);
    }

    public void clustomerPublicDateTaskOld(){

        //补充未跟进天数
        customerService.updateCustomerNoFollowUpDate(new ArrayList<>());

        LambdaQueryWrapper<CustomerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.eq(CustomerEntity::getHasOrderForm, 0);

        int batchSize = 1000; // 每批1000条
        int current = 1; // 当前页码
        boolean hasMore = true;

        LambdaQueryWrapper<SystemRuleModuleConfigEntity> customerWrapper = new LambdaQueryWrapper<>();
        customerWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CUSTOMER.code);
        customerWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        customerWrapper.last("limit 1");
        SystemRuleModuleConfigEntity customerConfig = this.getOne(customerWrapper);

        LambdaQueryWrapper<SystemRuleModuleConfigEntity> clusWrapper = new LambdaQueryWrapper<>();
        clusWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CLUS.code);
        clusWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        clusWrapper.last("limit 1");
        SystemRuleModuleConfigEntity clusConfig = this.getOne(clusWrapper);

        List<StorleadCalendarEntity> calendarList = storleadCalendarService.list();

        while (hasMore) {
            Page<CustomerEntity> page = new Page<>(current, batchSize);
            IPage<CustomerEntity> result = customerService.page(page, wrapper);
            List<CustomerEntity> customerEntityList = result.getRecords();

            // 处理当前批次数据
            processBatch(customerEntityList,customerConfig,clusConfig,calendarList);

            // 判断是否还有下一页
            hasMore = current * batchSize < result.getTotal();
            current++;
        }

        //要更新的客户最新信息


        //对有订单的客户，清除全部的置公时间等
        LambdaUpdateWrapper<CustomerEntity> clearWrapper = new LambdaUpdateWrapper<>();
        clearWrapper.eq(CustomerEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
        clearWrapper.eq(CustomerEntity::getHasOrderForm,1);
        clearWrapper.set(CustomerEntity::getPublicDate,null);
        clearWrapper.set(CustomerEntity::getPublicRemindDate,null);
        clearWrapper.set(CustomerEntity::getGarbageDate,null);
        clearWrapper.set(CustomerEntity::getGarbageRemindDate,null);
        clearWrapper.set(CustomerEntity::getDeleteDate,null);
        clearWrapper.set(CustomerEntity::getDeleteRemindDate,null);
        customerService.update(clearWrapper);
    }

    private void processBatch(List<CustomerEntity> customerEntityList,SystemRuleModuleConfigEntity customerConfig,SystemRuleModuleConfigEntity clusConfig,List<StorleadCalendarEntity> calendarList) {
        if (CollectionUtils.isEmpty(customerEntityList)){
            return;
        }

        List<CustomerEntity> customerUpdateList = new ArrayList<>();
        for (CustomerEntity customerEntity : customerEntityList) {
            try {
                if (customerEntity.getCustomerForm().equals(10)){
                    //客户规则对应时间写入
                    customerDateUpdate(customerEntity,customerConfig,customerUpdateList,calendarList);
                }else {
                    //线索规则对应时间写入
                    clusDateUpdate(customerEntity,clusConfig,customerUpdateList,calendarList);
                }
            } catch (Exception e){
                log.error("clustomerPublicDateTask -- error = ",e);
            }
        }
        if (!CollectionUtils.isEmpty(customerUpdateList)){
            customerService.updateBatchById(customerUpdateList);
        }

    }

    @Override
    public void updateCustomerDate(Long customerId) {
        if (ObjectUtils.isEmpty(customerId)){
            return;
        }
        CustomerEntity customerEntity = customerService.getById(customerId);
        if (ObjectUtils.isEmpty(customerEntity)){
            return;
        }
        updateCustomerDate(customerEntity);
    }
    @Override
    public void updateCustomerDate(CustomerEntity customerEntity) {
//        LambdaQueryWrapper<CustomerEntity> wrapper = new LambdaQueryWrapper();
//        wrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
//        wrapper.eq(CustomerEntity::getHasOrderForm, 0);
//        List<CustomerEntity> customerEntityList = customerService.list(wrapper);
        if (ObjectUtils.isEmpty(customerEntity)){
            return;
        }
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> customerWrapper = new LambdaQueryWrapper<>();
        customerWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CUSTOMER.code);
        customerWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        customerWrapper.last("limit 1");
        SystemRuleModuleConfigEntity customerConfig = this.getOne(customerWrapper);
//
        LambdaQueryWrapper<SystemRuleModuleConfigEntity> clusWrapper = new LambdaQueryWrapper<>();
        clusWrapper.eq(SystemRuleModuleConfigEntity::getResourceType, ResourceTypeEnum.CLUS.code);
        clusWrapper.eq(SystemRuleModuleConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        clusWrapper.last("limit 1");
        SystemRuleModuleConfigEntity clusConfig = this.getOne(clusWrapper);
        List<StorleadCalendarEntity> calendarList = storleadCalendarService.list();
        List<CustomerEntity> customerUpdateList = new ArrayList<>();
//        customerUpdateList.add(customerEntity);
        if (customerEntity.getCustomerForm().equals(10)){
            //客户规则对应时间写入
            customerDateUpdate(customerEntity,customerConfig,customerUpdateList,calendarList);
        }else {
            //线索规则对应时间写入
            clusDateUpdate(customerEntity,clusConfig,customerUpdateList,calendarList);
        }
        if (!CollectionUtils.isEmpty(customerUpdateList)){
            customerService.updateBatchById(customerUpdateList);
        }
        //补充未跟进天数
        customerService.updateCustomerNoFollowUpDate(Arrays.asList(customerEntity.getId()));
    }

    private void customerDateUpdate(CustomerEntity customerEntity,SystemRuleModuleConfigEntity customerConfig,List<CustomerEntity> customerUpdateList,List<StorleadCalendarEntity> calendarList) {
        List<String> customerTypeList = new ArrayList<>();
        //如果是垃圾客户
        if (customerEntity.getHasGarbageCustomer() ) {
            //垃圾客户清空设置
            if (customerConfig.getHasGarbageEmptyClues()){

                Integer maxGarbageEmptyCluesNum = customerConfig.getMaxGarbageEmptyCluesNum()+1;
                Date deleteDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxGarbageEmptyCluesNum);
                Date deleteRemindDate = storleadCalendarService.dateOff(deleteDate,1,calendarList);
                //查询出来需要清空的客户
                customerEntity.setDeleteDate(deleteDate);
                customerEntity.setDeleteRemindDate(deleteRemindDate);
//                customerService.updateById(customerEntity);
                customerUpdateList.add(customerEntity);
                return;
            }
            return;
        }

        //如果是公海客户
        if (customerEntity.getHasPublicCustomer()) {
            //公海客户
            if (customerConfig.getHasGarbageClues()) {
                Integer maxGarbageCluesNum = customerConfig.getMaxGarbageCluesNum()+1;
                Date garbageDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxGarbageCluesNum);
                Date garbageRemindDate = storleadCalendarService.dateOff(garbageDate,1,calendarList);
                customerEntity.setGarbageDate(garbageDate);
                customerEntity.setGarbageRemindDate(garbageRemindDate);
//                customerService.updateById(customerEntity);
                customerUpdateList.add(customerEntity);
                return;
            }
            return;
        }
        //是否开启客户类型区分
        if (customerConfig.getHasDistinguishCustomerType()) {
            //开启客户类型区分
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                List<CustomerTypeRulesVO> customerTypeRulesList = objectMapper.readValue(customerConfig.getCustomerTypeRules(),
                        objectMapper.getTypeFactory().constructCollectionType(List.class,  CustomerTypeRulesVO.class));
                Map<String, CustomerTypeRulesVO> customerTypeRulesMap = customerTypeRulesList.stream().collect(Collectors.toMap(CustomerTypeRulesVO::getCustomerTypeDictValue, Function.identity()));
                CustomerTypeRulesVO customerTypeRulesVO = customerTypeRulesMap.get(customerEntity.getCustomerTypeDictValue());

                if (!ObjectUtils.isEmpty(customerTypeRulesVO) && customerTypeRulesVO.getCustomerTypeFollowDate() != null){

                    Integer maxFollowNum = customerTypeRulesVO.getCustomerTypeFollowDate()+1;//普通客户最大未跟进天数
                    Integer maxTransactionNum = customerTypeRulesVO.getCustomerTypeTransactionDate()+1;//普通客户最大未成交天数
                    Date publicFollowDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxFollowNum);
                    Date publicTransactionDate = DateUtil.getDateByOffset(customerEntity.getOwnerTime(), Calendar.DATE,maxTransactionNum);
                    Date publicDate = publicFollowDate.before(publicTransactionDate)
                            ? publicFollowDate
                            : publicTransactionDate;
                    Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
                    customerEntity.setPublicDate(publicDate);
                    customerEntity.setPublicRemindDate(publicRemindDate);
//                    customerService.updateById(customerEntity);
                    customerUpdateList.add(customerEntity);
                    return;
                }
            } catch (Exception e) {
                log.error(e);
                e.printStackTrace();
            }
//            return;
        }
        if (customerConfig.getHasDistinguishKeyRegular()){
            //未开启客户类型区分,且开启了重点客户区分
            //如果是重点客户
            if (customerEntity.getHasFollowCustomer()){
                //查询归属人是否已离职，如果已离职，执行普通客户跟进逻辑
                Long ownerId = customerEntity.getOwnerBy();
                UserEntity userEntity = userService.getById(ownerId);
                if (userEntity.getStatus().equals(5)){
                    Integer maxRegularFollowPublicCluesNum = customerConfig.getRegularFollowDate()+1;//普通客户最大未跟进天数
                    Integer maxRegularTransactionPublicCluesNum = customerConfig.getRegularTransactionDate()+1;//普通客户最大未成交天数
                    Date publicFollowDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxRegularFollowPublicCluesNum);
                    Date publicTransactionDate = DateUtil.getDateByOffset(customerEntity.getOwnerTime(), Calendar.DATE,maxRegularTransactionPublicCluesNum);
                    Date publicDate = publicFollowDate.before(publicTransactionDate)
                            ? publicFollowDate
                            : publicTransactionDate;
                    Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
                    customerEntity.setPublicDate(publicDate);
                    customerEntity.setPublicRemindDate(publicRemindDate);
//                customerService.updateById(customerEntity);
                    customerUpdateList.add(customerEntity);
                }else {
                    Integer maxKeyFollowPublicCluesNum = customerConfig.getKeyFollowDate()+1;//重点客户最大未跟进天数
                    Integer maxKeyTransactionPublicCluesNum = customerConfig.getKeyTransactionDate()+1;//重点客户最大未成交天数

                    Date publicFollowDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxKeyFollowPublicCluesNum);
                    Date publicTransactionDate = DateUtil.getDateByOffset(customerEntity.getOwnerTime(), Calendar.DATE,maxKeyTransactionPublicCluesNum);
                    Date publicDate = publicFollowDate.before(publicTransactionDate)
                            ? publicFollowDate
                            : publicTransactionDate;
                    Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
                    customerEntity.setPublicDate(publicDate);
                    customerEntity.setPublicRemindDate(publicRemindDate);
//                customerService.updateById(customerEntity);
                    customerUpdateList.add(customerEntity);
                }


            }else {
                Integer maxRegularFollowPublicCluesNum = customerConfig.getRegularFollowDate()+1;//普通客户最大未跟进天数
                Integer maxRegularTransactionPublicCluesNum = customerConfig.getRegularTransactionDate()+1;//普通客户最大未成交天数
                Date publicFollowDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxRegularFollowPublicCluesNum);
                Date publicTransactionDate = DateUtil.getDateByOffset(customerEntity.getOwnerTime(), Calendar.DATE,maxRegularTransactionPublicCluesNum);
                Date publicDate = publicFollowDate.before(publicTransactionDate)
                        ? publicFollowDate
                        : publicTransactionDate;
                Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
                customerEntity.setPublicDate(publicDate);
                customerEntity.setPublicRemindDate(publicRemindDate);
//                customerService.updateById(customerEntity);
                customerUpdateList.add(customerEntity);
            }
        return;

        }else if (customerConfig.getHasPublicClues()){
            //只开启了客户置公
            //置公时间
            Integer maxPublicCluesNum = customerConfig.getMaxPublicCluesNum()+1;
            Date publicDate =DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxPublicCluesNum);
            Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
            customerEntity.setPublicDate(publicDate);
            customerEntity.setPublicRemindDate(publicRemindDate);
//            customerService.updateById(customerEntity);
            customerUpdateList.add(customerEntity);
            return;
        }

    }

    private void clusDateUpdate(CustomerEntity customerEntity,SystemRuleModuleConfigEntity customerConfig,List<CustomerEntity> customerUpdateList,List<StorleadCalendarEntity> calendarList) {

        //如果是公海线索
        if (customerEntity.getHasPublicCustomer()) {
            //公海线索
            if (customerConfig.getHasClearClues()) {

                Integer maxClearCluesNum = customerConfig.getMaxClearCluesNum()+1;

//                Integer maxGarbageCluesNum = customerConfig.getMaxGarbageCluesNum();
                Date deleteDate = DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxClearCluesNum);
                Date deleteRemindDate = storleadCalendarService.dateOff(deleteDate,1,calendarList);
                customerEntity.setDeleteDate(deleteDate);
                customerEntity.setDeleteRemindDate(deleteRemindDate);
//                customerService.updateById(customerEntity);
                customerUpdateList.add(customerEntity);
                return;
            }
            return;
        }
        //是否开启线索置公
        if (customerConfig.getHasPublicClues()){
            //只开启了客户置公
            //置公时间
            Integer maxPublicCluesNum = customerConfig.getMaxPublicCluesNum()+1;
            Date publicDate =DateUtil.getDateByOffset(customerEntity.getLastFollowUpTime(), Calendar.DATE,maxPublicCluesNum);
            Date publicRemindDate = storleadCalendarService.dateOff(publicDate,3,calendarList);
            customerEntity.setPublicDate(publicDate);
            customerEntity.setPublicRemindDate(publicRemindDate);
//            customerService.updateById(customerEntity);
            customerUpdateList.add(customerEntity);
            return;
        }

    }

    private void sendNotifications(List<CustomerEntity> customers, String eventType) {
        if (!CollectionUtils.isEmpty(customers)) {
            for (CustomerEntity customer : customers) {
                assembleService.sendCustomerEventMessages(customer.getId(), eventType);
            }
        }
    }

    private void sendNotifications(List<CustomerEntity> customers, String eventType, Set<Long> userIdSet) {
        if (!CollectionUtils.isEmpty(customers)) {
            for (CustomerEntity customer : customers) {
                assembleService.sendCustomerEventMessages(customer.getId(), eventType, userIdSet);
            }
        }
    }
    //更新客户归属人
    public void updateCustomerStatus(List<Long> idList){
        for (Long id : idList) {
            CustomerEntity customerNow = customerService.getById(id);
            //添加归属记录
            CustomerOwnerRecordEntity customerOwnerRecord = new CustomerOwnerRecordEntity();
            customerOwnerRecord.setCustomerId(customerNow.getId());
            customerOwnerRecord.setOldOwnerBy(customerNow.getFormerOwnerBy());
            customerOwnerRecord.setCreateBy(1L);
            customerOwnerRecord.setOperatingType(4);

            try {
                // 创建 Gson 实例
                Gson gson = new Gson();
                CustomerAllVO customerAllVO =new CustomerAllVO();
                BeanUtils.copyProperties(customerNow,customerAllVO);
                customerAllVO.setCustomerCompany(customerCompanyService.getByCustomerId(customerNow.getId()));
                // 将 CustomerEntity 对象转换为 JSON 字符串
                String jsonString = gson.toJson(customerAllVO);
                customerOwnerRecord.setCustomerDetail(jsonString);
            } catch (Exception e) {
                log.error(e);
//                return Result.error("客户数据更新异常");
            }
            customerOwnerService.save(customerOwnerRecord);
        }
    }


}
