Kaynağa Gözat

消息自动回复以及转接

wks 10 ay önce
ebeveyn
işleme
440484a391

+ 3 - 3
src/main/java/com/ydtech/handler/CreateAndUpdateMetaObjectHandler.java

@@ -13,7 +13,7 @@ import org.apache.ibatis.reflection.MetaObject;
 import java.util.Date;
 import java.util.Date;
 
 
 /**
 /**
- *  MP注入处理器  抄来的
+ * MP注入处理器  抄来的
  *
  *
  * @author: lig
  * @author: lig
  * @date: 2023年06月30日 0030
  * @date: 2023年06月30日 0030
@@ -55,7 +55,7 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 //                baseEntity.setUpdateBy(username);
 //                baseEntity.setUpdateBy(username);
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
-            throw new SystemException(StringUtils.toString(HttpStatus.HTTP_UNAUTHORIZED),"自动注入异常 => " + e.getMessage());
+            log.error(StringUtils.toString(HttpStatus.HTTP_UNAUTHORIZED), "自动注入异常 => " + e.getMessage());
         }
         }
     }
     }
 
 
@@ -77,7 +77,7 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
                 }
                 }
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
-            throw new SystemException(StringUtils.toString(HttpStatus.HTTP_UNAUTHORIZED),"自动注入异常 => " + e.getMessage());
+            log.error(StringUtils.toString(HttpStatus.HTTP_UNAUTHORIZED), "自动注入异常 => " + e.getMessage());
         }
         }
     }
     }
 }
 }

+ 4 - 1
src/main/java/com/ydtech/modules/admin/components/wechat/customer/request/CustomerSendMsgOnEventRequest.java

@@ -55,9 +55,12 @@ public class CustomerSendMsgOnEventRequest extends CustomerBaseRequest {
     }
     }
 
 
 
 
-    public static CustomerSendMsgOnEventRequest sendMsgOnEvent(SysUser sysUser, String msgid, String code) {
+    public static CustomerSendMsgOnEventRequest sendMsgOnEvent(SysUser sysUser, String code, boolean workingHours) {
         String messages = "你好!" + sysUser.getName() + "(" + sysUser.getId() + ")" + "很高兴为你服务";
         String messages = "你好!" + sysUser.getName() + "(" + sysUser.getId() + ")" + "很高兴为你服务";
         // 发送欢迎信息
         // 发送欢迎信息
+        if (!workingHours) {
+            messages = "您好" + sysUser.getName() + "(" + sysUser.getId() + ")" + "~当前客服暂时不在服务时间,服务时间为:周一至周五:8:30-12:00,14:00-18:30,周六至周日:9:00-12:00,14:00-18:30,当前未到服务时间,请您在服务时段内咨询~";
+        }
         CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = new CustomerSendMsgOnEventRequest();
         CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = new CustomerSendMsgOnEventRequest();
         customerSendMsgOnEventRequest.setMsgtype("text");
         customerSendMsgOnEventRequest.setMsgtype("text");
         customerSendMsgOnEventRequest.setCode(code);
         customerSendMsgOnEventRequest.setCode(code);

+ 1 - 1
src/main/java/com/ydtech/modules/admin/components/wechat/customer/response/CustomerSyncMsgResponse.java

@@ -40,7 +40,7 @@ public class CustomerSyncMsgResponse extends CustomerBaseResponse{
         private String msgid;
         private String msgid;
 
 
         @JsonProperty("send_time")
         @JsonProperty("send_time")
-        private Integer sendTime;
+        private long sendTime;
 
 
         @JsonProperty("origin")
         @JsonProperty("origin")
         private Integer origin;
         private Integer origin;

+ 18 - 0
src/main/java/com/ydtech/modules/admin/dao/WorkScheduleConfigMapper.java

@@ -0,0 +1,18 @@
+package com.ydtech.modules.admin.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ydtech.modules.admin.model.po.WorkScheduleConfig;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * <p>
+ * 工作时间配置表 Mapper 接口
+ * </p>
+ *
+ * @author author
+ * @since 2024-01-01
+ */
+@Mapper
+public interface WorkScheduleConfigMapper extends BaseMapper<WorkScheduleConfig> {
+
+}

+ 7 - 0
src/main/java/com/ydtech/modules/admin/model/SysUser.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import com.google.gson.annotations.SerializedName;
 import com.ydtech.exception.SystemException;
 import com.ydtech.exception.SystemException;
 import com.ydtech.modules.base.model.BaseEntity;
 import com.ydtech.modules.base.model.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModel;
