package com.sandu.ximon.admin.service; import com.sandu.common.execption.BusinessException; import com.sandu.common.redis.RedisService; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.ximon.admin.param.LampPostParam; import com.sandu.ximon.admin.param.PoleParam; import com.sandu.ximon.admin.redis.LightKey; import com.sandu.ximon.admin.security.CountSet; import com.sandu.ximon.dao.domain.LampPost; import com.sandu.ximon.dao.domain.Pole; import com.sandu.ximon.dao.mapper.ClientMapper; import com.sandu.ximon.dao.mapper.LampPostMapper; import com.sandu.ximon.dao.mapper.PoleMapper; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; /** * 灯杆相关 * * @author chenjiantian */ @Service @Slf4j @AllArgsConstructor public class PoleService extends BaseServiceImpl { private final CountSet countSet; private final LampCountService lampCountService; private final ClientMapper clientMapper; private final LampPostMapper lampPostMapper; private final RedisService redisService; public boolean addPole(PoleParam param) { Pole pole = new Pole(); BeanUtils.copyProperties(param, pole); pole.setPoleCode(generatePoleCode()); return save(pole); } public boolean updatePole(Long poleId, PoleParam param) { Pole pole = getById(poleId); if (pole == null) { throw new BusinessException("未找到该灯杆"); } Pole update = new Pole(); BeanUtils.copyProperties(param, update); update.setId(poleId); return updateById(update); } /** * 删除灯杆 */ public boolean deletePole(Long poleId){ Pole pole = getById(poleId); if (pole == null) { throw new BusinessException("未找到该灯杆"); } return removeById(poleId); } /** * 生成灯杆编号 */ private Long generatePoleCode() { StringBuilder sb = new StringBuilder(); String date = new SimpleDateFormat("yyMMdd").format(new Date()); sb.append(date); String key = LightKey.POLE_SN.key(null); Long increment = redisService.incr(key, 1); String incrementStr = increment.toString(); if (incrementStr.length() <= 4) { sb.append(String.format("%04d", increment)); } else { sb.append(incrementStr); } return Long.parseLong(sb.toString()); } }