7.8
15832144755
2021-07-08 2080267e3245f0df9efc621c279922dc5ebb7c8e
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
package com.hxzkoa.services;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.hxzkoa.json.tb_achor;
import com.hxzkoa.json.tb_realocation;
import com.hxzkoa.json.tb_tag;
import com.hxzkoa.json.tb_track;
import com.hxzkoa.util.Config;
import com.hxzkoa.util.ModifyConfig;
 
@Service
public class LocationService {
    @PersistenceContext
    private EntityManager em;
    @Autowired
    private CaozuoService cs;
 
    public List<tb_realocation> getRealTimeLocation(String date) {
        String sql = null;
        Query query = null;
        sql = "SELECT id,tagid,x,y,z,layer,time FROM tb_track_" +date+ "";
        query = this.em.createNativeQuery(sql);
        List resultList = query.getResultList();
        List<tb_realocation> tb_realocationList = new ArrayList<tb_realocation>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_realocation realocation = new tb_realocation();
                Object[] obj = (Object[]) resultList.get(i);
                realocation.setId((int) obj[0]);
                realocation.setTagid(obj[1] == null ? "" : obj[1].toString());
                realocation.setX(obj[2] == null ? "" : obj[2].toString());
                realocation.setY(obj[3] == null ? "" : obj[3].toString());
                realocation.setZ(obj[4] == null ? "" : obj[4].toString());
                realocation.setLayer(obj[5] == null ? "" : obj[5].toString());
                realocation.setTime(obj[6] == null ? "" : obj[6].toString());
                tb_realocationList.add(realocation);
            }
        }
        return tb_realocationList;
    }
 
    public List<tb_realocation> getRealTimeLocation(int page,int perPage) {
        String sql = null;
        Query query = null;
        sql = "SELECT id,tagid,x,y,z,layer,time FROM tb_realocation LIMIT :start,:end";
        query = this.em.createNativeQuery(sql);
        query.setParameter("start", (page - 1) * perPage);
        query.setParameter("end", perPage);
        List resultList = query.getResultList();
        List<tb_realocation> tb_realocationList = new ArrayList<tb_realocation>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_realocation realocation = new tb_realocation();
                Object[] obj = (Object[]) resultList.get(i);
                realocation.setId((int) obj[0]);
                realocation.setTagid(obj[1] == null ? "" : obj[1].toString());
                realocation.setX(obj[2] == null ? "" : obj[2].toString());
                realocation.setY(obj[3] == null ? "" : obj[3].toString());
                realocation.setZ(obj[4] == null ? "" : obj[4].toString());
                realocation.setLayer(obj[5] == null ? "" : obj[5].toString());
                realocation.setTime(obj[6] == null ? "" : obj[6].toString());
                tb_realocationList.add(realocation);
            }
        }
        return tb_realocationList;
    }
 
    public int getRealTimeLocationCount(String date) {
        String sql = null;
        Query query = null;
        sql = "SELECT count(1) FROM tb_track_" +date+ "";
        query = this.em.createNativeQuery(sql);
        return Integer.parseInt(query.getSingleResult().toString());
    }
 
    public List<tb_realocation> searchRealTimeLocation(String input,String date) {
        String sql = null;
        Query query = null;
        sql = "SELECT id,tagid,x,y,z,layer,time FROM tb_track_" +date+ " WHERE tagid = :p_tagid";
        query = this.em.createNativeQuery(sql);
        query.setParameter("p_tagid", input);
        List resultList = query.getResultList();
        List<tb_realocation> tb_realocationList = new ArrayList<tb_realocation>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_realocation realocation = new tb_realocation();
                Object[] obj = (Object[]) resultList.get(i);
                realocation.setId((int) obj[0]);
                realocation.setTagid(obj[1] == null ? "" : obj[1].toString());
                realocation.setX(obj[2] == null ? "" : obj[2].toString());
                realocation.setY(obj[3] == null ? "" : obj[3].toString());
                realocation.setZ(obj[4] == null ? "" : obj[4].toString());
                realocation.setLayer(obj[5] == null ? "" : obj[5].toString());
                realocation.setTime(obj[6] == null ? "" : obj[6].toString());
                tb_realocationList.add(realocation);
            }
        }
        return tb_realocationList;
    }
 
    @Transactional
    public void realTimeLocation_delete(String[] checkVal,String date) {
        String sql = null;
        Query query = null;
        for (int i = 0; i < checkVal.length; i++) {
            int id = Integer.parseInt(checkVal[i]);
            sql = "DELETE FROM tb_track_" +date+ " WHERE id = :id";
            cs.tb_caozuo("tb_realocation", 2);
            query = this.em.createNativeQuery(sql);
            query.setParameter("id", id);
            query.executeUpdate();
        }
    }
 
    @Transactional
    public void realTimeLocation_deleteAll(String date) {
        String sql = null;
        Query query = null;
        sql = "DELETE FROM tb_track_" +date+ "";
        query = this.em.createNativeQuery(sql);
        query.executeUpdate();
    }
 
    @Transactional
    public int track_add(String date,tb_track track) {
        String sql = null;
        Query query = null;
        sql = "INSERT INTO tb_track_"+date+"(tagid,x,y,z,layer,time) VALUES(:tag_id,:x,:y,:z,:layer,now())";
        query = this.em.createNativeQuery(sql);
        query.setParameter("tag_id", track.getTagid());
        query.setParameter("x", track.getX());
        query.setParameter("y", track.getY());
        query.setParameter("z", track.getZ());
        query.setParameter("layer", track.getLayer());
        int executeUpdate = query.executeUpdate();
        return executeUpdate;
    }
    
    public List<tb_track> getTrack(String date,int page,int perPage) {
        // 日期格式 例20210101 
        String sql = null;
        Query query = null;
        sql = "SELECT id,tagid,x,y,z,layer,time FROM tb_track_"+ date +" LIMIT :start,:end";
        query = this.em.createNativeQuery(sql);
        query.setParameter("start", (page - 1) * perPage);
        query.setParameter("end", perPage);
        List resultList = query.getResultList();
        List<tb_track> tb_trackList = new ArrayList<tb_track>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_track track = new tb_track();
                Object[] obj = (Object[]) resultList.get(i);
                track.setId((int) obj[0]);
                track.setTagid(obj[1] == null ? "" : obj[1].toString());
                track.setX(obj[2] == null ? "" : obj[2].toString());
                track.setY(obj[3] == null ? "" : obj[3].toString());
                track.setZ(obj[4] == null ? "" : obj[4].toString());
                track.setLayer(obj[5] == null ? "" : obj[5].toString());
                track.setTime(obj[6] == null ? "" : obj[6].toString());
                tb_trackList.add(track);
            }
        }
        return tb_trackList;
    }
    
    public List<String> getTrackDateList() {
        String sql = null;
        Query query = null;
        sql = "select right(table_name,8) from information_schema.tables where table_schema='hxzkuwb' and table_name like 'tb_track_%'";
        query = this.em.createNativeQuery(sql);
        List<String> resultList = query.getResultList();
        return resultList;
    }
    
    @Transactional
    public void tag_time(tb_tag tag) {
        String sql = null;
        Query query = null;
        sql = "UPDATE tb_tag SET addtime=:addtime WHERE tag_id=:tag_id";
        cs.tb_caozuo("tb_tag", 3);
        query = this.em.createNativeQuery(sql);
        query.setParameter("addtime", tag.getAddtime());
        query.setParameter("tag_id", tag.getTag_id());
        query.executeUpdate();
    }
}