MongoTemplates.java 12.8 KB
package com.objecteye.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.objecteye.pojo.FaceInfoParam;
import com.objecteye.pojo.RabbitMQVehicle;
import com.objecteye.pojo.VehicleCondition;
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.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Component
public class MongoTemplates {
    @Autowired
    private MongoTemplate mongoTemplate;

    //mongodb插入车辆数据
    public RabbitMQVehicle insertData(RabbitMQVehicle rabbitMQVehicle) {
        return mongoTemplate.insert(rabbitMQVehicle);

    }

    //mongodb插入司机数据
    public void insertData(FaceInfoParam faceInfoParam) {
        FaceInfoParam save = mongoTemplate.insert(faceInfoParam);
        System.out.println(save.getId());
    }

    //mongodb查询根据id
    public <T> T findById(Class<T> entityClass, String id) {
        return mongoTemplate.findById(id, entityClass);
    }

    //mongodb查询所有的数据信息
    public <T> List<T> findAll(Class<T> entityClass) {
        return mongoTemplate.findAll(entityClass);
    }

    //mongodb查询根据多条件
    public JSONObject findBy(VehicleCondition vehicleCondition, Integer vehicle_special_type_number) {
        JSONObject jsonObject = new JSONObject();

        List<JSONObject> vehicleTable = null;
        try {
            Query query = new Query();
            //必须条件
            Criteria c = new Criteria();
            //范围条件
            //模糊查询号牌
            int currentpage = vehicleCondition.getCurrentpage();
            int pagevolume = vehicleCondition.getPagevolume();
            String hphm = vehicleCondition.getHphm();
            Long starttime = vehicleCondition.getStarttime();
            Long endtime = vehicleCondition.getEndtime();
            int[] condition = vehicleCondition.getCondition();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //特殊品类检索
            if (vehicle_special_type_number != null) {
                c.and("vehicle_special_type").is(vehicle_special_type_number);
            }

            //模糊号牌检索
            if (hphm != null && hphm.length() > 0) {
                hphm = hphm.replaceAll("\\?", ".?").replaceAll("\\*", ".*");
                Pattern pattern = Pattern.compile("^.*" + hphm + ".*$", Pattern.CASE_INSENSITIVE);
                c.and("vehicle_plate_hphm").regex(pattern);
            }

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

            /*无号牌(nullhphm):1          污损号牌(stained):2          主驾驶安全带(driverbelt):3          副驾驶安全带(copilotbelt):4
             主驾驶打电话(drivercall):5         主驾驶吸烟(driversmoke):6         是否苫盖(cover):7*/
            //条件查询
            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(-1);  //当识别度为负数时认为是污损号牌
                            //因目前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不支持苫盖,所以此处略
                            c.and("vehicleplatedetectscore").is(-1);   //因为检测分数永远都不可能为负数
                            break;
                    }

                }
            }

            query.addCriteria(c);
            long totalNumber = mongoTemplate.count(query, JSONObject.class, "rabbitMQVehicle");

            //按时间进行排序
            query.with(new Sort(Sort.Direction.DESC, "pictime"));
            // 分页
            query.skip((currentpage - 1) * pagevolume).limit(pagevolume);

            vehicleTable = mongoTemplate.find(query, JSONObject.class, "rabbitMQVehicle");

            JSONObject data = new JSONObject();
            JSONArray array = new JSONArray();
            for (JSONObject json : vehicleTable) {
                String id = json.containsKey("_id") ? json.getString("_id") : null;
                String hphm_new = json.containsKey("vehicle_plate_hphm") ? json.getString("vehicle_plate_hphm") : null;
                String equipmentName = json.containsKey("equipmentName") ? json.getString("equipmentName") : null;
                String pictime = json.containsKey("pictime") ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(json.getLong("pictime"))) : null;
                String recordid = json.containsKey("recordid") ? json.getString("recordid") : null;

                json.put("site", equipmentName);
                json.put("id", id);
                json.put("hphm", hphm_new);
                json.put("phototime", pictime);
                json.put("recordid", recordid);

                //添加snapshoturl的值
                String snapshoturl = json.getString("snapshoturl");
                json.put("picurl", snapshoturl);   //此处将原来的全景图url换成了快照图绝对路径

                array.add(json);
            }

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

            data.put("total", totalPage);
            data.put("row", array);
            if (totalNumber > 0 && array != null && array.size() > 0) {
                jsonObject.put("code", 200);
                jsonObject.put("message", "success");
                jsonObject.put("data", data);
            } else {
                jsonObject.put("code", 201);
                jsonObject.put("message", "没有查询到符合要求的结果");
                jsonObject.put("data", data);
            }


        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 202);
            jsonObject.put("message", "请检查数据是否正确");
            jsonObject.put("data", null);
        }
        return jsonObject;

    }


    /**
     * 根据车辆条件查询符合条件的车辆列表
     *
     * @param vehicleCondition
     * @return
     */
    public List<RabbitMQVehicle> getVehcieListBy(VehicleCondition vehicleCondition) {
        List<RabbitMQVehicle> rabbitMqVehicles = null;
        try {
            Query query = new Query();
            //必须条件
            Criteria c = new Criteria();
            //范围条件
            //模糊查询号牌
            int currentpage = vehicleCondition.getCurrentpage();
            int pagevolume = vehicleCondition.getPagevolume();
            String hphm = vehicleCondition.getHphm();
            Long starttime = vehicleCondition.getStarttime();
            Long endtime = vehicleCondition.getEndtime();
            int[] condition = vehicleCondition.getCondition();

            //模糊号牌检索
            if (hphm != null && hphm.length() > 0) {
                hphm = hphm.replaceAll("\\?", "\\\\S").replaceAll("\\*", ".*");
                Pattern pattern = Pattern.compile(hphm, Pattern.CASE_INSENSITIVE);
                c.and("vehicle_plate_hphm").regex(pattern);
            }

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

            if (vehicleCondition.getVehicleSpecialType() != null) {
                c.and("vehicle_special_type").is(vehicleCondition.getVehicleSpecialType());
            }

            if (vehicleCondition.getVehicleColorIndex() != null) {
                c.and("vehicle_color_index").is(vehicleCondition.getVehicleColorIndex());
            }
            if (vehicleCondition.getVehicleRecgIssueYear() != null) {
                c.and("vehicle_recg_issue_year").is(vehicleCondition.getVehicleRecgIssueYear());
            }
            if (vehicleCondition.getVehicleRecgBrand() != null) {
                c.and("vehicle_recg_brand").is(vehicleCondition.getVehicleRecgBrand());
            }
            if (vehicleCondition.getVehicleRecgType() != null) {
                c.and("vehicle_recg_type").is(vehicleCondition.getVehicleRecgType());
            }
            if (vehicleCondition.getVehicleRecgSubbrand() != null) {
                c.and("vehicle_recg_subbrand").is(vehicleCondition.getVehicleRecgSubbrand());
            }

            /*无号牌(nullhphm):1          污损号牌(stained):2          主驾驶安全带(driverbelt):3          副驾驶安全带(copilotbelt):4
             主驾驶打电话(drivercall):5         主驾驶吸烟(driversmoke):6         是否苫盖(cover):7*/
            //条件查询
            if (condition != null && condition.length > 0) {
                for (int i : condition) {
                    switch (i) {
                        case 1:
                            //无号牌
                            c.and("vehicleplatedetectscore").lte(0.5);
                            break;
                        case 2:
                            //污损号牌(stained)
                            //因目前sdk不支持污损号牌检测,所以此处略
                            break;
                        case 3:
                            //主驾驶安全带(driverbelt):
                            c.and("vehicle_illegal_driver_belt_status").is(1000);
                            break;
                        case 4:
                            //副驾驶安全带(copilotbelt):4
                            c.and("vehicle_illegal_copilot_belt_status").is(1000);
                            break;
                        case 5:
                            //主驾驶打电话(drivercall):
                            c.and("vehicle_illegal_driver_phone_status").is(1000);
                            break;
                        case 6:
                            //主驾驶吸烟(driversmoke):
                            c.and("vehicle_illegal_driver_smoke_status").is(1000);
                            break;
                        case 7:
                            //是否苫盖(cover):
                            //因目前sdk不支持苫盖,所以此处略
                            break;
                        default:
                            break;
                    }
                }
            }
            query.addCriteria(c);

            // 分页
            query.skip((currentpage - 1) * pagevolume).limit(pagevolume);
            query.fields().include("id");
            query.fields().include("vehicle_plate_hphm");
            query.fields().include("picurl");
            query.fields().include("snapshoturl");
            query.fields().include("recordid");
            query.fields().include("equipmentName");
            query.fields().include("pictime");
            return mongoTemplate.find(query, RabbitMQVehicle.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rabbitMqVehicles;
    }
}