guohongjin
2024-05-01 1901fceb6ddaa56a57f3131191454554c3e77e68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cn.stylefeng.rest.modular.work;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyTestRecordTopicDTO;
import cn.stylefeng.guns.modular.business.entity.MentalAppointment;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.modular.business.service.impl.ImBizService;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 定时任务-心理咨询预约
 */
@Slf4j
@Component
@EnableScheduling
public class MentalAppointmentWork {
 
    @Resource
    private ImBizService imBizService;
 
    @Resource
    private IMentalTestRecordService mentalTestRecordService;
 
    @Resource
    private IMentalAppointmentService mentalAppointmentService;
 
    /**
     * 固定时间延迟 @Scheduled(fixedDelay = 时间间隔(毫秒) )
     *
     * @Scheduled(fixedRate = 1000 * 60)
     * Corn表达式 @Scheduled(cron = Corn表达式)
     * @Scheduled(cron = "0 0/30 * * * *")
     */
    @Resource
    private CacheOperatorApi<List<Object>> roleInfoCacheApi;
//    @Resource
//    private CacheOperatorApi<List<Object>> roleInfoCacheApi1;
    @Resource
    private CustomerService customerService;
    @Resource
    private ICounsellingInfoService counsellingInfoService;
    @Resource
    private ICounsellingOrderService counsellingOrderService;
    @Resource
    private ICounsellingTagService counsellingTagService;
    @Resource
    private ICounsellingSetMealService counsellingSetMealService;
    @Resource
    private ICourseService courseService;
    @Resource
    private ICourseOrderService courseOrderService;
//    @Scheduled(fixedRate = 1000 * 60)
//    public void list1() {
//        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<Course>().eq(Course::getIsDelete,false)
//                .orderByDesc(Course::getSort,Course::getCreateTime);
//            lambdaQueryWrapper.eq(Course::getListingStatus,1);
////        lambdaQueryWrapper.like(StrUtil.isNotBlank(courseRequest.getCourseName()),Course::getCourseName,courseRequest.getCourseName());
//        List<Course> page = this.courseService.list(lambdaQueryWrapper);
//
//        if (CollectionUtil.isNotEmpty(page)){
//            List<CourseResponseDTO> courseResponseDTOList = BeanUtil.copyToList(page,CourseResponseDTO.class, CopyOptions.create());
//            //查询当前人是否购买过课程
//            List<CourseOrder> courseOrderList = this.courseOrderService.list(new LambdaQueryWrapper<CourseOrder>().select(CourseOrder::getCourseId).eq(CourseOrder::getUserId, LoginContext.me().getLoginUser().getUserId()).eq(CourseOrder::getStatusFlag,1));
//            if (CollectionUtil.isNotEmpty(courseOrderList)){
//                List<Long> courseIds = courseOrderList.stream().map(CourseOrder::getCourseId).collect(Collectors.toList());
//                courseResponseDTOList.forEach(course1 -> {
//                    if (courseIds.contains(course1.getId())){
//                        course1.setIsBuy(true);
//                        CourseOrder courseOrder = this.courseOrderService.getOne(new LambdaQueryWrapper<CourseOrder>().select(CourseOrder::getId).eq(CourseOrder::getUserId, LoginContext.me().getLoginUser().getUserId()).eq(CourseOrder::getStatusFlag,1)
//                                .eq(CourseOrder::getCourseId,course1.getId()).orderByAsc(CourseOrder::getPayTime)
//                                .last(" limit 1 "));
//                        if(courseOrder != null){
//                            course1.setCourseOrderId(courseOrder.getId());
//                        }
//                    }else{
//                        course1.setIsBuy(false);
//                    }
//                });
//            }
//            roleInfoCacheApi.put("course", Collections.singletonList(courseResponseDTOList));
//        }
//
//
//
//
//    }
//        @Scheduled(fixedRate = 1000 * 60)
//
//    public void list() {
//        LambdaQueryWrapper<CounsellingInfo> lambdaQueryWrapper = new LambdaQueryWrapper<CounsellingInfo>().eq(CounsellingInfo::getIsDelete,false)
//                .orderByDesc(CounsellingInfo::getSort,CounsellingInfo::getCreateTime).eq(CounsellingInfo::getListingStatus,1);
//        //默认普通查询
////        if (searchType != null  && searchType.intValue() == 1){
//            List<Customer> customerList1 = customerService.getWorkerListByLineStatusAndPost(null, null, PostIdEnum.PO_22.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
//            if (CollectionUtil.isNotEmpty(customerList1)){
//                List<Long> customerIds = customerList1.stream().map(Customer::getCustomerId).collect(Collectors.toList());
//                lambdaQueryWrapper.in(CounsellingInfo::getUserId,customerIds);
//            }
////        }
//        List<CounsellingInfo> page = this.counsellingInfoService.list(lambdaQueryWrapper);
//        if (CollectionUtil.isNotEmpty(page)){
//            List<Long> counseIds = page.stream().map(CounsellingInfo::getId).collect(Collectors.toList());
//            QueryWrapper<CounsellingOrder> orderQueryWrapper = new QueryWrapper<CounsellingOrder>().select("counselling_info_id counsellingInfoId,count(1) num ");
//            //查询是否首次咨询
//            orderQueryWrapper.lambda().eq(CounsellingOrder::getUserId,LoginContext.me().getLoginUser().getUserId()).in(CounsellingOrder::getStatusFlag,1,2).in(CounsellingOrder::getCounsellingInfoId,counseIds)
//                    .eq(CounsellingOrder::getOrderType,1).groupBy(CounsellingOrder::getCounsellingInfoId).having(" num > 0 ");
//            List<Map<String,Object>> mapList = this.counsellingOrderService.listMaps(orderQueryWrapper);
//            Map<Long,Object> fristMap = new HashMap<>();
//            if (CollectionUtil.isNotEmpty(mapList)){
//
//                mapList.stream().forEach(stringObjectMap -> {
//                    fristMap.put(Long.parseLong(stringObjectMap.get("counsellingInfoId").toString()),stringObjectMap.get("num"));
//                });
//            }
//            //查询标签总条数
//            List<CounsellingTag> counsellingTags = this.counsellingTagService.list(new LambdaQueryWrapper<CounsellingTag>()
//                    .select(CounsellingTag::getId,CounsellingTag::getTagName));
//            List<Long> custommerIds = page.stream().map(CounsellingInfo::getUserId).collect(Collectors.toList());
//            //查询客户ids
//            List<Customer> customerList = this.customerService.list(new LambdaQueryWrapper<Customer>().select(Customer::getCustomerId,Customer::getNickName).in(Customer::getCustomerId,custommerIds));
//            //查询套餐最低价
//            List<Map<String,Object>> lowMapList = this.counsellingSetMealService.listMaps(new QueryWrapper<CounsellingSetMeal>().select(" counselling_info_id counsellingInfoId,IFNULL(min(price),0) price ").lambda().in(CounsellingSetMeal::getCounsellingInfoId,counseIds)
//                    .eq(CounsellingSetMeal::getSetMealType,1).eq(CounsellingSetMeal::getIsDelete,0));
//
//            List<CounsellinginfoResponseDTO> counsellinginfoResponseDTOS = BeanUtil.copyToList(page,CounsellinginfoResponseDTO.class, CopyOptions.create());
//            counsellinginfoResponseDTOS.stream().forEach(counsellinginfoResponseDTO -> {
//                if (fristMap.get(counsellinginfoResponseDTO.getId()) != null){
//                    counsellinginfoResponseDTO.setIsFirstBuy(true);
//                }
//                if (StrUtil.isNotBlank(counsellinginfoResponseDTO.getCounsellingTagIds())){
//                    List<String> counsellingTagList = Arrays.asList(counsellinginfoResponseDTO.getCounsellingTagIds().split(","));
//                    String tagNames = counsellingTags.stream().filter(cou -> counsellingTagList.contains(cou.getId().toString())).map(CounsellingTag::getTagName).collect(Collectors.joining(","));
//                    //获取课程标签名称
////                    LambdaQueryWrapper<CounsellingTag> counsellingTagLambdaQueryWrapper = new QueryWrapper<CounsellingTag>().select(" GROUP_CONCAT(tag_name)  tagName ").lambda();
////                    Map<String,Object> map = counsellingTagService.getMap(counsellingTagLambdaQueryWrapper.in(CounsellingTag::getId,counsellinginfoResponseDTO.getCounsellingTagIds().split(",")));
////                    if (ObjectUtil.isNotEmpty(map)){
//                    counsellinginfoResponseDTO.setCounsellingTagNames(tagNames);
////                    }
//                }
//                counsellinginfoResponseDTO.setPersonalProfile(null);
//
//                counsellinginfoResponseDTO.setNikeName(customerList.stream().filter(cus -> counsellinginfoResponseDTO.getUserId().longValue() == cus.getCustomerId().longValue()).findFirst().get().getNickName());
//
////                BigDecimal lowPrice = this.counsellingSetMealService.getObj(new QueryWrapper<CounsellingSetMeal>().select(" counselling_info_id counsellingInfoId,IFNULL(min(price),0) price ").lambda().eq(CounsellingSetMeal::getCounsellingInfoId,counsellinginfoResponseDTO.getId())
////                        .eq(CounsellingSetMeal::getSetMealType,1).eq(CounsellingSetMeal::getIsDelete,0),Convert::toBigDecimal);
//
//
//                if (CollectionUtil.isNotEmpty(lowMapList)){
//                    lowMapList.stream().forEach(stringObjectMap -> {
//                        if (stringObjectMap.get("counsellingInfoId") != null){
//                            counsellinginfoResponseDTO.setLowPrice(new BigDecimal(stringObjectMap.get("price").toString()));
//                        }
//                    });
//                    if (counsellinginfoResponseDTO.getLowPrice() == null){
//                        counsellinginfoResponseDTO.setLowPrice(new BigDecimal(0));
//                    }
//                }
//
//
//            });
//            roleInfoCacheApi.put("customer", Collections.singletonList(counsellinginfoResponseDTOS));
//        }
//    }
    @Scheduled(fixedRate = 1000 * 30)
    public void autoStartMentalAppointment() {
        // 当前时间
        DateTime dateTime = DateUtil.date();
        // 待开始预约单
        List<MentalAppointment> mentalAppointmentList = mentalAppointmentService.list(
                Wrappers.<MentalAppointment>lambdaUpdate()
                        .eq(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.WAIT_SERVICE.getCode())
                        .apply("DATE_ADD(appointment_day, INTERVAL TIME_TO_SEC(begin_time_point) SECOND) < NOW()")
//                        .le(MentalAppointment::getAppointmentDay, dateTime.toDateStr())
//                        .le(MentalAppointment::getBeginTimePoint, DateUtil.format(dateTime, "HH:mm"))
        );
 
        // 我的测试(含题库信息)
        List<MentalTestMyTestRecordTopicDTO> myTestRecordTopicList = new ArrayList<>();
        if (CollUtil.isNotEmpty(mentalAppointmentList)) {
            myTestRecordTopicList = mentalTestRecordService.myTestRecordTopicByMentalAppointmentId(null, mentalAppointmentList.stream().map(MentalAppointment::getId).collect(Collectors.toList()));
        }
 
        for (MentalAppointment o : mentalAppointmentList) {
            if (o.getType()==null) {
                MentalTestMyTestRecordTopicDTO mentalTestMyTestRecordTopicDTO = myTestRecordTopicList.stream().filter(rt -> rt.getMentalAppointmentId().equals(o.getId())).findFirst().orElse(new MentalTestMyTestRecordTopicDTO());
 
                // 发送IM消息
                ImPushDataDTO pushData = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT.getCode())
                        .title(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT.getName())
                        .content("做了"
                                + ObjUtil.defaultIfEmpty(mentalTestMyTestRecordTopicDTO.getTitle(), "心理测试题")
                                + ",帮我分析一下!")
                        .objId(ObjUtil.toString(o.getId()))
                        .data1(ObjUtil.toString(o.getUserId()))
                        .data2(ObjUtil.toString(o.getWorkerId()))
                        .data3(ObjUtil.toString(mentalTestMyTestRecordTopicDTO.getId()))
                        .build();
                imBizService.messageSendPrivate(
                        ObjUtil.toString(o.getUserId()),
                        new String[]{ObjUtil.toString(o.getWorkerId())},
                        pushData
                );
            }
 
            // 开始预约咨询
            mentalAppointmentService.update(
                    Wrappers.<MentalAppointment>lambdaUpdate()
                            .eq(MentalAppointment::getId, o.getId())
                            .set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.IN_SERVICE.getCode())
                            .set(MentalAppointment::getConsultStatus, MentalAppointmentConsultStatusEnum.IN_CONSULT.getCode())
            );
            log.info("开始预约咨询: {}", JSONUtil.toJsonStr(o));
        }
    }
 
