wks преди 1 година
родител
ревизия
5c6c6515f8

+ 1 - 7
src/main/java/com/ydtech/modules/admin/model/po/SysWechatCustomer.java

@@ -64,16 +64,11 @@ public class SysWechatCustomer implements Serializable {
      */
     @TableField("open_kf_id")
     private String openKfId;
-    /**
-     * 值班客服
-     */
-    @TableField(value = "duty_customer_id")
-    private long dutyCustomerId;
 
     @TableField(exist = false)
     private static final long serialVersionUID = 1L;
 
-    public SysWechatCustomer(long id, String nickname, String avatar, String userId, String receptionist, String receptionistId, String openKfId, long dutyCustomerId) {
+    public SysWechatCustomer(long id, String nickname, String avatar, String userId, String receptionist, String receptionistId, String openKfId) {
         this.id = id;
         this.nickname = nickname;
         this.avatar = avatar;
@@ -81,7 +76,6 @@ public class SysWechatCustomer implements Serializable {
         this.receptionist = receptionist;
         this.receptionistId = receptionistId;
         this.openKfId = openKfId;
-        this.dutyCustomerId = dutyCustomerId;
     }
 
     public SysWechatCustomer(SysWechatCustomerAddVo sysWechatCustomerAddVo) {

+ 13 - 9
src/main/java/com/ydtech/modules/admin/model/po/SysWechatCustomerDuty.java

@@ -4,21 +4,21 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
-import java.io.Serializable;
-import java.time.LocalDateTime;
-import java.util.Date;
-
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
 import com.ydtech.utils.DateTimeUtil;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
 /**
  * 微信客服值班时间
+ *
  * @TableName sys_wechat_customer_duty
  */
-@TableName(value ="sys_wechat_customer_duty")
+@TableName(value = "sys_wechat_customer_duty")
 @AllArgsConstructor
 @NoArgsConstructor
 @Data
@@ -28,19 +28,21 @@ public class SysWechatCustomerDuty implements Serializable {
      */
     @TableId(value = "id", type = IdType.AUTO)
     private long id;
-
+    /**
+     * 值班客服
+     */
+    @TableField(value = "duty_customer_id")
+    private long dutyCustomerId;
     /**
      * 值班开始时间
      */
     @TableField(value = "start_time")
     private LocalDateTime startTime;
-
     /**
      * 值班结束时间
      */
     @TableField(value = "end_time")
     private LocalDateTime endTime;
-
     /**
      * 客服id
      */
@@ -51,9 +53,11 @@ public class SysWechatCustomerDuty implements Serializable {
     private static final long serialVersionUID = 1L;
 
 
-    public SysWechatCustomerDuty(long customerId, SysWechatCustomerDutyRulesVo.DutyDateTime dutyDateTime) {
+    public SysWechatCustomerDuty(long customerId, long dutyCustomerId, SysWechatCustomerDutyRulesVo.DutyDateTime dutyDateTime) {
         this.startTime = DateTimeUtil.parseLocalDateTime(dutyDateTime.getStartTime(), DateTimeUtil.DATETIME_PATTERN);
         this.endTime = DateTimeUtil.parseLocalDateTime(dutyDateTime.getEndTime(), DateTimeUtil.DATETIME_PATTERN);
         this.customerId = customerId;
+        this.dutyCustomerId = dutyCustomerId;
     }
+
 }

+ 26 - 3
src/main/java/com/ydtech/modules/admin/model/vo/wechat/SysWechatCustomerDutyRulesVo.java

@@ -1,5 +1,7 @@
 package com.ydtech.modules.admin.model.vo.wechat;
 
+import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
+import com.ydtech.utils.DateTimeUtil;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -7,6 +9,7 @@ import lombok.Data;
 import javax.validation.constraints.NotBlank;
 import java.io.Serializable;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @Data
 @ApiModel("值班规则配置")
@@ -18,10 +21,26 @@ public class SysWechatCustomerDutyRulesVo implements Serializable {
     private String customerId;
 
     @ApiModelProperty("值班客服")
-    private String dutyCustomerId;
+    private List<DutyCustomerDTO> dutyCustomerDTOS;
 
-    @ApiModelProperty("值班规则列表")
-    private List<DutyDateTime> dutyDateTimeList;
+    @Data
+    @ApiModel("值班客服")
+    public static class DutyCustomerDTO implements Serializable {
+
+        private static final long serialVersionUID = -6002812616686817550L;
+
+        @ApiModelProperty("值班客服")
+        private String dutyCustomerId;
+
+        @ApiModelProperty("值班规则列表")
+        private List<DutyDateTime> dutyDateTimeList;
+
+        public DutyCustomerDTO(Long dutyCustomerId, List<SysWechatCustomerDuty> sysWechatCustomerDuties) {
+            this.dutyCustomerId = dutyCustomerId.toString();
+            this.dutyDateTimeList = sysWechatCustomerDuties.stream().map(DutyDateTime::new).collect(Collectors.toList());
+        }
+
+    }
 
     @Data
     @ApiModel("值班时间")
@@ -37,6 +56,10 @@ public class SysWechatCustomerDutyRulesVo implements Serializable {
         @ApiModelProperty("结束时间")
         private String endTime;
 
+        public DutyDateTime(SysWechatCustomerDuty sysWechatCustomerDuty) {
+            this.startTime = DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getStartTime());
+            this.endTime = DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getEndTime());
+        }
     }
 
 }

+ 16 - 3
src/main/java/com/ydtech/modules/admin/model/vo/wechat/SysWechatCustomerVo.java

@@ -3,7 +3,9 @@ package com.ydtech.modules.admin.model.vo.wechat;
 import com.baomidou.mybatisplus.annotation.TableField;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.util.List;
 
@@ -39,9 +41,20 @@ public class SysWechatCustomerVo {
     private String receptionistId;
 
     @ApiModelProperty("值班客服")
-    private String dutyCustomer;
+    private DutyCustomerDTO dutyCustomerDTO;
 
-    @ApiModelProperty("值班时间")
-    private List<String> dutyDatetime;
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    @ApiModel("值班客服")
+    public static class DutyCustomerDTO {
+
+        @ApiModelProperty("值班客服")
+        private String dutyCustomer;
+
+        @ApiModelProperty("值班时间")
+        private List<String> dutyDatetime;
+
+    }
 
 }

+ 8 - 0
src/main/java/com/ydtech/modules/admin/service/SysWechatCustomerDutyService.java

@@ -2,6 +2,7 @@ package com.ydtech.modules.admin.service;
 
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.ydtech.exception.SystemException;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
@@ -32,4 +33,11 @@ public interface SysWechatCustomerDutyService extends IService<SysWechatCustomer
         return list(wr);
     }
 
+    default void isDutyCustomerId(Long customerId) {
+        List<SysWechatCustomerDuty> list = lambdaQuery().eq(SysWechatCustomerDuty::getDutyCustomerId, customerId).list();
+        if (!list.isEmpty()) {
+            throw new SystemException("此客服还在值班客服中配置,请删除后重试");
+        }
+    }
+
 }

+ 0 - 6
src/main/java/com/ydtech/modules/admin/service/SysWechatCustomerService.java

@@ -67,12 +67,6 @@ public interface SysWechatCustomerService extends BaseIService<SysWechatCustomer
         }
     }
 
