package com.dsh.course.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeOrderSettleModel;
import com.alipay.api.domain.OpenApiRoyaltyDetailInfoPojo;
import com.alipay.api.domain.RoyaltyDetailInfos;
import com.alipay.api.request.AlipayTradeOrderSettleRequest;
import com.alipay.api.request.AlipayTradeRoyaltyRelationBindRequest;
import com.alipay.api.response.AlipayTradeOrderSettleResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRoyaltyRelationBindResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.course.entity.*;
import com.dsh.course.feignclient.account.AppUserClient;
import com.dsh.course.feignclient.account.StudentClient;
import com.dsh.course.feignclient.account.model.AppUser;
import com.dsh.course.feignclient.account.model.Student;
import com.dsh.course.feignclient.activity.CouponClient;
import com.dsh.course.feignclient.activity.UserCouponClient;
import com.dsh.course.feignclient.activity.model.Coupon;
import com.dsh.course.feignclient.activity.model.SendCouponReq;
import com.dsh.course.feignclient.activity.model.UserCoupon;
import com.dsh.course.feignclient.other.StoreClient;
import com.dsh.course.feignclient.other.model.Store;
import com.dsh.course.mapper.CoursePackageStudentMapper;
import com.dsh.course.mapper.TCoursePackageMapper;
import com.dsh.course.feignclient.model.CourseOfStoreVo;
import com.dsh.course.model.*;
import com.dsh.course.service.*;
import com.dsh.course.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 课包 服务实现类
*
*
* @author administrator
* @since 2023-06-14
*/
@Service
public class TCoursePackageServiceImpl extends ServiceImpl implements TCoursePackageService {
private String aliAppid = "2021004105665036";//支付宝appid
private String appPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCi5i9nW/hGLJ3A06cZxTQdviFC7THpdSihoTYGLr9q006hu0V26ecBMY/o4w5bvIX0Ok/yofmZsVcCJpAPvbXL/uqVrIjnRRxXiaeBFThlxoBUTdunvbUSDYfzlEhJr5NvUKI6H6lz2niXlQGx4qy8Hau4ccWit9kM8jwUvsBVQoFgJA+xrjMvooA7YLopQtpOD+UJr5thApTSf1xrnr1W12yolTLEH15JmNV372cqXrYUuqnY0QsaPtxeqJUGAOcGdVLllQ7easEznP8DFBvDdHATcmp2SHNQDUEWN6MCVPbMgY06NQVqAXxqjTAYSVh+6TRu6bofPmpYC3TZB003AgMBAAECggEBAJAcR2+PA3NBYUYHeFrqBRMS8uX8ZR19kjZ7IgoSLTFaQsP9opRylPSPXhrPVBKAE5leRQAHn4MCSlESwHvMfxo7KFjFTFAc6dffZZpipYQUOc9bGampwJh58/3e/pyBgVMG6J23CPf/HJQtNFSkjd/V9+ayb/9l2dUEL3bC0fAZ/dbx8HsxdLw8wn3fLlWLj68hOMqa2deCZe3JdSVsPbeWqkh56FFsMLug0Nd+Ar4TgRl9/jnhXF0JWiD0LmPUYLhboY7EfUBzN4w1iYbDi1P+3zvoOYsiVKAXox9GMhQ2VzOO2UcSTuizSza2e98mGpabl/GpKmCz+RDFjtkX6eECgYEA2MyCij65eO3aGIm3FUe93DULRBYTfX8qJQSJq2WOWA3mmQlEW6L3O2B5/lG2h+8WmN6iLEs9eHpgycGYp7vAqgrANEn16ACVcuyx0scFtrZfZ+kmHMzFfiUWxJjVYk/6YngsGVBLdw6ueM42C8TTP67X9tU5TdVGoGWuqEj4W98CgYEAwFqwprXOch5Pqk/RPbb49r0Ou03K/UbciWnWWKzUhFFNS8MdlQPoDvQZbMwHLeWsa2VhaKITK3x5biLQb3U+0GLOn6lTvEyrEUH+ucREyLgVYTRAvwBPtnvlrzpyxPk2HnslQjju8WrvvLLBMKWUjlTrTOzhaHT21gz3pHMiOakCgYEAhLmfaXdBITGshb054sNLDtdCkGpbgEcrzAHdLps769iGxkYQHXHFngpQZUwtTUcoNGqIKknd1jZFrv7gsD+XkgKG7PwimehRlkwmCX5ilxtLiVgJRzRt6+5U5AMVD90a0tHzXYP0z2yjj73fBJF5KtGl0a10KZxaYrQdm1UhB00CgYBZZgzx/k9rtHC8LAqIj1CYhHejT92G53c6Gkl3vyOqN4sgKhfGmSEySfrDGPRBPZxr8ZtbIPCd5mUdberH0osWGMYFaJI1UsCy7aQwvGpniz7MhZeN7dweaOjwDs8mgtjHQ96mL4XGCDhR0BZ/wIURvZ/6iaGdhbbu9unlsWj3uQKBgQCmZYdsbbZkd3ev6f8rwyvMz+DrCQyYpY44cegBYuJgrZiQnL2fJioeN7ixX0UM48SfwsZEIrzshP/LGAwnc2MdjxKUl4jLN8SEe0NAjXOnz9Zaw740+aOmLpXcLWdP4uM2gIhWsvW1tEkQZCXmm7c9s/RsU8Pmzv+YL3+fSijOzA==";//支付宝开发者应用私钥
private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAouYvZ1v4RiydwNOnGcU0Hb4hQu0x6XUooaE2Bi6/atNOobtFdunnATGP6OMOW7yF9DpP8qH5mbFXAiaQD721y/7qlayI50UcV4mngRU4ZcaAVE3bp721Eg2H85RISa+Tb1CiOh+pc9p4l5UBseKsvB2ruHHForfZDPI8FL7AVUKBYCQPsa4zL6KAO2C6KULaTg/lCa+bYQKU0n9ca569VtdsqJUyxB9eSZjVd+9nKl62FLqp2NELGj7cXqiVBgDnBnVS5ZUO3mrBM5z/AxQbw3RwE3JqdkhzUA1BFjejAlT2zIGNOjUFagF8ao0wGElYfuk0bum6Hz5qWAt02QdNNwIDAQAB";//支付宝应用公钥
private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB";//支付宝支付公钥
@Resource
private StoreClient storeClient;
@Autowired
private GDMapGeocodingUtil gdMapGeocodingUtil;
@Autowired
private TCoursePackagePaymentService coursePackagePaymentService;
@Autowired
private ICoursePackagePaymentConfigService coursePackagePaymentConfigService;
@Autowired
private CourseCounsumService courseCounsumService;
@Autowired
private TCoursePackageDiscountService coursePackageDiscountService;
@Resource
private AppUserClient appUserClient;
@Resource
private StudentClient studentClient;
@Resource
private UserCouponClient userCouponClient;
@Resource
private CouponClient couponClient;
@Autowired
private PayMoneyUtil payMoneyUtil;
@Autowired
private ICoursePackageSchedulingService coursePackageSchedulingService;
@Autowired
private CoursePackageStudentService coursePackageStudentService;
@Resource
private CoursePackageStudentMapper cpsMapper;
@Autowired
private ICoursePackageOrderStudentService coursePackageOrderStudentService;
@Autowired
private ICoursePackageOrderService coursePackageOrderService;
@Autowired
private TOrderService orderService;
@Autowired
private TCoursePackageService coursePackageService;
@Resource
private CoursePackageStudentMapper coursePackageStudentMapper;
@Override
public List queryStoreOfCourse() {
return this.baseMapper.queryStoreOfCourse();
}
/**
* 获取课程列表
*
* @param coursePackageList
* @return
* @throws Exception
*/
@Override
public List queryCourseList(Integer uid, CoursePackageList coursePackageList) throws Exception {
AppUser appUser = appUserClient.queryAppUser(uid);
QueryWrapper wrapper = new QueryWrapper().in("status", Arrays.asList(1, 2))
.eq("auditStatus", 2).eq("state", 1);
if (null != coursePackageList.getCoursePackageTypeId()) {
wrapper.eq("coursePackageTypeId", coursePackageList.getCoursePackageTypeId());
}
if (null != coursePackageList.getStoreId()) {
wrapper.eq("storeId", coursePackageList.getStoreId());
}
if (null != coursePackageList.getStoreId()) {
wrapper.eq("storeId", coursePackageList.getStoreId());
}
if (ToolUtil.isNotEmpty(coursePackageList.getSearch())) {
wrapper.like("name", coursePackageList.getSearch());
List stores = storeClient.queryStoreListByName(coursePackageList.getSearch());
List collect = stores.stream().map(Store::getId).collect(Collectors.toList());
if (collect.size() > 0) {
wrapper.or().in("storeId", collect);
}
}
List list1 = this.list(wrapper.last(" order by sort desc, insertTime desc"));
Date currentTime = new Date();
List list = list1.stream()
.filter(coursePackage -> coursePackage.getType() != 2 || currentTime.before(coursePackage.getEndTime()))
.collect(Collectors.toList());
List listVos = new ArrayList<>();
for (TCoursePackage coursePackage : list) {
Store store = storeClient.queryStoreById(coursePackage.getStoreId());
Integer integer = coursePackagePaymentService.queryCountNumber(coursePackage.getId());
CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getOne(new QueryWrapper()
.eq("coursePackageId", coursePackage.getId()).orderByAsc("classHours").last(" limit 0, 1"));
CoursePackageListVo coursePackageListVo = new CoursePackageListVo();
coursePackageListVo.setId(coursePackage.getId());
coursePackageListVo.setName(coursePackage.getName());
coursePackageListVo.setStoreName(null != store ? store.getName() : "");
coursePackageListVo.setCoverDrawing(coursePackage.getCoverDrawing());
// 2.0修改
String classStartTime = coursePackage.getClassStartTime();
String classEndTime = coursePackage.getClassEndTime();
ArrayList classTime = new ArrayList<>();
String[] split = classStartTime.split(",");
String[] split3 = classEndTime.split(",");
if (ToolUtil.isNotEmpty(classStartTime)) {
for (int i = 0; i < split.length; i++) {
String s = split[i].substring(0, 5) + "-" + split3[i].substring(0, 5);
classTime.add(s);
}
}
coursePackageListVo.setClassStartTime(classTime);
coursePackageListVo.setType(coursePackage.getType());
coursePackageListVo.setApplicantsNumber(integer);
Double cashPayment1 = coursePackagePaymentConfig.getCashPayment();
Integer playPaiCoin = coursePackagePaymentConfig.getPlayPaiCoin();
if (ToolUtil.isNotEmpty(cashPayment1) && cashPayment1 > 0 && ToolUtil.isNotEmpty(playPaiCoin) && playPaiCoin > 0) {
coursePackageListVo.setPayType(3);
} else if (ToolUtil.isNotEmpty(cashPayment1) && cashPayment1 > 0) {
coursePackageListVo.setPayType(1);
} else if (ToolUtil.isNotEmpty(playPaiCoin) && playPaiCoin > 0) {
coursePackageListVo.setPayType(2);
}
if (coursePackagePaymentConfig != null) {
coursePackageListVo.setPlayPaiCoin(coursePackagePaymentConfig.getPlayPaiCoin());
}
//会员显示原价和会员价(最低)。非会员显示会员价和支付价(最低)
// if(appUser.getIsVip() == 0){//非会员
List list2 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 1).eq("auditStatus", 2).eq("status", 1));
Double vipPrice = coursePackagePaymentConfig.getCashPayment();
for (TCoursePackageDiscount coursePackageDiscount : list2) {
Double num1 = JSON.parseObject(coursePackageDiscount.getContent()).getDouble("discountMember");
if (vipPrice.compareTo(num1) > 0) {
vipPrice = num1;
}
}
coursePackageListVo.setVipPrice(vipPrice);
coursePackageListVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
Double paymentPrice = coursePackagePaymentConfig.getCashPayment();
List list3 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 3).eq("auditStatus", 2).eq("status", 1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (TCoursePackageDiscount coursePackageDiscount : list3) {
/**
* [{
* "startDate": "2023-01-01 00:00:00",
* "endDate": "2023-12-31 23:59:59",
* "startTime": "02:00:00",
* "endTime": "23:00:00",
* "weeks": [1, 2, 7],
* "cashPayment": 100
* }]
*/
JSONArray jsonArray = JSON.parseArray(coursePackageDiscount.getContent());
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String startDate = jsonObject.getString("startDate");
String endDate = jsonObject.getString("endDate");
String startTime = jsonObject.getString("startTime");
String endTime = jsonObject.getString("endTime");
List weeks = jsonObject.getJSONArray("weeks").toJavaList(Integer.class);
Double cashPayment = jsonObject.getDouble("cashPayment");
Date startDate_date = sdf.parse(startDate);
Date endDate_date = sdf.parse(endDate);
long timeMillis = System.currentTimeMillis();
if (timeMillis >= startDate_date.getTime() && timeMillis < endDate_date.getTime()) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.DAY_OF_WEEK);
boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
if (isFirstSunday) {
week = week - 1;
if (week == 0) {
week = 7;
}
}
if (!weeks.contains(week)) {
continue;
}
String[] split1 = startTime.split(":");
Integer hour1 = Integer.valueOf(split1[0]);
Calendar s = Calendar.getInstance();
s.setTime(date);
s.set(Calendar.HOUR_OF_DAY, hour1);
s.set(Calendar.MINUTE, Integer.valueOf(split1[1]));
s.set(Calendar.SECOND, Integer.valueOf(split1[2]));
String[] split2 = endTime.split(":");
Integer hour2 = Integer.valueOf(split2[0]);
Calendar e = Calendar.getInstance();
e.setTime(date);
e.set(Calendar.HOUR_OF_DAY, hour2);
e.set(Calendar.MINUTE, Integer.valueOf(split2[1]));
e.set(Calendar.SECOND, Integer.valueOf(split2[2]));
if (hour1 > hour2) {
if (s.getTimeInMillis() > date.getTime()) {
s.set(Calendar.DAY_OF_YEAR, s.get(Calendar.DAY_OF_YEAR) - 1);
} else {
e.set(Calendar.DAY_OF_YEAR, e.get(Calendar.DAY_OF_YEAR) + 1);
}
}
if (timeMillis >= s.getTimeInMillis() && timeMillis < e.getTimeInMillis() && paymentPrice.compareTo(cashPayment) > 0) {
paymentPrice = cashPayment;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
coursePackageListVo.setPaymentPrice(paymentPrice);
if (coursePackageListVo.getPaymentPrice() < coursePackageListVo.getVipPrice()) {
coursePackageListVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
}
if (appUser.getIsVip() == 1) {
if (coursePackageListVo.getVipPrice() < coursePackageListVo.getPaymentPrice()) {
coursePackageListVo.setPaymentPrice(coursePackageListVo.getVipPrice());
}
} else {
coursePackageListVo.setVipPrice(null);
}
Map distance = GeodesyUtil.getDistance(coursePackageList.getLon() + "," + coursePackageList.getLat(), store.getLon() + "," + store.getLat());
coursePackageListVo.setDistance(distance.get("WGS84") / 1000);
listVos.add(coursePackageListVo);
}
//销量排行
if (ToolUtil.isNotEmpty(coursePackageList.getSalesRanking()) && coursePackageList.getSalesRanking().equals("asc")) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Integer.compare(s1.getApplicantsNumber(), s2.getApplicantsNumber());
}
});
}
if (ToolUtil.isNotEmpty(coursePackageList.getSalesRanking()) && coursePackageList.getSalesRanking().equals("desc")) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Integer.compare(s2.getApplicantsNumber(), s1.getApplicantsNumber());
}
});
}
//距离排行
if (ToolUtil.isNotEmpty(coursePackageList.getDistanceSort()) && coursePackageList.getDistanceSort().equals("asc")) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Double.compare(s1.getDistance(), s2.getDistance());
}
});
}
if (ToolUtil.isNotEmpty(coursePackageList.getDistanceSort()) && coursePackageList.getDistanceSort().equals("desc")) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Double.compare(s2.getDistance(), s1.getDistance());
}
});
}
return listVos;
}
@Override
public List queryCourseListOne(CoursePackageList coursePackageList) throws Exception {
// AppUser appUser = appUserClient.queryAppUser(uid);
Map geocode = gdMapGeocodingUtil.geocode(coursePackageList.getLon(), coursePackageList.getLat());
String provinceCode = geocode.get("provinceCode");
String cityCode = geocode.get("cityCode");
QueryWrapper wrapper = new QueryWrapper().in("status", Arrays.asList(1, 2))
.eq("auditStatus", 2).eq("state", 1).eq("provinceCode", provinceCode).eq("cityCode", cityCode);
if (null != coursePackageList.getCoursePackageTypeId()) {
wrapper.eq("coursePackageTypeId", coursePackageList.getCoursePackageTypeId());
}
if (null != coursePackageList.getStoreId()) {
wrapper.eq("storeId", coursePackageList.getStoreId());
}
if (null != coursePackageList.getStoreId()) {
wrapper.eq("storeId", coursePackageList.getStoreId());
}
if (ToolUtil.isNotEmpty(coursePackageList.getSearch())) {
wrapper.like("name", coursePackageList.getSearch());
List stores = storeClient.queryStoreListByName(coursePackageList.getSearch());
List collect = stores.stream().map(Store::getId).collect(Collectors.toList());
if (collect.size() > 0) {
wrapper.or().in("storeId", collect);
}
}
List list = this.list(wrapper.last(" order by sort, insertTime desc"));
List listVos = new ArrayList<>();
for (TCoursePackage coursePackage : list) {
Store store = storeClient.queryStoreById(coursePackage.getStoreId());
Integer integer = coursePackagePaymentService.queryCountNumber(coursePackage.getId());
CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getOne(new QueryWrapper()
.eq("coursePackageId", coursePackage.getId()).orderByAsc("classHours").last(" limit 0, 1"));
CoursePackageListVo coursePackageListVo = new CoursePackageListVo();
coursePackageListVo.setId(coursePackage.getId());
coursePackageListVo.setName(coursePackage.getName());
coursePackageListVo.setStoreName(null != store ? store.getName() : "");
// coursePackageListVo.setPayType();
coursePackageListVo.setCoverDrawing(coursePackage.getCoverDrawing());
Double cashPayment1 = coursePackagePaymentConfig.getCashPayment();
Integer playPaiCoin = coursePackagePaymentConfig.getPlayPaiCoin();
if (ToolUtil.isNotEmpty(cashPayment1) && cashPayment1 > 0 && ToolUtil.isNotEmpty(playPaiCoin) && playPaiCoin > 0) {
coursePackageListVo.setPayType(3);
} else if (ToolUtil.isNotEmpty(cashPayment1) && cashPayment1 > 0) {
coursePackageListVo.setPayType(1);
} else if (ToolUtil.isNotEmpty(playPaiCoin) && playPaiCoin > 0) {
coursePackageListVo.setPayType(2);
}
// 2.0修改
String classStartTime = coursePackage.getClassStartTime();
String classEndTime = coursePackage.getClassEndTime();
ArrayList classTime = new ArrayList<>();
String[] split = classStartTime.split(",");
String[] split3 = classEndTime.split(",");
for (int i = 0; i < split.length; i++) {
String s = split[i] + "-" + split3[i];
classTime.add(s);
}
coursePackageListVo.setClassStartTime(classTime);
coursePackageListVo.setType(coursePackage.getType());
coursePackageListVo.setApplicantsNumber(integer);
coursePackageListVo.setPayType(coursePackage.getPayType());
coursePackageListVo.setPlayPaiCoin(coursePackagePaymentConfig.getPlayPaiCoin());
//会员显示原价和会员价(最低)。非会员显示会员价和支付价(最低)
if (false) {//非会员
List list2 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 1).eq("auditStatus", 2));
Double vipPrice = coursePackagePaymentConfig.getCashPayment();
for (TCoursePackageDiscount coursePackageDiscount : list2) {
Double num1 = JSON.parseObject(coursePackageDiscount.getContent()).getDouble("discountMember");
if (vipPrice.compareTo(num1) > 0) {
vipPrice = num1;
}
}
coursePackageListVo.setVipPrice(vipPrice);
Double paymentPrice = coursePackagePaymentConfig.getCashPayment();
List list3 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 3).eq("auditStatus", 2));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (TCoursePackageDiscount coursePackageDiscount : list3) {
/**
* [{
* "startDate": "2023-01-01 00:00:00",
* "endDate": "2023-12-31 23:59:59",
* "startTime": "02:00:00",
* "endTime": "23:00:00",
* "weeks": [1, 2, 7],
* "cashPayment": 100
* }]
*/
JSONArray jsonArray = JSON.parseArray(coursePackageDiscount.getContent());
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String startDate = jsonObject.getString("startDate");
String endDate = jsonObject.getString("endDate");
String startTime = jsonObject.getString("startTime");
String endTime = jsonObject.getString("endTime");
List weeks = jsonObject.getJSONArray("weeks").toJavaList(Integer.class);
Double cashPayment = jsonObject.getDouble("cashPayment");
Date startDate_date = sdf.parse(startDate);
Date endDate_date = sdf.parse(endDate);
long timeMillis = System.currentTimeMillis();
if (timeMillis >= startDate_date.getTime() && timeMillis < endDate_date.getTime()) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.DAY_OF_WEEK);
boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
if (isFirstSunday) {
week = week - 1;
if (week == 0) {
week = 7;
}
}
if (!weeks.contains(week)) {
continue;
}
String[] split1 = startTime.split(":");
Integer hour1 = Integer.valueOf(split1[0]);
Calendar s = Calendar.getInstance();
s.setTime(date);
s.set(Calendar.HOUR_OF_DAY, hour1);
s.set(Calendar.MINUTE, Integer.valueOf(split1[1]));
s.set(Calendar.SECOND, Integer.valueOf(split1[2]));
String[] split2 = endTime.split(":");
Integer hour2 = Integer.valueOf(split2[0]);
Calendar e = Calendar.getInstance();
e.setTime(date);
e.set(Calendar.HOUR_OF_DAY, hour2);
e.set(Calendar.MINUTE, Integer.valueOf(split2[1]));
e.set(Calendar.SECOND, Integer.valueOf(split2[2]));
if (hour1 > hour2) {
if (s.getTimeInMillis() > date.getTime()) {
s.set(Calendar.DAY_OF_YEAR, s.get(Calendar.DAY_OF_YEAR) - 1);
} else {
e.set(Calendar.DAY_OF_YEAR, e.get(Calendar.DAY_OF_YEAR) + 1);
}
}
if (timeMillis >= s.getTimeInMillis() && timeMillis < e.getTimeInMillis() && paymentPrice.compareTo(cashPayment) > 0) {
paymentPrice = cashPayment;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
coursePackageListVo.setPaymentPrice(paymentPrice);
} else {
List list2 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 1).eq("auditStatus", 2));
Double vipPrice = coursePackagePaymentConfig.getCashPayment();
for (TCoursePackageDiscount coursePackageDiscount : list2) {
Double num1 = JSON.parseObject(coursePackageDiscount.getContent()).getDouble("discountMember");
if (vipPrice.compareTo(num1) > 0) {
vipPrice = num1;
}
}
coursePackageListVo.setPaymentPrice(vipPrice);
coursePackageListVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
}
Map distance = GeodesyUtil.getDistance(coursePackageList.getLon() + "," + coursePackageList.getLat(), store.getLon() + "," + store.getLat());
coursePackageListVo.setDistance(distance.get("WGS84") / 1000);
listVos.add(coursePackageListVo);
}
//销量排行
if (ToolUtil.isNotEmpty(coursePackageList.getSalesRanking())) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Integer.compare(s1.getApplicantsNumber(), s2.getApplicantsNumber());
}
});
}
//距离排行
if (ToolUtil.isNotEmpty(coursePackageList.getDistanceSort())) {
Collections.sort(listVos, new Comparator() {
public int compare(CoursePackageListVo s1, CoursePackageListVo s2) {
return Double.compare(s1.getDistance(), s2.getDistance());
}
});
}
return listVos;
}
/**
* 获取课包详情
*
* @param uid
* @param id
* @return
* @throws Exception
*/
@Override
public CoursePackageInfo queryCourseInfo(Integer uid, Integer id, String lon, String lat, Integer stuId) throws Exception {
AppUser appUser = appUserClient.queryAppUser(uid);
TCoursePackage coursePackage = this.getById(id);
CoursePackageInfo coursePackageInfo = new CoursePackageInfo();
coursePackageInfo.setIsVip(appUser.getIsVip());
coursePackageInfo.setId(id);
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
if (coursePackage.getType() == 2) {
coursePackageInfo.setTime(format.format(coursePackage.getStartTime()) + "-" + format.format(coursePackage.getEndTime()));
}
coursePackageInfo.setCoverDrawing(coursePackage.getCoverDrawing());
coursePackageInfo.setName(coursePackage.getName());
Store store = storeClient.queryStoreById(coursePackage.getStoreId());
coursePackageInfo.setStoreName(store.getName());
coursePackageInfo.setStoreCoverDrawing(store.getCoverDrawing());
coursePackageInfo.setStoreAddress(store.getAddress());
coursePackageInfo.setLon(store.getLon());
coursePackageInfo.setLat(store.getLat());
if (ToolUtil.isNotEmpty(lon) && ToolUtil.isNotEmpty(lat)) {
Map distance = GeodesyUtil.getDistance(lon + "," + lat, store.getLon() + "," + store.getLat());
double wgs84 = new BigDecimal(distance.get("WGS84")).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
coursePackageInfo.setDistance(wgs84);
}
String[] split = coursePackage.getClassWeeks().split(";");
coursePackageInfo.setWeeks(Arrays.asList(split));
String classStartTime = coursePackage.getClassStartTime();
String classEndTime = coursePackage.getClassEndTime();
ArrayList classTime = new ArrayList<>();
String[] split4 = classStartTime.split(",");
String[] split3 = classEndTime.split(",");
if (ToolUtil.isNotEmpty(classStartTime)) {
for (int i = 0; i < split4.length; i++) {
String s = split4[i].substring(0, 5) + "-" + split3[i].substring(0, 5);
classTime.add(s);
}
}
coursePackageInfo.setTimes(classTime);
coursePackageInfo.setType(coursePackage.getType());
coursePackageInfo.setDetailDrawing(coursePackage.getDetailDrawing());
coursePackageInfo.setIntroduceDrawing(coursePackage.getIntroduceDrawing());
List list = new ArrayList<>();
List list1 = coursePackagePaymentConfigService.list(new QueryWrapper().eq("coursePackageId", id).orderByAsc("classHours"));
list1.forEach(coursePackagePaymentConfig -> {
CoursePackagePaymentConfigVo coursePackagePaymentConfigVo = new CoursePackagePaymentConfigVo();
coursePackagePaymentConfigVo.setId(coursePackagePaymentConfig.getId());
coursePackagePaymentConfigVo.setClassHours(coursePackagePaymentConfig.getClassHours());
coursePackagePaymentConfigVo.setPlayPaiCoin(coursePackagePaymentConfig.getPlayPaiCoin());
if (ToolUtil.isNotEmpty(coursePackagePaymentConfig.getCashPayment()) && coursePackagePaymentConfig.getCashPayment() > 0 && ToolUtil.isNotEmpty(coursePackagePaymentConfig.getPlayPaiCoin()) && coursePackagePaymentConfig.getPlayPaiCoin() > 0) {
coursePackagePaymentConfigVo.setPayType(3);
} else if (ToolUtil.isNotEmpty(coursePackagePaymentConfig.getCashPayment()) && coursePackagePaymentConfig.getCashPayment() > 0) {
coursePackagePaymentConfigVo.setPayType(1);
} else if (ToolUtil.isNotEmpty(coursePackagePaymentConfig.getPlayPaiCoin()) && coursePackagePaymentConfig.getPlayPaiCoin() > 0) {
coursePackagePaymentConfigVo.setPayType(2);
}
//会员显示原价和支付价(会员价)。非会员显示会员价和支付价(最低)
// if(appUser.getIsVip() == 0){//非会员
List list2 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 1).eq("auditStatus", 2));
Double vipPrice = coursePackagePaymentConfig.getCashPayment();
for (TCoursePackageDiscount coursePackageDiscount : list2) {
Double num1 = JSON.parseObject(coursePackageDiscount.getContent()).getDouble("discountMember");
if (vipPrice.compareTo(num1) > 0) {
vipPrice = num1;
}
}
coursePackagePaymentConfigVo.setVipPrice(vipPrice);
Double paymentPrice = coursePackagePaymentConfig.getCashPayment();
List list3 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
.eq("type", 3).eq("auditStatus", 2));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (TCoursePackageDiscount coursePackageDiscount : list3) {
/**
* [{
* "startDate": "2023-01-01 00:00:00",
* "endDate": "2023-12-31 23:59:59",
* "startTime": "02:00:00",
* "endTime": "23:00:00",
* "weeks": [1, 2, 7],
* "cashPayment": 100
* }]
*/
JSONArray jsonArray = JSON.parseArray(coursePackageDiscount.getContent());
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String startDate = jsonObject.getString("startDate");
String endDate = jsonObject.getString("endDate");
String startTime = jsonObject.getString("startTime");
String endTime = jsonObject.getString("endTime");
List weeks = jsonObject.getJSONArray("weeks").toJavaList(Integer.class);
Double cashPayment = jsonObject.getDouble("cashPayment");
Date startDate_date = sdf.parse(startDate);
Date endDate_date = sdf.parse(endDate);
long timeMillis = System.currentTimeMillis();
if (timeMillis >= startDate_date.getTime() && timeMillis < endDate_date.getTime()) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.DAY_OF_WEEK);
boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
if (isFirstSunday) {
week = week - 1;
if (week == 0) {
week = 7;
}
}
if (!weeks.contains(week)) {
continue;
}
String[] split1 = startTime.split(":");
Integer hour1 = Integer.valueOf(split1[0]);
Calendar s = Calendar.getInstance();
s.setTime(date);
s.set(Calendar.HOUR_OF_DAY, hour1);
s.set(Calendar.MINUTE, Integer.valueOf(split1[1]));
s.set(Calendar.SECOND, Integer.valueOf(split1[2]));
String[] split2 = endTime.split(":");
Integer hour2 = Integer.valueOf(split2[0]);
Calendar e = Calendar.getInstance();
e.setTime(date);
e.set(Calendar.HOUR_OF_DAY, hour2);
e.set(Calendar.MINUTE, Integer.valueOf(split2[1]));
e.set(Calendar.SECOND, Integer.valueOf(split2[2]));
if (hour1 > hour2) {
if (s.getTimeInMillis() > date.getTime()) {
s.set(Calendar.DAY_OF_YEAR, s.get(Calendar.DAY_OF_YEAR) - 1);
} else {
e.set(Calendar.DAY_OF_YEAR, e.get(Calendar.DAY_OF_YEAR) + 1);
}
}
if (timeMillis >= s.getTimeInMillis() && timeMillis < e.getTimeInMillis() && paymentPrice.compareTo(cashPayment) > 0) {
paymentPrice = cashPayment;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
coursePackagePaymentConfigVo.setPaymentPrice(paymentPrice);
// TCoursePackageDiscount discount = coursePackageDiscountService.getOne(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
// .eq("type", 2).eq("auditStatus", 2));
//
//
// //这里是续课优惠
// Double continuingMember = JSON.parseObject(discount.getContent()).getDouble("continuingUser");
// Double vipcontinuingMember = JSON.parseObject(discount.getContent()).getDouble("continuingMember");
//
//
// if (coursePackagePaymentConfigVo.getPaymentPrice()>continuingMember){
// coursePackagePaymentConfigVo.setPaymentPrice(continuingMember);
// }
// if (coursePackagePaymentConfigVo.getVipPrice()>vipcontinuingMember){
// coursePackagePaymentConfigVo.setVipPrice(vipcontinuingMember);
// }
if (appUser.getIsVip() == 1) {
if (coursePackagePaymentConfigVo.getVipPrice() < coursePackagePaymentConfigVo.getPaymentPrice()) {
coursePackagePaymentConfigVo.setPaymentPrice(coursePackagePaymentConfigVo.getVipPrice());
coursePackagePaymentConfigVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
}
}
if (coursePackagePaymentConfigVo.getPaymentPrice() < coursePackagePaymentConfigVo.getVipPrice()) {
coursePackagePaymentConfigVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
coursePackagePaymentConfigVo.setVipPrice(null);
}
// }
// else{
// List list2 = coursePackageDiscountService.list(new QueryWrapper().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
// .eq("type", 1).eq("auditStatus", 2));
// Double vipPrice = coursePackagePaymentConfig.getCashPayment();
// for (TCoursePackageDiscount coursePackageDiscount : list2) {
// Double num1 = JSON.parseObject(coursePackageDiscount.getContent()).getDouble("discountMember");
// if(vipPrice.compareTo(num1) > 0){
// vipPrice = num1;
// }
// }
// coursePackagePaymentConfigVo.setPaymentPrice(vipPrice);
// coursePackagePaymentConfigVo.setOriginalPrice(coursePackagePaymentConfig.getCashPayment());
// }
list.add(coursePackagePaymentConfigVo);
});
coursePackageInfo.setList(list);
StudentVo studentVo = new StudentVo();
Student student = null;
if(null != stuId){
student = studentClient.queryStudentById(stuId);
}else{
student = studentClient.queryDefaultStudent(uid);
}
if (null != student) {
studentVo.setId(student.getId());
studentVo.setPhone(student.getPhone());
studentVo.setName(student.getName());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
studentVo.setAge(Integer.valueOf(sdf.format(new Date())) - Integer.valueOf(sdf.format(student.getBirthday())));
}
coursePackageInfo.setStudent(studentVo);
return coursePackageInfo;
}
/**
* 课程包名支付
*
* @param uid
* @param paymentCourseVo
* @return
* @throws Exception
*/
@Override
public ResultUtil paymentCourse(Integer uid, PaymentCourseVo paymentCourseVo) throws Exception {
AppUser appUser = appUserClient.queryAppUser(uid);
CoursePackageInfo coursePackageInfo = queryCourseInfo(uid, paymentCourseVo.getId(), null, null, null);
//校验是否已经报满
Integer integer = coursePackagePaymentService.queryCountNumber(paymentCourseVo.getId());
TCoursePackage course = coursePackageService.getById(paymentCourseVo.getId());
if (course.getType() == 1) {
if (integer + paymentCourseVo.getStudentIds().split(";").length > course.getMaxSubscribeNumber()) {
return ResultUtil.error("报名失败,已达最大报名人数");
}
}
List list = coursePackageInfo.getList();
String[] students = paymentCourseVo.getStudentIds().split(";");
for (CoursePackagePaymentConfigVo coursePackagePaymentConfigVo : list) {
if (paymentCourseVo.getCoursePackagePaymentConfigId().compareTo(coursePackagePaymentConfigVo.getId()) == 0) {
CoursePackagePaymentConfig byId = coursePackagePaymentConfigService.getById(paymentCourseVo.getCoursePackagePaymentConfigId());
Double cashPayment = byId.getCashPayment();
Integer payType = coursePackagePaymentConfigVo.getPayType();
Integer playPaiCoin1 = byId.getPlayPaiCoin();
if (cashPayment != null || cashPayment > 0) {
payType = 1;
}
if (playPaiCoin1 != null || playPaiCoin1 > 0) {
payType = 2;
}
if ((cashPayment != null || cashPayment > 0) && (playPaiCoin1 != null || playPaiCoin1 > 0)) {
payType = 3;
}
if (cashPayment != null && cashPayment > 0 && playPaiCoin1 != null && playPaiCoin1 > 0) {
} else {
if (payType == 1 && paymentCourseVo.getPayType() == 3) {//现金支付
return ResultUtil.error("支付方式错误,请刷新后重试");
}
if (payType == 2 && paymentCourseVo.getPayType() != 3) {//玩湃币支付
return ResultUtil.error("支付方式错误,请刷新后重试");
}
}
Double paymentPrice = coursePackagePaymentConfigVo.getPaymentPrice();//支付价
paymentPrice = new BigDecimal(students.length).multiply(new BigDecimal(paymentPrice)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
Double playPaiCoin = coursePackagePaymentConfigVo.getPlayPaiCoin().doubleValue();//支付币
playPaiCoin = new BigDecimal(students.length).multiply(new BigDecimal(playPaiCoin)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
if (paymentCourseVo.getPayType() == 3 && playPaiCoin.compareTo(paymentCourseVo.getPrice()) != 0) {
return ResultUtil.error("支付金额异常,请刷新后重试");
}
}
}
Double price = paymentCourseVo.getPrice();//支付金额
//校验优惠券
Long couponId = paymentCourseVo.getCouponId();
if (null != couponId && paymentCourseVo.getPayType() != 3) {
UserCoupon userCoupon = userCouponClient.queryUserCouponById(couponId);
if (userCoupon.getStatus() == 2) {
return ResultUtil.error("优惠券已被使用");
}
Coupon coupon = couponClient.queryCouponById(userCoupon.getCouponId());
long time = coupon.getEndTime().getTime();
if (System.currentTimeMillis() >= time) {
return ResultUtil.error("优惠券已过期");
}
if (coupon.getType() == 1) {//满减
JSONObject jsonObject = JSON.parseObject(coupon.getContent());
// {"conditionalAmount":50,"deductionAmount":10,"experienceName":""}
Double num1 = jsonObject.getDouble("conditionalAmount");
Double num2 = jsonObject.getDouble("deductionAmount");
if (price.compareTo(num1) <= 0) {
return ResultUtil.error("该优惠券无法使用");
}
price = new BigDecimal(price).subtract(new BigDecimal(num2)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
}
if (coupon.getType() == 2) {//代金券
JSONObject jsonObject = JSON.parseObject(coupon.getContent());
Double num1 = jsonObject.getDouble("conditionalAmount");
if (price.compareTo(num1) <= 0) {
return ResultUtil.error("该优惠券无法使用");
}
price = new BigDecimal(price).subtract(new BigDecimal(num1)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
}
userCoupon.setStatus(2);
userCouponClient.edit(userCoupon);
}
if (paymentCourseVo.getPayType() == 1) {//微信支付
return weChatPaymentCourse(uid, price, paymentCourseVo);
}
if (paymentCourseVo.getPayType() == 2) {//支付宝支付
return aliPaymentCourse(uid, price, paymentCourseVo);
}
if (paymentCourseVo.getPayType() == 3) {//玩湃币支付
if (appUser.getPlayPaiCoins() < price.intValue()) {
return ResultUtil.error("报名失败,玩湃币不足,请充值!");
}
return playPaiCoinPaymentCourse(appUser, price, paymentCourseVo);
}
return ResultUtil.success();
}
/**
* 课程微信支付
*
* @param uid
* @param paymentPrice
* @param paymentCourseVo
* @return
* @throws Exception
*/
public ResultUtil weChatPaymentCourse(Integer uid, Double paymentPrice, PaymentCourseVo paymentCourseVo) throws Exception {
CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getById(paymentCourseVo.getCoursePackagePaymentConfigId());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
Long orderId = paymentCourseVo.getOrderId();
if(null == orderId){
//生成支付订单
CoursePackageOrder coursePackageOrder = new CoursePackageOrder();
coursePackageOrder.setCode(code);
coursePackageOrder.setAppUserId(uid);
coursePackageOrder.setStudentIds(paymentCourseVo.getStudentIds());
coursePackageOrder.setCoursePackageId(paymentCourseVo.getId());
coursePackageOrder.setPayType(paymentCourseVo.getPayType());
coursePackageOrder.setClassHours(coursePackagePaymentConfig.getClassHours());
coursePackageOrder.setOriginalPrice(paymentCourseVo.getPrice());
coursePackageOrder.setUserCouponId(paymentCourseVo.getCouponId());
coursePackageOrder.setCashPayment(new BigDecimal(paymentPrice));
coursePackageOrder.setPayUserType(1);
coursePackageOrder.setPayStatus(1);
coursePackageOrder.setPayUserId(uid);
coursePackageOrder.setStatus(1);
coursePackageOrder.setState(1);
coursePackageOrder.setInsertTime(new Date());
coursePackageOrderService.save(coursePackageOrder);
orderId = coursePackageOrder.getId();
}else{
CoursePackageOrder coursePackageOrder = coursePackageOrderService.getById(orderId);
code = coursePackageOrder.getCode();
coursePackageOrder.setPayType(1);
coursePackageOrder.setAppUserId(null);
coursePackageOrder.setPayUserType(1);
coursePackageOrder.setPayUserId(uid);
coursePackageOrderService.updateById(coursePackageOrder);
}
ResultUtil weixinpay = payMoneyUtil.weixinpay("购买课程", coursePackagePaymentConfig.getId().toString(), code, paymentPrice.toString(), "/base/course/weChatPaymentCourseCallback", "APP", "");
if (weixinpay.getCode() == 200) {
String finalCode = code;
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);
CoursePackageOrder coursePackageOrder1 = coursePackageOrderService.getOne(new QueryWrapper()
.eq("code", finalCode).eq("state", 1));
if (coursePackageOrder1.getPayStatus() == 2) {
break;
}
ResultUtil