//    @Scheduled(fixedRate = 1000 * 30)
//    public void autoStartMentalAppointment1() {
//        // 当前时间
//        DateTime dateTime = DateUtil.date();
//        // 待开始预约单
//        List<MentalAppointment> mentalAppointmentList = mentalAppointmentService.list(
//                Wrappers.<MentalAppointment>lambdaUpdate()
//                        .eq(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.WAIT_SERVICE.getCode())
//                        .eq(MentalAppointment::getType,"1")
//                        .apply("DATE_ADD(appointment_day, INTERVAL TIME_TO_SEC(begin_time_point) SECOND) < NOW()")
////                        .le(MentalAppointment::getAppointmentDay, dateTime.toDateStr())
////                        .le(MentalAppointment::getBeginTimePoint, DateUtil.format(dateTime, "HH:mm"))
//        );
//
//        for (MentalAppointment o : mentalAppointmentList) {
//            // 开始预约咨询
//            mentalAppointmentService.update(
//                    Wrappers.<MentalAppointment>lambdaUpdate()
//                            .eq(MentalAppointment::getId, o.getId())
//                            .set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.IN_SERVICE.getCode())
//                            .set(MentalAppointment::getConsultStatus, MentalAppointmentConsultStatusEnum.IN_CONSULT.getCode())
//            );
//            log.info("开始预约咨询: {}", JSONUtil.toJsonStr(o));
//        }
//    }
 
    @Scheduled(fixedRate = 1000 * 60)
    public void autoEndMentalAppointment() {
        // 当前时间
        DateTime dateTime = DateUtil.date();
 
        // 服务中预约单
        List<MentalAppointment> mentalAppointmentList = mentalAppointmentService.list(
                Wrappers.<MentalAppointment>lambdaUpdate()
                        .eq(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.IN_SERVICE.getCode())
                        .le(MentalAppointment::getAppointmentDay, dateTime.toDateStr())
                        .le(MentalAppointment::getEndTimePoint, DateUtil.format(dateTime, "HH:mm"))
        );
        for (MentalAppointment o : mentalAppointmentList) {
            // 结束咨询
            mentalAppointmentService.update(
                    Wrappers.<MentalAppointment>lambdaUpdate()
                            .eq(MentalAppointment::getId, o.getId())
                            .set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.DONE.getCode())
                            .set(MentalAppointment::getConsultStatus, MentalAppointmentConsultStatusEnum.DONE.getCode())
            );
            log.info("结束咨询: {}", JSONUtil.toJsonStr(o));
 
            try {
                // 推送消息内容
                String pushContent = "1v1咨询结束";
 
                // IM推送数据json
                ImPushDataDTO pushData_u = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.C_TO_C_TIP_STOP_IM.getCode())
                        .objId(ObjUtil.toString(o.getId()))
                        .title("通知")
                        .content(pushContent)
                        .data1(ObjUtil.toString(o.getWorkerId()))
                        .data2(ObjUtil.toString(o.getUserId()))
                        .build();
                // 通知性格分析师
                imBizService.messageSendSystem(o.getWorkerId()+"", new String[]{o.getUserId()+""}, pushData_u, null, null, false);
 
                // IM推送数据json
                ImPushDataDTO pushData = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.C_TO_C_TIP_STOP_IM.getCode())
                        .objId(ObjUtil.toString(o.getId()))
                        .title("通知")
                        .content(pushContent)
                        .data1(ObjUtil.toString(o.getUserId()))
                        .data2(ObjUtil.toString(o.getWorkerId()))
                        .build();
                // 通知性格分析师
                imBizService.messageSendSystem(o.getUserId()+"", new String[]{o.getWorkerId()+""}, pushData, ImUserTypeEnum.WORKER, PostIdEnum.PO_31, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
    }
}