-    default void isDutyCustomerId(Long customerId) {
-        List<SysWechatCustomer> list = lambdaQuery().eq(SysWechatCustomer::getDutyCustomerId, customerId).list();
-        if (!list.isEmpty()) {
-            throw new SystemException("此客服还在值班客服中配置,请删除后重试");
-        }
-    }
 
     void getCacheCustomerId(SysWechatCustomer sysWechatCustomer);
 

+ 25 - 10
src/main/java/com/ydtech/modules/admin/service/impl/SysWechatCustomerBusinessServiceImpl.java

@@ -26,9 +26,7 @@ import me.chanjar.weixin.cp.bean.WxCpBaseResp;
 import me.chanjar.weixin.cp.bean.WxCpDepart;
 import me.chanjar.weixin.cp.bean.WxCpUser;
 import me.chanjar.weixin.cp.bean.kf.*;
-import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
@@ -100,12 +98,28 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
             // 设置值班时间
             sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> {
                 List<SysWechatCustomerDuty> sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId()));
-                if (!CollectionUtils.isEmpty(sysWechatCustomerDuties)) {
-                    List<String> collect = sysWechatCustomerDuties.stream()
+                Map<Long, List<SysWechatCustomerDuty>> collect = sysWechatCustomerDuties.stream()
+                        .collect(Collectors.groupingBy(SysWechatCustomerDuty::getDutyCustomerId));
+
+                Map<Long, SysWechatCustomer> collect2 = sysWechatCustomerService.lambdaQuery()
+                        .in(SysWechatCustomer::getId, collect.keySet())
+                        .list()
+                        .stream()
+                        .collect(Collectors.toMap(SysWechatCustomer::getId, x -> x));
+
+                collect.keySet().forEach(sysWechatCustomerVoId -> {
+                    SysWechatCustomerVo.DutyCustomerDTO dutyCustomerDTO = new SysWechatCustomerVo.DutyCustomerDTO();
+                    List<SysWechatCustomerDuty> sysWechatCustomerDuties1 = collect.get(sysWechatCustomerVoId);
+                    List<String> collect1 = sysWechatCustomerDuties1.stream()
                             .map(x -> DateTimeUtil.startAndEndTimeFormatting(x.getStartTime(), x.getEndTime()))
                             .collect(Collectors.toList());
-                    sysWechatCustomerVo.setDutyDatetime(collect);
-                }
+                    SysWechatCustomer sysWechatCustomer = collect2.get(sysWechatCustomerVoId);
+                    if(!ObjectUtils.isEmpty(sysWechatCustomer)) {
+                        dutyCustomerDTO.setDutyCustomer(sysWechatCustomer.getNickname());
+                    }
+                    dutyCustomerDTO.setDutyDatetime(collect1);
+                    sysWechatCustomerVo.setDutyCustomerDTO(dutyCustomerDTO);
+                });
             });
         }
 
