package com.dsh.course.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.dsh.course.entity.CoursePackagePaymentConfig; import com.dsh.course.entity.TCoursePackage; import com.dsh.course.entity.TCoursePackageDiscount; import com.dsh.course.feignclient.account.AppUserClient; import com.dsh.course.feignclient.account.model.AppUser; import com.dsh.course.feignclient.other.StoreClient; import com.dsh.course.feignclient.other.model.Store; import com.dsh.course.mapper.TCoursePackageMapper; import com.dsh.course.feignclient.model.CourseOfStoreVo; import com.dsh.course.model.CoursePackageList; import com.dsh.course.model.CoursePackageListVo; import com.dsh.course.service.ICoursePackagePaymentConfigService; import com.dsh.course.service.TCoursePackageDiscountService; import com.dsh.course.service.TCoursePackagePaymentService; import com.dsh.course.service.TCoursePackageService; import com.dsh.course.util.GDMapGeocodingUtil; import com.dsh.course.util.GeodesyUtil; import com.dsh.course.util.ToolUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** *

* 课包 服务实现类 *

* * @author administrator * @since 2023-06-14 */ @Service public class TCoursePackageServiceImpl extends ServiceImpl implements TCoursePackageService { @Resource private StoreClient storeClient; @Autowired private GDMapGeocodingUtil gdMapGeocodingUtil; @Autowired private TCoursePackagePaymentService coursePackagePaymentService; @Autowired private ICoursePackagePaymentConfigService coursePackagePaymentConfigService; @Autowired private TCoursePackageDiscountService coursePackageDiscountService; @Resource private AppUserClient appUserClient; @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); 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("id", 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()).last(" order by if(payType = 1, cashPayment, playPaiCoin)")); CoursePackageListVo coursePackageListVo = new CoursePackageListVo(); coursePackageListVo.setId(coursePackage.getId()); coursePackageListVo.setName(coursePackage.getName()); coursePackageListVo.setStoreName(null != store ? store.getName() : ""); coursePackageListVo.setCoverDrawing(coursePackage.getCoverDrawing()); coursePackageListVo.setClassStartTime(coursePackage.getClassStartTime() + "-" + coursePackage.getClassEndTime()); coursePackageListVo.setApplicantsNumber(integer); coursePackageListVo.setOriginalPrice(coursePackagePaymentConfig.getPayType() == 1 ? coursePackagePaymentConfig.getCashPayment() : coursePackagePaymentConfig.getPlayPaiCoin().doubleValue()); TCoursePackageDiscount coursePackageDiscount = coursePackageDiscountService.getOne(new QueryWrapper().eq("coursePackageId", coursePackage.getId()).eq("auditStatus", 2).eq("type", 1)); if(1 == appUser.getIsVip() && null != coursePackageDiscount){ //{"num1":100} JSONObject jsonObject = JSON.parseObject(coursePackageDiscount.getContent()); coursePackageListVo.setVipPrice(jsonObject.getDouble("num1")); } coursePackageDiscount = coursePackageDiscountService.getOne(new QueryWrapper().eq("coursePackageId", coursePackage.getId()).eq("auditStatus", 2).eq("type", 3)); if(null == coursePackageDiscount){ coursePackageListVo.setPaymentPrice(coursePackageListVo.getOriginalPrice()); }else{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * [{ * "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++) { 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[] split = startTime.split(":"); Integer hour1 = Integer.valueOf(split[0]); Calendar s = Calendar.getInstance(); s.setTime(date); s.set(Calendar.HOUR_OF_DAY, hour1); s.set(Calendar.MINUTE, Integer.valueOf(split[1])); s.set(Calendar.SECOND, Integer.valueOf(split[2])); String[] split1 = endTime.split(":"); Integer hour2 = Integer.valueOf(split1[0]); Calendar e = Calendar.getInstance(); e.setTime(date); e.set(Calendar.HOUR_OF_DAY, hour2); e.set(Calendar.MINUTE, Integer.valueOf(split1[1])); e.set(Calendar.SECOND, Integer.valueOf(split1[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()){ coursePackageListVo.setPaymentPrice(cashPayment); } } } } 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; } }