package cn.com.goldenwater.dcproj.service.impl.system; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.constValue.SplitValue; import cn.com.goldenwater.dcproj.constValue.StatusEnum; import cn.com.goldenwater.dcproj.dao.BisInspAllDao; import cn.com.goldenwater.dcproj.dao.BisInspAllRlationPersDao; import cn.com.goldenwater.dcproj.dao.OlBisInspOrgDao; import cn.com.goldenwater.dcproj.dao.PersPositionDao; import cn.com.goldenwater.dcproj.dao.PersPositionLatestDao; import cn.com.goldenwater.dcproj.dto.BisInspAllRlationPersDto; import cn.com.goldenwater.dcproj.dto.LoginUser; import cn.com.goldenwater.dcproj.dto.OlBisInspOrgDto; import cn.com.goldenwater.dcproj.model.BisInspAll; import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers; import cn.com.goldenwater.dcproj.model.BisInspOrg; import cn.com.goldenwater.dcproj.model.OlBisInspOrg; import cn.com.goldenwater.dcproj.model.PersPosition; import cn.com.goldenwater.dcproj.model.PersPositionLatest; import cn.com.goldenwater.dcproj.model.PersPositionViewLog; import cn.com.goldenwater.dcproj.param.BisInspAllParam; import cn.com.goldenwater.dcproj.param.BisInspAllRlationPersParam; import cn.com.goldenwater.dcproj.param.OlBisInspOrgParam; import cn.com.goldenwater.dcproj.param.PersPositionBaseParam; import cn.com.goldenwater.dcproj.param.PersPositionParam; import cn.com.goldenwater.dcproj.service.BisInspAllRlationPersService; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.service.PersPositionService; import cn.com.goldenwater.dcproj.service.PersPositionViewLogService; import cn.com.goldenwater.dcproj.utils.AMapUtil; import cn.com.goldenwater.dcproj.utils.BeanUtil; import cn.com.goldenwater.dcproj.utils.GeoUtil; import cn.com.goldenwater.dcproj.utils.MapUtil; import cn.com.goldenwater.dcproj.utils.expExcel.ExcelExport; import cn.com.goldenwater.dcproj.utils.expExcel.ExportAbstract; import cn.com.goldenwater.id.util.UuidUtil; import cn.com.goldenwater.target.CheckException; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.commons.collections.map.HashedMap; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Optional; /** * @author zhaohg * @date 2019-2-21 */ @Service @Transactional(rollbackFor = Exception.class) public class PersPositionServiceImpl extends AbstractCrudService implements PersPositionService { @Autowired private PersPositionDao persPositionDao; @Autowired private PersPositionLatestDao persPositionLatestDao; @Autowired private BisInspAllDao bisInspAllDao; @Autowired private OlBisInspOrgDao olBisInspOrgDao; @Autowired private BisInspAllRlationPersDao bisInspAllRlationPersDao; @Autowired private BisInspAllRlationPersService bisInspAllRlationPersService; @Autowired private PersPositionViewLogService persPositionViewLogService; @Autowired private OlBisInspOrgService olBisInspOrgService; private StringBuffer stb = new StringBuffer(); public PersPositionServiceImpl(PersPositionDao persPositionDao) { super(persPositionDao); this.persPositionDao = persPositionDao; } @Override public List findListByPersId(PersPositionParam persPositionParam) { return persPositionDao.findListByPersId(persPositionParam); } @Override public int insert(PersPosition persPosition) { int insert = persPositionDao.insert(persPosition); PersPositionLatest persPositionLatest = persPositionLatestDao.get(persPosition.getPersId()); Boolean isFirst = false; if (null == persPositionLatest && StringUtils.isBlank(persPositionLatest.getPersId())) { persPositionLatest = new PersPositionLatest(); isFirst = true; } persPositionLatest.setPersId(persPosition.getPersId()); persPositionLatest.setPersName(persPosition.getPersName()); persPositionLatest.setMobile(persPosition.getMobile()); persPositionLatest.setGroupId(persPosition.getGroupId()); persPositionLatest.setOrgId(persPosition.getOrgId()); persPositionLatest.setOperateTime(persPosition.getOperateTime()); persPositionLatest.setAdCode(persPosition.getAdCode()); persPositionLatest.setLon(persPosition.getLon()); persPositionLatest.setLat(persPosition.getLat()); persPositionLatest.setLongitude(persPosition.getLongitude()); persPositionLatest.setLatitude(persPosition.getLatitude()); if (persPosition.getAddress() == null || StringUtils.isBlank(persPosition.getAddress())) { double lon = persPosition.getLon(); double lat = persPosition.getLat(); String location = lon + "," + lat; String regeo = AMapUtil.regeo(location); com.alibaba.fastjson.JSONObject jsStr = com.alibaba.fastjson.JSONObject.parseObject(regeo); String status = jsStr.getString("status"); if (StatusEnum.START.getStatus().equals(status)) { com.alibaba.fastjson.JSONObject regeocode = jsStr.getJSONObject("regeocode"); if (regeocode != null) { String formatted_address = regeocode.getString("formatted_address"); persPositionLatest.setAddress(formatted_address); } } } if (!isFirst && StringUtils.isNotBlank(persPositionLatest.getPersId())) { persPositionLatestDao.update(persPositionLatest); } else { persPositionLatestDao.insert(persPositionLatest); } return insert; } @Override public List findClassify(Date operateTime, String persid, String province) { BisInspAllParam param = new BisInspAllParam(); param.setId("___"); List list = bisInspAllDao.findList(param); for (BisInspAll bisInspAll : list) { PersPositionParam persPositionParam = new PersPositionParam(); persPositionParam.setId(bisInspAll.getId()); persPositionParam.setProvince(province); int count = bisInspAllRlationPersDao.findListCountByClassify(persPositionParam); persPositionParam.setOperateTime(operateTime); int onLineCount = bisInspAllRlationPersDao.findListCountByClassify(persPositionParam); bisInspAll.setCount(count); bisInspAll.setOnLineCount(onLineCount); } return list; } @Override public PageInfo findPersListByClassify(PersPositionParam persPositionParam) { PageHelper.startPage(persPositionParam); List list = bisInspAllRlationPersDao.findListByClassify(persPositionParam); PageInfo pageInfo = new PageInfo(list); return pageInfo; } @Override public PersPosition getPersPosition(String persId) { List persPositionList = persPositionDao.getPersPosition(persId); if (null != persPositionList && persPositionList.size() > 0) { return persPositionList.get(0); } return new PersPosition(); } @Override public List findOrgList(Date operateTime) { int z1 = 0; int z2 = 0; ArrayList result = new ArrayList<>(); List list = olBisInspOrgDao.findList(new OlBisInspOrgParam()); for (OlBisInspOrg org : list) { if (null != org && StringUtils.isNotBlank(org.getOrgId())) { BisInspAllRlationPersParam param = new BisInspAllRlationPersParam(); String orgPid = org.getOrgId(); StringBuffer orgId = new StringBuffer(); List bisInspOrgList = this.persPositionDao.getOrgByPid(orgPid); if (bisInspOrgList.size() > 0) { orgId = getOrgId(bisInspOrgList).append(orgPid); stb = new StringBuffer(); } else { orgId.append(orgPid); } param.setOrgId(orgId.toString()); //递归获取下级所有orgId int count = bisInspAllRlationPersDao.selectCount(param); String newOrgId = orgId.toString(); if (orgId.toString().contains(SplitValue.DOUHAO_SPLIT)) { newOrgId = newOrgId.replace(",", "','"); newOrgId = "'" + newOrgId + "'"; } else { newOrgId = "'" + orgId.toString() + "'"; } int onlineCount = persPositionDao.selectCountByOrgId(operateTime, newOrgId); OlBisInspOrgDto orgDto = new OlBisInspOrgDto(); orgDto.setCount(count); orgDto.setOnLineCount(onlineCount); orgDto.setOrgId(org.getOrgId()); orgDto.setOrgNm(org.getOrgNm()); orgDto.setOrgPid(org.getOrgPid()); orgDto.setRlcode(org.getRlcode()); result.add(orgDto); if ("11".equals(org.getOrgId())) { z1 = count; } if ("12".equals(org.getOrgId())) { z2 = count; } } } for (OlBisInspOrgDto ol : result) { if ("1".equals(ol.getOrgId())) { ol.setCount(z1 + z2); } } return result; } @Override public PageInfo findPersPageByOrgId(PersPositionParam persPositionParam, HttpServletResponse response) { String orgPid = persPositionParam.getOrgId(); StringBuffer orgId = new StringBuffer(); List bisInspOrgList = this.persPositionDao.getOrgByPid(orgPid); if (bisInspOrgList.size() > 0) { orgId = getOrgId(bisInspOrgList).append(orgPid); stb = new StringBuffer(); } else { orgId.append(orgPid); } persPositionParam.setOrgId(orgId.toString()); if (StatusEnum.START.getStatus().equals(persPositionParam.getIsExport())) { List> mapList = new ArrayList<>(); for (BisInspAllRlationPersDto pers : bisInspAllRlationPersDao.findListByOrgIdOrderByOperateTime(persPositionParam.getPersId(), persPositionParam.getOrgId(), persPositionParam.getOperateTime(), persPositionParam.getPersName(), persPositionParam.getOrderBy(), persPositionParam.getCertificateCode(), persPositionParam.getDeptName(), persPositionParam.getStatus(), persPositionParam.getProvince(), true)) { mapList.add(BeanUtil.transBean2Map(pers)); } ExportAbstract export = new ExcelExport(); export.setFileName("人员列表"); export.setExport_ps_export(true); export.setExport_ps_type(ExportAbstract.XLS); export.setExport_bzip(false); export.setTitle("人员列表"); ArrayList cols = new ArrayList(); cols.add("persName[姓名]"); cols.add("sex[性别]"); cols.add("dpnm[部门]"); cols.add("dppost[职称]"); cols.add("mobilenumb[联系方式]"); cols.add("orgNm[机构]"); cols.add("bornDate[出生日期]"); export.setCols(cols); export.setGroupable(false); // 设置视图指标 export.setLevel(1); export.setLocksize(0); try { export.Export(response); export.ExportHeadForCustom(response);//导出表头 export.ContinueExport(mapList); export.EndExport(); } catch (Exception e) { e.printStackTrace(); } } PageHelper.startPage(persPositionParam); List list = bisInspAllRlationPersDao.findListByOrgIdOrderByOperateTime(persPositionParam.getPersId(), persPositionParam.getOrgId(), persPositionParam.getOperateTime(), persPositionParam.getPersName(), persPositionParam.getOrderBy(), persPositionParam.getCertificateCode(), persPositionParam.getDeptName(), persPositionParam.getStatus(), persPositionParam.getProvince(), true); PageInfo pageInfo = new PageInfo(list); return pageInfo; } @Override public OlBisInspOrgDto findPersCount(Date operateTime, String orgId) { //设置pc 在线时间为 1小时 String startTime; String endTime; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); endTime = sdf.format(calendar.getTime()); calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) - 1); startTime = sdf.format(calendar.getTime()); BisInspAllRlationPersParam param = new BisInspAllRlationPersParam(); int count = bisInspAllRlationPersDao.selectCount(param); int onlineCount = persPositionDao.selectOnlineCount(operateTime); int onLineCountPc = bisInspAllRlationPersService.selectCountOnlinePers(startTime, endTime, orgId); OlBisInspOrgDto orgDto = new OlBisInspOrgDto(); orgDto.setCount(count); orgDto.setOnLineCount(onlineCount); orgDto.setOnLineCountPc(onLineCountPc); return orgDto; } @Override public List findNodeByPersId(Date operateTime, String persId, String orgId) { String province = olBisInspOrgService.getProvince(orgId); List list = bisInspAllDao.findNodeByPersId(persId, province); for (BisInspAll bisInspAll : list) { PersPositionParam persPositionParam = new PersPositionParam(); persPositionParam.setId(bisInspAll.getId()); int count = bisInspAllRlationPersDao.findListCountByClassify(persPositionParam); persPositionParam.setOperateTime(operateTime); int onLineCount = bisInspAllRlationPersDao.findListCountByClassify(persPositionParam); bisInspAll.setCount(count); bisInspAll.setOnLineCount(onLineCount); } return list; } @Override public void insertList(List persPositions) { if (null == persPositions || 0 == persPositions.size()) { return; } int i = 0; PersPosition position = null; for (PersPosition persPosition : persPositions) { String uuid = UuidUtil.uuid(); // 生成uuid persPosition.setId(uuid); Map map = GeoUtil.gcj02towgs84(persPosition.getLon(), persPosition.getLat()); persPosition.setLatitude(map.get("lat")); persPosition.setLongitude(map.get("lon")); if (i == 0) { position = persPosition; } else { if (position.getOperateTime().getTime() < persPosition.getOperateTime().getTime()) { position = persPosition; } } i++; } persPositionDao.insertList(persPositions); PersPositionLatest persPositionLatest = persPositionLatestDao.get(position.getPersId()); Boolean isFirst = false; if (null == persPositionLatest) { persPositionLatest = new PersPositionLatest(); isFirst = true; } persPositionLatest.setPersId(position.getPersId()); persPositionLatest.setPersName(position.getPersName()); persPositionLatest.setMobile(position.getMobile()); persPositionLatest.setGroupId(position.getGroupId()); persPositionLatest.setOrgId(position.getOrgId()); persPositionLatest.setOperateTime(position.getOperateTime()); persPositionLatest.setAdCode(position.getAdCode()); persPositionLatest.setLon(position.getLon()); persPositionLatest.setLat(position.getLat()); persPositionLatest.setLongitude(position.getLongitude()); persPositionLatest.setLatitude(position.getLatitude()); if (position.getAddress() == null || StringUtils.isBlank(position.getAddress())) { double lon = position.getLon(); double lat = position.getLat(); String location = lon + SplitValue.DOUHAO_SPLIT + lat; String regeo = AMapUtil.regeo(location); com.alibaba.fastjson.JSONObject jsStr = com.alibaba.fastjson.JSONObject.parseObject(regeo); if (jsStr != null) { String status = jsStr.getString("status"); if (StatusEnum.START.getStatus().equals(status)) { com.alibaba.fastjson.JSONObject regeocode = jsStr.getJSONObject("regeocode"); if (regeocode != null) { String formatted_address = regeocode.getString("formatted_address"); persPositionLatest.setAddress(formatted_address); } } } } if (!isFirst && StringUtils.isNotBlank(persPositionLatest.getPersId())) { persPositionLatestDao.update(persPositionLatest); } else { persPositionLatestDao.insert(persPositionLatest); } } @Override public PersPosition fingPositionByPersid(String persid, String sttm, String entm, LoginUser loginUser, String orgId) { // 增加查询记录 BisInspAllRlationPers pers = bisInspAllRlationPersDao.get(persid); PersPositionViewLog persPositionViewLog = new PersPositionViewLog(); persPositionViewLog.setPersIdViewed(persid); persPositionViewLog.setPersNameViewed(pers.getPersName()); persPositionViewLog.setOrgId(orgId); BisInspOrg inspOrg = olBisInspOrgService.getDefaultOrg(orgId); persPositionViewLog.setAdCode(inspOrg.getAdCode()); persPositionViewLog.setAdName(inspOrg.getOrgNm()); persPositionViewLog.setPersId(loginUser.getPersId()); persPositionViewLog.setPersName(loginUser.getPersName()); persPositionViewLog.setNote(String.format("persid:%s;sttm:%s;entm:%s",persid,sttm,entm)); persPositionViewLogService.insert(persPositionViewLog); // 查询轨迹 //设置日期格式 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式 SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"); //设置日期格式 SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss"); PersPosition p1 = new PersPosition(); List persPositionList = this.persPositionDao.fingPositionByPersid(persid, sttm, entm); List> tList = new ArrayList<>(); if (persPositionList.size() > 0) { List list = new ArrayList(); List listGd = new ArrayList(); //高德 Date stDate = new Date(); Date date = new Date(); Date date1 = new Date(); int i = 0; for (PersPosition p : persPositionList) { i++; if (i == 1) { p1.setAdCode(p.getAdCode()); p1.setGroupId(p.getGroupId()); p1.setId(p.getId()); p1.setLatitude(p.getLatitude()); p1.setLongitude(p.getLongitude()); p1.setLon(p.getLon()); p1.setLat(p.getLat()); p1.setOrgId(p.getOrgId()); p1.setPersName(p.getPersName()); p1.setPersId(p.getPersId()); date = p.getOperateTime(); stDate = date; } else { date1 = p.getOperateTime(); //作比较 long a = date1.getTime() - date.getTime(); a = a / (60000); if (a < 2) { List l = new ArrayList(); List lgd = new ArrayList(); l.add(p.getLongitude()); l.add(p.getLatitude()); lgd.add(p.getLon()); lgd.add(p.getLat()); list.add(l); listGd.add(lgd); } else { Date enDate = date; Map map = new HashedMap(); map.put("stDate", df.format(stDate)); map.put("enDate", df.format(enDate)); map.put("pointList", list); map.put("pointListGd", listGd); map.put("mileage", MapUtil.calculateArrayLineDistance(listGd));//里程 map.put("date", df1.format(stDate));//日期 map.put("timeStr", df2.format(stDate) + "~" + df2.format(enDate));//时间段 tList.add(map); stDate = date1; List l = new ArrayList(); List lgd = new ArrayList(); l.add(p.getLongitude()); l.add(p.getLatitude()); lgd.add(p.getLon()); lgd.add(p.getLat()); list = new ArrayList<>(); listGd = new ArrayList<>(); list.add(l); listGd.add(lgd); } date = date1; } } Date enDate = date; Map map = new HashedMap(); map.put("stDate", df.format(stDate)); map.put("enDate", df.format(enDate)); map.put("pointList", list); map.put("pointListGd", listGd); map.put("mileage", MapUtil.calculateArrayLineDistance(listGd)); map.put("date", df1.format(stDate));//日期 map.put("timeStr", df2.format(stDate) + "~" + df2.format(enDate));//时间段 tList.add(map); } p1.setItudeList(tList); return p1; } @Override public PageInfo fingPositionList(PersPositionBaseParam persPositionBaseParam) { PageHelper.startPage(persPositionBaseParam); List list = this.persPositionDao.fingPositionList(persPositionBaseParam); PageInfo pageInfo = new PageInfo(list); return pageInfo; } @Override public List findPersListByOrgId(PersPositionParam persPositionParam) { /* 山东需求:综合展示-督查人员所在位置设置最高权限人员查看,其他人员只能查看近24小时位置信息 */ // 1. 判断用户是否管理员 BisInspAllRlationPers per = bisInspAllRlationPersService.get(persPositionParam.getPersId()); Optional.ofNullable(per).orElseThrow(() -> new CheckException("persId invalid")); Optional.of(per).map(BisInspAllRlationPers::getPersType).ifPresent(type -> { if ("1".equals(type)) { // 是管理员 isAdmin 设置为 true persPositionParam.setAdmin(true); } }); persPositionParam.setProvince(olBisInspOrgService.getProvince(persPositionParam.getOrgId())); return this.bisInspAllRlationPersDao.findListByOrgIdOrderByOperateTime(persPositionParam.getPersId(), persPositionParam.getOrgId(), persPositionParam.getOperateTime(), persPositionParam.getPersName(), persPositionParam.getOrderBy(), persPositionParam.getCertificateCode(), persPositionParam.getDeptName(), persPositionParam.getStatus(), persPositionParam.getProvince(), persPositionParam.getAdmin() ); } @Override public List findPersListById(PersPositionParam persPositionParam) { if (StringUtils.isNotBlank(persPositionParam.getPersId())) { if ("1".equals(bisInspAllRlationPersDao.get(persPositionParam.getPersId()).getPersType())) { persPositionParam.setPersId(""); } } List list = bisInspAllRlationPersDao.findListByPersId(persPositionParam); return list; } @Override public PageInfo findPersPageById(PersPositionParam persPositionParam) { if (StringUtils.isNotBlank(persPositionParam.getPersId())) { if ("1".equals(bisInspAllRlationPersDao.get(persPositionParam.getPersId()).getPersType())) { persPositionParam.setPersId(""); } } PageHelper.startPage(persPositionParam); List list = bisInspAllRlationPersDao.findListByPersId(persPositionParam); PageInfo pageInfo = new PageInfo<>(list); return pageInfo; } public StringBuffer getOrgId(List bisInspOrgList) { for (BisInspOrg bisInspOrg : bisInspOrgList) { stb.append(bisInspOrg.getOrgId()); stb.append(SplitValue.DOUHAO_SPLIT); List bisList = persPositionDao.getOrgByPid(bisInspOrg.getOrgId()); if (bisList.size() > 0) { getOrgId(bisList); } } return stb; } }