VehicleCurrencyServiceImpl.java 20.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
package com.objecteye.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.objecteye.common.CommonResult;
import com.objecteye.dao.MongoTemplates;
import com.objecteye.entity.SyVehicleModel;
import com.objecteye.entity.SyVehicleNjbNumber;
import com.objecteye.entity.SyVehicleType;
import com.objecteye.pojo.*;
import com.objecteye.service.VehicleCurrencyService;
import com.objecteye.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

@Service
public class VehicleCurrencyServiceImpl implements VehicleCurrencyService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    public MongoTemplates mongoTemplates;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    public HttpClientUtils httpClientUtils;
    @Autowired
    public VehicleDetailsUtils vehicleDetailsUtils;
    @Autowired
    public RabbitMQVehicleTools rabbitMQVehicleTools;
    @Autowired
    private CompareDistance compareDistance;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 多条件查询
     *
     * @param vehicleCurrencyReques
     * @return
     */
    @Override
    public JSONObject findByCondition(VehicleCurrencyReques vehicleCurrencyReques) {

        JSONObject data = new JSONObject();
        int totalPage = 0;
        List<VehicleCurrencyResult> vehicleCurrencyResultList = null;
        try {
            Query query = new Query();
            //必须条件
            Criteria c = new Criteria();

            int currentpage = vehicleCurrencyReques.getCurrentpage();
            int pagevolume = vehicleCurrencyReques.getPagevolume();
            String hphm = vehicleCurrencyReques.getHphm();
            Long starttime = vehicleCurrencyReques.getStartTime();
            Long endtime = vehicleCurrencyReques.getEndTime();
            int[] condition = vehicleCurrencyReques.getCondition();
            String cllx = vehicleCurrencyReques.getCllx();
            Integer csys = vehicleCurrencyReques.getCsys();
            String brand = vehicleCurrencyReques.getBrand();
            String subbrand = vehicleCurrencyReques.getSubbrand();
            String birthday = vehicleCurrencyReques.getBirthday();
            Integer njbgs = vehicleCurrencyReques.getNjbgs();
            Integer hplx = vehicleCurrencyReques.getHplx();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //模糊号牌检索
            if (hphm != null && hphm.length() > 0) {
                c.and("vehicle_plate_hphm").regex(".*" + hphm + ".*");
            }

            //时间段范围内
            if (starttime != 0L && endtime != 0L) {
                c.and("pictime").gte(starttime).lte(endtime);
            }

            //车辆类型检索
            if (cllx != null && cllx.length() > 0) {
                c.and("vehicle_recg_type").is(cllx);
            }

            //车身颜色检索
            if (csys != null) {
                c.and("vehicle_color_index").is(csys);
            }

            //车辆型号检索
            if (brand != null && brand.length() > 0) {
                //车辆品牌
                c.and("vehicle_recg_brand").is(brand);
            }
            if (subbrand != null && subbrand.length() > 0) {
                //车辆品牌
                c.and("vehicle_recg_subbrand").is(subbrand);
            }
            if (birthday != null && birthday.length() > 0) {
                //车辆出场日期
                c.and("vehicle_recg_issue_year").is(birthday);
            }

            //年检标个数检索
            if (njbgs != null) {
                //获取年检标个数
                c.and("vehicle_pendant_detect_njbnumber").is(njbgs);
            }


            //号牌类型检索
            if (hplx != null) {
                c.and("vehicleplatetype").is(hplx);
            }

            /*无号牌(nullhphm):1          污损号牌(stained):2          主驾驶安全带(driverbelt):3          副驾驶安全带(copilotbelt):4
             主驾驶打电话(drivercall):5         主驾驶吸烟(driversmoke):6         是否苫盖(cover):7     有挂摆件(pendant): 8     有遮阳板(sunlouver): 9*/
            //条件查询
            if (condition != null && condition.length > 0) {
                for (int i : condition) {
                    switch (i) {
                        case 1:
                            //无号牌
                            c.and("vehicle_plate_numScore").is(0);
                            break;
                        case 2:
                            //污损号牌(stained)
                            //当识别度为负数时认为是污损号牌
                            c.and("vehicleplatedetectscore").is(-2);
                            //因目前sdk不支持污损号牌检测,所以此处略
                            break;
                        case 3:
                            //主驾驶未系安全带(driverbelt):
                            c.and("vehicle_illegal_driver_person_status").is(1004);
                            c.and("vehicle_illegal_driver_belt_status").is(1000);
                            break;
                        case 4:
                            //副驾驶未系安全带(copilotbelt):4
                            c.and("vehicle_illegal_copilot_person_status").is(1004);
                            c.and("vehicle_illegal_copilot_belt_status").is(1000);
                            break;
                        case 5:
                            //主驾驶打电话(drivercall):
                            c.and("vehicle_illegal_driver_person_status").is(1004);
                            c.and("vehicle_illegal_driver_phone_status").is(1000);
                            break;
                        case 6:
                            //主驾驶吸烟(driversmoke):
                            c.and("vehicle_illegal_driver_person_status").is(1004);
                            c.and("vehicle_illegal_driver_smoke_status").is(1000);
                            break;
                        case 7:
                            //是否苫盖(cover):
                            //因目前sdk不支持苫盖,所以此处略
                            break;
                        case 8:
                            //有挂摆件(pendant):
                            c.and("vehicle_pendant_detect_gjexis").is(1);
                            break;
                        case 9:
                            // 有遮阳板(sunlouver):
                            c.and("vehicle_pendant_detect_zybexis").is(1);
                            break;
                        default:
                            break;
                    }
                }
            }
            query.addCriteria(c);
            long totalNumber = mongoTemplate.count(query, VehicleCurrencyResult.class, "rabbitMQVehicle");

            //按时间进行排序
            query.with(Sort.by(Sort.Order.desc("pictime")));
            // 分页
            query.skip((currentpage - 1) * pagevolume).limit(pagevolume);

            vehicleCurrencyResultList = mongoTemplate.find(query, VehicleCurrencyResult.class, "rabbitMQVehicle");
            //遍历list,转换时间
            for (VehicleCurrencyResult vehicleCurrencyResult : vehicleCurrencyResultList) {
                vehicleCurrencyResult.setPhototime(sdf.format(Long.parseLong(vehicleCurrencyResult.getPhototime())));
                //改变原有的全景图为现在的快照图 此处返回快照图的绝对路径
                vehicleCurrencyResult.setPicurl(vehicleCurrencyResult.getSnapshoturl());
            }
            //根据总条数获取到总页数
            totalPage = (int) Math.ceil(((double) totalNumber) / pagevolume);

            data.put("total", totalPage);
            data.put("row", vehicleCurrencyResultList);
        } catch (Exception e) {
            data.put("total", totalPage);
            data.put("row", vehicleCurrencyResultList);
            e.printStackTrace();
        }
        return data;
    }

    @Override
    public CommonResult<PicVehicleDataResult> findVehicleByPic(int number, double threshold, int currentpage, int pagevolume, MultipartFile picfile, Long starttime, Long endtime) throws InterruptedException {
        //封装结果的对象
        if (picfile == null) {
            return CommonResult.success(201, "图片为空", null);
        }

        String vehicleInfoByBase64 = httpClientUtils.multiToVehicle(picfile);
        JSONObject dataJson = JSONObject.parseObject(vehicleInfoByBase64).getJSONObject("result");

        List<RabbitMQVehicle> rabbitMQVehicles = rabbitMQVehicleTools.sdkToRabbitVehicle(dataJson);

        //获取到用户选中的车辆
        int size = rabbitMQVehicles.size();
        if (number < size) {
            RabbitMQVehicle rabbitMqVehicle = rabbitMQVehicles.get(number);

            List<RabbitMQVehicle> vehcieListBy = mongoTemplates.getVehcieListBy(new VehicleCondition(0, 0, rabbitMqVehicle.getVehicle_plate_hphm(),
                    null, starttime, endtime, null, rabbitMqVehicle.getVehicle_color_index(), rabbitMqVehicle.getVehicle_recg_issue_year(),
                    rabbitMqVehicle.getVehicle_recg_brand(), rabbitMqVehicle.getVehicle_recg_type(), rabbitMqVehicle.getVehicle_recg_subbrand()));

            //获取该车辆的特征信息
            double[] currentFeature = rabbitMqVehicle.getVehicle_fea_feature();
            double currentFea = compareDistance.getDistance(currentFeature);

            Vector<PicVehicleCompare> pvc = new Vector<>();
            int arrCount = 1000;
            int threadNumber = (int) Math.ceil((double) vehcieListBy.size() / arrCount);
            final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
            for (int i = 0; i < threadNumber; i++) {
                int index = i;
                new Thread(() -> {
                    int fromIndex = index * arrCount;
                    int toIndex = fromIndex + arrCount;
                    toIndex = Math.min(toIndex, vehcieListBy.size());
                    for (RabbitMQVehicle rmqv : vehcieListBy.subList(fromIndex, toIndex)) {
                        double[] dbFeature = (double[]) redisTemplate.opsForHash().get(GlobalUtil.VEHICLE_FEATURE, rmqv.getId());
                        if (dbFeature != null) {
                            //进行比对
                            double dbFea = (double) redisTemplate.opsForHash().get(GlobalUtil.VEHICLE_FEATURE_DISTANCE, rmqv.getId());
                            double similar = compareDistance.simDistance(currentFeature, currentFea, dbFeature, dbFea);
                            if (threshold <= similar) {
                                if (similar > 1) {
                                    similar = 1.00;
                                }
                                PicVehicleCompare picVehicleCompare = new PicVehicleCompare();
                                picVehicleCompare.setSimilar(similar);
                                picVehicleCompare.setRabbitMQVehicle(rmqv);
                                pvc.add(picVehicleCompare);
                            }
                        }
                    }
                    countDownLatch.countDown();
                }).start();
            }
            countDownLatch.await();

            //对row进行排序
            pvc.sort((o1, o2) -> {
                if (o2.getSimilar() == o1.getSimilar()) {
                    return 0;
                } else {
                    double similar = o2.getSimilar();
                    double similar1 = o1.getSimilar();
                    return similar > similar1 ? 1 : -1;
                }
            });

            //对row进行分页查询
            ArrayList<PicVehicleCompare> picVehicleComparesNew = new ArrayList<>();
            int startNumber = (currentpage - 1) * pagevolume;
            int endNumber = startNumber + pagevolume;
            endNumber = pagevolume > (pvc.size() - startNumber) ? pvc.size() : endNumber;
            for (int m = startNumber; m < endNumber; m++) {
                PicVehicleCompare picVehicleCompare = pvc.get(m);
                picVehicleComparesNew.add(picVehicleCompare);
            }

            //获取total(总条数)
            int total = pvc.size();
            //根据总条数获取总页数
            int totalPage = (int) Math.ceil(((double) total) / pagevolume);

            ArrayList<PicVehicleRow> picVehicleRows = new ArrayList<>();
            for (PicVehicleCompare next : picVehicleComparesNew) {
                //获取id
                RabbitMQVehicle rabbitMqVehicle1 = next.getRabbitMQVehicle();
                // 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
                double similar = next.getSimilar();
                String picurl = rabbitMqVehicle1.getPicurl();

                //获取到快照路径
                String snapshoturl = rabbitMqVehicle1.getSnapshoturl();

                String bg = String.format("%.3f", similar);

                //因为需要将快照图的值给picurl所以需要下面的返回方式
                PicVehicleRow picVehicleRow = new PicVehicleRow(rabbitMqVehicle1.getId(), rabbitMqVehicle1.getRecordid(), rabbitMqVehicle1.getVehicle_plate_hphm(),
                        snapshoturl, picurl, bg,
                        simpleDateFormat.format(new Date(rabbitMqVehicle1.getPictime())));

                picVehicleRows.add(picVehicleRow);
            }

            PicVehicleDataResult picVehicleDataResult = new PicVehicleDataResult(totalPage, picVehicleRows);

            if (picVehicleDataResult.getRow().size() <= 0) {
                return CommonResult.success(201, "没有查询到任何数据", null);
            } else {
                return CommonResult.success(picVehicleDataResult);
            }
        }
        return CommonResult.success(202, "没有符合要求的数据", null);
    }


    /**
     * 查询所有的车辆型号首字母
     *
     * @return
     */
    @Override
    public CommonResult<List<String>> selectDisplayForInitials() {
        CommonResult<List<String>> commonResult = null;
        try {
            List<String> initialsArr = mongoTemplate.find(new Query(), SyVehicleModel.class).stream()
                    .map(SyVehicleModel::getInitials).distinct().collect(Collectors.toList());
            commonResult = returnCommonResult(initialsArr);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }

        return commonResult;
    }

    /**
     * 根据车辆首字母查询所有的车辆品牌
     *
     * @param initials
     * @return
     */
    @Override
    public CommonResult<List<String>> selectDisplayForBrand(String initials) {
        CommonResult<List<String>> commonResult = null;
        try {
            List<String> brandArr = mongoTemplate.find(Query.query(Criteria.where("initals").is(initials)), SyVehicleModel.class)
                    .stream().map(SyVehicleModel::getBrand).distinct().collect(Collectors.toList());
            commonResult = returnCommonResult(brandArr);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 根据车辆车辆品牌查询所有的车辆子品牌
     *
     * @param brand
     * @return
     */
    @Override
    public CommonResult<List<String>> selectDisplayForSubbrand(String brand) {

        CommonResult<List<String>> commonResult = null;
        try {
            List<String> subbrandArr = mongoTemplate.find(Query.query(Criteria.where("brand").is(brand)), SyVehicleModel.class)
                    .stream().map(SyVehicleModel::getSubbrand).distinct().collect(Collectors.toList());
            commonResult = returnCommonResult(subbrandArr);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 根据车辆子品牌查询所有的车辆年限
     *
     * @param subbrand
     * @return
     */
    @Override
    public CommonResult<List<String>> selectDisplayForBirthday(String subbrand) {
        CommonResult<List<String>> commonResult = null;
        try {
            List<String> birthdayArr = mongoTemplate.find(Query.query(Criteria.where("subbrand").is(subbrand)), SyVehicleModel.class)
                    .stream().map(SyVehicleModel::getBirthday).distinct().collect(Collectors.toList());
            commonResult = returnCommonResult(birthdayArr);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 返回所有车辆类型列表
     *
     * @return
     */
    @Override
    public CommonResult<List<String>> displayVehicleTypeList() {
        CommonResult<List<String>> commonResult = null;
        try {
            List<String> displayVehicleTypeList = mongoTemplate.find(new Query(), SyVehicleType.class)
                    .stream().map(SyVehicleType::getName).distinct().collect(Collectors.toList());

            commonResult = returnCommonResult(displayVehicleTypeList);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }

        return commonResult;
    }

    /**
     * 返回所有车辆车身颜色列表
     *
     * @return
     */
    @Override
    public CommonResult<List<NameValue>> displayVehicleColorList() {
        CommonResult<List<NameValue>> commonResult = null;
        try {
            List<NameValue> displayVehicleColorList = mongoTemplate.find(new Query(), NameValue.class, "syVehicleColor")
                    .stream().distinct().collect(Collectors.toList());
            commonResult = returnCommonResult1(displayVehicleColorList);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }

        return commonResult;
    }


    /**
     * 返回所有车辆号牌类型列表
     *
     * @return
     */
    @Override
    public CommonResult<List<NameValue>> displayVehiclePlateTypeList() {
        CommonResult<List<NameValue>> commonResult = null;
        try {
            List<NameValue> displayVehiclePlateTypeList = mongoTemplate
                    .find(new Query(), NameValue.class, "syVehiclePlateType").stream().distinct().collect(Collectors.toList());
            commonResult = returnCommonResult1(displayVehiclePlateTypeList);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }

        return commonResult;
    }

    /**
     * 返回所有车辆年检标个数列表
     *
     * @return
     */
    @Override
    public CommonResult<List<String>> displayVehicleNjbNumberList() {
        CommonResult<List<String>> commonResult = null;
        try {
            List<String> displayVehicleNjbNumberList = mongoTemplate.find(new Query(), SyVehicleNjbNumber.class)
                    .stream().map(syVehicleNjbNumber -> String.valueOf(syVehicleNjbNumber.getNjbNumber()))
                    .distinct().collect(Collectors.toList());
            commonResult = returnCommonResult(displayVehicleNjbNumberList);
        } catch (Exception e) {
            commonResult = CommonResult.success(null, "数据查询失败");
            e.printStackTrace();
        }

        return commonResult;
    }


    public CommonResult<List<String>> returnCommonResult(List<String> list) {
        CommonResult<List<String>> commonResult;
        if (list.size() <= 0) {
            commonResult = CommonResult.success(201, "没有查询到任何数据", null);
        } else {
            commonResult = CommonResult.success(list);
        }
        return commonResult;
    }

    public CommonResult<List<NameValue>> returnCommonResult1(List<NameValue> list) {
        CommonResult<List<NameValue>> commonResult;
        if (list.size() <= 0) {
            commonResult = CommonResult.success(201, "没有查询到任何数据", null);
        } else {
            commonResult = CommonResult.success(list);
        }
        return commonResult;
    }


}