@@ -158,7 +172,7 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
     @Override
     public void deleteWechatCustomer(String customerId) throws WxErrorException {
         SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
-        sysWechatCustomerService.isDutyCustomerId(sysWechatCustomer.getId());
+        sysWechatCustomerDutyService.isDutyCustomerId(sysWechatCustomer.getId());
         WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel();
         wxCpKfAccountDel.setOpenKfid(sysWechatCustomer.getOpenKfId());
         WxCpBaseResp wxCpBaseResp;
@@ -172,7 +186,7 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
     @Override
     public void updateWechatCustomer(String customerId, SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
         SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
-        if(!sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname())){
+        if (!sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname())) {
             sysWechatCustomerService.isNicknameRepeat(sysWechatCustomerAddVo.getNickname());
         }
         WxCpKfAccountUpd wxCpKfAccountUpd = new WxCpKfAccountUpd();
@@ -256,8 +270,9 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
                 .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
                 .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
                 .list();
-        if (sysWechatCustomer.getDutyCustomerId() > 0 && list != null && !list.isEmpty()) {
-            sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomer.getDutyCustomerId());
+        if (list != null && !list.isEmpty()) {
+            SysWechatCustomerDuty sysWechatCustomerDuty = list.get(0);
+            sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomerDuty.getDutyCustomerId());
         }
 
         if (ObjectUtils.isEmpty(sysWechatCustomer)) {

+ 44 - 27
src/main/java/com/ydtech/modules/admin/service/impl/SysWechatCustomerServiceImpl.java

@@ -22,6 +22,7 @@ import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
@@ -59,20 +60,15 @@ public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerM
         List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.getByCustomerId(customerId);
         SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRulesVo = new SysWechatCustomerDutyRulesVo();
         sysWechatCustomerDutyRulesVo.setCustomerId(String.valueOf(customer.getId()));
-        if (customer.getDutyCustomerId() > 0) {
-            sysWechatCustomerDutyRulesVo.setDutyCustomerId(String.valueOf(customer.getDutyCustomerId()));
-        }else {
-            sysWechatCustomerDutyRulesVo.setDutyCustomerId(null);
-        }
-        List<SysWechatCustomerDutyRulesVo.DutyDateTime> collect = sysWechatCustomerDuties.stream()
-                .map(sysWechatCustomerDuty -> {
-                    SysWechatCustomerDutyRulesVo.DutyDateTime dutyDateTime = new SysWechatCustomerDutyRulesVo.DutyDateTime();
-                    dutyDateTime.setEndTime(DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getEndTime()));
-                    dutyDateTime.setStartTime(DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getStartTime()));
-                    return dutyDateTime;
-                })
+
+        Map<Long, List<SysWechatCustomerDuty>> listMap = sysWechatCustomerDuties.stream()
+                .collect(Collectors.groupingBy(SysWechatCustomerDuty::getDutyCustomerId));
+
+        List<SysWechatCustomerDutyRulesVo.DutyCustomerDTO> dutyCustomerDTOS = listMap.keySet()
+                .stream()
+                .map(x -> new SysWechatCustomerDutyRulesVo.DutyCustomerDTO(x, listMap.get(x)))
                 .collect(Collectors.toList());
