package com.glxp.api.controller.sync; import cn.hutool.core.collection.CollUtil; import com.glxp.api.annotation.AuthRuleAnnotation; import com.glxp.api.constant.Constant; import com.glxp.api.entity.system.SyncDataBustypeEntity; import com.glxp.api.entity.system.SyncDataSetEntity; import com.glxp.api.res.system.SyncDataSetResponse; import com.glxp.api.service.sync.SyncDataBustypeService; import com.glxp.api.service.sync.SyncDataChangeBustypeService; import com.glxp.api.service.sync.SyncDataSetService; import com.glxp.api.util.RedisUtil; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.validation.Valid; import java.util.Date; import java.util.List; @RestController public class SyncDataSetController { @Resource private SyncDataSetService syncDataSetService; @Resource private SyncDataBustypeService syncDataBustypeService; @Resource private SyncDataChangeBustypeService syncDataChangeBustypeService; @Resource private RedisUtil redisUtil; @AuthRuleAnnotation("system/param/syncData/config") @GetMapping("/system/param/syncData/config") public BaseResponse list() { Integer delaySyncTime = (Integer) redisUtil.get(Constant.DELAY_SYNC_TIME); if (delaySyncTime != null) { long time = redisUtil.getExpire(Constant.DELAY_SYNC_TIME); delaySyncTime = Math.toIntExact(time) / 60; } SyncDataSetResponse syncDataSetResponse = syncDataSetService.selectSet(); syncDataSetResponse.setDelaySyncTime(delaySyncTime); return ResultVOUtils.success(syncDataSetResponse); } @AuthRuleAnnotation("system/param/syncData/save") @PostMapping("/system/param/syncData/save") public BaseResponse save(@RequestBody @Valid SyncDataSetResponse syncDataSetResponse ) { SyncDataSetEntity syncDataSetEntity = new SyncDataSetEntity(); BeanUtils.copyProperties(syncDataSetResponse, syncDataSetEntity); syncDataSetEntity.setUpdateTime(new Date()); syncDataSetEntity.setId(1); List busTypes = syncDataSetResponse.getBusTypes(); syncDataBustypeService.deleteAll(1); if (CollUtil.isNotEmpty(busTypes)) syncDataBustypeService.inserts(busTypes); List toInBusTypes = syncDataSetResponse.getToInBusTypes(); syncDataBustypeService.deleteAll(2); if (CollUtil.isNotEmpty(toInBusTypes)) syncDataBustypeService.inserts(toInBusTypes); if (CollUtil.isNotEmpty(syncDataSetResponse.getChangeBusTypes())) { syncDataChangeBustypeService.deleteAll(); syncDataChangeBustypeService.inserts(syncDataSetResponse.getChangeBusTypes()); } boolean b = syncDataSetService.insert(syncDataSetEntity); if (syncDataSetResponse.getDelaySyncTime() != null && syncDataSetResponse.getDelaySyncTime() > 0) { redisUtil.set(Constant.DELAY_SYNC_TIME, syncDataSetResponse.getDelaySyncTime(), (syncDataSetResponse.getDelaySyncTime() + 1) * 60); redisUtil.set("SPS_SYNC_GEN_DATA", System.currentTimeMillis() + syncDataSetResponse.getDelaySyncTime() * 60 * 1000); } if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } return ResultVOUtils.success(); } }