| | |
| | | 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.other.StoreClient; |
| | | import com.dsh.course.feignclient.other.model.GetDistanceVo; |
| | | import com.dsh.course.mapper.CoursePackagePaymentConfigMapper; |
| | | import com.dsh.course.mapper.TCoursePackageDiscountMapper; |
| | | import com.dsh.course.mapper.TCoursePackageMapper; |
| | | import com.dsh.course.model.vo.response.WeekLimitedResponse; |
| | | import com.dsh.course.service.TCoursePackageDiscountService; |
| | | import com.dsh.course.util.DateTimeHelper; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.dsh.course.util.LocalDateTimeUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | |
| | | @Resource |
| | | private CoursePackagePaymentConfigMapper cppcMapper; |
| | | |
| | | @Resource |
| | | private StoreClient sreClient; |
| | | |
| | | |
| | | @Override |
| | | public WeekLimitedResponse getWeeksBenefitCourse(Integer appUserId,Integer discountType,String lon,String lat) { |
| | | Date start = DateTimeHelper.getTodayTime(); |
| | | Date lastTime = DateTimeHelper.getWeekOfLastDay(); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| | | WeekLimitedResponse response= new WeekLimitedResponse(); |
| | | public WeekLimitedResponse getWeeksBenefitCourse(Integer appUserId, Integer discountType, String lon, String lat) { |
| | | Date localTime = DateTimeHelper.getWXTime(); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | WeekLimitedResponse response = new WeekLimitedResponse(); |
| | | List<WeekLimitedResponse.Details> list = new ArrayList<>(); |
| | | |
| | | QueryWrapper<TCoursePackageDiscount> tCoursePackageDiscountQueryWrapper = new QueryWrapper<>(); |
| | | tCoursePackageDiscountQueryWrapper.eq("auditStatus",2 ); |
| | | tCoursePackageDiscountQueryWrapper.eq("auditStatus", 2); |
| | | List<TCoursePackageDiscount> tCoursePackageDiscounts = new ArrayList<>(); |
| | | if (null == discountType || discountType == 1){ |
| | | tCoursePackageDiscountQueryWrapper.eq("type",3); |
| | | int weekStr = LocalDateTimeUtils.getWeekStr(); |
| | | if (null == discountType || discountType == 1) { |
| | | tCoursePackageDiscountQueryWrapper.eq("type", 3); |
| | | response.setDiscountType(1); |
| | | tCoursePackageDiscounts = this.baseMapper.selectList(tCoursePackageDiscountQueryWrapper); |
| | | if (tCoursePackageDiscounts.size() > 0){ |
| | | if (tCoursePackageDiscounts.size() > 0) { |
| | | for (TCoursePackageDiscount tCoursePackageDiscount : tCoursePackageDiscounts) { |
| | | WeekLimitedResponse.Details ls = new WeekLimitedResponse.Details(); |
| | | |
| | | /** |
| | | * [{ |
| | | * "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 |
| | | * }] |
| | | */ |
| | | try { |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | JsonNode rootNode = null; |
| | | rootNode = objectMapper.readTree(tCoursePackageDiscount.getContent()); |
| | | String courseHours = rootNode.get("courseHours").asText(); |
| | | String lifespanEnd = rootNode.get("lifespanEnd").asText(); |
| | | Date endTime = simpleDateFormat.parse(lifespanEnd); |
| | | if (endTime.compareTo(lastTime) < 0){ |
| | | continue; |
| | | JSONArray jsonArray = JSON.parseArray(tCoursePackageDiscount.getContent()); |
| | | JSONObject jsonObject = jsonArray.getJSONObject(0); |
| | | Date parse1 = null; |
| | | Date parse2 = null; |
| | | String startDate = jsonObject.getString("startDate"); |
| | | String endDate = jsonObject.getString("endDate"); |
| | | try { |
| | | parse1 = simpleDateFormat.parse(startDate); |
| | | parse2 = simpleDateFormat.parse(endDate); |
| | | } catch (ParseException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId()); |
| | | GetDistanceVo distanceVo = new GetDistanceVo(); |
| | | distanceVo.setLatitude(lat); |
| | | distanceVo.setLongitude(lon); |
| | | distanceVo.setStoreId(coursePackage.getStoreId()); |
| | | ls.setStoreDistance(sreClient.calculateDistance(distanceVo)); |
| | | // TODO: 2023/7/10 限时折扣查询 |
| | | list.add(ls); |
| | | List<Integer> weeks = jsonObject.getJSONArray("weeks").toJavaList(Integer.class); |
| | | Double cashPayment = jsonObject.getDouble("cashPayment"); |
| | | Date tomorrowDate = null; |
| | | if (weekStr != 7) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(localTime); |
| | | calendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | tomorrowDate = calendar.getTime(); |
| | | } |
| | | |
| | | boolean isWithinRange = false; |
| | | if (weeks.contains(weekStr)) { |
| | | isWithinRange = true; |
| | | } |
| | | |
| | | if (isWithinRange) { |
| | | // 判断当前日期是否在开始时间和结束时间范围内 |
| | | if (isDateWithinRange(localTime, parse1, parse2)) { |
| | | WeekLimitedResponse.Details resde = new WeekLimitedResponse.Details(); |
| | | TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId()); |
| | | |
| | | resde.setImage(coursePackage.getCoverDrawing()); |
| | | resde.setCoursePackageName(coursePackage.getName()); |
| | | CoursePackagePaymentConfig paymentConfig = cppcMapper.selectById(tCoursePackageDiscount.getCoursePackagePaymentConfigId()); |
| | | resde.setCourseHours(paymentConfig.getClassHours()); |
| | | resde.setDataTime("今日"); |
| | | resde.setDate(localTime); |
| | | resde.setOriginalPrice(paymentConfig.getCashPayment()); |
| | | resde.setDiscountPrice(cashPayment); |
| | | resde.setStatus(1); |
| | | GetDistanceVo distanceVo = new GetDistanceVo(); |
| | | distanceVo.setLatitude(lat); |
| | | distanceVo.setLongitude(lon); |
| | | distanceVo.setStoreId(coursePackage.getStoreId()); |
| | | resde.setStoreDistance(sreClient.calculateDistance(distanceVo)); |
| | | list.add(resde); |
| | | } |
| | | } |
| | | Calendar tempCalendar = Calendar.getInstance(); |
| | | tempCalendar.setTime(localTime); |
| | | tempCalendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | |
| | | while (tempCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { |
| | | Date tempDate = tempCalendar.getTime(); |
| | | int weekNumOfDate = DateTimeHelper.getWeekNumOfDate(tempDate); |
| | | if (isDateWithinRange(tempDate, parse1, parse2) && weeks.contains(weekNumOfDate)) { |
| | | WeekLimitedResponse.Details resde = new WeekLimitedResponse.Details(); |
| | | TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId()); |
| | | |
| | | resde.setImage(coursePackage.getCoverDrawing()); |
| | | resde.setCoursePackageName(coursePackage.getName()); |
| | | CoursePackagePaymentConfig paymentConfig = cppcMapper.selectById(tCoursePackageDiscount.getCoursePackagePaymentConfigId()); |
| | | resde.setCourseHours(paymentConfig.getClassHours()); |
| | | String weekOfDate = ""; |
| | | if (weekStr != 7 && tempDate.equals(tomorrowDate)) { |
| | | weekOfDate = "明日"; |
| | | } else { |
| | | weekOfDate = DateTimeHelper.getWeekOfDate(tempDate); |
| | | } |
| | | resde.setDataTime(weekOfDate); |
| | | resde.setDate(tempDate); |
| | | resde.setOriginalPrice(paymentConfig.getCashPayment()); |
| | | resde.setDiscountPrice(cashPayment); |
| | | resde.setStatus(2); |
| | | GetDistanceVo distanceVo = new GetDistanceVo(); |
| | | distanceVo.setLatitude(lat); |
| | | distanceVo.setLongitude(lon); |
| | | distanceVo.setStoreId(coursePackage.getStoreId()); |
| | | resde.setStoreDistance(sreClient.calculateDistance(distanceVo)); |
| | | list.add(resde); |
| | | } |
| | | tempCalendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | }else { |
| | | tCoursePackageDiscountQueryWrapper.eq("type",4); |
| | | response.setDiscountType(2); |
| | | tCoursePackageDiscounts = this.baseMapper.selectList(tCoursePackageDiscountQueryWrapper); |
| | | if (tCoursePackageDiscounts.size() > 0){ |
| | | for (TCoursePackageDiscount tCoursePackageDiscount : tCoursePackageDiscounts) { |
| | | try { |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | JsonNode rootNode = null; |
| | | rootNode = objectMapper.readTree(tCoursePackageDiscount.getContent()); |
| | | String courseHours = rootNode.get("courseHours").asText(); |
| | | String lifespanStart = rootNode.get("lifespanStart").asText(); |
| | | String lifespanEnd = rootNode.get("lifespanEnd").asText(); |
| | | String classHours = rootNode.get("classHours").asText(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } else { |
| | | tCoursePackageDiscountQueryWrapper.eq("type", 4); |
| | | response.setDiscountType(2); |
| | | tCoursePackageDiscounts = this.baseMapper.selectList(tCoursePackageDiscountQueryWrapper); |
| | | if (tCoursePackageDiscounts.size() > 0) { |
| | | for (TCoursePackageDiscount tCoursePackageDiscount : tCoursePackageDiscounts) { |
| | | // [{ |
| | | // "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], |
| | | // "hour": 10 |
| | | // }] |
| | | try { |
| | | JSONArray jsonArray = JSON.parseArray(tCoursePackageDiscount.getContent()); |
| | | JSONObject jsonObject = jsonArray.getJSONObject(0); |
| | | Date parse1 = null; |
| | | Date parse2 = null; |
| | | String startDate = jsonObject.getString("startDate"); |
| | | String endDate = jsonObject.getString("endDate"); |
| | | try { |
| | | parse1 = simpleDateFormat.parse(startDate); |
| | | parse2 = simpleDateFormat.parse(endDate); |
| | | } catch (ParseException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | List<Integer> weeks = jsonObject.getJSONArray("weeks").toJavaList(Integer.class); |
| | | Integer cashPayment = jsonObject.getInteger("hour"); |
| | | |
| | | Date tomorrowDate = null; |
| | | if (weekStr != 7) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(localTime); |
| | | calendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | tomorrowDate = calendar.getTime(); |
| | | } |
| | | |
| | | boolean isWithinRange = false; |
| | | if (weeks.contains(weekStr)) { |
| | | isWithinRange = true; |
| | | } |
| | | |
| | | if (isWithinRange) { |
| | | // 判断当前日期是否在开始时间和结束时间范围内 |
| | | if (isDateWithinRange(localTime, parse1, parse2)) { |
| | | WeekLimitedResponse.Details resde = new WeekLimitedResponse.Details(); |
| | | TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId()); |
| | | |
| | | resde.setImage(coursePackage.getCoverDrawing()); |
| | | resde.setCoursePackageName(coursePackage.getName()); |
| | | CoursePackagePaymentConfig paymentConfig = cppcMapper.selectById(tCoursePackageDiscount.getCoursePackagePaymentConfigId()); |
| | | resde.setCourseHours(paymentConfig.getClassHours()); |
| | | resde.setDonateHours(cashPayment); |
| | | resde.setDataTime("今日"); |
| | | resde.setDate(localTime); |
| | | resde.setOriginalPrice(paymentConfig.getCashPayment()); |
| | | resde.setStatus(1); |
| | | GetDistanceVo distanceVo = new GetDistanceVo(); |
| | | distanceVo.setLatitude(lat); |
| | | distanceVo.setLongitude(lon); |
| | | distanceVo.setStoreId(coursePackage.getStoreId()); |
| | | resde.setStoreDistance(sreClient.calculateDistance(distanceVo)); |
| | | list.add(resde); |
| | | } |
| | | } |
| | | Calendar tempCalendar = Calendar.getInstance(); |
| | | tempCalendar.setTime(localTime); |
| | | tempCalendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | |
| | | while (tempCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { |
| | | Date tempDate = tempCalendar.getTime(); |
| | | int weekNumOfDate = DateTimeHelper.getWeekNumOfDate(tempDate); |
| | | if (isDateWithinRange(tempDate, parse1, parse2) && weeks.contains(weekNumOfDate)) { |
| | | WeekLimitedResponse.Details resde = new WeekLimitedResponse.Details(); |
| | | TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId()); |
| | | |
| | | resde.setImage(coursePackage.getCoverDrawing()); |
| | | resde.setCoursePackageName(coursePackage.getName()); |
| | | CoursePackagePaymentConfig paymentConfig = cppcMapper.selectById(tCoursePackageDiscount.getCoursePackagePaymentConfigId()); |
| | | resde.setCourseHours(paymentConfig.getClassHours()); |
| | | resde.setDonateHours(cashPayment); |
| | | String weekOfDate = ""; |
| | | if (weekStr != 7 && tempDate.equals(tomorrowDate)) { |
| | | weekOfDate = "明日"; |
| | | } else { |
| | | weekOfDate = DateTimeHelper.getWeekOfDate(tempDate); |
| | | } |
| | | resde.setDataTime(weekOfDate); |
| | | resde.setDate(tempDate); |
| | | resde.setOriginalPrice(paymentConfig.getCashPayment()); |
| | | resde.setStatus(2); |
| | | GetDistanceVo distanceVo = new GetDistanceVo(); |
| | | distanceVo.setLatitude(lat); |
| | | distanceVo.setLongitude(lon); |
| | | distanceVo.setStoreId(coursePackage.getStoreId()); |
| | | resde.setStoreDistance(sreClient.calculateDistance(distanceVo)); |
| | | list.add(resde); |
| | | } |
| | | tempCalendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | return response; |
| | | } |
| | | |
| | | |
| | | private static boolean isDateWithinRange(Date date, Date startTime, Date endTime) { |
| | | return date.after(startTime) && date.before(endTime); |
| | | } |
| | | |
| | | } |