yanghb
2024-04-15 6bc25b33e90d45904d1843e927fa709dfeb51d7f
rest/src/main/java/cn/stylefeng/rest/modular/work/MentalAppointmentWork.java
@@ -1,26 +1,42 @@
package cn.stylefeng.rest.modular.work;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.CounsellinginfoResponseDTO;
import cn.stylefeng.guns.modular.business.dto.CourseResponseDTO;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyTestTopicDTO;
import cn.stylefeng.guns.modular.business.entity.MentalAppointment;
import cn.stylefeng.guns.modular.business.service.IMentalAppointmentService;
import cn.stylefeng.guns.modular.business.service.IMentalTestRecordService;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.modular.business.service.impl.ImBizService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.rule.enums.*;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -47,7 +63,136 @@
     * Corn表达式 @Scheduled(cron = Corn表达式)
     * @Scheduled(cron = "0 0/30 * * * *")
     */
    @Resource
    private CacheOperatorApi<List<CounsellinginfoResponseDTO>> roleInfoCacheApi;
    @Resource
    private CacheOperatorApi<List<CourseResponseDTO>> 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);
                    }
                });
            }
            roleInfoCacheApi1.put("course",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",counsellinginfoResponseDTOS);
        }
    }
    @Scheduled(fixedRate = 1000 * 30)
    public void autoStartMentalAppointment() {
        // 当前时间