package com.dsh.account.service.impl;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.account.dto.*;
import com.dsh.account.entity.*;
import com.dsh.account.feignclient.activity.IntroduceRewardsClient;
import com.dsh.account.feignclient.activity.UserConponClient;
import com.dsh.account.feignclient.activity.model.IntrduceOfUserRequest;
import com.dsh.account.feignclient.competition.DeductionCompetitionsClient;
import com.dsh.account.feignclient.competition.ParticipantClient;
import com.dsh.account.feignclient.competition.model.GetStuSourseList;
import com.dsh.account.feignclient.competition.model.PurchaseRecordVo;
import com.dsh.account.feignclient.competition.model.SaveParticipant;
import com.dsh.account.feignclient.course.*;
import com.dsh.account.feignclient.course.model.*;
import com.dsh.account.feignclient.other.NoticeClient;
import com.dsh.account.feignclient.other.QuestionClient;
import com.dsh.account.feignclient.other.SiteClient;
import com.dsh.account.feignclient.other.StoreClient;
import com.dsh.account.feignclient.other.model.*;
import com.dsh.account.mapper.CoachMapper;
import com.dsh.account.mapper.TAppUserMapper;
import com.dsh.account.mapper.TStudentMapper;
import com.dsh.account.model.vo.classDetails.ClasspaymentRequest;
import com.dsh.account.model.vo.classDetails.StuEditInfoReq;
import com.dsh.account.model.vo.classDetails.classInsVo.ClassDetailsInsVo;
import com.dsh.account.model.vo.classDetails.classInsVo.StuDetailsReq;
import com.dsh.account.model.vo.classDetails.classInsVo.StuListVo;
import com.dsh.account.model.vo.exploreDetail.LonLatRequest;
import com.dsh.account.model.vo.exploreDetail.QuestionIns;
import com.dsh.account.model.vo.exploreDetail.StoreDetailsVo;
import com.dsh.account.model.vo.exploreDetail.StoreOfCourseVo;
import com.dsh.account.model.vo.sourceDetail.CouponStuAvailableVo;
import com.dsh.account.model.vo.sourceDetail.CourseDetailsOfContinuationResp;
import com.dsh.account.model.vo.sourceDetail.RecordTimeRequest;
import com.dsh.account.service.TCourseInfoRecordService;
import com.dsh.account.service.TStudentService;
import com.dsh.account.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 学员信息 服务实现类
*
*
* @author administrator
* @since 2023-06-14
*/
@Service
public class TStudentServiceImpl extends ServiceImpl implements TStudentService {
@Resource
private CourseSessionNameClient sessionNameClient;
@Resource
private DeductionCompetitionsClient dcttClient;
@Resource
private CancelListClient cancelcClient;
@Resource
private IntroduceRewardsClient idrClient;
@Resource
private CourseRecordClient crClient;
@Resource
private CoursePaymentClient couPayClient;
@Resource
private UserConponClient userCClient;
@Resource
private StoreClient storeClient;
@Resource
private NoticeClient noClient;
@Resource
private QuestionClient quesClient;
@Resource
private TAppUserMapper tauMapper;
@Resource
private CoachMapper coachMapper;
@Resource
private ParticipantClient participantClient;
@Autowired
private CoursePackageClient coursePackageClient;
@Autowired
private PayMoneyUtil payMoneyUtil;
@Resource
private SiteClient steClient;
@Resource
private CourseListClient culisClient;
@Autowired
private TCourseInfoRecordService courseInfoRecordService;
@Override
public ResultUtil addStuOfAppUser(StuDetailsReq stu, Integer appUserId) throws Exception {
TStudent student = new TStudent();
if(ToolUtil.isEmpty(stu.getIdCard())){
return ResultUtil.error("请填写身份证号");
}
Boolean aBoolean = JuHeUtil.idcardAuthentication(stu.getIdCard(), stu.getName());
if (!aBoolean) {
return ResultUtil.error("身份证和姓名不匹配");
}
student.setAppUserId(appUserId);
student.setName(stu.getName());
student.setHeadImg(stu.getHeadImg());
student.setPhone(ToolUtil.isNotEmpty(stu.getPhone()) ? stu.getPhone() : "");
student.setSex(stu.getSex());
student.setIdCard(ToolUtil.isNotEmpty(stu.getIdCard()) ? stu.getIdCard() : "");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
student.setBirthday(sdf.parse(stu.getBirthday()));
student.setHeight(stu.getHeight());
student.setWeight(stu.getWeight());
BigDecimal bigDecimal = BigDecimal.valueOf(stu.getWeight());
double v = stu.getHeight() / 100;
double v1 = v * v;
BigDecimal bigDecimal1 = new BigDecimal(v1).setScale(2, RoundingMode.HALF_UP);
double v2 = bigDecimal.doubleValue() / bigDecimal1.doubleValue();
BigDecimal bigDecimal2 = new BigDecimal(v2).setScale(2, RoundingMode.HALF_UP);
student.setBmi(bigDecimal2.doubleValue());
student.setInsertTime(new Date());
student.setState(1);
List tStudents = this.baseMapper.selectList(new LambdaQueryWrapper().eq(TStudent::getAppUserId, appUserId));
if (tStudents.size() > 0) {
student.setIsDefault(2);
} else {
student.setIsDefault(1);
}
this.baseMapper.insert(student);
//同步信息到参赛人员信息中
SaveParticipant saveParticipant = new SaveParticipant();
saveParticipant.setAppUserId(appUserId);
saveParticipant.setName(student.getName());
saveParticipant.setBirthday(student.getBirthday());
saveParticipant.setGender(student.getSex());
saveParticipant.setHeight(student.getHeight().intValue());
saveParticipant.setWeight(student.getWeight());
saveParticipant.setPhone(student.getPhone());
saveParticipant.setIdcard(student.getIdCard());
participantClient.saveParticipant(saveParticipant);
return ResultUtil.success();
}
@Override
public ClassDetailsInsVo querySessionDetailsDt(Integer userIdFormRedis, Integer lessonId, Integer stuId) {
ClassDetailsInsVo insVo = new ClassDetailsInsVo();
Date localMonthStart = DateTimeHelper.getCurrentMouthStart();
Date localMonthEnd = DateTimeHelper.getCurrentMouthEnd();
GetStuSessionList getStuSessionList = new GetStuSessionList();
getStuSessionList.setStartTime(localMonthStart);
getStuSessionList.setEndTime(localMonthEnd);
getStuSessionList.setStuId(stuId);
getStuSessionList.setAppUserId(userIdFormRedis);
List stuSessionList = sessionNameClient.getStuSessionList(getStuSessionList);
TStudent tStudent = this.baseMapper.selectById(stuId);
if (ToolUtil.isNotEmpty(tStudent)) {
insVo.setStuId(tStudent.getId());
insVo.setStuName(tStudent.getName());
insVo.setStuImage(tStudent.getHeadImg());
List purchaseRecordVoList = new ArrayList<>();
GetStuSourseList getStuSourseList = new GetStuSourseList();
getStuSourseList.setStartTime(localMonthStart);
getStuSourseList.setEndTime(localMonthEnd);
getStuSourseList.setAppUserId(userIdFormRedis);
List stuSourseList = dcttClient.getStuSourseList(getStuSourseList);
purchaseRecordVoList.addAll(stuSourseList);
GetStuSessionList sessionList = new GetStuSessionList();
sessionList.setStartTime(localMonthStart);
sessionList.setEndTime(localMonthEnd);
sessionList.setStuId(stuId);
sessionList.setAppUserId(userIdFormRedis);
List cancelCourseList = cancelcClient.getCancelCourseList(sessionList);
purchaseRecordVoList.addAll(cancelCourseList);
List purchaseRecordVos = sessionNameClient.queryCourseDetails(getStuSessionList);
purchaseRecordVoList.addAll(purchaseRecordVos);
List tAppUsers = tauMapper.selectList(new QueryWrapper()
.eq("referralUserId", userIdFormRedis)
.between("insertTime", localMonthStart, localMonthEnd));
List userIds = tAppUsers.stream().map(TAppUser::getId).collect(Collectors.toList());
IntrduceOfUserRequest request = new IntrduceOfUserRequest();
request.setStartTime(localMonthStart);
request.setEndTime(localMonthEnd);
request.setUserIds(userIds);
List purchaseRecordVos1 = idrClient.queryAppUsersofIntroduce(request);
purchaseRecordVoList.addAll(purchaseRecordVos1);
insVo.setSessionNames(stuSessionList);
insVo.setDetails(dealDataOfTime(purchaseRecordVoList));
GetStuOfCoursesDetails getStuOfCoursesDetails = new GetStuOfCoursesDetails();
getStuOfCoursesDetails.setStuId(stuId);
getStuOfCoursesDetails.setAppUserId(userIdFormRedis);
StuWithCoursesListVo stuOfCoursesDetails = couPayClient.getStuOfCoursesDetails(getStuOfCoursesDetails);
insVo.setTotalNums(ToolUtil.isEmpty(stuOfCoursesDetails.getTotalNums()) ? 0 : stuOfCoursesDetails.getTotalNums());
insVo.setDeductedNums(ToolUtil.isEmpty(stuOfCoursesDetails.getDeductedNums()) ? 0 : stuOfCoursesDetails.getDeductedNums());
insVo.setRemainingNums(ToolUtil.isEmpty(stuOfCoursesDetails.getRemainingNums()) ? 0 : stuOfCoursesDetails.getRemainingNums());
GetStudentCourse course = new GetStudentCourse();
course.setCourseId(lessonId);
course.setStuId(stuId);
course.setAppUserId(userIdFormRedis);
Integer deductionClassHour = crClient.getDeductionClassHour(course);
insVo.setDeductionClassHours(deductionClassHour);
}
return insVo;
}
@Override
public List queryDeduRecordDetails(RecordTimeRequest timeRequest, Integer appUserId) {
List record1 = sessionNameClient.getRecord(timeRequest);
return record1;
}
@Override
public List queryStuOfConponDetails(Integer appUserId) {
List availableVos = new ArrayList<>();
availableVos = userCClient.queryUserWithConponList(appUserId);
return availableVos;
}
@Override
public CourseDetailsOfContinuationResp queryStuOfCourseDetails(Integer lessonId, Integer stuId, Integer appUserId) {
CourseDetailsOfContinuationResp resp = new CourseDetailsOfContinuationResp();
GetStudentCourse getStudentCourse = new GetStudentCourse();
getStudentCourse.setCourseId(lessonId);
getStudentCourse.setStuId(stuId);
getStudentCourse.setAppUserId(appUserId);
StudentOfCourseVo studentCourse = couPayClient.getStudentCourse(getStudentCourse);
resp.setCoursePackageId(studentCourse.getCoursePackageId());
resp.setStuId(stuId);
resp.setPackageImg(studentCourse.getPackageImg());
resp.setCourseName(studentCourse.getCourseName());
Coach coach = coachMapper.selectById(studentCourse.getCoachId());
resp.setTeacherName(coach.getName());
resp.setCourseWeek(studentCourse.getCourseWeek());
resp.setCourseTime(studentCourse.getCourseTime());
StoreDetailOfCourse courseOfStore = storeClient.getCourseOfStore(studentCourse.getStoreId());
resp.setStoreName(courseOfStore.getStoreName());
resp.setStoreAddr(courseOfStore.getStoreAddr());
resp.setTypeList(studentCourse.getTypeList());
TStudent tStudent = this.baseMapper.selectById(stuId);
resp.setStuName(tStudent.getName());
resp.setStuPhone(tStudent.getPhone());
resp.setStuAge(DateUtil.age(tStudent.getBirthday()));
resp.setAmount(studentCourse.getAmount());
resp.setVipAmount(studentCourse.getVipAmount());
resp.setWpGold(studentCourse.getWpGold());
return resp;
}
@Override
public List querySystemNoticeDetails() {
return noClient.getSysNoticeDetails();
}
@Override
public SysNotice queryNoticeData(Integer noId) {
return noClient.getSysNoticeBuId(noId);
}
@Override
public List queryQuestionData() {
return quesClient.getSysQuestionDetails();
}
@Override
public QuestionIns queryQuestionDataInfo(Integer quesId) {
return quesClient.getSysQuestionBuId(quesId);
}
@Override
public List queryCustomerDetails() {
return noClient.queryCustomerTel();
}
@Override
public List queryIndexOfExplores(LonLatRequest llrequest) {
GetAllNearbyStoreList getAllNearbyStoreList = new GetAllNearbyStoreList();
getAllNearbyStoreList.setLongitude(llrequest.getLongitude());
getAllNearbyStoreList.setLatitude(llrequest.getLatitude());
return storeClient.getAllNearbyStoreList(getAllNearbyStoreList);
}
@Override
public List switchStudentActions(Integer appUserId, Integer stuId) {
List stuListVos = new ArrayList<>();
List tStudents = this.baseMapper.selectList(new QueryWrapper()
.eq("appUserId", appUserId)
.eq("state", 1));
if (tStudents.size() > 0) {
tStudents.forEach(sts -> {
if (Objects.equals(sts.getId(), stuId)) {
sts.setIsDefault(1);
} else {
sts.setIsDefault(2);
}
this.baseMapper.updateById(sts);
StuListVo vo = new StuListVo();
vo.setStuId(sts.getId());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
vo.setBirthday(simpleDateFormat.format(sts.getBirthday()));
vo.setStuName(sts.getName());
vo.setStuAge(DateUtil.age(sts.getBirthday()));
vo.setStuHeight(sts.getHeight());
vo.setStuWeight(sts.getWeight());
vo.setIsNot(sts.getIsDefault());
stuListVos.add(vo);
});
}
return stuListVos;
}
public static List dealDataOfTime(List purchaseRecords) {
Collections.sort(purchaseRecords, new Comparator() {
@Override
public int compare(PurchaseRecordVo record1, PurchaseRecordVo record2) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
Date date1 = null;
Date date2 = null;
try {
date1 = dateFormat.parse(record1.getPurchaseTime());
date2 = dateFormat.parse(record2.getPurchaseTime());
} catch (ParseException e) {
e.printStackTrace();
}
// 倒序排序
return date2.compareTo(date1);
}
});
return purchaseRecords;
}
@Override
public ResultUtil renewClassPayment(Integer userIdFormRedis, ClasspaymentRequest request) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
TCoursePackagePayment packagePayment = new TCoursePackagePayment();
packagePayment.setCode(code);
packagePayment.setAppUserId(userIdFormRedis);
packagePayment.setStudentId(request.getStuId());
packagePayment.setCoursePackageId(request.getLessonId());
packagePayment.setPayType(request.getPayType());
packagePayment.setAbsencesNumber(0);
packagePayment.setPayUserType(1);
packagePayment.setPayStatus(1);
packagePayment.setPayUserId(userIdFormRedis);
packagePayment.setStatus(1);
packagePayment.setState(1);
packagePayment.setInsertTime(new Date());
couPayClient.savePaymentCoursePackage(packagePayment);
Integer hour = couPayClient.getClassHour(request.getCourseConfigId());
try {
switch (request.getPayType()) {
case 1:
return WeChatPayment(code, request.getPayAmount(), hour);
case 2:
return AlipayPayment(code, request.getPayAmount(), hour);
case 3:
int i = PlaypaiGoldPayment(code, request);
switch (i) {
case 1:
return ResultUtil.success();
case 2:
return ResultUtil.error("用户未登录!");
case 3:
return ResultUtil.error("续课失败,玩湃币不足,请充值");
case 4:
return ResultUtil.error("续课失败,请联系管理员");
}
default:
break;
}
} catch (Exception e) {
ResultUtil.runErr();
}
return ResultUtil.success();
}
public ResultUtil WeChatPayment(String code, BigDecimal amount, Integer hour) throws Exception {
ResultUtil weixinpay = payMoneyUtil.weixinpay("课包续费", "", code, amount.toString(),
"/base/coursePackage/wechatPaymentCallback", "APP", "");
if (weixinpay.getCode() == 200) {
new Thread(new Runnable() {
@Override
public void run() {
try {
int num = 1;
int wait = 0;
while (num <= 10) {
int min = 5000;
wait += (min * num);
Thread.sleep(wait);
TCoursePackagePayment coursePackagePayment = couPayClient.getCoursePackagePaymentByCode(code);
if (coursePackagePayment.getPayStatus() == 2) {
break;
}
ResultUtil