@@ -104,6 +105,12 @@ public class SysUser extends BaseEntity {
     @TableField(value = "alipay")
     @TableField(value = "alipay")
     private String alipay;
     private String alipay;
 
 
+    /**
+     * 微信客服id
+     */
+    @SerializedName("external_userid")
+    private String externalUserId;
+
     /**
     /**
      * 审批意见
      * 审批意见
      */
      */

+ 113 - 0
src/main/java/com/ydtech/modules/admin/model/po/WorkScheduleConfig.java

@@ -0,0 +1,113 @@
+package com.ydtech.modules.admin.model.po;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+import java.time.LocalTime;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 工作时间配置表
+ * </p>
+ *
+ * @author author
+ * @since 2024-01-01
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@TableName("work_schedule_config")
+public class WorkScheduleConfig implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键ID
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 日期类型:工作日/休息日
+     */
+    @TableField("day_type")
+    private String dayType;
+
+    /**
+     * 时间段:上午/下午
+     */
+    @TableField("time_slot")
+    private String timeSlot;
+
+    /**
+     * 开始时间
+     */
+    @TableField("start_time")
+    @JsonFormat(pattern = "HH:mm:ss")
+    private LocalTime startTime;
+
+    /**
+     * 结束时间
+     */
+    @TableField("end_time")
+    @JsonFormat(pattern = "HH:mm:ss")
+    private LocalTime endTime;
+
+    /**
+     * 是否生效
+     */
+    @TableField("is_active")
+    private Boolean active;
+
+    /**
+     * 创建时间
+     */
+    @TableField(value = "created_at", fill = FieldFill.INSERT)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private LocalDateTime createdAt;
+
+    /**
+     * 更新时间
+     */
+    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private LocalDateTime updatedAt;
+
+    /**
+     * 日期类型枚举
+     */
+    @AllArgsConstructor
+    @Getter
+    public enum DayType {
+
+        WEEKDAY("weekday", "工作日"),
+        WEEKEND("weekend", "休息日");
+
+        private final String code;
+
+        private final String desc;
+
+
+    }
+
+    /**
+     * 时间段枚举
+     */
+    @AllArgsConstructor
+    @Getter
+    public enum TimeSlot {
+        MORNING("morning", "上午"),
+        AFTERNOON("afternoon", "下午");
+
+        private final String code;
+        private final String desc;
+
+    }
+}

+ 22 - 0
src/main/java/com/ydtech/modules/admin/service/IWorkScheduleConfigService.java

@@ -0,0 +1,22 @@
+package com.ydtech.modules.admin.service;
+
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ydtech.modules.admin.model.po.WorkScheduleConfig;
+
+/**
+ * <p>
+ * 工作时间配置表 服务类
+ * </p>
+ *
+ * @author author
+ * @since 2024-01-01
+ */
+public interface IWorkScheduleConfigService extends IService<WorkScheduleConfig> {
+
+    /**
+     * 判断现在的时间是否是工作时间 true 工作时间 false 休息时间
+     */
+    boolean isWorkingHours();
+
+}

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

@@ -58,6 +58,7 @@ public interface SysWechatCustomerService extends BaseIService<SysWechatCustomer
 
 
     /**
     /**
      * 昵称是否存在
      * 昵称是否存在
+     *
      * @param nickname
      * @param nickname
      */
      */
     default void isNicknameRepeat(String nickname) {
     default void isNicknameRepeat(String nickname) {
@@ -70,4 +71,14 @@ public interface SysWechatCustomerService extends BaseIService<SysWechatCustomer
 
 
     SysWechatCustomer getCacheCustomerId(SysWechatCustomer sysWechatCustomer);
     SysWechatCustomer getCacheCustomerId(SysWechatCustomer sysWechatCustomer);
 
 
+
+    /**
+     * 判断管理人是否一致
+     *
+     * @param externalUserId 企业微信id
+     * @param sysWechatCustomer       客服
+     * @return true 一致  false 不一致
+     *
+     */
+    boolean whetherTheManagersAreConsistent(String externalUserId, SysWechatCustomer sysWechatCustomer);
 }
 }

+ 21 - 0
src/main/java/com/ydtech/modules/admin/service/impl/SysWechatCustomerServiceImpl.java

@@ -6,11 +6,13 @@ import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.github.pagehelper.PageInfo;
 import com.ydtech.exception.SystemException;
 import com.ydtech.exception.SystemException;
 import com.ydtech.modules.admin.dao.SysWechatCustomerMapper;
 import com.ydtech.modules.admin.dao.SysWechatCustomerMapper;