-        sysWechatCustomerDutyRulesVo.setDutyDateTimeList(collect);
+        sysWechatCustomerDutyRulesVo.setDutyCustomerDTOS(dutyCustomerDTOS);
         return sysWechatCustomerDutyRulesVo;
     }
 
@@ -80,26 +76,47 @@ public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerM
     @Transactional(rollbackFor = Exception.class)
     public void saveOrUpdateCustomerDutyRules(SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRules) {
         SysWechatCustomer customer = getByIdExist(sysWechatCustomerDutyRules.getCustomerId(), "客服不存在");
-        SysWechatCustomer dutyCustomer = getByIdExist(sysWechatCustomerDutyRules.getDutyCustomerId(), "值班客服不存在");
-
-        Optional.ofNullable(sysWechatCustomerDutyRules.getDutyDateTimeList())
-                        .ifPresent(sysWechatCustomerDutyRulesList -> {
-                            sysWechatCustomerDutyRulesList.forEach(sysWechatCustomerDutyRule -> {
-                                if (DateTimeUtil.timeComparison(sysWechatCustomerDutyRule.getStartTime(), sysWechatCustomerDutyRule.getEndTime())) {
-                                    throw new SystemException("结束时间必须晚于开始时间");
-                                }
-                            });
+        //
+        List<String> dutyCustomerIdList = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
+                .stream()
+                .map(SysWechatCustomerDutyRulesVo.DutyCustomerDTO::getDutyCustomerId)
+                .collect(Collectors.toList());
+        //
+        List<SysWechatCustomer> sysWechatCustomerList = lambdaQuery().in(SysWechatCustomer::getId, sysWechatCustomerDutyRules.getCustomerId())
+                .list();
+        //
+        List<SysWechatCustomer> sysWechatCustomers = sysWechatCustomerList.stream()
+                .filter(sysWechatCustomer -> !dutyCustomerIdList.contains(String.valueOf(sysWechatCustomer.getId())))
+                .collect(Collectors.toList());
+
+        if (!sysWechatCustomers.isEmpty()) {
+            throw new SystemException("");
+        }
+
+        sysWechatCustomerDutyRules.getDutyCustomerDTOS().forEach(sysWechatCustomer -> {
+            Optional.ofNullable(sysWechatCustomer.getDutyDateTimeList())
+                    .ifPresent(sysWechatCustomerDutyRulesList -> {
+                        sysWechatCustomerDutyRulesList.forEach(sysWechatCustomerDutyRule -> {
+                            if (DateTimeUtil.timeComparison(sysWechatCustomerDutyRule.getStartTime(), sysWechatCustomerDutyRule.getEndTime())) {
+                                throw new SystemException("结束时间必须晚于开始时间");
+                            }
                         });
+                    });
+        });
 
-        customer.setDutyCustomerId(dutyCustomer.getId());
-        updateById(customer);
-        // 清楚再删除
+        // 清除再删除
         sysWechatCustomerDutyService.clearCustomerDuty(String.valueOf(customer.getId()));
 
-        List<SysWechatCustomerDuty> collect = sysWechatCustomerDutyRules.getDutyDateTimeList()
+        List<SysWechatCustomerDuty> collect = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
                 .stream()
-                .map(x -> new SysWechatCustomerDuty(customer.getId(), x))
+                .flatMap(dutyCustomerDTO -> {
+                    String dutyCustomerId = dutyCustomerDTO.getDutyCustomerId();
+                    return dutyCustomerDTO.getDutyDateTimeList().stream()
+                            .map(dutyDateTime -> new SysWechatCustomerDuty(customer.getId(), Long.parseLong(dutyCustomerId), dutyDateTime));
+
+                })
                 .collect(Collectors.toList());
+
         sysWechatCustomerDutyService.saveBatch(collect);
     }