liujie
2023-10-26 e030255c23c7ba3e2cbad1036a810d6d72fa864f
cloud-server-course/src/main/java/com/dsh/course/service/impl/TCoursePackageDiscountServiceImpl.java
@@ -1,10 +1,40 @@
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.LambdaQueryWrapper;
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.CoursePackagePaymentConfig;
import com.dsh.course.entity.TCoursePackage;
import com.dsh.course.entity.TCoursePackageDiscount;
import com.dsh.course.entity.TCoursePackagePayment;
import com.dsh.course.feignclient.other.StoreClient;
import com.dsh.course.feignclient.other.model.GetDistanceVo;
import com.dsh.course.feignclient.other.model.Store;
import com.dsh.course.mapper.CoursePackagePaymentConfigMapper;
import com.dsh.course.mapper.TCoursePackageDiscountMapper;
import com.dsh.course.mapper.TCoursePackageMapper;
import com.dsh.course.mapper.TCoursePackagePaymentMapper;
import com.dsh.course.model.DiscountList;
import com.dsh.course.model.QueryDiscountList;
import com.dsh.course.model.vo.response.Details;
import com.dsh.course.model.vo.response.ExchangeCoursePackageResponse;
import com.dsh.course.service.TCoursePackageDiscountService;
import com.dsh.course.util.DateTimeHelper;
import com.dsh.course.util.LocalDateTimeUtils;
import com.dsh.course.util.StrUtils;
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;
/**
 * <p>
@@ -17,4 +47,380 @@
@Service
public class TCoursePackageDiscountServiceImpl extends ServiceImpl<TCoursePackageDiscountMapper, TCoursePackageDiscount> implements TCoursePackageDiscountService {
    @Resource
    private TCoursePackageMapper tcpMapper;
    @Resource
    private TCoursePackagePaymentMapper tcppMapper;
    @Resource
    private CoursePackagePaymentConfigMapper cppcMapper;
    @Resource
    private StoreClient sreClient;
    @Override
    public List<Details> getWeeksBenefitCourse(Integer appUserId, Integer discountType, String lon, String lat) {
        Date localTime = DateTimeHelper.getWXTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Details> list = new ArrayList<>();
        QueryWrapper<TCoursePackageDiscount> tCoursePackageDiscountQueryWrapper = new QueryWrapper<>();
        tCoursePackageDiscountQueryWrapper.eq("auditStatus", 2);
        List<TCoursePackageDiscount> tCoursePackageDiscounts = new ArrayList<>();
        int weekStr = LocalDateTimeUtils.getWeekStr();
        if (null == discountType || discountType == 1) {
            tCoursePackageDiscountQueryWrapper.eq("type", 3);
            tCoursePackageDiscounts = this.baseMapper.selectList(tCoursePackageDiscountQueryWrapper);
            if (tCoursePackageDiscounts.size() > 0) {
                for (TCoursePackageDiscount tCoursePackageDiscount : tCoursePackageDiscounts) {
                    TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId());
//                    判断预约人数是否已满
                    Integer maxSubscribeNumber = coursePackage.getMaxSubscribeNumber();
                    Integer integer = tcppMapper.selectCount(new QueryWrapper<TCoursePackagePayment>()
                            .eq("appUserId",appUserId )
                            .eq("status",1));
                    if (integer >= maxSubscribeNumber){
                        continue;
                    }
                    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);
                        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)) {
                                Details resde = new Details();
                                resde.setCoursePackageDiscountId(tCoursePackageDiscount.getId());
                                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());
                                String s = sreClient.calculateDistance(distanceVo);
                                resde.setStoreDistance(s);
                                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)) {
                                Details resde = new Details();
                                resde.setCoursePackageDiscountId(tCoursePackageDiscount.getId());
                                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);
            tCoursePackageDiscounts = this.baseMapper.selectList(tCoursePackageDiscountQueryWrapper);
            if (tCoursePackageDiscounts.size() > 0) {
                for (TCoursePackageDiscount tCoursePackageDiscount : tCoursePackageDiscounts) {
                    TCoursePackage coursePackage = tcpMapper.selectById(tCoursePackageDiscount.getCoursePackageId());
//                    判断预约人数是否已满
                    Integer maxSubscribeNumber = coursePackage.getMaxSubscribeNumber();
                    Integer integer = tcppMapper.selectCount(new QueryWrapper<TCoursePackagePayment>()
                            .eq("appUserId",appUserId )
                            .eq("status",1));
                    if (integer >= maxSubscribeNumber){
                        continue;
                    }
                    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)) {
                                Details resde = new Details();
                                resde.setCoursePackageDiscountId(tCoursePackageDiscount.getId());
                                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)) {
                                Details resde = new Details();
                                resde.setCoursePackageDiscountId(tCoursePackageDiscount.getId());
                                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();
                    }
                }
            }
        }
        System.out.println("list"+list);
        return list;
    }
    @Override
    public ExchangeCoursePackageResponse getWeekFreeCourseDetails(Integer coursePackageDiscountId,String lat,String lon) {
        ExchangeCoursePackageResponse packageResponse = new ExchangeCoursePackageResponse();
        TCoursePackageDiscount coursePackageDiscount = this.baseMapper.selectById(coursePackageDiscountId);
        Integer coursePackageId = coursePackageDiscount.getCoursePackageId();
        packageResponse.setCoursePackageDiscountId(coursePackageId);
        TCoursePackage coursePackage = tcpMapper.selectById(coursePackageId);
        packageResponse.setCoverDrawing(coursePackage.getCoverDrawing());
        packageResponse.setIntroduceDrawing(coursePackage.getIntroduceDrawing());
        packageResponse.setCoursePackageName(coursePackage.getName());
        Store store = sreClient.queryStoreById(coursePackage.getStoreId());
        packageResponse.setStoreNameAddr(store.getName() +"(" +store.getAddress()+")");
        GetDistanceVo distanceVo = new GetDistanceVo();
        distanceVo.setLatitude(lat);
        distanceVo.setLongitude(lon);
        distanceVo.setStoreId(coursePackage.getStoreId());
        String s = sreClient.calculateDistance(distanceVo);
        packageResponse.setDistance(Double.valueOf(s));
        if (coursePackageDiscount.getType() == 1 || coursePackageDiscount.getType() == 2){
            return packageResponse;
        }
        JSONArray jsonArray = JSON.parseArray(coursePackageDiscount.getContent());
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        CoursePackagePaymentConfig paymentConfig = cppcMapper.selectById(coursePackageDiscount.getCoursePackagePaymentConfigId());
        if (coursePackageDiscount.getType() == 3){
            Double cashPayment = jsonObject.getDouble("cashPayment");
            packageResponse.setClassHours(paymentConfig.getClassHours());
            packageResponse.setCostPrice(paymentConfig.getCashPayment());
            packageResponse.setDiscountPrice(cashPayment);
        }else {
            packageResponse.setClassHours(paymentConfig.getClassHours());
            Integer cashPayment = jsonObject.getInteger("hour");
            packageResponse.setFreeClassHours(cashPayment);
            packageResponse.setCostPrice(paymentConfig.getCashPayment());
            TCoursePackageDiscount coursePackageDiscount1 = this.baseMapper.selectOne(new QueryWrapper<TCoursePackageDiscount>()
                    .eq("type", 1)
                    .eq("coursePackageId",coursePackage.getId()));
            String content = coursePackageDiscount1.getContent();
            JSONObject jsonObject1 = JSON.parseObject(content);
            Double vipPrice = jsonObject1.getDouble("num1");
            packageResponse.setDiscountPrice(vipPrice);
            packageResponse.setWanpaiGold(paymentConfig.getPlayPaiCoin());
        }
        String[] split = coursePackage.getClassWeeks().split(";");
        if (split.length > 0){
            StringBuilder courWeeks = new StringBuilder("每");
            for (String integer : split) {
                courWeeks.append(integer).append("、");
            }
            if (courWeeks.length() > 0 && courWeeks.charAt(courWeeks.length() - 1) == '、'){
                courWeeks.deleteCharAt(courWeeks.length() - 1);
            }
            packageResponse.setWeekTime(courWeeks.toString());
        }
        packageResponse.setTime(coursePackage.getClassStartTime()+"-"+coursePackage.getClassEndTime());
        return packageResponse;
    }
    @Override
    public List<DiscountList> queryDiscountListAudit(QueryDiscountList queryDiscountList) {
        Page<DiscountList> discountListPage = new Page<>(queryDiscountList.getOffset(), queryDiscountList.getLimit());
        List<DiscountList> discountLists = this.baseMapper.queryDiscountListAudit(discountListPage, queryDiscountList.getPCode(), queryDiscountList.getCCode(), queryDiscountList.getName(), queryDiscountList.getType(), queryDiscountList.getIds());
        for (DiscountList discountList : discountLists) {
            List<TCoursePackageDiscount> tCoursePackageDiscounts = this.baseMapper.selectList(new LambdaQueryWrapper<TCoursePackageDiscount>().eq(TCoursePackageDiscount::getCoursePackageId, discountList.getCourseId()).ne(TCoursePackageDiscount::getAuditStatus,2).orderByAsc(TCoursePackageDiscount::getType));
            long count = tCoursePackageDiscounts.stream().filter(e -> e.getAuditStatus().equals(2)).count();
            if(count>0){
                discountList.setStatus(2);
            }else {
                discountList.setStatus(1);
            }
            String type="";
            for (int i = 0; i < tCoursePackageDiscounts.size(); i++) {
                if(tCoursePackageDiscounts.get(i).getType()==1){
                    type += "会员折扣、";
                }else if(tCoursePackageDiscounts.get(i).getType()==3){
                    type += "限时折扣、";
                }else if(tCoursePackageDiscounts.get(i).getType()==4){
                    type += "赠送课时、";
                }
            }
            if(type.length()>0){
                type = type.substring(0,type.length()-1);
            }
            discountList.setType(type);
        }
        return discountLists;
    }
    @Override
    public List<DiscountList> queryDiscountList(QueryDiscountList queryDiscountList) {
        Page<DiscountList> discountListPage = new Page<>(queryDiscountList.getOffset(), queryDiscountList.getLimit());
        List<Integer> ids = queryDiscountList.getIds();
        if(ids.size()==0){
            ids.add(-1);
            queryDiscountList.setIds(ids);
        }
        List<DiscountList> discountLists = this.baseMapper.queryDiscountList(discountListPage, queryDiscountList.getPCode(), queryDiscountList.getCCode(), queryDiscountList.getName(), queryDiscountList.getType(), queryDiscountList.getIds());
        for (DiscountList discountList : discountLists) {
            List<TCoursePackageDiscount> tCoursePackageDiscounts = this.baseMapper.selectList(new LambdaQueryWrapper<TCoursePackageDiscount>().eq(TCoursePackageDiscount::getCoursePackageId, discountList.getCourseId()).eq(TCoursePackageDiscount::getAuditStatus,2).orderByAsc(TCoursePackageDiscount::getType));
            long count = tCoursePackageDiscounts.stream().filter(e -> e.getStatus().equals(2)).count();
            if(count>0){
                discountList.setStatus(2);
            }else {
                discountList.setStatus(1);
            }
            String type="";
            for (int i = 0; i < tCoursePackageDiscounts.size(); i++) {
                if(tCoursePackageDiscounts.get(i).getType()==1){
                    type += "会员折扣、";
                }else if(tCoursePackageDiscounts.get(i).getType()==3){
                    type += "限时折扣、";
                }else if(tCoursePackageDiscounts.get(i).getType()==4){
                    type += "赠送课时、";
                }
            }
            if(type.length()>0){
                type = type.substring(0,type.length()-1);
            }
            discountList.setType(type);
        }
        return discountLists;
    }
    private static boolean isDateWithinRange(Date date, Date startTime, Date endTime) {
        return date.after(startTime) && date.before(endTime);
    }
}