123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- package com.ruoyi.sim.service.impl;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Objects;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.utils.DateUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import com.ruoyi.sim.mapper.SimMapper;
- import com.ruoyi.sim.domain.Sim;
- import org.springframework.transaction.annotation.Isolation;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- /**
- * 模拟器Service业务层处理
- *
- * @author tom
- * @date 2024-12-13
- */
- @Service
- public class SimService {
- @Autowired
- private SimMapper simMapper;
- /**
- * 查询模拟器
- *
- * @param simId 模拟器主键
- * @return 模拟器
- */
- public Sim selectSimBySimId(Long simId) {
- return simMapper.selectSimBySimId(simId);
- }
- /**
- * 查询模拟器列表
- *
- * @param sim 模拟器
- * @return 模拟器
- */
- public List<Sim> selectSimList(Sim sim) {
- return simMapper.selectSimList(sim);
- }
- /**
- * 新增模拟器
- *
- * @param sim 模拟器
- * @return 结果
- */
- public int insertSim(Sim sim) {
- sim.setCreateTime(DateUtils.getNowDate());
- return simMapper.insertSim(sim);
- }
- /**
- * 修改模拟器
- *
- * @param sim 模拟器
- * @return 结果
- */
- public int updateSim(Sim sim) {
- sim.setUpdateTime(DateUtils.getNowDate());
- return simMapper.updateSim(sim);
- }
- /**
- * 批量删除模拟器
- *
- * @param simIds 需要删除的模拟器主键
- * @return 结果
- */
- public int deleteSimBySimIds(Long[] simIds) {
- return simMapper.deleteSimBySimIds(simIds);
- }
- /**
- * 删除模拟器信息
- *
- * @param simId 模拟器主键
- * @return 结果
- */
- public int deleteSimBySimId(Long simId) {
- return simMapper.deleteSimBySimId(simId);
- }
- // -------------------------------- tom add --------------------------------
- private static final Logger l = LoggerFactory.getLogger(SimService.class);
- /**
- * @param simId
- * @return true:存在,false:不存在
- */
- public boolean existBySimId(final Long simId) {
- if (simId == null) {
- return false;
- }
- if (simId == 0) {
- return false;
- }
- Sim s = selectSimBySimId(simId);
- if (s == null) {
- return false;
- }
- return true;
- }
- /**
- * @param simNum
- * @return true:存在,false:不存在
- */
- public boolean existBySimNum(final String simNum) {
- if (StringUtils.isEmpty(simNum)) {
- return false;
- }
- Sim s = uniqueBySimNum(simNum);
- return s != null;
- }
- /**
- * 通过sim_num获取唯一模拟器Sim对象。
- *
- * @param simNum
- * @return
- */
- public Sim uniqueBySimNum(final String simNum) {
- Sim q = new Sim();
- q.setSimNum(simNum);
- List<Sim> list = simMapper.selectSimList(q);
- if (list.isEmpty()) {
- return null;
- } else if (list.size() == 1) {
- return list.get(0);
- } else {
- throw new IllegalArgumentException("Sim数据错误。");
- }
- }
- public List<Sim> listAll() {
- return selectSimList(new Sim());
- }
- /**
- * 获取所有没有被禁用的模拟器列表
- *
- * @return
- */
- public AjaxResult listAllEnableAj() {
- return AjaxResult.success(listAllEnable());
- }
- public List<Sim> listAllEnable() {
- List<Sim> list = new ArrayList<>();
- listAll()
- .stream()
- .filter(Objects::nonNull)
- .filter(s -> !StringUtils.equals(Sim.State.DISABLE, s.getSimState()))
- .forEach(list::add);
- return list;
- }
- /**
- * 获取所有在线的模拟器列表
- *
- * @return
- */
- public AjaxResult listAllOnlineAj() {
- return AjaxResult.success(listAllOnline());
- }
- public List<Sim> listAllOnline() {
- List<Sim> list = new ArrayList<>();
- listAll()
- .stream()
- .filter(Objects::nonNull)
- .filter(s -> Sim.State.ONLINE.equals(s.getSimState()))
- .forEach(list::add);
- return list;
- }
- public List<Sim> listAllEnableBySimType(String simType) {
- List<Sim> list = new ArrayList<>();
- listAll()
- .stream()
- .filter(Objects::nonNull)
- .filter(s -> !Sim.State.DISABLE.equals(s.getSimState()))
- .filter(s -> s.getSimType().equals(simType))
- .forEach(list::add);
- return list;
- }
- /**
- * 修改所有启用的模拟器 在线/离线 状态。
- *
- * @param simState
- * @return 修改模拟器状态的数量。
- */
- public int updateAllEnableState(final String simState) {
- List<Sim> list = listAllEnable();
- for (Sim sim : list) {
- sim.setSimState(simState);
- simMapper.updateSim(sim);
- }
- return list.size();
- }
- public List<String> listSimTypes() {
- return Sim.TYPE_SET.stream().toList();
- }
- public boolean checkSimTypeOk(String simType) {
- return Sim.TYPE_SET.contains(simType);
- }
- public boolean checkState(String simState) {
- return !Sim.STATE_SET.contains(simState);
- }
- /**
- * 修改模拟器状态,并重置充电计数。
- *
- * @param simId
- * @param simState
- * @return
- */
- @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
- public int updateSimStateBySimId(final Long simId, final String simState) {
- // check
- if (checkState(simState)) {
- throw new IllegalArgumentException("simState wrong!");
- }
- //
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- return 0;
- }
- // 离线强制重置 chargingCount = 0,归零。
- if (StringUtils.equals(simState, Sim.State.OFFLINE)) {
- updateChargingCountResetBySimId(simId);
- }
- q.setSimState(simState);
- return updateSim(q);
- }
- @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
- public int updateSimStateBySimNum(final String simNum, final String simState) {
- return updateSimStateBySimId(uniqueBySimNum(simNum).getSimId(), simState);
- }
- /**
- * 充满量
- * 单位:s
- */
- public static final Integer CHARGING_TOTAL = 120;
- /**
- * 充电步长量
- * 单位:s
- *
- * @param simId
- */
- public static final Integer CHARGING_STEP = 30;
- /**
- * 充电起始量。
- */
- public static final Integer CHARGING_START = 0;
- /**
- * 充电count步长+1
- *
- * @param simId
- * @return new value.
- */
- @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
- public Integer updateChargingCountPlusBySimId(final Long simId) {
- l.info("updateChargingCountPlusBySimId simId = {}", simId);
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- throw new IllegalArgumentException("simId");
- }
- if (q.getChargingCount() >= CHARGING_TOTAL) {// 超过上限。
- return q.getChargingCount();
- }
- Integer countNew = q.getChargingCount() + CHARGING_STEP;
- q.setChargingCount(countNew);
- updateSim(q);
- return countNew;
- }
- /**
- * 充电count归零
- *
- * @param simId
- * @return new value.
- */
- @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
- public Integer updateChargingCountResetBySimId(final Long simId) {
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- throw new IllegalArgumentException("simId");
- }
- q.setChargingCount(CHARGING_START);
- updateSim(q);
- return CHARGING_START;
- }
- /**
- * 是否充电完成
- *
- * @param simId
- * @return
- */
- public Boolean isChargingCountFullBySimId(final Long simId) {
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- throw new IllegalArgumentException("simId");
- }
- return (q.getChargingCount() >= CHARGING_TOTAL);
- }
- public Long getChargingCountPercentage(final Long simId) {
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- throw new IllegalArgumentException("simId");
- }
- double p = q.getChargingCount() / ((double) CHARGING_TOTAL);
- return Math.round(p * 100);
- }
- public boolean isSimStateBySimId(Long simId, String simState) {
- // check
- if (checkState(simState)) {
- }
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- return false;
- }
- return (q.getSimState().equals(simState));
- }
- public boolean isSimDisable(Long simId) {
- return isSimStateBySimId(simId, Sim.State.DISABLE);
- }
- /**
- * 更新模拟器序列号。
- *
- * @param simId
- * @param simSn
- * @return
- */
- public int updateSimSnBySimId(Long simId, String simSn) {
- // check
- if (StringUtils.isEmpty(simSn)) {
- return 0;
- }
- //
- Sim q = selectSimBySimId(simId);
- if (Objects.isNull(q)) {
- return 0;
- }
- q.setSimSn(simSn);
- q.setUpdateTime(DateUtils.getNowDate());
- return updateSim(q);
- }
- public int updateAllEnableStateInit() {
- List<Sim> list = simMapper.selectSimList(new Sim());
- int count = 0;
- for (Sim o : list) {
- if (o == null) {
- continue;
- }
- if (!Objects.equals(o.getSimState(), Sim.State.DISABLE)) {
- o.setSimState(Sim.State.ENABLE_INIT);
- }
- updateSim(o);
- count = count + 1;
- }
- return count;
- }
- @Async("tp-log")
- public void updateLastSentTime(Sim s) {
- if (s == null) {
- return;
- }
- s.setLastSentTime(DateUtils.getNowDate());
- updateSim(s);
- }
- @Async("tp-log")
- public void updateLastReceivedTime(Sim s) {
- if (s == null) {
- return;
- }
- s.setLastReceivedTime(DateUtils.getNowDate());
- updateSim(s);
- }
- }
|