模板、设备排序

This commit is contained in:
Jerry Yan 2025-02-20 18:37:23 +08:00
parent 7b40ae85d0
commit 240706c11c
19 changed files with 177 additions and 1 deletions

View File

@ -4,7 +4,9 @@ import com.github.pagehelper.PageInfo;
import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity; import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
import com.ycwl.basic.model.pc.device.req.DeviceAddOrUpdateReq; import com.ycwl.basic.model.pc.device.req.DeviceAddOrUpdateReq;
import com.ycwl.basic.model.pc.device.req.DeviceReqQuery; import com.ycwl.basic.model.pc.device.req.DeviceReqQuery;
import com.ycwl.basic.model.pc.device.req.DeviceSortRequest;
import com.ycwl.basic.model.pc.device.resp.DeviceRespVO; import com.ycwl.basic.model.pc.device.resp.DeviceRespVO;
import com.ycwl.basic.model.pc.template.req.TemplateSortRequest;
import com.ycwl.basic.service.pc.DeviceService; import com.ycwl.basic.service.pc.DeviceService;
import com.ycwl.basic.utils.ApiResponse; import com.ycwl.basic.utils.ApiResponse;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -54,6 +56,12 @@ public class DeviceController {
return deviceService.updateStatus(id); return deviceService.updateStatus(id);
} }
@ApiOperation("排序设备")
@PostMapping("/sort")
public ApiResponse<Boolean> sortDevice(@RequestBody DeviceSortRequest request) {
return deviceService.sortDevice(request.getDeviceId(), request.getAfterDeviceId());
}
@GetMapping("/config/{id}") @GetMapping("/config/{id}")
public ApiResponse<DeviceConfigEntity> getConfig(@PathVariable("id") Long id) { public ApiResponse<DeviceConfigEntity> getConfig(@PathVariable("id") Long id) {
return ApiResponse.success(deviceService.getConfig(id)); return ApiResponse.success(deviceService.getConfig(id));

View File

@ -5,6 +5,7 @@ import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
import com.ycwl.basic.model.pc.template.entity.TemplateConfigEntity; import com.ycwl.basic.model.pc.template.entity.TemplateConfigEntity;
import com.ycwl.basic.model.pc.template.entity.TemplateEntity; import com.ycwl.basic.model.pc.template.entity.TemplateEntity;
import com.ycwl.basic.model.pc.template.req.TemplateReqQuery; import com.ycwl.basic.model.pc.template.req.TemplateReqQuery;
import com.ycwl.basic.model.pc.template.req.TemplateSortRequest;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO; import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import com.ycwl.basic.service.pc.TemplateService; import com.ycwl.basic.service.pc.TemplateService;
import com.ycwl.basic.utils.ApiResponse; import com.ycwl.basic.utils.ApiResponse;
@ -63,6 +64,11 @@ public class TemplateController {
return templateService.updateStatus(id); return templateService.updateStatus(id);
} }
@ApiOperation("排序模板")
@PostMapping("/sort")
public ApiResponse<Boolean> sortTemplate(@RequestBody TemplateSortRequest request) {
return templateService.sortTemplate(request.getTemplateId(), request.getAfterTemplateId());
}
@GetMapping("/config/{id}") @GetMapping("/config/{id}")
public ApiResponse<TemplateConfigEntity> getConfig(@PathVariable("id") Long id) { public ApiResponse<TemplateConfigEntity> getConfig(@PathVariable("id") Long id) {

View File

@ -45,4 +45,8 @@ public interface DeviceMapper {
int updateOnlineStatus(Long id, String ipAddr, int online, Date keepaliveAt); int updateOnlineStatus(Long id, String ipAddr, int online, Date keepaliveAt);
DeviceEntity getByDeviceNo2(String deviceNo); DeviceEntity getByDeviceNo2(String deviceNo);
List<Long> listAllByScenicId(Long scenicId);
int updateSort(Long id, Integer sort);
} }

View File

@ -36,6 +36,8 @@ public interface TaskMapper {
int countByMemberIdStauFinish(String userId); int countByMemberIdStauFinish(String userId);
List<TaskRespVO> selectNotRunning(); List<TaskRespVO> selectNotRunning();
List<TaskEntity> selectAllNotRunning();
List<TaskEntity> selectAllRunning();
void assignToWorker(@Param("taskId") Long taskId, @Param("workerId") Long workerId); void assignToWorker(@Param("taskId") Long taskId, @Param("workerId") Long workerId);
void deassign(@Param("taskId") Long taskId); void deassign(@Param("taskId") Long taskId);

View File

@ -35,5 +35,9 @@ public interface TemplateMapper {
List<TemplateEntity> listEnabledByScenicId(Long scenicId); List<TemplateEntity> listEnabledByScenicId(Long scenicId);
List<TemplateEntity> listEnabled(); List<TemplateEntity> listEnabled();
List<Long> listEnabledTemplateIdByScenicId(Long scenicId); List<Long> listEnabledTemplateIdByScenicId(Long scenicId);
List<Long> listAllTemplateIdByScenicId(Long scenicId);
List<ContentPageVO> listFor(@Param("scenicId") Long scenicId); List<ContentPageVO> listFor(@Param("scenicId") Long scenicId);
int updateSort(Long templateId, Integer sort);
} }

View File

@ -47,6 +47,7 @@ public class DeviceEntity {
private Integer online; private Integer online;
private String ipAddr; private String ipAddr;
private Date keepaliveAt; private Date keepaliveAt;
private Integer sort;
private Date createAt; private Date createAt;
private Date updateAt; private Date updateAt;
} }

View File

@ -0,0 +1,13 @@
package com.ycwl.basic.model.pc.device.req;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class DeviceSortRequest {
@ApiModelProperty(value = "被操作模板的ID", required = true)
private Long deviceId;
@ApiModelProperty(value = "排在其后的模板ID", required = false)
private Long afterDeviceId;
}

View File

@ -55,4 +55,5 @@ public class TaskEntity {
private Date createTime; private Date createTime;
private Date updateTime; private Date updateTime;
private Integer automatic; private Integer automatic;
private Date startTime;
} }

