Pu Zhibing
2 天以前 949bf4798368d0fce115993427e03758d4b3c897
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
package com.stylefeng.guns.modular.system.controller.specialTrain;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.TDriverLine;
import com.stylefeng.guns.modular.system.model.TLocation;
import com.stylefeng.guns.modular.system.model.TRegion;
import com.stylefeng.guns.modular.system.service.ITLocationService;
import com.stylefeng.guns.modular.system.service.ITRegionService;
import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.model.GeoFencingPolygon;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.TSite;
import com.stylefeng.guns.modular.system.service.ITSiteService;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * 跨城站点管理控制器
 *
 * @author fengshuonan
 * @Date 2020-09-04 10:35:51
 */
@Controller
@RequestMapping("/tSite")
public class TSiteController extends BaseController {
    
    private String PREFIX = "/system/tSite/";
    
    @Autowired
    private ITSiteService tSiteService;
    
    @Autowired
    private ITRegionService tRegionService;
    
    @Autowired
    private ITLocationService itLocationService;
    
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
    
    @Autowired
    private MongoTemplate mongoTemplate;
    
    /**
     * 跳转到跨城站点管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tSite.html";
    }
    
    /**
     * 跳转到添加跨城站点管理
     */
    @RequestMapping("/tSite_add")
    public String tSiteAdd(Model model) {
        //省
        List<TRegion> provinceList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList", provinceList);
        return PREFIX + "tSite_add.html";
    }
    
