package com.stylefeng.guns.modular.system.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.stylefeng.guns.modular.system.dao.ServerCarModelMapper; import com.stylefeng.guns.modular.system.dao.SystemPriceMapper; import com.stylefeng.guns.modular.system.model.Company; import com.stylefeng.guns.modular.system.model.OpenCity; import com.stylefeng.guns.modular.system.model.ServerCarModel; import com.stylefeng.guns.modular.system.model.SystemPriceCity; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil; import com.stylefeng.guns.modular.system.util.GDMapGeocodingUtil; import com.stylefeng.guns.modular.system.util.ResultUtil; import com.stylefeng.guns.modular.system.warpper.BaseWarpper; import com.stylefeng.guns.modular.system.warpper.ServerCarModelWarpper; import lombok.Data; 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.util.*; @Service public class ServerCarModelServiceImpl extends ServiceImpl implements IServerCarModelService { @Resource private ServerCarModelMapper serverCarModelMapper; @Resource private ISystemPriceCityService systemPriceCityService; @Resource private SystemPriceMapper systemPriceMapper; @Autowired private GDMapElectricFenceUtil gdMapElectricFenceUtil; @Autowired private GDMapGeocodingUtil gdMapGeocodingUtil; @Autowired private ICompanyCityService companyCityService; @Autowired private IOpenCityService openCityService; @Autowired private IOpenCityBusinessService openCityBusinessService; /** * 根据起点和终点获取车型 * @param startLonLat * @param endLonLat * @param type 业务类型 * @return * @throws Exception */ @Override public ResultUtil> queryServerCarModel(String startLonLat, String endLonLat, Integer type) throws Exception { //查找与起点匹配的企业(经营范围) Map geocode = gdMapGeocodingUtil.geocode(startLonLat.split(",")[0], startLonLat.split(",")[1]); Company query = companyCityService.query(geocode.get("districtCode")); if(null == query){ return ResultUtil.error("起点暂无企业提供服务", new ArrayList<>()); } if(type == 1 && query.getIsSpe() == 2){ return ResultUtil.success(new ArrayList<>()); } Map distance = gdMapElectricFenceUtil.getDistance(startLonLat, endLonLat, 1); if (null == distance) { return ResultUtil.error("获取预估距离出错", new ArrayList<>()); } String distance1 = distance.get("distance");//距离(米) double distance1_ = Double.valueOf(distance1).doubleValue(); String duration = distance.get("duration");//时间(秒) long duration_ = Long.valueOf(duration).longValue(); OpenCity openCity = openCityService.openCity1(geocode.get("districtCode")); if(null == openCity){ return ResultUtil.error("起点暂未开通"); } //专车车型 List price = new ArrayList<>(); List baseWarppers = openCityBusinessService.queryBusinessById(openCity.getId()); Optional first = baseWarppers.stream().filter(s->s.getId() == 1).findFirst(); if(first.isPresent()){ price = this.getPrice(query.getId(), distance1_, duration_, 0, 1, geocode); } //出租车车型 ServerCarModelWarpper serverCarModelWarpper = null; first = baseWarppers.stream().filter(s->s.getId() == 2).findFirst(); if(first.isPresent()){ serverCarModelWarpper = this.getTextPrice(query.getId(), distance1_, duration_, 0, 2, geocode); if(serverCarModelWarpper!=null){ price.add(serverCarModelWarpper); } } return ResultUtil.success(price); } /** * 获取业务类型对应的所有服务车型 * * @param type * @return */ @Override public List> queryServerCarModels(Integer type) { return serverCarModelMapper.queryServerCarModel(type); } /** * 快捷叫车无终点,只返回可选车辆信息,不计算预估价格。 * * @param startLonLat 起点经纬度 * @return 车辆信息 * @throws Exception */ @Override public ResultUtil> queryQuickCallServerCarModel(String startLonLat) throws Exception { Map geocode = gdMapGeocodingUtil.geocode(startLonLat.split(",")[0], startLonLat.split(",")[1]); Company query = companyCityService.query(geocode.get("districtCode")); if (null == query) { return ResultUtil.error("起点暂无企业提供服务", new ArrayList<>()); } OpenCity openCity = openCityService.openCity1(geocode.get("districtCode")); if (null == openCity) { return ResultUtil.error("起点暂未开通", new ArrayList<>()); } List baseWarppers = openCityBusinessService.queryBusinessById(openCity.getId()); List data = new ArrayList<>(); Optional privateCarBusiness = baseWarppers.stream().filter(s -> s.getId() == 1).findFirst(); if (privateCarBusiness.isPresent() && !Integer.valueOf(2).equals(query.getIsSpe())) { data.addAll(this.getPrice(query.getId(), 0, 0, 0, 1, geocode)); } Optional taxiBusiness = baseWarppers.stream().filter(s -> s.getId() == 2).findFirst(); if (taxiBusiness.isPresent() && !Integer.valueOf(2).equals(query.getIsTaxi())) { ServerCarModelWarpper taxiWarpper = this.getTextPrice(query.getId(), 0, 0, 0, 2, geocode); if (taxiWarpper != null) { data.add(taxiWarpper); } } // 快捷叫车不能参与拼车合单,前端不展示拼车选项。 for (ServerCarModelWarpper warpper : data) { warpper.setMileage(0D); warpper.setDuration(0D); warpper.setPc_check(0); } return ResultUtil.success(data); } /** * 计算价格 * * @param companyId * @param distance * @param duration * @param wait * @param type * @return * @throws Exception */ public List getPrice(Integer companyId, double distance, long duration, long wait, Integer type, Map geocode) throws Exception { System.err.println("预估里程:" + distance); System.err.println("预估时间:" + duration); String provinceCode = geocode.get("provinceCode"); String cityCode = geocode.get("cityCode"); String districtCode = geocode.get("districtCode"); //获取匹配的城市价格配置 SystemPriceCity systemPriceCity = systemPriceCityService.queryOne(provinceCode, cityCode, districtCode); System.err.println("获取匹配的城市价格配置:" + systemPriceCity); if (null == systemPriceCity) { return new ArrayList<>(); } List> list = serverCarModelMapper.queryServerCarModel(type); System.err.println("获取匹配的城市价格配置111:" + list); List data = new ArrayList<>(); for (Map map : list) { Map query1 = systemPriceMapper.query(companyId, type, Integer.valueOf(map.get("id").toString()), systemPriceCity.getId()); if (null == query1) {//排除没有设置价格的车型 continue; } //开始根据不同的方式计算金额 double amount = 0; if (type == 1) {//专车 double d = distance / 1000;//实际公里 double t = duration / 60;//实际时间 JSONObject jsonObject = JSON.parseObject(query1.get("content").toString());//等待费 Double waitMoney = jsonObject.getDouble("num2"); JSONObject contentPutOne = JSON.parseObject(query1.get("contentPutOne").toString());//一人拼成 JSONObject contentNotOne = JSON.parseObject(query1.get("contentNotOne").toString());//一人未拼成 JSONObject contentPutTwo = JSON.parseObject(query1.get("contentPutTwo").toString());//2人拼成 JSONObject contentNotTwo = JSON.parseObject(query1.get("contentNotTwo").toString());//2人未拼成 JSONObject contentPutThree = JSON.parseObject(query1.get("contentPutThree").toString());//3人拼成 JSONObject contentNotThree = JSON.parseObject(query1.get("contentNotThree").toString());//3人未拼成 JSONObject contentExclusive = JSON.parseObject(query1.get("contentExclusive").toString());//独享 JSONObject contentPrice = JSON.parseObject(query1.get("contentPrice").toString());//一口价 //一口价 ServerCarModelWarpper serverCarModelWarpper = ServerCarModelWarpper.getServerCarModelWarpper(map); serverCarModelWarpper.setFareTypeNote1(query1.get("fareTypeNote1").toString()); serverCarModelWarpper.setFareTypeNote2(query1.get("fareTypeNote2").toString()); serverCarModelWarpper.setFareTypeNote3(query1.get("fareTypeNote3").toString()); serverCarModelWarpper.setContentPutOne(contentPutOne); serverCarModelWarpper.setContentNotOne(contentNotOne); serverCarModelWarpper.setContentPutTwo(contentPutTwo); serverCarModelWarpper.setContentNotTwo(contentNotTwo); serverCarModelWarpper.setContentPutThree(contentPutThree); serverCarModelWarpper.setContentNotThree(contentNotThree); serverCarModelWarpper.setContentExclusive(contentExclusive); serverCarModelWarpper.setContentPrice(contentPrice); serverCarModelWarpper.setWaitPrice(jsonObject); serverCarModelWarpper.setAmount(this.calculationPrice(contentExclusive, distance, duration, wait, waitMoney)); serverCarModelWarpper.setBuyNowAmount(this.calculationPrice(contentPrice, distance, duration, wait, waitMoney)); serverCarModelWarpper.setPutTogetherOneAmount(this.calculationPrice(contentPutOne, distance, duration, wait, waitMoney)); serverCarModelWarpper.setNotAssembledOneAmount(this.calculationPrice(contentNotOne, distance, duration, wait, waitMoney)); serverCarModelWarpper.setPutTogetherTwoAmount(this.calculationPrice(contentPutTwo, distance, duration, wait, waitMoney)); serverCarModelWarpper.setNotAssembledTwoAmount(this.calculationPrice(contentNotTwo, distance, duration, wait, waitMoney)); serverCarModelWarpper.setPutTogetherThreeAmount(this.calculationPrice(contentPutThree, distance, duration, wait, waitMoney)); serverCarModelWarpper.setNotAssembledThreeAmount(this.calculationPrice(contentNotThree, distance, duration, wait, waitMoney)); serverCarModelWarpper.setMileage(d); serverCarModelWarpper.setDuration(t); serverCarModelWarpper.setIsOpen(Integer.parseInt(query1.get("isOpen").toString())); serverCarModelWarpper.setIsOpenExclusive(Integer.parseInt(query1.get("isOpenExclusive").toString())); serverCarModelWarpper.setIsOpenPrice(Integer.parseInt(query1.get("isOpenPrice").toString())); String isCheck = map.get("isCheck").toString(); String[] split = isCheck.split(","); serverCarModelWarpper.setDx_check(Integer.valueOf(split[0])); serverCarModelWarpper.setYkj_check(Integer.valueOf(split[1])); serverCarModelWarpper.setPc_check(Integer.valueOf(split[2])); data.add(serverCarModelWarpper); } } return data; } public ServerCarModelWarpper getTextPrice(Integer companyId, double distance, long duration, long wait, Integer type, Map geocode) throws Exception { String provinceCode = geocode.get("provinceCode"); String cityCode = geocode.get("cityCode"); String districtCode = geocode.get("districtCode"); //获取匹配的城市价格配置 SystemPriceCity systemPriceCity = systemPriceCityService.queryOne(provinceCode, cityCode, districtCode); if (null == systemPriceCity) { return null; } Map query1 = systemPriceMapper.query(companyId, type, null, systemPriceCity.getId()); if (null == query1) {//排除没有设置价格的车型 return null; } Map map = new HashMap<>(); double d = distance / 1000;//实际公里 double t = duration / 60;//实际时间 JSONObject jsonObject = JSON.parseObject(query1.get("content").toString());//等待费 Double num1 = jsonObject.getDouble("num1"); Double num2 = jsonObject.getDouble("num2"); JSONArray mileageList = jsonObject.getJSONArray("mileageList"); Double num4 = jsonObject.getDouble("num4"); Double num5 = jsonObject.getDouble("num5"); String num6 = jsonObject.getString("num6"); Double num7 = jsonObject.getDouble("num7"); Double num8 = jsonObject.getDouble("num8"); String[] split = num6.split(" - "); Integer time1 = Integer.valueOf(split[0].replace(":", "")); Integer time2 = Integer.valueOf(split[1].replace(":", "")); long start_date = 0; long end_date = 0; if(time1.compareTo(time2) > 0){ //跨天 String[] split1 = split[1].split(":"); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1); calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split1[0])); calendar.set(Calendar.MINUTE, Integer.valueOf(split1[1])); calendar.set(Calendar.SECOND, 59); end_date = calendar.getTimeInMillis(); String[] split2 = split[0].split(":"); calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split2[0])); calendar.set(Calendar.MINUTE, Integer.valueOf(split2[1])); calendar.set(Calendar.SECOND, 0); start_date = calendar.getTimeInMillis(); }else{ String[] split1 = split[1].split(":"); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split1[0])); calendar.set(Calendar.MINUTE, Integer.valueOf(split1[1])); calendar.set(Calendar.SECOND, 59); end_date = calendar.getTimeInMillis(); String[] split2 = split[0].split(":"); calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split2[0])); calendar.set(Calendar.MINUTE, Integer.valueOf(split2[1])); calendar.set(Calendar.SECOND, 0); start_date = calendar.getTimeInMillis(); } Double amount = 0D; if (System.currentTimeMillis() > start_date && System.currentTimeMillis() <= end_date) { amount = num7; BigDecimal multiply = BigDecimal.ZERO; BigDecimal yt = BigDecimal.ZERO; if (d > num8) { if(null != mileageList){ List list = new ArrayList<>(); for (int i = 0; i < mileageList.size(); i++) { MileageObject mileageObject = mileageList.getObject(i, MileageObject.class); list.add(mileageObject); } //根据配置的里程倒序排序 list.sort(new Comparator() { @Override public int compare(MileageObject o1, MileageObject o2) { return o1.getMileage().compareTo(o2.getMileage()) * -1; } }); //计算每个阶段的增加费用 Double last = new BigDecimal(d).setScale(0, RoundingMode.DOWN).doubleValue(); for (int i = 0; i < list.size(); i++) { Double mileage = list.get(i).getMileage(); Double price = list.get(i).getPrice(); if(d > mileage){ if(last <= num2){ break; } multiply = multiply.add((new BigDecimal(last).subtract(new BigDecimal(mileage))).multiply(new BigDecimal(price))); last = mileage; } } } if (d > num4) { yt = new BigDecimal(d).subtract(new BigDecimal(num4)).multiply(new BigDecimal(num5)); } amount = new BigDecimal(num7).add(multiply).add(yt).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); } map.put("qibujia", num7); map.put("lichengfei", multiply.doubleValue()); map.put("yuantufei", yt.doubleValue()); map.put("zongfeiyong", amount); } else { amount = num1; BigDecimal multiply = BigDecimal.ZERO; BigDecimal yt = BigDecimal.ZERO; if (d > num2) { if(null != mileageList){ List list = new ArrayList<>(); for (int i = 0; i < mileageList.size(); i++) { MileageObject mileageObject = mileageList.getObject(i, MileageObject.class); list.add(mileageObject); } //根据配置的里程倒序排序 list.sort(new Comparator() { @Override public int compare(MileageObject o1, MileageObject o2) { return o1.getMileage().compareTo(o2.getMileage()) * -1; } }); //计算每个阶段的增加费用 Double last = new BigDecimal(d).setScale(0, RoundingMode.DOWN).doubleValue(); for (int i = 0; i < list.size(); i++) { Double mileage = list.get(i).getMileage(); Double price = list.get(i).getPrice(); if(d > mileage){ if(last <= num2){ break; } multiply = multiply.add((new BigDecimal(last).subtract(new BigDecimal(mileage))).multiply(new BigDecimal(price))); last = mileage; } } } if (d > num4) { yt = new BigDecimal(d).subtract(new BigDecimal(num4)).multiply(new BigDecimal(num5)); } amount = new BigDecimal(num1).add(multiply).add(yt).setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); } map.put("qibujia", num1); map.put("lichengfei", multiply.doubleValue()); map.put("yuantufei", yt.doubleValue()); map.put("zongfeiyong", amount); } List> list = serverCarModelMapper.queryServerCarModel(4); String isCheck = "0"; if(list.size() > 0){ Map objectMap = list.get(0); isCheck = objectMap.get("isCheck").toString(); } ServerCarModelWarpper serverCarModelWarpper = new ServerCarModelWarpper(); serverCarModelWarpper.setId(0); serverCarModelWarpper.setName("出租车"); serverCarModelWarpper.setAmount(map); serverCarModelWarpper.setDx_check(Integer.valueOf(isCheck)); serverCarModelWarpper.setContentPutOne(new JSONObject()); serverCarModelWarpper.setContentNotOne(new JSONObject()); serverCarModelWarpper.setContentPutTwo(new JSONObject()); serverCarModelWarpper.setContentNotTwo(new JSONObject()); serverCarModelWarpper.setContentPutThree(new JSONObject()); serverCarModelWarpper.setContentNotThree(new JSONObject()); serverCarModelWarpper.setContentExclusive(jsonObject); serverCarModelWarpper.setContentPrice(new JSONObject()); return serverCarModelWarpper; } public Map calculationPrice(JSONObject rule, double distance, long duration, long wait, Double waitMoney) throws Exception { Map map = new HashMap<>(); //等待费 Date date = new Date(); double d = distance / 1000;//实际公里 double t = duration / 60;//实际时间 double d1 = (d - rule.getDouble("num2")) < 0 ? 0 : d - rule.getDouble("num2");//超出起步里程的公里 double t1 = (t - rule.getDouble("num3")) < 0 ? 0 : t - rule.getDouble("num3");//超过起步分钟数的时间 double yt1 = 0;//远途1段 double yt2 = 0;//远途2段 double yt3 = 0;//远途3段 //夜间服务处理逻辑 Calendar s = Calendar.getInstance(); s.setTime(date); s.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num14").split(" - ")[0].split(":")[0])); s.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num14").split(" - ")[0].split(":")[1])); Calendar e = Calendar.getInstance(); e.setTime(date); e.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num14").split(" - ")[1].split(":")[0])); e.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num14").split(" - ")[1].split(":")[1])); if (date.getTime() > s.getTimeInMillis() && date.getTime() < e.getTimeInMillis()) { if (d > rule.getDouble("num6")) { yt1 = rule.getDouble("num18") * (d - rule.getDouble("num6")); } if (d > rule.getDouble("num9")) { yt2 = rule.getDouble("num19") * (d - rule.getDouble("num9")); } if (d > rule.getDouble("num12")) { yt3 = rule.getDouble("num20") * (d - rule.getDouble("num12")); } double amount = rule.getDouble("num15") + (d1 * rule.getDouble("num16")) + (t1 * rule.getDouble("num17")) + (wait * waitMoney) + yt1 + yt2 + yt3; map.put("qibujia", rule.getDouble("num15")); map.put("lichengfei", d1 * rule.getDouble("num16")); map.put("shichangfei", t1 * rule.getDouble("num17")); map.put("dengdaifei", wait * waitMoney); map.put("yuantufei", yt1 + yt2 + yt3); map.put("zongfeiyong", amount); }else{ Calendar s1 = Calendar.getInstance(); s1.setTime(date); s1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num21").split(" - ")[0].split(":")[0])); s1.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num21").split(" - ")[0].split(":")[1])); Calendar e1 = Calendar.getInstance(); e1.setTime(date); e1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num21").split(" - ")[1].split(":")[0])); e1.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num21").split(" - ")[1].split(":")[1])); Calendar s2 = Calendar.getInstance(); s2.setTime(date); s2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num22").split(" - ")[0].split(":")[0])); s2.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num22").split(" - ")[0].split(":")[1])); Calendar e2 = Calendar.getInstance(); e2.setTime(date); e2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num22").split(" - ")[1].split(":")[0])); e2.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num22").split(" - ")[1].split(":")[1])); //高峰时段处理逻辑 if ((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis()) || (date.getTime() > s2.getTimeInMillis() && date.getTime() < e2.getTimeInMillis())) { if (d > rule.getDouble("num6")) { yt1 = rule.getDouble("num26") * (d - rule.getDouble("num6")); } if (d > rule.getDouble("num9")) { yt2 = rule.getDouble("num27") * (d - rule.getDouble("num9")); } if (d > rule.getDouble("num12")) { yt3 = rule.getDouble("num28") * (d - rule.getDouble("num12")); } double amount = rule.getDouble("num23") + (d1 * rule.getDouble("num24")) + (t1 * rule.getDouble("num25")) + (wait * waitMoney) + yt1 + yt2 + yt3; map.put("qibujia", rule.getDouble("num23")); map.put("lichengfei", d1 * rule.getDouble("num24")); map.put("shichangfei", t1 * rule.getDouble("num25")); map.put("dengdaifei", wait * waitMoney); map.put("yuantufei", yt1 + yt2 + yt3); map.put("zongfeiyong", amount); }else{ //其他时间段的计算 if (d > rule.getDouble("num6")) { yt1 = rule.getDouble("num8") * (d - rule.getDouble("num6")); } if (d > rule.getDouble("num9")) { yt2 = rule.getDouble("num11") * (d - rule.getDouble("num9")); } if (d > rule.getDouble("num12")) { yt3 = rule.getDouble("num13") * (d - rule.getDouble("num12")); } double amount = rule.getDouble("num1") + (d1 * rule.getDouble("num4")) + (t1 * rule.getDouble("num5")) + (wait * waitMoney) + yt1 + yt2 + yt3; map.put("qibujia", rule.getDouble("num1")); map.put("lichengfei", d1 * rule.getDouble("num4")); map.put("shichangfei", t1 * rule.getDouble("num5")); map.put("dengdaifei", wait * waitMoney); map.put("yuantufei", yt1 + yt2 + yt3); map.put("zongfeiyong", amount); } } return map; } @Data public static class MileageObject{ private Double mileage; private Double price; } }