package com.zoniot.ccrc.service.impl; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.zoniot.ccrc.commom.exception.ServiceException; import com.zoniot.ccrc.commom.model.DeviceData; import com.zoniot.ccrc.commom.model.Pagination; import com.zoniot.ccrc.commom.utils.ExcelUtil; import com.zoniot.ccrc.commom.utils.UserUtil; import com.zoniot.ccrc.dao.OrganizationMapper; import com.zoniot.ccrc.dto.BuildingSelectDto; import com.zoniot.ccrc.dto.BuildingSelectInfoDto; import com.zoniot.ccrc.dto.DeviceDto; import com.zoniot.ccrc.dto.LoginUser; import com.zoniot.ccrc.service.BuildingService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.text.SimpleDateFormat; import java.util.List; import com.zoniot.ccrc.entity.Device; import com.zoniot.ccrc.dao.DeviceMapper; import com.zoniot.ccrc.service.DeviceService; import static com.google.common.collect.Lists.newArrayList; @Slf4j @Service public class DeviceServiceImpl implements DeviceService{ @Resource private DeviceMapper deviceMapper; @Autowired private BuildingService buildingService; @Resource private OrganizationMapper organizationMapper; @Override public int insertSelective(Device record) { return deviceMapper.insertSelective(record); } @Override public int updateByPrimaryKeySelective(Device record) { return deviceMapper.updateByPrimaryKeySelective(record); } @Override public int updateBatch(List list) { return deviceMapper.updateBatch(list); } @Override public int batchInsert(List list) { return deviceMapper.batchInsert(list); } @Override public Pagination pageList(Integer sysId, Integer deviceTypeId, Integer orgId, Integer province, Integer city, Integer region, Integer communityId, Integer buildingId, String deviceNo, String clientName, String locDesc, Integer status, Integer valveStatus, String sortColumn, String sortOrder, int pageNum, int pageSize) { LoginUser loginUser = UserUtil.getCurrentUser(); Integer userId = null; List communityIds = null; if (loginUser.getRoleType() !=null && loginUser.getRoleType() == 4) userId = loginUser.getId(); if (orgId != null) communityIds = organizationMapper.findCommunityIds(orgId); PageHelper.startPage(pageNum,pageSize); List list = deviceMapper.getList(loginUser.getSiteId(),userId,sysId,deviceTypeId,orgId,province,city,region,communityId,buildingId,deviceNo,clientName,locDesc,status,valveStatus,communityIds,sortColumn,sortOrder); return new Pagination<>(list); } @Override public void getDeviceListExcel(Integer sysId, Integer deviceTypeId, Integer orgId, Integer province, Integer city, Integer region, Integer communityId, Integer buildingId, String deviceNo, String clientName, String locDesc, Integer status, Integer valveStatus, String sortColumn, String sortOrder, HttpServletResponse httpServletResponse) { String title = "设备列表"; LoginUser loginUser = UserUtil.getCurrentUser(); Integer userId = null; List communityIds = null; if (loginUser.getRoleType() !=null && loginUser.getRoleType() == 4) userId = loginUser.getId(); if (orgId != null) communityIds = organizationMapper.findCommunityIds(orgId); List list = deviceMapper.getList(loginUser.getSiteId(),userId,sysId,deviceTypeId,orgId,province,city,region,communityId,buildingId,deviceNo,clientName,locDesc,status,valveStatus,communityIds,sortColumn,sortOrder); //水表档案号、水表电子号、设备状态、当前读数、机构、客户编号、客户名称、手机号、标签、建筑、安装地址、网格员 SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String[] rowsName = new String[]{"序号", "水表档案号", "水表电子号", "设备状态", "当前读数", "机构", "客户编号", "客户名称", "手机号", "标签","建筑","安装地址","网格员"}; List dataList = newArrayList(); Object[] objs = null; for (int i = 0; i < list.size(); i++) { DeviceDto data = list.get(i); objs = new Object[rowsName.length]; objs[0] = i; objs[1] = data.getFileNo(); objs[2] = data.getMeterNo(); objs[3] = getDeviceStatusName(data.getDeviceStatus()); objs[4] = data.getMeterReading(); objs[5] = data.getOrgName(); objs[6] = data.getCustomerNo(); objs[7] = data.getCustomerName(); objs[8] = data.getCustomerPhone(); objs[9] = data.getLabel(); objs[10] = data.getBuildingName(); objs[11] = data.getLocDesc(); objs[12] = data.getUsername(); dataList.add(objs); } ExcelUtil excelUtil = new ExcelUtil(title, rowsName, dataList); try { excelUtil.export(httpServletResponse); } catch (Exception e) { throw new ServiceException(-900, "导出异常"); } } private String getDeviceStatusName(Integer deviceStatus){ //1:正常 2:故障 3:无 4: 预警 5:未启用 if (deviceStatus == 1) return "正常"; if (deviceStatus == 2) return "故障"; if (deviceStatus == 4) return "预警"; if (deviceStatus == 5) return "未启用"; return ""; } @Override public List deviceAreaList(Integer sysId, Integer deviceTypeId, Integer orgId, Integer province, Integer city, Integer region, Integer communityId, Integer buildingId, String deviceNo, String clientName, String locDesc, Integer status, Integer valveStatus) { LoginUser loginUser = UserUtil.getCurrentUser(); Integer userId = null; List communityIds = null; if (loginUser.getRoleType() !=null && loginUser.getRoleType() == 4) userId = loginUser.getId(); if (orgId != null) communityIds = organizationMapper.findCommunityIds(orgId); List list = deviceMapper.deviceAreaList(loginUser.getSiteId(),userId,sysId,deviceTypeId,orgId,province,city,region,communityId,buildingId,deviceNo,clientName,locDesc,status,valveStatus,communityIds); return buildingService.areaList(list); } @Override public DeviceDto deviceDetailV2(Long deviceId) { return deviceMapper.findDeviceDetail(deviceId); } @Override public void syncDevice(DeviceData deviceData) { Integer siteId = organizationMapper.findSiteId(deviceData.getCustomerId()); //Integer siteId = 1; Device device = new Device(); device.setId(deviceData.getId()); device.setDeviceNo(deviceData.getDeviceNo()); device.setMeterNo(deviceData.getMeterNo()); device.setFileNo(deviceData.getFileNo()); device.setSealNo(deviceData.getSealNo()); device.setSiteId(siteId); device.setSysId(deviceData.getSysId()); device.setDeviceTypeId(deviceData.getDeviceTypeId()); device.setManufacturerId(deviceData.getManufacturerId()); device.setBuildingId(deviceData.getBuildingId()); device.setCommunityId(deviceData.getCommunityId()); device.setLocDesc(deviceData.getLocDesc()); device.setMeterReading(deviceData.getMeterReading()); device.setValveStatus(deviceData.getValveStatus()); device.setDeviceStatus(deviceData.getDeviceStatus()); device.setLastReceiveTime(deviceData.getLastReceiveTime()); device.setStatus(deviceData.getStatus()); device.setCreateBy("system"); device.setDateCreate(deviceData.getDateCreate()); device.setUpdateBy("system"); device.setDateUpdate(deviceData.getDateUpdate()); log.info("syncDevice device {}", JSON.toJSONString(device)); deviceMapper.replaceSelective(device); } }