package com.moral.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.MonitorPoint;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.MonitorPointMapper;
import com.moral.api.pojo.form.device.MonitorPointQueryForm;
import com.moral.api.service.DeviceService;
import com.moral.api.service.MonitorPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import com.moral.util.RegionCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author moral
* @since 2021-07-01
*/
@Service
public class MonitorPointServiceImpl extends ServiceImpl implements MonitorPointService {
@Autowired
MonitorPointMapper monitorPointMapper;
@Autowired
OrganizationService organizationService;
@Autowired
DeviceService deviceService;
@Override
public List queryByOrgIdAndRegionCode(MonitorPointQueryForm form) {
//取参
Integer organizationId = form.getOrganizationId();
Integer regionCode = form.getRegionCode();
String region = null;
if (regionCode != null) {
region = RegionCodeUtils.regionCodeConvertToName(regionCode);
}
//查询子组织
List childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
List organizationIds = new ArrayList<>();
for (Organization organization : childrenOrganization) {
organizationIds.add(organization.getId());
}
organizationIds.add(organizationId);
//查询站点
QueryWrapper queryMonitorPointsWrapper = new QueryWrapper<>();
//如果region不为空,就查询当前组织,所选城市下所有站点及设备信息
//如果region为空,则查询当前组织下所有的站点和设备
if (region != null){
queryMonitorPointsWrapper.eq(region, regionCode);
}
queryMonitorPointsWrapper.in("organization_id", organizationIds);
queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
List monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
//查询站点对应的设备
for (MonitorPoint monitorPoint : monitorPoints) {
List devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId());
monitorPoint.setDevices(devices);
}
return monitorPoints;
}
}