gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/StMoncrService.java

92 lines
2.9 KiB
Java

package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gunshi.project.xyt.entity.basedata.CheckStringSearch;
import com.gunshi.project.xyt.entity.basedata.GeneralSearch;
import com.gunshi.project.xyt.mapper.StMoncrBMapper;
import com.gunshi.project.xyt.model.StMoncrB;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
/**
* @author Sun Lejun
* @version 1.0
* @date 2024/1/26
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Data
public class StMoncrService {
private final StMoncrBMapper stMoncrBMapper;
public Page<StMoncrB> queryBySearch(GeneralSearch search){
Page<StMoncrB> page = search.getPageSo().toPage();
return stMoncrBMapper.queryBySearch(page, search);
}
/**
* 新增监测断面
* @param stMoncrB 监测断面对象
*/
public void insert(StMoncrB stMoncrB) {
CheckStringSearch checkStringSearch = new CheckStringSearch();
checkStringSearch.setKeyword(stMoncrB.getCrCd());
checkCode(checkStringSearch);
stMoncrB.setCrId(IdWorker.getId());
stMoncrB.setSortOn(getMaxSortOn());
stMoncrB.setTm(new Date());
stMoncrBMapper.insert(stMoncrB);
}
/**
* 更新监测断面
* @param stMoncrB 监测断面对象
*/
public void update(StMoncrB stMoncrB) {
CheckStringSearch checkStringSearch = new CheckStringSearch();
checkStringSearch.setKeyword(stMoncrB.getCrCd());
checkStringSearch.setId(stMoncrB.getCrId());
checkCode(checkStringSearch);
stMoncrB.setTm(new Date());
stMoncrBMapper.updateById(stMoncrB);
}
public int getMaxSortOn(){
QueryWrapper<StMoncrB> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(StMoncrB.COL_STATUS, 1);
queryWrapper.orderByDesc(StMoncrB.COL_SORT_ON);
StMoncrB stMoncrB = stMoncrBMapper.selectOne(queryWrapper, false);
if(stMoncrB == null){
return 1;
}else {
return stMoncrB.getSortOn() + 1;
}
}
/**
* 检查断面编码是否存在
* @param search 查询对象
*/
public void checkCode(CheckStringSearch search){
QueryWrapper<StMoncrB> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(StMoncrB.COL_STATUS, 1);
queryWrapper.eq(StMoncrB.COL_CR_CD, search.getKeyword());
if(search.getId() != null){
queryWrapper.ne(StMoncrB.COL_CR_ID, search.getId());
}
StMoncrB stMoncrB = stMoncrBMapper.selectOne(queryWrapper, false);
if(stMoncrB != null){
throw new IllegalArgumentException("断面编码已存在");
}
}
}