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.log.LogObjectHolder; import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.shiro.ShiroUser; 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.TLocation; import com.stylefeng.guns.modular.system.model.TRegion; import com.stylefeng.guns.modular.system.model.TSite; import com.stylefeng.guns.modular.system.service.ITLocationService; import com.stylefeng.guns.modular.system.service.ITRegionService; import com.stylefeng.guns.modular.system.service.ITSiteService; 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.util.model.GeoFencingPolygon; import org.springframework.beans.factory.annotation.Autowired; 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.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; 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; @Autowired private GDMapGeocodingUtil gdMapGeocodingUtil; /** * 跳转到跨城站点管理首页 */ @RequestMapping("") public String index() { return PREFIX + "tSite.html"; } /** * 跳转到添加跨城站点管理 */ @RequestMapping("/tSite_add") public String tSiteAdd(Model model) { //省 List provinceList = tRegionService.selectList(new EntityWrapper().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 provinceList = tRegionService.selectList(new EntityWrapper().eq("parent_id", 0)); model.addAttribute("provinceList",provinceList); TRegion province = tRegionService.selectOne(new EntityWrapper().eq("code", tSite.getProvinceCode())); //市 List cityList = tRegionService.selectList(new EntityWrapper().eq("parent_id", province.getId())); model.addAttribute("cityList",cityList); if (SinataUtil.isNotEmpty(tSite.getCityCode())){ TRegion city = tRegionService.selectOne(new EntityWrapper().eq("code", tSite.getCityCode())); //区 List areaList = tRegionService.selectList(new EntityWrapper().eq("parent_id", city.getId())); model.addAttribute("areaList",areaList); }else{ model.addAttribute("areaList",new ArrayList<>()); } //查询所有的站点区域 List locationList = itLocationService.selectList(new EntityWrapper().eq("siteId", tSite.getId()).ne("state", 3)); model.addAttribute("locationList",locationList); model.addAttribute("disabled","disabled"); Integer type = itLocationService.selectList(new EntityWrapper().eq("siteId", tSite.getId())).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> page = new PageFactory>().defaultPage(); ShiroUser user = ShiroKit.getUser(); Integer companyId = null; if(1 != user.getRoleType()){ companyId = user.getObjectId(); } page.setRecords(tSiteService.getSiteList(page, beginTime, endTime, name, insertUser, city, state, companyId)); 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); String address = (ToolUtil.isNotEmpty(tSite.getProvince()) ? tSite.getProvince() : "") + (ToolUtil.isNotEmpty(tSite.getCity()) ? tSite.getCity() : "") + (ToolUtil.isNotEmpty(tSite.getDistrict()) ? tSite.getDistrict() : "") + tSite.getName(); String location = gdMapGeocodingUtil.geocoding1(address); tSite.setLonLat(location); tSite.setCompanyId(ShiroKit.getUser().getObjectId()); tSite.setDefaultPointAddress(ToolUtil.formatting(tSite.getDefaultPointAddress())); 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 String[] s1 = coordinate.split("_"); for (String s2 : s1) { GeoFencingPolygon gfp = new GeoFencingPolygon(); gfp.setId(IdGenerator.getId()); gfp.setSiteId(id); gfp.setName(jsonObject.getString("name")); String[] split = s2.split(";"); List 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) { String address = (ToolUtil.isNotEmpty(tSite.getProvince()) ? tSite.getProvince() : "") + (ToolUtil.isNotEmpty(tSite.getCity()) ? tSite.getCity() : "") + (ToolUtil.isNotEmpty(tSite.getDistrict()) ? tSite.getDistrict() : "") + tSite.getName(); String location1 = gdMapGeocodingUtil.geocoding1(address); tSite.setLonLat(location1); tSite.setDefaultPointAddress(ToolUtil.formatting(tSite.getDefaultPointAddress())); tSiteService.updateById(tSite); //删除站点区域 JSONArray jsonArray = JSON.parseArray(subArr); List siteId = itLocationService.selectList(new EntityWrapper().eq("siteId", tSite.getId())); List del = new ArrayList<>(); List 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); } }