+import com.ydtech.modules.admin.model.SysUser;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
 import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerQueryVo;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerQueryVo;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerVo;
 import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerVo;
+import com.ydtech.modules.admin.service.SysUserService;
 import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
 import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
 import com.ydtech.modules.admin.service.SysWechatCustomerService;
 import com.ydtech.modules.admin.service.SysWechatCustomerService;
 import com.ydtech.utils.DateTimeUtil;
 import com.ydtech.utils.DateTimeUtil;
@@ -21,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
 
 
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Optional;
@@ -40,6 +43,8 @@ public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerM
 
 
     private final StringRedisTemplate redisTemplate;
     private final StringRedisTemplate redisTemplate;
 
 
+    private final SysUserService sysUserService;
+
     @Override
     @Override
     @Transactional(rollbackFor = Exception.class)
     @Transactional(rollbackFor = Exception.class)
     public void deleteAllInfo(String customerId) {
     public void deleteAllInfo(String customerId) {
@@ -141,6 +146,22 @@ public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerM
         redisTemplate.opsForValue().set(KEY, String.valueOf(sysWechatCustomer.getId()));
         redisTemplate.opsForValue().set(KEY, String.valueOf(sysWechatCustomer.getId()));
         return sysWechatCustomer;
         return sysWechatCustomer;
     }
     }
+
+
+    @Override
+    public boolean whetherTheManagersAreConsistent(String externalUserId, SysWechatCustomer sysWechatCustomer) {
+        SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getExternalUserId, externalUserId).one();
+        if (ObjectUtils.isEmpty(sysUser)) {
+            return true;
+        }
+        if(!StringUtils.hasLength(sysUser.getLeaderId())){
+            return true;
+        }
+
+        return sysWechatCustomer.getUserId().equals(sysUser.getLeaderId());
+    }
+
+
 }
 }
 
 
 
 

+ 75 - 30
src/main/java/com/ydtech/modules/admin/service/impl/WechatCustomerServiceImpl.java

@@ -6,27 +6,29 @@ import com.ydtech.modules.admin.components.WechatCustomerComponents;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerAddContactWayRequest;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerAddContactWayRequest;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerListRequest;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerListRequest;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest;
 import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest;
-import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSyncMsgRequest;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddContactWayResponse;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddContactWayResponse;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
-import com.ydtech.modules.admin.components.wechat.customer.response.CustomerSyncMsgResponse;
 import com.ydtech.modules.admin.model.SysUser;
 import com.ydtech.modules.admin.model.SysUser;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
+import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
 import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
 import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
 import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
 import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
-import com.ydtech.modules.admin.service.SysUserService;
-import com.ydtech.modules.admin.service.SysWechatCustomerService;
-import com.ydtech.modules.admin.service.WechatCustomerService;
+import com.ydtech.modules.admin.service.*;
 import com.ydtech.utils.StringUtils;
 import com.ydtech.utils.StringUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
 import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
 import me.chanjar.weixin.cp.api.WxCpKfService;
 import me.chanjar.weixin.cp.api.WxCpKfService;
+import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgListResp;
+import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgSendRequest;
 import me.chanjar.weixin.cp.bean.kf.WxCpKfServiceStateResp;
 import me.chanjar.weixin.cp.bean.kf.WxCpKfServiceStateResp;
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfEventMsg;
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.ObjectUtils;
 
 
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
@@ -45,6 +47,9 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
 
 
     private final SysWechatCustomerService sysWechatCustomerService;
     private final SysWechatCustomerService sysWechatCustomerService;
 
 
+    private final IWorkScheduleConfigService workScheduleConfigService;
+
+    private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
 
 
     @Override
     @Override
     public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
     public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
@@ -63,32 +68,55 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
         WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
         WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
 
 
         // 获取消息
         // 获取消息
