|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl.hksync; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.doumee.core.haikang.service.HKService; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.DateUtil; | 
|---|
|  |  |  | import com.doumee.dao.business.DeviceMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.PlatformDeviceMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.PlatformJobMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.PlatformMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Device; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Platform; | 
|---|
|  |  |  | import com.doumee.dao.business.model.PlatformDevice; | 
|---|
|  |  |  | import com.doumee.dao.business.model.PlatformJob; | 
|---|
|  |  |  | 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 java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class HkSyncPlatformsServiceImpl extends HkSyncBaseServiceImpl { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PlatformMapper platformMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PlatformJobMapper platformJobMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PlatformDeviceMapper platformDeviceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 同步海康月台数据 | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | //    @Async | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public String syncPlatforms(PlatformsListRequest param){ | 
|---|
|  |  |  | if(Constants.DEALING_HK_SYNCPLATFORM){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ; | 
|---|
|  |  |  | 
|---|
|  |  |  | List<Platform> deleteList = new ArrayList<>(); | 
|---|
|  |  |  | List<Platform> addList = new ArrayList<>(); | 
|---|
|  |  |  | List<Platform> editList = new ArrayList<>(); | 
|---|
|  |  |  | List<PlatformDevice> deviceList = new ArrayList<>(); | 
|---|
|  |  |  | Date date = new Date(); | 
|---|
|  |  |  | //查询全部门禁设备数据 | 
|---|
|  |  |  | List<Platform> allList = platformMapper.selectList(null); | 
|---|
|  |  |  | List<Platform> allList = platformMapper.selectList(new QueryWrapper<Platform>().lambda().eq(Platform::getIsdeleted,Constants.ZERO)); | 
|---|
|  |  |  | List<PlatformListInfoResponse> allHkList = getAllHkList(param); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取增删改数据集合 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | getDataChangeList(allList,allHkList,addList,editList,deleteList,date); | 
|---|
|  |  |  | getDataChangeList(allList,allHkList,addList,deviceList,editList,deleteList,date); | 
|---|
|  |  |  | if(deleteList.size()>0){ | 
|---|
|  |  |  | //逻辑删除 | 
|---|
|  |  |  | List<Integer> platIds = new ArrayList<>(); | 
|---|
|  |  |  | for(Platform d : deleteList){ | 
|---|
|  |  |  | platformMapper.updateById(d); | 
|---|
|  |  |  | platIds.add(d.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //更新删除的月台信息删正在执行的作业状态为【待签到】状态 | 
|---|
|  |  |  | platformJobMapper.update(null,  new UpdateWrapper<PlatformJob>().lambda() | 
|---|
|  |  |  | .eq(PlatformJob::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .notIn(PlatformJob::getStatus,Constants.PlatformJobStatus.WART_SIGN_IN.getKey() | 
|---|
|  |  |  | ,Constants.PlatformJobStatus.WAIT_CONFIRM.getKey() | 
|---|
|  |  |  | ,Constants.PlatformJobStatus.DONE.getKey() | 
|---|
|  |  |  | ,Constants.PlatformJobStatus.LEAVED.getKey() | 
|---|
|  |  |  | ,Constants.PlatformJobStatus.CANCEL.getKey() ) | 
|---|
|  |  |  | .in(PlatformJob::getPlatformId,platIds) | 
|---|
|  |  |  | .set(PlatformJob::getPlatformId,null) | 
|---|
|  |  |  | .set(PlatformJob::getPlatforms,null) | 
|---|
|  |  |  | .set(PlatformJob::getStatus,Constants.PlatformJobStatus.WART_SIGN_IN.getKey()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(addList.size()>0){ | 
|---|
|  |  |  | platformMapper.insert(addList); | 
|---|
|  |  |  | if(deviceList.size()>0){ | 
|---|
|  |  |  | platformDeviceMapper.insert(deviceList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(editList.size()>0){ | 
|---|
|  |  |  | for(Platform d : editList){ | 
|---|
|  |  |  | 
|---|
|  |  |  | //更新状态 (月台状态 0-无车 1-有车 2-超时停靠 3-错误停靠) | 
|---|
|  |  |  | for(PlatformStatusInfoResponse d : allHkList){ | 
|---|
|  |  |  | platformMapper.update(null,new UpdateWrapper<Platform>().lambda() | 
|---|
|  |  |  | .set(Platform::getStatus,d.getStatus()) | 
|---|
|  |  |  | .set(Platform::getPlatformStatus,d.getStatus()) | 
|---|
|  |  |  | .set(Platform::getEditDate,date) | 
|---|
|  |  |  | .eq(Platform::getHkId,d.getPlatformtId())); | 
|---|
|  |  |  | .eq(Platform::getHkId,d.getPlatformId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private void getDataChangeList(List<Platform> allList, | 
|---|
|  |  |  | List<PlatformListInfoResponse> allHkList, | 
|---|
|  |  |  | List<Platform> addList, | 
|---|
|  |  |  | List<Platform> addList , | 
|---|
|  |  |  | List<PlatformDevice> deviceList, | 
|---|
|  |  |  | List<Platform> editList, | 
|---|
|  |  |  | List<Platform> deleteList, Date date) { | 
|---|
|  |  |  | if(allHkList!=null && allHkList.size()>0){ | 
|---|
|  |  |  | 
|---|
|  |  |  | //如果已存在,则更新数据 | 
|---|
|  |  |  | model =  initDataByHkData(model,device,date); | 
|---|
|  |  |  | editList.add(model); | 
|---|
|  |  |  | //清空监控点数据 | 
|---|
|  |  |  | platformDeviceMapper.delete(new UpdateWrapper<PlatformDevice>().lambda() | 
|---|
|  |  |  | .eq(PlatformDevice::getPlatformId,model.getId()) | 
|---|
|  |  |  | .eq(PlatformDevice::getType,Constants.ONE) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | deviceList.addAll(getCameraList(device.getCameras(),model)); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //如果不存在,则新增数据 | 
|---|
|  |  |  | model = new Platform(); | 
|---|
|  |  |  | model =  initDataByHkData(model,device,date); | 
|---|
|  |  |  | platformMapper.insert(model); | 
|---|
|  |  |  | addList.add(model); | 
|---|
|  |  |  | deviceList.addAll(getCameraList(device.getCameras(),model)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //判断获取删除的门禁设备,逻辑删除 | 
|---|
|  |  |  | getDeleteList(allList,allHkList,deleteList,date); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<PlatformDevice> getCameraList(List<PlatformCameraInfoResponse> cameras, Platform model) { | 
|---|
|  |  |  | List<PlatformDevice> list = new ArrayList<>(); | 
|---|
|  |  |  | if(cameras!=null && cameras.size()>0){ | 
|---|
|  |  |  | for(PlatformCameraInfoResponse param :cameras){ | 
|---|
|  |  |  | PlatformDevice d = new PlatformDevice(); | 
|---|
|  |  |  | d.setCreateDate(model.getCreateDate()); | 
|---|
|  |  |  | d.setPlatformId(model.getId()); | 
|---|
|  |  |  | d.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | d.setDeviceId(param.getCameraId()); | 
|---|
|  |  |  | d.setName(param.getCameraName()); | 
|---|
|  |  |  | d.setType(Constants.ONE); | 
|---|
|  |  |  | list.add(d); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private Platform initDataByHkData(Platform model, PlatformListInfoResponse device,Date date ) { | 
|---|
|  |  |  | model.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | model.setCreateDate(date); | 
|---|
|  |  |  | model.setHkDate(date); | 
|---|
|  |  |  | model.setHkId(device.getPlatformId()); | 
|---|
|  |  |  | model.setCreateDate(DateUtil.getISO8601DateByStr(device.getCreateTime())); | 
|---|
|  |  |  | 
|---|
|  |  |  | model.setRemark(JSONObject.toJSONString(device)); | 
|---|
|  |  |  | return  model; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Platform getExistedDevice(PlatformListInfoResponse device, List<Platform> allList) { | 
|---|
|  |  |  | if(allList.size()>0){ | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|