View File

@ -0,0 +1,13 @@
package com.ycwl.basic.model.pc.template.req;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class TemplateSortRequest {
@ApiModelProperty(value = "被操作模板的ID", required = true)
private Long templateId;
@ApiModelProperty(value = "排在其后的模板ID", required = false)
private Long afterTemplateId;
}

View File

@ -10,7 +10,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component @Component
public class DeviceRepository { public class DeviceRepository {
@ -126,4 +128,9 @@ public class DeviceRepository {
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, device.getId()), JSONObject.toJSONString(device)); redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, device.getId()), JSONObject.toJSONString(device));
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, device.getNo()), JSONObject.toJSONString(device)); redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, device.getNo()), JSONObject.toJSONString(device));
} }
public List<DeviceEntity> getAllDeviceByScenicId(Long scenicId) {
List<Long> deviceIdList = deviceMapper.listAllByScenicId(scenicId);
return deviceIdList.stream().map(this::getDevice).collect(Collectors.toList());
}
} }

View File

@ -68,6 +68,15 @@ public class TemplateRepository {
return idList.stream().map(this::getTemplate).collect(Collectors.toList()); return idList.stream().map(this::getTemplate).collect(Collectors.toList());
} }
public List<TemplateRespVO> getAllTemplateListByScenicId(Long scenicId) {
List<Long> idList;
idList = templateMapper.listAllTemplateIdByScenicId(scenicId);
if (idList == null || idList.isEmpty()) {
return Collections.emptyList();
}
return idList.stream().map(this::getTemplate).collect(Collectors.toList());
}
public TemplateRespVO getTemplate(Long templateId) { public TemplateRespVO getTemplate(Long templateId) {
if (redisTemplate.hasKey(String.format(TEMPLATE_CACHE_KEY, templateId))) { if (redisTemplate.hasKey(String.format(TEMPLATE_CACHE_KEY, templateId))) {
return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(TEMPLATE_CACHE_KEY, templateId)), TemplateRespVO.class); return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(TEMPLATE_CACHE_KEY, templateId)), TemplateRespVO.class);

View File

