| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.technology.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.technology.bean.dto.TechnologyParamDto; |
| | | import com.ruoyi.technology.bean.vo.TechnologyParamVo; |
| | | import com.ruoyi.technology.mapper.TechnologyParamMapper; |
| | | import com.ruoyi.technology.pojo.TechnologyParam; |
| | | import com.ruoyi.technology.service.TechnologyParamService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class TechnologyParamServiceImpl extends ServiceImpl<TechnologyParamMapper, TechnologyParam> implements TechnologyParamService { |
| | | |
| | | private static final List<Integer> VALID_PARAM_TYPES = Arrays.asList(1, 2, 3, 4); |
| | | private static final String PARAM_CODE_PREFIX = "PARAM_"; |
| | | private static final Byte DATE_PARAM_TYPE = (byte) 4; |
| | | |
| | | /** |
| | | * å页æ¥è¯¢åºç¡åæ°å¹¶æ ¼å¼åæ¥æç±»åå±ç¤ºã |
| | | */ |
| | | @Override |
| | | public IPage<TechnologyParamVo> baseParamList(Page<TechnologyParamDto> page, TechnologyParamDto baseParam) { |
| | | LambdaQueryWrapper<TechnologyParam> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (baseParam != null && StringUtils.isNotEmpty(baseParam.getParamName())) { |
| | | queryWrapper.like(TechnologyParam::getParamName, baseParam.getParamName()); |
| | | } |
| | | queryWrapper.orderByDesc(TechnologyParam::getId); |
| | | |
| | | Page<TechnologyParam> entityPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal()); |
| | | Page<TechnologyParam> paramPage = page(entityPage, queryWrapper); |
| | | |
| | | Page<TechnologyParamVo> resultPage = new Page<>(paramPage.getCurrent(), paramPage.getSize(), paramPage.getTotal()); |
| | | List<TechnologyParamVo> records = new ArrayList<>(paramPage.getRecords().size()); |
| | | for (TechnologyParam item : paramPage.getRecords()) { |
| | | TechnologyParamVo vo = new TechnologyParamVo(); |
| | | BeanUtils.copyProperties(item, vo); |
| | | if (DATE_PARAM_TYPE.equals(item.getParamType()) && StringUtils.isNotEmpty(item.getParamFormat())) { |
| | | vo.setParamFormat(toDisplayDatePattern(item.getParamFormat())); |
| | | } |
| | | records.add(vo); |
| | | } |
| | | resultPage.setRecords(records); |
| | | return resultPage; |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢åºç¡åæ°å¹¶çæå¯ä¸åæ°ç¼ç ã |
| | | */ |
| | | @Override |
| | | public int addBaseParam(TechnologyParamDto baseParam) { |
| | | if (baseParam == null) { |
| | | throw new RuntimeException("æ°å¢åæ°ä¸è½ä¸ºç©º"); |
| | | } |
| | | checkBaseParam(baseParam); |
| | | baseParam.setParamCode(generateParamCode()); |
| | | baseParam.setCreateUser(SecurityUtils.getUsername()); |
| | | baseParam.setCreateTime(LocalDateTime.now()); |
| | | if (baseParam.getIsRequired() == null) { |
| | | baseParam.setIsRequired((byte) 0); |
| | | } |
| | | return baseMapper.insert(baseParam); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹åºç¡åæ°å¹¶ä¿çåæåæ°ç¼ç ã |
| | | */ |
| | | @Override |
| | | public int updateBaseParam(TechnologyParamDto baseParam) { |
| | | if (baseParam == null || baseParam.getId() == null) { |
| | | throw new RuntimeException("ä¿®æ¹åæ°IDä¸è½ä¸ºç©º"); |
| | | } |
| | | checkBaseParam(baseParam); |
| | | TechnologyParam current = baseMapper.selectById(baseParam.getId()); |
| | | if (current == null) { |
| | | throw new RuntimeException("åæ°ä¸åå¨"); |
| | | } |
| | | if (StringUtils.isEmpty(baseParam.getParamCode())) { |
| | | baseParam.setParamCode(current.getParamCode()); |
| | | } |
| | | baseParam.setUpdateUser(SecurityUtils.getUsername()); |
| | | baseParam.setUpdateTime(LocalDateTime.now()); |
| | | return baseMapper.updateById(baseParam); |
| | | } |
| | | |
| | | /** |
| | | * åæ°å®ä¹åæ³æ ¡éªã |
| | | */ |
| | | private void checkBaseParam(TechnologyParamDto baseParam) { |
| | | if (StringUtils.isEmpty(baseParam.getParamName())) { |
| | | throw new RuntimeException("åæ°åç§°ä¸è½ä¸ºç©º"); |
| | | } |
| | | if (baseParam.getParamType() == null || !VALID_PARAM_TYPES.contains(Integer.valueOf(baseParam.getParamType()))) { |
| | | throw new RuntimeException("éæ³åæ°ç±»å"); |
| | | } |
| | | |
| | | if (DATE_PARAM_TYPE.equals(baseParam.getParamType())) { |
| | | if (StringUtils.isEmpty(baseParam.getParamFormat())) { |
| | | throw new RuntimeException("æ¥æç±»åå¿
é¡»é
ç½®åæ°æ ¼å¼(å¦: yyyy-MM-dd)"); |
| | | } |
| | | try { |
| | | String standardPattern = normalizeDatePattern(baseParam.getParamFormat()); |
| | | DateTimeFormatter.ofPattern(standardPattern); |
| | | baseParam.setParamFormat(standardPattern); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("æ¥ææ ¼å¼éæ³: " + baseParam.getParamFormat()); |
| | | } |
| | | } else { |
| | | baseParam.setParamFormat(baseParam.getParamFormat()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * çæåæ°å¯ä¸ç¼ç ï¼ä¾å·¥èºåæ°åç产订åå¿«ç
§å¼ç¨ã |
| | | */ |
| | | private String generateParamCode() { |
| | | LambdaQueryWrapper<TechnologyParam> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.select(TechnologyParam::getParamCode) |
| | | .likeRight(TechnologyParam::getParamCode, PARAM_CODE_PREFIX) |
| | | .orderByDesc(TechnologyParam::getParamCode) |
| | | .last("limit 1"); |
| | | |
| | | TechnologyParam last = baseMapper.selectOne(wrapper); |
| | | int nextNum = 1; |
| | | if (last != null && StringUtils.isNotEmpty(last.getParamCode())) { |
| | | try { |
| | | String numStr = last.getParamCode().replace(PARAM_CODE_PREFIX, ""); |
| | | nextNum = Integer.parseInt(numStr) + 1; |
| | | } catch (Exception e) { |
| | | log.error("è§£æ paramCode å¼å¸¸", e); |
| | | } |
| | | } |
| | | return PARAM_CODE_PREFIX + String.format("%04d", nextNum); |
| | | } |
| | | |
| | | private String normalizeDatePattern(String pattern) { |
| | | return pattern.trim() |
| | | .replace('Y', 'y') |
| | | .replace('D', 'd') |
| | | .replace('S', 's'); |
| | | } |
| | | |
| | | private String toDisplayDatePattern(String pattern) { |
| | | return normalizeDatePattern(pattern); |
| | | } |
| | | |
| | | /** |
| | | * æ¹éå é¤åºç¡åæ°ã |
| | | */ |
| | | @Override |
| | | public int deleteBaseParamByIds(Long[] ids) { |
| | | if (ids == null || ids.length == 0) { |
| | | throw new RuntimeException("å é¤IDä¸è½ä¸ºç©º"); |
| | | } |
| | | return baseMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | } |