luodangjia
2024-04-16 0a23c31baef81d149563da16486f3f7d8d9f33e8
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
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.CourseOrderResponseDTO;
import cn.stylefeng.guns.modular.business.dto.CourseResponseDTO;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.request.CourseOrderRequest;
import cn.stylefeng.guns.modular.business.dto.request.CoursePlaceOrderRequest;
import cn.stylefeng.guns.modular.business.dto.request.OrderPayRequest;
import cn.stylefeng.guns.modular.business.entity.Course;
import cn.stylefeng.guns.modular.business.entity.CourseChapter;
import cn.stylefeng.guns.modular.business.entity.CourseOrder;
import cn.stylefeng.guns.modular.business.entity.CouserChapterLearningRate;
import cn.stylefeng.guns.modular.business.mapper.CourseOrderMapper;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.utils.GeneralUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.*;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 课程订单信息 服务实现类
 * </p>
 *
 * @author guohongjin
 * @since 2024-01-02
 */
@Service
public class CourseOrderServiceImpl extends ServiceImpl<CourseOrderMapper, CourseOrder> implements ICourseOrderService {
 
    @Autowired
    private ICourseChapterService courseChapterService;
 
    @Autowired
    private ICouserChapterLearningRateService couserChapterLearningRateService;
 
    @Autowired
    private ICourseService courseService;
 
    @Autowired
    private CustomerService customerService;
 
    @Autowired
    private IImGroupService iImGroupService;
 
    @Resource
    private ImBizService imBizService;
 
 
    @Override
    public Page<CourseOrderResponseDTO> findCounseOrderPage(Page<CourseOrderResponseDTO> page, CourseOrderRequest courseOrderRequest) {
        return this.baseMapper.findCounseOrderPage(page, courseOrderRequest);
    }
 
    @Override
    public CourseOrderResponseDTO getCourseChapterInfoByOrderId(Long orderId) {
        CourseOrder byId = this.getById(orderId);
        CourseOrderResponseDTO courseOrderResponseDTO = BeanUtil.copyProperties(byId,CourseOrderResponseDTO.class);
        //查询章节信息
        List<CourseChapter> courseChapterList = this.courseChapterService.getAllByCourseId(courseOrderResponseDTO.getCourseId());
        //查询章节学习进度
        List<CouserChapterLearningRate> couserChapterLearningRates = this.couserChapterLearningRateService.list(new LambdaQueryWrapper<CouserChapterLearningRate>().eq(CouserChapterLearningRate::getCourseId,courseOrderResponseDTO.getCourseId())
                .eq(CouserChapterLearningRate::getCourseOrderId,orderId));
        Map<Long,CouserChapterLearningRate> learnMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(couserChapterLearningRates)){
            learnMap = couserChapterLearningRates.stream().collect(Collectors.toMap(CouserChapterLearningRate::getCourseChapterId, Function.identity()));
            courseOrderResponseDTO.setStudyCourseChapter(learnMap.size());
        }
        //查询总课程章节
        long totalChapter = this.courseChapterService.count(new LambdaQueryWrapper<CourseChapter>().eq(CourseChapter::getCourseId,courseOrderResponseDTO.getCourseId())
                .eq(CourseChapter::getIsDelete,false).ne(CourseChapter::getParentId,0).isNotNull(CourseChapter::getMinuteNum));
        courseOrderResponseDTO.setTotalCourseChapter(Integer.parseInt(totalChapter+""));
        //处理课程学习进度
        List<CourseResponseDTO.CourseChapterResponseDTO> courseChapterResponseDTOList = new ArrayList<>();
        courseChapterResponseDTOList = turnChapterResponseDTO(courseChapterResponseDTOList,courseChapterList,learnMap);
        courseOrderResponseDTO.setCourseChapterResponseDTOList(courseChapterResponseDTOList);
 