@ -3,6 +3,7 @@ package com.ycwl.basic.service.impl.pc;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.ycwl.basic.model.pc.device.entity.DeviceEntity; import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import com.ycwl.basic.model.wvp.WvpSyncReqVo; import com.ycwl.basic.model.wvp.WvpSyncReqVo;
import com.ycwl.basic.repository.DeviceRepository; import com.ycwl.basic.repository.DeviceRepository;
import com.ycwl.basic.mapper.DeviceMapper; import com.ycwl.basic.mapper.DeviceMapper;
@ -18,6 +19,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* @Authorlongbinbin * @Authorlongbinbin
@ -123,4 +126,37 @@ public class DeviceServiceImpl implements DeviceService {
} }
} }
} }
@Override
public ApiResponse<Boolean> sortDevice(Long deviceId, Long afterDeviceId) {
DeviceEntity device = deviceRepository.getDevice(deviceId);
if (device == null) {
return ApiResponse.fail("设备不存在");
}
List<DeviceEntity> scenicDeviceList = deviceRepository.getAllDeviceByScenicId(device.getScenicId());
AtomicInteger sortNum = new AtomicInteger(0);
for (DeviceEntity item : scenicDeviceList) {
item.setSort(sortNum.addAndGet(1));
}
Optional<DeviceEntity> templateOptional = scenicDeviceList.stream().filter(item -> item.getId().equals(deviceId)).findAny();
Optional<DeviceEntity> afterTemplateOptional = scenicDeviceList.stream().filter(item -> item.getId().equals(afterDeviceId)).findAny();
if (!templateOptional.isPresent()) {
return ApiResponse.fail("设备不存在");
}
if (afterTemplateOptional.isPresent()) {
DeviceEntity afterTemplate = afterTemplateOptional.get();
Integer newSort = afterTemplate.getSort();
DeviceEntity oldTemplate = templateOptional.get();
Integer oldSort = oldTemplate.getSort();
afterTemplate.setSort(oldSort);
oldTemplate.setSort(newSort);
}
scenicDeviceList.forEach(item -> {
deviceMapper.updateSort(item.getId(), item.getSort());
deviceRepository.clearDeviceCache(item.getId());
deviceRepository.clearDeviceCache(item.getNo());
deviceRepository.clearDeviceCache(item.getNo2());
});
return ApiResponse.success(true);
}
} }

View File

@ -16,6 +16,8 @@ import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* @Authorlongbinbin * @Authorlongbinbin
@ -123,4 +125,35 @@ public class TemplateServiceImpl implements TemplateService {
templateMapper.updateConfigById(config); templateMapper.updateConfigById(config);
templateRepository.clearTemplateCache(config.getTemplateId()); templateRepository.clearTemplateCache(config.getTemplateId());
} }
@Override
public ApiResponse<Boolean> sortTemplate(Long templateId, Long afterTemplateId) {
TemplateRespVO template = templateRepository.getTemplate(templateId);
if (template == null) {
return ApiResponse.fail("模版不存在");
}
List<TemplateRespVO> scenicTemplateList = templateRepository.getAllTemplateListByScenicId(template.getScenicId());
AtomicInteger sortNum = new AtomicInteger(0);
for (TemplateRespVO item : scenicTemplateList) {
item.setSort(sortNum.addAndGet(1));
}
Optional<TemplateRespVO> templateOptional = scenicTemplateList.stream().filter(item -> item.getId().equals(templateId)).findAny();
Optional<TemplateRespVO> afterTemplateOptional = scenicTemplateList.stream().filter(item -> item.getId().equals(afterTemplateId)).findAny();
if (!templateOptional.isPresent()) {
return ApiResponse.fail("模版不存在");
}
if (afterTemplateOptional.isPresent()) {
TemplateRespVO afterTemplate = afterTemplateOptional.get();
Integer newSort = afterTemplate.getSort();
TemplateRespVO oldTemplate = templateOptional.get();
Integer oldSort = oldTemplate.getSort();
afterTemplate.setSort(oldSort);
oldTemplate.setSort(newSort);
}
scenicTemplateList.forEach(item -> {
templateMapper.updateSort(item.getId(), item.getSort());
templateRepository.clearTemplateCache(item.getId());
});
return ApiResponse.success(true);
}
} }

View File

@ -27,4 +27,6 @@ public interface DeviceService {
void saveConfig(Long configId, DeviceConfigEntity config); void saveConfig(Long configId, DeviceConfigEntity config);
void updateDevices(Long scenicId, WvpSyncReqVo reqVo); void updateDevices(Long scenicId, WvpSyncReqVo reqVo);
ApiResponse<Boolean> sortDevice(Long deviceId, Long afterDeviceId);
} }

View File

@ -26,4 +26,6 @@ public interface TemplateService {
TemplateConfigEntity getConfig(Long templateId); TemplateConfigEntity getConfig(Long templateId);
void saveConfig(Long configId, TemplateConfigEntity config); void saveConfig(Long configId, TemplateConfigEntity config);
ApiResponse<Boolean> sortTemplate(Long templateId, Long afterTemplateId);
} }

View File

@ -96,7 +96,7 @@ public class JwtTokenUtil {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
JwtInfo jwtInfo = new JwtInfo(); JwtInfo jwtInfo = new JwtInfo();
jwtInfo.setUserId(3954940354104528896L); jwtInfo.setUserId(3936940597855784960L);
jwtInfo.setName("微信用户"); jwtInfo.setName("微信用户");
System.out.println(generateToken(jwtInfo, 86400)); System.out.println(generateToken(jwtInfo, 86400));
} }

View File

@ -58,6 +58,11 @@
update_at = now() update_at = now()
where id = #{id} where id = #{id}
</update> </update>
<update id="updateSort">
update device
set sort = #{sort}
where id = #{id}
</update>
<delete id="deleteById"> <delete id="deleteById">
delete from device where id = #{id} delete from device where id = #{id}
</delete> </delete>
@ -85,6 +90,7 @@
and d.create_at &lt;= #{endTime} and d.create_at &lt;= #{endTime}
</if> </if>
</where> </where>
order by sort
</select> </select>
<select id="getById" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO"> <select id="getById" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
select d.id, scenic_id, d.name, no, d.longitude, d.latitude, d.status, create_at, d.update_at, s.name scenic_name select d.id, scenic_id, d.name, no, d.longitude, d.latitude, d.status, create_at, d.update_at, s.name scenic_name
@ -123,6 +129,7 @@
select * select *
from device from device
where status = 1 where status = 1
order by sort
</select> </select>
<select id="getByDeviceNo2" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity"> <select id="getByDeviceNo2" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity">
select * select *
@ -130,4 +137,10 @@
where no_2 = #{deviceNo} where no_2 = #{deviceNo}
limit 1 limit 1
</select> </select>
<select id="listAllByScenicId" resultType="java.lang.Long">
select id
from device
where scenic_id = #{scenicId}
order by sort
</select>
</mapper> </mapper>

View File

@ -85,6 +85,11 @@
where status = 0 and worker_id is null where status = 0 and worker_id is null
limit 1 limit 1
</select> </select>
<select id="selectAllNotRunning" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
select *
from task
where status = 0 and worker_id is null
</select>
<select id="countTask" resultType="java.lang.Integer"> <select id="countTask" resultType="java.lang.Integer">
select count(1) from task select count(1) from task
<where> <where>
@ -123,4 +128,9 @@
<if test="endTime!= null">and create_time &lt;= #{endTime} </if> <if test="endTime!= null">and create_time &lt;= #{endTime} </if>
</where> </where>
</select> </select>
<select id="selectAllRunning" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
select *
from task
where status = 2
</select>
</mapper> </mapper>

View File

@ -54,6 +54,11 @@
</set> </set>
where id = #{id} where id = #{id}
</update> </update>
<update id="updateSort">
update template
set sort = #{sort}
where id = #{templateId}
</update>
<delete id="deleteById"> <delete id="deleteById">
delete from template where id = #{id} delete from template where id = #{id}
</delete> </delete>
@ -84,6 +89,7 @@
<if test="startTime!= null">and t.create_time &gt;= #{startTime} </if> <if test="startTime!= null">and t.create_time &gt;= #{startTime} </if>
<if test="endTime!= null">and t.create_time &lt;= #{endTime} </if> <if test="endTime!= null">and t.create_time &lt;= #{endTime} </if>
</where> </where>
order by scenic_id, sort
</select> </select>
<select id="getById" resultType="com.ycwl.basic.model.pc.template.resp.TemplateRespVO"> <select id="getById" resultType="com.ycwl.basic.model.pc.template.resp.TemplateRespVO">
select t.*, s.name as scenic_name select t.*, s.name as scenic_name
@ -127,4 +133,10 @@
from template from template
where status = 1 and pid = 0 where status = 1 and pid = 0
</select> </select>
<select id="listAllTemplateIdByScenicId" resultType="java.lang.Long">
select t.id
from template t
where t.scenic_id = #{scenicId} and t.pid = 0
order by sort
</select>
</mapper> </mapper>