-        CustomerSyncMsgRequest customerSyncMsgRequest = new CustomerSyncMsgRequest();
-        customerSyncMsgRequest.setToken(wechatCustomerCallbackVo.getToken());
-        customerSyncMsgRequest.setOpenKfid(wechatCustomerCallbackVo.getOpenKfId());
-        CustomerSyncMsgResponse customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
-        while (customerSyncMsgResponse.getHasMore() == 1) {
-            String nextCursor = customerSyncMsgResponse.getNextCursor();
-            customerSyncMsgRequest.setCursor(nextCursor);
-            customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
+        WxCpKfMsgListResp wxCpKfMsgListResp = wxCpKfService.syncMsg(null, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
+
+        while (wxCpKfMsgListResp.getHasMore() == 1) {
+            String nextCursor = wxCpKfMsgListResp.getNextCursor();
+            wxCpKfMsgListResp = wxCpKfService.syncMsg(nextCursor, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
         }
         }
-        List<CustomerSyncMsgResponse.MsgListDTO> msgList = customerSyncMsgResponse.getMsgList();
-        CustomerSyncMsgResponse.MsgListDTO msgListDTO = customerSyncMsgResponse.getMsgList().get(msgList.size() - 1);
+        List<WxCpKfMsgListResp.WxCpKfMsgItem> msgList = wxCpKfMsgListResp.getMsgList();
+        WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem = wxCpKfMsgListResp.getMsgList().get(msgList.size() - 1);
+
+        SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wechatCustomerCallbackVo.getOpenKfId()).one();
+        boolean workingHours = workScheduleConfigService.isWorkingHours();
+        List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.lambdaQuery()
+                .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId())
+                .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
+                .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
+                .list();
+
+        // 改变会话状态
+        if (wxCpKfMsgItem.getOrigin() == 3) {
+            // 1 管理人是否一致
+            // 2 休息日
+            // 3 工作时间内 安排了值班客服
+            if (!sysWechatCustomerService.whetherTheManagersAreConsistent(wxCpKfMsgItem.getExternalUserId(), sysWechatCustomer) || !workingHours || !sysWechatCustomerDuties.isEmpty()) {
+                WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId());
+                Integer state1 = serviceState.getServiceState();
+                if (state1 == 0) {
+                    state1 = 1;
+                    wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
+                    restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
+                } else if (state1 == 1) {
+                    restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
+                } else if (state1 == 3) {
+                    state1 = 4;
+                    wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
+                }
+            }
 
 
-        if (msgListDTO.getOrigin() == 3) {
-            List<CustomerSyncMsgResponse.MsgListDTO> msgListDTOList = msgList.stream()
-                    .filter(x -> "event".equals(x.getMsgtype()))
-                    .collect(Collectors.toList());
+            List<WxCpKfMsgListResp.WxCpKfMsgItem> msgListDTOList = msgList.stream().filter(x -> "event".equals(x.getMsgType())).collect(Collectors.toList());
             msgListDTOList.forEach(x -> {
             msgListDTOList.forEach(x -> {
                 try {
                 try {
-                    CustomerSyncMsgResponse.MsgListDTO.EventDTO xEvent = x.getEvent();
-                    WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid());
-                    if (serviceState.getServiceState() == 0) {
-                        List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery()
-                                .eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid())
-                                .list();
-                        wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid(), 3, list.get(0)
-                                .getReceptionistId());
+                    WxCpKfEventMsg xEvent = x.getEvent();
+                    WxCpKfServiceStateResp wxCpKfServiceStateResp = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId());
+                    Integer state = wxCpKfServiceStateResp.getServiceState();
+                    // 休息时间
+                    List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid()).list();
+                    String receptionistId = list.get(0).getReceptionistId();
+                    if (workScheduleConfigService.isWorkingHours() && (state == 0 || state == 1)) {
+                        state = 3;
+                        wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId(), state, receptionistId);
                     }
                     }
                 } catch (WxErrorException e) {
                 } catch (WxErrorException e) {
                     throw new RuntimeException(e);
                     throw new RuntimeException(e);
@@ -96,7 +124,7 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
             });
             });
         }
         }
 
 
-        CustomerSyncMsgResponse.MsgListDTO.EventDTO event = msgListDTO.getEvent();
+        WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
         if (ObjectUtils.isEmpty(event)) {
         if (ObjectUtils.isEmpty(event)) {
             return;
             return;
         }
         }
@@ -105,14 +133,31 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
         if (StringUtils.isEmpty(scene)) {
         if (StringUtils.isEmpty(scene)) {
             return;
             return;
         }
         }
-        if (msgListDTO.getOrigin() == 4) {
+
+        // 发送欢迎语
+        if (wxCpKfMsgItem.getOrigin() == 4) {
             SysUser sysUser = sysUserService.getById(scene);
             SysUser sysUser = sysUserService.getById(scene);
-            CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, msgListDTO.getMsgid(), event.getWelcomeCode());
+            sysUser.setExternalUserId(event.getExternalUserId());
+            sysUserService.updateById(sysUser);
+            CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, event.getWelcomeCode(), workingHours);
             wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
             wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
         }
         }
 
 
     }
     }
 
 