        if (byId.getUserId()!=null) {
            Customer byId1 = customerService.getById(byId.getUserId());
            courseOrderResponseDTO.setUserName(byId1.getNickName());
        }
        if (byId.getCourseUserId()!=null) {
            Customer byId1 = customerService.getById(byId.getCourseUserId());
            courseOrderResponseDTO.setCourseName(byId1.getNickName());
        }
        if (byId.getTutoringUserId()!=null) {
            Customer byId1 = customerService.getById(byId.getTutoringUserId());
            courseOrderResponseDTO.setTutoringUserName(byId1.getNickName());
        }
 
 
        return courseOrderResponseDTO;
    }
 
 
 
    private List<CourseResponseDTO.CourseChapterResponseDTO> turnChapterResponseDTO(List<CourseResponseDTO.CourseChapterResponseDTO> courseChapterResponseDTOList,List<CourseChapter> courseChapterList,
                                                                                    Map<Long,CouserChapterLearningRate> learnMap){
        if (CollectionUtil.isNotEmpty(courseChapterList)){
            courseChapterResponseDTOList.addAll(BeanUtil.copyToList(courseChapterList,CourseResponseDTO.CourseChapterResponseDTO.class));
            courseChapterResponseDTOList.forEach(courseChapterResponseDTO -> {
                if (learnMap.get(courseChapterResponseDTO.getId()) != null){
                    CouserChapterLearningRate couserChapterLearningRate = learnMap.get(courseChapterResponseDTO.getId());
                    courseChapterResponseDTO.setLearnedTime(couserChapterLearningRate.getLearnedTime());
                    courseChapterResponseDTO.setTotalTime(couserChapterLearningRate.getTotalTime());
                    courseChapterResponseDTO.setLastStudyTime(couserChapterLearningRate.getLastStudyTime());
                }
            });
            courseChapterList.stream().forEach(courseChapter -> {
                if (CollectionUtil.isNotEmpty(courseChapter.getChildList())){
                    List<CourseResponseDTO.CourseChapterResponseDTO> courseChapterResponseDTOS = new ArrayList<>();
                    this.turnChapterResponseDTO(courseChapterResponseDTOS,courseChapter.getChildList(),learnMap);
                    courseChapterResponseDTOList.forEach(courseChapterResponseDTO -> {
                        if (courseChapterResponseDTO.getId().longValue() == courseChapter.getId().longValue()){
                            courseChapterResponseDTO.setChildCourseChapterResponseDTOList(courseChapterResponseDTOS);
                        }
                    });
                }
            });
            return courseChapterResponseDTOList;
        }
        return null;
    }
 
    @Override
    public CourseOrder createCourseOrder(CoursePlaceOrderRequest coursePlaceOrderRequest) {
        Course course = this.courseService.getById(coursePlaceOrderRequest.getCourseId());
        CourseOrder courseOrder = new CourseOrder();
        courseOrder.setUserId(coursePlaceOrderRequest.getUserId());
        courseOrder.setCourseId(coursePlaceOrderRequest.getCourseId());
        courseOrder.setCreateUser(LoginContext.me().getLoginUser().getUserId());
        //生成单号
        courseOrder.setOrderNo(GeneralUtil.generateOrderNo(OrderTypeEnum.COURSE_ORDER));
 
        courseOrder.setCourseJson(JSONUtil.toJsonStr(course));
        courseOrder.setTotalTime(course.getTotalTime());
        courseOrder.setLearnedTime(0l);
        courseOrder.setOrderAmount(course.getPrice());
        courseOrder.setPayAmount(courseOrder.getOrderAmount());
        Customer customerOld = this.customerService.getById(coursePlaceOrderRequest.getUserId());
        //是否是后台创建订单
 
            if (customerOld.getClassWorkerId() != null){
                courseOrder.setCourseUserId(customerOld.getClassWorkerId());
            }else{
                //幸福顾问
                Customer worker = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_11.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                if (worker != null){
                    courseOrder.setCourseUserId(worker.getCustomerId());
                }else{
                    Customer worker3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_11.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (worker3 != null){
                        courseOrder.setCourseUserId(worker3.getCustomerId());
                    }
                }
            }
 
            //课程辅导
            Customer worker2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_12.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
            if (worker2 != null){
                courseOrder.setTutoringUserId(worker2.getCustomerId());
            }else{
                Customer worker3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_12.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                if (worker3 != null){
                    courseOrder.setTutoringUserId(worker3.getCustomerId());
                }
            }
        if (coursePlaceOrderRequest.getIsBack()){
            Customer customer = this.customerService.getById(coursePlaceOrderRequest.getUserId());
            courseOrder.setPhone(customer.getTelephone());
 
            courseOrder.setPayTime(new Date());
            courseOrder.setStatusFlag(1);
            Date nowDate = new Date();
            courseOrder.setEffectiveBeginTime(nowDate);
            courseOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(nowDate,course.getCourseValidity())));
            this.save(courseOrder);
            //生成群聊
            iImGroupService.createGroup(1,null,null,null,courseOrder.getCourseUserId(),courseOrder.getTutoringUserId(),courseOrder.getUserId(),courseOrder.getId());
            UpdateWrapper<Course> courseUpdateWrapper = new UpdateWrapper<Course>().setSql(" buy_num = buy_num+1 ");
            //添加购买人数
            this.courseService.update(courseUpdateWrapper.lambda().eq(Course::getId,courseOrder.getCourseId()));
        }else{
            courseOrder.setPhone(coursePlaceOrderRequest.getPhone());
            courseOrder.setPayType(coursePlaceOrderRequest.getPayType());
            courseOrder.setStatusFlag(0);
            this.save(courseOrder);
        }
 
        if (customerOld.getClassWorkerId() == null && courseOrder.getCourseUserId() != null){
            customerOld.setClassWorkerId(courseOrder.getCourseUserId());
            this.customerService.updateCustomerRemoveCache(customerOld);
//            if (!coursePlaceOrderRequest.getIsBack()){
//
//                // 推送消息内容
//                String pushContent = "用户:"+customerOld.getNickName()+",有未支付的课程订单,";
//                // IM推送数据json
//                ImPushDataDTO pushData = ImPushDataDTO.builder()
//                        .type(ImPushTypeEnum.S_TO_W_TIP_COURSE_NOT_PAY.getCode())
//                        .objId(ObjUtil.toString(courseOrder.getId()))
//                        .title("通知")
//                        .content(pushContent)
////                    .extra("去查看。")
//                        .build();
//                // 发送预约提示
//                imBizService.messageSendSystem(courseOrder.getUserId()+"", new String[]{customerOld.getClassWorkerId()+""}, pushData, ImUserTypeEnum.WORKER, PostIdEnum.PO_11, true);
//
//            }
        }
 
        return courseOrder;
    }
 
    @Override
    public void payCourseOrder(OrderPayRequest orderPayRequest) {
        CourseOrder courseOrder = this.getOne(new LambdaQueryWrapper<CourseOrder>().eq(CourseOrder::getOrderNo,orderPayRequest.getOrderNo()));
        if (courseOrder == null){
            throw new ServiceException("订单号不存在!");
        }
        Course course = this.courseService.getById(courseOrder.getCourseId());
        courseOrder.setTransactionNo(orderPayRequest.getTransactionNo());
        courseOrder.setStatusFlag(1);
        courseOrder.setPayTime(new Date());
        courseOrder.setPayType(orderPayRequest.getPayType());
        Customer customerOld = this.customerService.getById(courseOrder.getUserId());
        if (courseOrder.getCourseUserId() == null){
            //幸福顾问
            if (customerOld.getClassWorkerId() != null){
                courseOrder.setCourseUserId(customerOld.getClassWorkerId());
            }else{
                //幸福顾问
                Customer worker = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_11.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                if (worker != null){
                    courseOrder.setCourseUserId(worker.getCustomerId());
                }else{
                    Customer worker3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_11.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (worker3 != null){
                        courseOrder.setCourseUserId(worker3.getCustomerId());
                    }
                }
            }
        }
        if (courseOrder.getTutoringUserId() == null) {
            //课程辅导
            Customer worker2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_12.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
            if (worker2 != null) {
                courseOrder.setTutoringUserId(worker2.getCustomerId());
            } else {
                Customer worker3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_12.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                if (worker3 != null) {
                    courseOrder.setTutoringUserId(worker3.getCustomerId());
                }
            }
        }
        Date nowDate = new Date();
        courseOrder.setEffectiveBeginTime(nowDate);
        courseOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(nowDate,course.getCourseValidity())));
        //生成群聊
        iImGroupService.createGroup(1,null,null,null,courseOrder.getCourseUserId(),courseOrder.getTutoringUserId(),courseOrder.getUserId(),courseOrder.getId());
        UpdateWrapper<Course> courseUpdateWrapper = new UpdateWrapper<Course>().setSql(" buy_num = buy_num+1 ");
        //添加购买人数
        this.courseService.update(courseUpdateWrapper.lambda().eq(Course::getId,courseOrder.getCourseId()));
        this.updateById(courseOrder);
        if (customerOld.getClassWorkerId() == null && courseOrder.getCourseUserId() != null){
            customerOld.setClassWorkerId(courseOrder.getCourseUserId());
            this.customerService.updateCustomerRemoveCache(customerOld);
        }
    }
 
    @Override
    public CourseOrder getCourseOrderByNo(String orderNo) {
        return this.getOne(
                Wrappers.<CourseOrder>lambdaQuery()
                        .eq(CourseOrder::getOrderNo, orderNo)
                        .last("limit 1")
        );
 
    }
}