    /**
     * 跳转到修改跨城站点管理
     */
    @RequestMapping("/tSite_update/{tSiteId}")
    public String tSiteUpdate(@PathVariable Integer tSiteId, Model model) {
        TSite tSite = tSiteService.selectById(tSiteId);
        model.addAttribute("item", tSite);
        LogObjectHolder.me().set(tSite);
        
        //省
        List<TRegion> provinceList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList", provinceList);
        
        TRegion province = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tSite.getProvinceCode()));
        //市
        List<TRegion> cityList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", province.getId()));
        model.addAttribute("cityList", cityList);
        
        if (SinataUtil.isNotEmpty(tSite.getCityCode())) {
            TRegion city = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tSite.getCityCode()));
            //区
            List<TRegion> areaList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", city.getId()));
            model.addAttribute("areaList", areaList);
        } else {
            model.addAttribute("areaList", new ArrayList<>());
        }
        if (SinataUtil.isNotEmpty(tSite.getDistrictCode())) {
            TRegion area = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tSite.getDistrictCode()));
            //乡镇街道
            List<TRegion> townList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", area.getId()));
            model.addAttribute("townList", townList);
        } else {
            model.addAttribute("townList", new ArrayList<>());
        }
        //查询所有的站点区域
        List<TLocation> locationList = itLocationService.selectList(new EntityWrapper<TLocation>().eq("siteId", tSite.getId()).ne("state", 3));
        model.addAttribute("locationList", locationList);
        model.addAttribute("locationList1", JSON.toJSONString(locationList));
        model.addAttribute("disabled", "disabled");
        
        if (CollectionUtils.isNotEmpty(locationList)) {
            Integer type = locationList.get(0).getType();
            model.addAttribute("type1", type == 1 ? "block" : "none");
            model.addAttribute("type2", type == 1 ? "none" : "block");
            model.addAttribute("checked1", type == 1 ? "checked" : "");
            model.addAttribute("checked2", type == 1 ? "" : "checked");
        }
        return PREFIX + "tSite_edit.html";
    }
    
    /**
     * 获取跨城站点管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String insertTime, String name, String insertUser, String city, Integer state) {
        String beginTime = null;
        String endTime = null;
        if (SinataUtil.isNotEmpty(insertTime)) {
            String[] timeArray = insertTime.split(" - ");
            beginTime = timeArray[0];
            endTime = timeArray[1];
        }
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        if (ShiroKit.getUser().getRoleType() != 1) {
            if (ShiroKit.getUser().getRoleType() == 2){
                page.setRecords(tSiteService.getCompanySiteList(page, beginTime, endTime, name, insertUser, city, state,ShiroKit.getUser().getId()));
            }
        } else {
            page.setRecords(tSiteService.getSiteList(page, beginTime, endTime, name, insertUser, city, state));
        }
        return super.packForBT(page);
    }
    
    /**
     * 新增跨城站点管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TSite tSite, @RequestParam String subArr) {
        tSite.setInsertTime(new Date());
        tSite.setInsertUserId(ShiroKit.getUser().getId());
        tSite.setState(1);
        tSiteService.insert(tSite);
        
        //添加站点区域
        addLocation(subArr, tSite.getId());
        return ResultUtil.success();
    }
    
    /**
     * 添加站点区域
     *
     * @param subArr
     * @param id
     */
    public void addLocation(String subArr, Integer id) {
        JSONArray jsonArray = JSON.parseArray(subArr);
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            TLocation location = new TLocation();
            location.setSiteId(id);
            location.setName(jsonObject.getString("name"));
            location.setInsertTime(new Date());
            location.setAddTime(jsonObject.getString("time"));
            location.setState(1);
            location.setUpdateTime(new Date());
            Integer type = jsonObject.getInteger("areaType");
            location.setType(type);
            if (1 == type) {
                if (SinataUtil.isNotEmpty(jsonObject.getString("provinceCode"))) {
                    location.setProvince(jsonObject.getString("province"));
                    location.setProvinceCode(jsonObject.getString("provinceCode"));
                }
                if (SinataUtil.isNotEmpty(jsonObject.getString("cityCode"))) {
                    location.setCity(jsonObject.getString("city"));
                    location.setCityCode(jsonObject.getString("cityCode"));
                }
                if (SinataUtil.isNotEmpty(jsonObject.getString("districtCode"))) {
                    location.setDistrict(jsonObject.getString("district"));
                    location.setDistrictCode(jsonObject.getString("districtCode"));
                }
            } else if (2 == type) {
                String coordinate = jsonObject.getString("coordinate");
                coordinate = coordinate.substring(0, coordinate.length() - 1);
                location.setCoordinate(coordinate);
                //将电子围栏坐标信息存储到MongoDB
                GeoFencingPolygon gfp = new GeoFencingPolygon();
                gfp.setId(IdGenerator.getId());
                gfp.setSiteId(id);
                gfp.setName(jsonObject.getString("name"));
                String[] split = coordinate.split(";");
                List<Point> points = new ArrayList<>();
                for (String s : split) {
                    String[] str = s.split(",");
                    Point point = new Point(Double.parseDouble(str[0]), Double.parseDouble(str[1]));
                    points.add(point);
                }
                //最后一个点要和第一个点相同
                points.add(new Point(points.get(0).getX(), points.get(0).getY()));
                gfp.setGeoJsonPolygon(new GeoJsonPolygon(points));
                mongoTemplate.save(gfp);
            }
            itLocationService.insert(location);
        }
    }
    
    /**
     * 操作跨城站点管理
     * optType 1=删除 2=冻结 3=解冻
     */
    @RequestMapping(value = "/opt")
    @ResponseBody
    public Object opt(@RequestParam Integer tSiteId, @RequestParam Integer optType) {
        TSite tSite = tSiteService.selectById(tSiteId);
        if (1 == optType) {
            tSite.setState(3);
            //删除mongoDB的区域信息
            Criteria criteria =Criteria.where("siteId").is(tSite.getId());
            Query query = new Query(criteria);
            mongoTemplate.remove(query,GeoFencingPolygon.class);
        } else if (2 == optType) {
            tSite.setState(2);
        } else if (3 == optType) {
            tSite.setState(1);
        }
        tSiteService.updateById(tSite);
        return SUCCESS_TIP;
    }
    
    /**
     * 修改跨城站点管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TSite tSite, @RequestParam String subArr) {
        tSiteService.updateById(tSite);
        
        //删除站点区域
        JSONArray jsonArray = JSON.parseArray(subArr);
        //查询站点对应区域数据
        List<TLocation> siteId = itLocationService.selectList(new EntityWrapper<TLocation>().eq("siteId", tSite.getId()));
        List<TLocation> del = new ArrayList<>();
        List<Object> add = new ArrayList<>();
        //遍历区域数据
        for (TLocation location : siteId) {
            boolean b = true;
            for(int i = 0; i < jsonArray.size(); i++){
                if(jsonArray.getJSONObject(i).getIntValue("id") == location.getId()){
                    b = false;
                }
            }
            if (b) {
                del.add(location);
                continue;
            }
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            if (jsonArray.getJSONObject(i).getInteger("id") == null) {
                add.add(jsonArray.get(i));
            }
        }
        for (TLocation location : del) {
            if (location.getType() == 2) {
                //删除mongoDB的区域信息
                Criteria criteria =Criteria.where("siteId").is(tSite.getId());
                Query query = new Query(criteria);
                mongoTemplate.remove(query,GeoFencingPolygon.class);
            }
            itLocationService.deleteById(location.getId());
        }
        
        //添加站点区域
        addLocation(JSON.toJSONString(add), tSite.getId());
        return SUCCESS_TIP;
    }
    
    /**
     * 跨城站点管理详情
     */
    @RequestMapping(value = "/detail/{tSiteId}")
    @ResponseBody
    public Object detail(@PathVariable("tSiteId") Integer tSiteId) {
        return tSiteService.selectById(tSiteId);
    }
}