+    private void restTimeSendMsg(String externalUserId, String openKfId) throws WxErrorException {
+        String message = "您咨询的客服目前暂时不在服务时间,请您前往“晋掌柜App”点击“微信客服”,将为您安排新的客服为您提供服务~(客服服务时间:周一至周五  8:30-18:30,周六至周日  8:30-18:30,午间 12:00-14:00 休息)";
+        WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();
+        wxCpKfMsgSendRequest.setToUser(externalUserId);
+        wxCpKfMsgSendRequest.setOpenKfid(openKfId);
+        wxCpKfMsgSendRequest.setMsgType("text");
+        WxCpKfTextMsg wxCpKfTextMsg = new WxCpKfTextMsg();
+        wxCpKfTextMsg.setContent(message);
+        wxCpKfMsgSendRequest.setText(wxCpKfTextMsg);
+        wxCpKfService.sendMsg(wxCpKfMsgSendRequest);
+    }
+
+
     @Override
     @Override
     public List<CustomerListResponse.AccountListDTO> list() {
     public List<CustomerListResponse.AccountListDTO> list() {
         CustomerListRequest customerListResponse = new CustomerListRequest(0, 100);
         CustomerListRequest customerListResponse = new CustomerListRequest(0, 100);

+ 47 - 0
src/main/java/com/ydtech/modules/admin/service/impl/WorkScheduleConfigServiceImpl.java

@@ -0,0 +1,47 @@
+package com.ydtech.modules.admin.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ydtech.modules.admin.constants.DataPermissionsEnum;
+import com.ydtech.modules.admin.dao.WorkScheduleConfigMapper;
+import com.ydtech.modules.admin.model.po.WorkScheduleConfig;
+import com.ydtech.modules.admin.service.IWorkScheduleConfigService;
+import com.ydtech.modules.admin.utils.WorkdayUtils;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalTime;
+import java.util.List;
+
+@Service
+public class WorkScheduleConfigServiceImpl extends ServiceImpl<WorkScheduleConfigMapper, WorkScheduleConfig> implements IWorkScheduleConfigService {
+
+    @Override
+    public boolean isWorkingHours() {
+        WorkScheduleConfig.DayType dayType = WorkdayUtils.isTodayWeekend() ? WorkScheduleConfig.DayType.WEEKEND : WorkScheduleConfig.DayType.WEEKDAY;
+        List<WorkScheduleConfig> list = lambdaQuery().eq(WorkScheduleConfig::getDayType, dayType.getCode()).list();
+        LocalTime now = LocalTime.now();
+        for (WorkScheduleConfig workScheduleConfig : list) {
+            if (isTimeInRange(now, workScheduleConfig.getStartTime(), workScheduleConfig.getEndTime())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static boolean isTimeInRange(LocalTime checkTime, LocalTime startTime, LocalTime endTime) {
+        if (startTime.equals(endTime)) {
+            // 开始时间等于结束时间,表示全天
+            return true;
+        }
+        if (startTime.isBefore(endTime)) {
+            // 正常情况:开始时间在结束时间之前(同一天内)
+            return !checkTime.isBefore(startTime) && !checkTime.isAfter(endTime);
+        } else {
+            // 跨天情况:开始时间在结束时间之后(如 22:00 - 06:00)
+            return !checkTime.isBefore(startTime) || !checkTime.isAfter(endTime);
+        }
+    }
+
+}
+
+
+

+ 47 - 0
src/main/java/com/ydtech/modules/admin/utils/WorkdayUtils.java

@@ -0,0 +1,47 @@
+package com.ydtech.modules.admin.utils;
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+
+/**
+ * 工作日工具类
+ * 默认规则:周一至周五为工作日,周六周日为休息日
+ */
+public class WorkdayUtils {
+
+    /**
+     * 判断指定日期是否为工作日
+     * @param date 日期
+     * @return true-工作日, false-休息日
+     */
+    public static boolean isWorkday(LocalDate date) {
+        DayOfWeek dayOfWeek = date.getDayOfWeek();
+        return dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY;
+    }
+
+    /**
+     * 判断指定日期是否为休息日
+     * @param date 日期
+     * @return true-休息日, false-工作日
+     */
+    public static boolean isWeekend(LocalDate date) {
+        return !isWorkday(date);
+    }
+
+    /**
+     * 判断当前日期是否为工作日
+     * @return true-工作日, false-休息日
+     */
+    public static boolean isTodayWorkday() {
+        return isWorkday(LocalDate.now());
+    }
+
+    /**
+     * 判断当前日期是否为休息日
+     * @return true-休息日, false-工作日
+     */
+    public static boolean isTodayWeekend() {
+        return isWeekend(LocalDate.now());
+    }
+
+
+}