package org.jeecg.modules.dry.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.api.vo.Result; import org.jeecg.common.config.TenantContext; import org.jeecg.common.constant.MqttConstant; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.DateUtils; import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.oConvertUtils; import org.jeecg.config.mybatis.MybatisPlusSaasConfig; import org.jeecg.modules.dry.api.EmqxApi; import org.jeecg.modules.dry.entity.DryEquipment; import org.jeecg.modules.dry.service.IDryEquipmentService; import org.jeecg.modules.dry.vo.MoEquVo; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @Api(tags = "移动端") @RestController @RequestMapping("/mobile") @Slf4j public class MobileController { @Autowired private IDryEquipmentService dryEquipmentService; @Autowired private RedisUtil redisUtil; @ApiOperation(value = "设备列表查询", notes = "设备列表查询") @GetMapping(value = "/equ/list") public Result> queryPageList(DryEquipment dryEquipment, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { //------------------------------------------------------------------------------------------------ //是否开启系统管理模块的多租户数据隔离【SAAS多租户模式】 if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) { dryEquipment.setTenantId(oConvertUtils.getInt(TenantContext.getTenant(), 0)); } //------------------------------------------------------------------------------------------------ QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(dryEquipment, req.getParameterMap()); Page page = new Page(pageNo, pageSize); Page voPage = new Page(pageNo, pageSize); IPage pageList = dryEquipmentService.page(page, queryWrapper); comp(pageList, voPage); return Result.OK(voPage); } private void comp(IPage pageList, Page page) { //当前租户id int tenantId = oConvertUtils.getInt(TenantContext.getTenant(), 0); List collect = pageList.getRecords().stream().map(item -> { MoEquVo vo = new MoEquVo(); BeanUtils.copyProperties(item, vo); String clientid = "client-" + tenantId + "-" + item.getCode(); JSONObject client = (JSONObject) redisUtil.get(MqttConstant.MQTT_ONLINE_CLIENT + clientid); //组装状态数据 if (client != null) { vo.setOnline(true); //连接时间 String st = client.getString("connectedAt"); vo.setUpTime(st); vo.setClientId(clientid); } return vo; }).collect(Collectors.toList()); //排序 collect.sort(Comparator.comparing(obj -> obj.getCode(), Comparator.nullsLast(Comparator.naturalOrder()))); collect.sort(Comparator.comparing(obj -> obj.getOnline(), Comparator.nullsLast(Comparator.naturalOrder()))); BeanUtils.copyProperties(pageList, page); page.setRecords(collect); } }