package cn.stylefeng.guns.modular.business.service.impl;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.http.HttpUtil;
|
import cn.stylefeng.guns.modular.business.dto.MentalAppointmentPageDTO;
|
import cn.stylefeng.guns.modular.business.dto.MentalTestMyMentalAppointmentDTO;
|
import cn.stylefeng.guns.modular.business.dto.UserMentalAppointmentPageResponseDTO;
|
import cn.stylefeng.guns.modular.business.entity.MentalAnalysisSpecialTimeConfig;
|
import cn.stylefeng.guns.modular.business.entity.MentalAnalysisTimeConfig;
|
import cn.stylefeng.guns.modular.business.entity.MentalAppointment;
|
import cn.stylefeng.guns.modular.business.entity.OrderConsultOne;
|
import cn.stylefeng.guns.modular.business.mapper.MentalAppointmentMapper;
|
import cn.stylefeng.guns.modular.business.service.IMentalAnalysisSpecialTimeConfigService;
|
import cn.stylefeng.guns.modular.business.service.IMentalAnalysisTimeConfigService;
|
import cn.stylefeng.guns.modular.business.service.IMentalAppointmentService;
|
import cn.stylefeng.guns.modular.business.service.IOrderConsultOneService;
|
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
|
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
|
import cn.stylefeng.roses.kernel.rule.enums.*;
|
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 性格分析预约 服务实现类
|
* </p>
|
*
|
* @author goupan
|
* @since 2024-01-01
|
*/
|
@Slf4j
|
@Service
|
public class MentalAppointmentServiceImpl extends ServiceImpl<MentalAppointmentMapper, MentalAppointment> implements IMentalAppointmentService {
|
|
@Value("${refund.alipay-url}")
|
private String refundAlipayUrl;
|
@Value("${refund.wxpay-url}")
|
private String refundWxpayUrl;
|
|
@Resource
|
private CustomerService customerService;
|
|
@Resource
|
private IMentalAppointmentService mentalAppointmentService;
|
|
@Resource
|
private IMentalAnalysisTimeConfigService mentalAnalysisTimeConfigService;
|
|
@Resource
|
private IMentalAnalysisSpecialTimeConfigService mentalAnalysisSpecialTimeConfigService;
|
|
@Resource
|
private IOrderConsultOneService orderConsultOneService;
|
|
@Override
|
public List<MentalTestMyMentalAppointmentDTO> myMentalAppointment(Long userId, String consultantName, List<Integer> statusFlagList) {
|
return this.baseMapper.myMentalAppointment(userId, consultantName, statusFlagList);
|
}
|
|
@Override
|
public Page<UserMentalAppointmentPageResponseDTO> userMentalAppointmentPage(Page<Object> page, Long userId, String name, String telephone, Integer statusFlag) {
|
return this.baseMapper.userMentalAppointmentPage(page, userId, name, telephone, statusFlag);
|
}
|
|
@Override
|
public Page<MentalAppointmentPageDTO> getPage(Page<Object> page, Long counsellingInfoId, String workerNickName, String userNickName, String userTelephone, Integer statusFlag, List<Integer> statusFlagList) {
|
return this.baseMapper.mentalAnalysisTimeConfigSchedule(page, null, null, counsellingInfoId, workerNickName, userNickName, userTelephone, statusFlag, statusFlagList);
|
}
|
|
@Override
|
public List<MentalAppointmentPageDTO> mentalAnalysisTimeConfigSchedule(String searchBeginTime, String searchEndTime, Long counsellingInfoId, String workerNickName, String userNickName, String userTelephone, Integer statusFlag, List<Integer> statusFlagList) {
|
return this.baseMapper.mentalAnalysisTimeConfigSchedule(searchBeginTime, searchEndTime, counsellingInfoId, workerNickName, userNickName, userTelephone, statusFlag, statusFlagList);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public boolean mentalAppointmentCancel(Long id) {
|
// 获取当前登录用户信息
|
LoginUser loginUser = LoginContext.me().getLoginUser();
|
Long userId = loginUser.getUserId();
|
|
// 性格分析预约
|
MentalAppointment mentalAppointment = this.getById(id);
|
Assert.isTrue(userId.equals(mentalAppointment.getUserId()), "预约用户信息错误");
|
|
// 服务中可取消
|
if (mentalAppointment != null
|
&& MentalAppointmentStatusEnum.WAIT_SERVICE.getCode().equals(mentalAppointment.getStatusFlag())) {
|
// 1V1咨询订单信息
|
OrderConsultOne orderConsultOne = orderConsultOneService.getOne(
|
Wrappers.<OrderConsultOne>lambdaQuery()
|
.eq(OrderConsultOne::getMentalAppointmentId, id)
|
.gt(OrderConsultOne::getPayAmount, 0)
|
.isNotNull(OrderConsultOne::getPayType)
|
);
|
|
boolean update = orderConsultOneService.update(
|
Wrappers.<OrderConsultOne>lambdaUpdate()
|
.set(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.CANCEL.getCode())
|
.set(OrderConsultOne::getRefundAmount, orderConsultOne.getPayAmount())
|
.set(OrderConsultOne::getRefundTime, new Date())
|
.set(OrderConsultOne::getCancelTime, new Date())
|
.set(OrderConsultOne::getCancelRemark, "申请取消")
|
.eq(OrderConsultOne::getId, orderConsultOne.getId())
|
.eq(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
|
);
|
if (update) {
|
this.update(
|
Wrappers.<MentalAppointment>lambdaUpdate()
|
.set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.CANCEL.getCode())
|
.eq(MentalAppointment::getId, mentalAppointment.getId())
|
);
|
}
|
Assert.isTrue(update, "订单取消失败");
|
|
Boolean isRefund = true;
|
if (PayTypeEnum.ALIPAY.getCode().toString().equals(orderConsultOne.getPayType())) {
|
Map<String, Object> map = new HashMap(2);
|
map.put("outTradeNo", orderConsultOne.getOrderNo());
|
map.put("tradeNo", orderConsultOne.getTransactionNo());
|
String result = HttpUtil.createGet(refundAlipayUrl).form(map).execute().body();
|
log.info("1v1咨询订单退款支付宝:{},{},{}", orderConsultOne.getOrderNo(), orderConsultOne.getTransactionNo(), result);
|
isRefund = BooleanUtil.toBoolean(result);
|
} else if (PayTypeEnum.WXPAY.getCode().toString().equals(orderConsultOne.getPayType())) {
|
// TODO: 1v1咨询订单退款微信
|
String result = "";
|
log.info("1v1咨询订单退款微信:{},{},{}", orderConsultOne.getOrderNo(), orderConsultOne.getTransactionNo(), result);
|
isRefund = BooleanUtil.toBoolean(result);
|
}
|
Assert.isTrue(isRefund, "退款失败");
|
return true;
|
}
|
|
return false;
|
}
|
|
@Override
|
public Long assignMentalAppointmentWorkerId(Date appointmentDay, String beginTimePoint, String endTimePoint) {
|
// 预约时间日期
|
String appointmentDayYmd = DateUtil.format(appointmentDay, DatePattern.NORM_DATE_PATTERN);
|
|
// 获取预约时段有服务的分析师(不可预约)
|
List<MentalAppointment> sameTimeAppointmentList = mentalAppointmentService.list(
|
Wrappers.<MentalAppointment>lambdaQuery()
|
.isNull(MentalAppointment::getType)
|
.eq(MentalAppointment::getAppointmentDay, appointmentDayYmd)
|
.eq(MentalAppointment::getBeginTimePoint, beginTimePoint)
|
.eq(MentalAppointment::getEndTimePoint, endTimePoint).ne(MentalAppointment::getType,1)
|
.in(MentalAppointment::getStatusFlag, Arrays.asList(
|
MentalAppointmentStatusEnum.WAIT_PAY.getCode(),
|
MentalAppointmentStatusEnum.WAIT_SERVICE.getCode(),
|
MentalAppointmentStatusEnum.IN_SERVICE.getCode()
|
))
|
);
|
List<Long> neWorkerIdList = sameTimeAppointmentList.stream().map(MentalAppointment::getWorkerId).collect(Collectors.toList());
|
|
// 查询特殊时间段,取消当天的分析师(不可预约)
|
List<Long> isCancelDaySpecialTimeConfigList = mentalAnalysisSpecialTimeConfigService.listObjs(
|
Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
|
.select(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId)
|
.eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
|
.eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 1)
|
, obj -> {
|
Long counsellingInfoId = (Long) obj;
|
return counsellingInfoId;
|
}
|
);
|
neWorkerIdList.addAll(isCancelDaySpecialTimeConfigList);
|
neWorkerIdList.stream().distinct();
|
|
// 获取当前星期
|
Integer weekDay = DateUtil.dayOfWeekEnum(appointmentDay).getIso8601Value();
|
// 预约时间段配置匹配的性格分析师
|
List<MentalAnalysisTimeConfig> eqWorkerTimeConfigList = mentalAnalysisTimeConfigService.getWorkerListByAppointmentTime(CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), weekDay, appointmentDayYmd, beginTimePoint, endTimePoint);
|
// 分析师ID(预约时间段配置匹配)
|
List<Long> eqWorkerIdByTimeConfigList = eqWorkerTimeConfigList.isEmpty() ? Collections.singletonList(0L) :
|
eqWorkerTimeConfigList.stream()
|
.map(MentalAnalysisTimeConfig::getCounsellingInfoId)
|
.distinct()
|
.collect(Collectors.toList());
|
|
// 查询有特殊时间段配置,分析师(可预约)
|
List<MentalAnalysisSpecialTimeConfig> specialTimeConfigList = mentalAnalysisSpecialTimeConfigService.list(
|
Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
|
.in(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId, eqWorkerIdByTimeConfigList)
|
.eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
|
.eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 0)
|
);
|
|
// 根据预约时间段、特殊时间段(优先)判断是否符合预约时间
|
List<Long> eqWorkerIdList = eqWorkerTimeConfigList.stream()
|
.filter(wtc -> {
|
// 特殊时间段(优先),存在判断是否符合时间段,不存在直接过
|
boolean isPresentSpecialTimeConfig = specialTimeConfigList.stream().filter(stc -> stc.getCounsellingInfoId().equals(wtc.getCounsellingInfoId())).findFirst().isPresent();
|
if (isPresentSpecialTimeConfig) {
|
// 如果存在特殊时间段,判断是否有匹配预约时间段的特殊时段设置,未设置(不可预约)
|
return specialTimeConfigList.stream().filter(stc -> stc.getSpecialBeginTimePoint().equals(beginTimePoint)
|
&& stc.getSpecialEndTimePoint().equals(endTimePoint)
|
)
|
.findFirst()
|
.isPresent();
|
} else {
|
return true;
|
}
|
})
|
.map(MentalAnalysisTimeConfig::getCounsellingInfoId)
|
.collect(Collectors.toList());
|
|
// 特殊时间段符合的分析师(可预约)
|
List<MentalAnalysisSpecialTimeConfig> specialTimePointList = mentalAnalysisSpecialTimeConfigService.list(
|
Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
|
.select(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId)
|
.eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
|
.eq(MentalAnalysisSpecialTimeConfig::getSpecialBeginTimePoint, beginTimePoint)
|
.eq(MentalAnalysisSpecialTimeConfig::getSpecialEndTimePoint, endTimePoint)
|
.eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 0)
|
);
|
// 特殊时间段符合的分析师ID
|
List<Long> specialTimePointWorkerIdList = specialTimePointList.stream().map(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId).collect(Collectors.toList());
|
// 特殊时间段的补充
|
eqWorkerIdList.addAll(specialTimePointWorkerIdList);
|
|
if (CollUtil.isEmpty(eqWorkerIdList)) {
|
// 没有分配
|
eqWorkerIdList = Collections.singletonList(0L);
|
} else {
|
// 去重
|
eqWorkerIdList.stream().distinct();
|
}
|
|
// 分配性格分析师
|
Long consultWorkerId = customerService.randomWorkerIdByLineStatusAndPostNeWorkerId(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_31.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), eqWorkerIdList, neWorkerIdList);
|
if (consultWorkerId == null) {
|
consultWorkerId = customerService.randomWorkerIdByLineStatusAndPostNeWorkerId(null, null, PostIdEnum.PO_31.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), eqWorkerIdList, neWorkerIdList);
|
}
|
|
return consultWorkerId;
|
}
|
}
|