gunshi-project-ss/src/main/java/com/gunshi/project/ss/service/ByPlanService.java

107 lines
4.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.gunshi.project.ss.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.ss.entity.so.ByPlanPageSo;
import com.gunshi.project.ss.mapper.ByPlanDetailMapper;
import com.gunshi.project.ss.mapper.ByPlanMapper;
import com.gunshi.project.ss.model.ByPlan;
import com.gunshi.project.ss.model.ByPlanDetail;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ByPlanService extends ServiceImpl<ByPlanMapper, ByPlan> {
@Autowired
private ByPlanDetailMapper byPlanDetailMapper;
@Autowired
private ByPlanDetailService byPlanDetailService;
@Autowired
private SysUserMapper sysUserMapper;
public boolean update(ByPlan dto) {
LambdaQueryWrapper<ByPlan> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ByPlan::getPlanId, dto.getPlanId());
ByPlan one = getOne(queryWrapper);
if(Objects.isNull(one)){
throw new RuntimeException("该计划不存在,请检查");
}
boolean save = updateById(dto);
List<ByPlanDetail> byPlanDetail = dto.getByPlanDetail();
if(byPlanDetail == null || byPlanDetail.isEmpty()){
throw new IllegalArgumentException("防止部位至少需要一条数据");
}
byPlanDetail.forEach(detail -> {
detail.setId(IdWorker.getId());
detail.setPlanId(dto.getPlanId());
});
LambdaQueryWrapper<ByPlanDetail> queryWrapper2 = new LambdaQueryWrapper<>();
//先删除,再新增
queryWrapper2.eq(ByPlanDetail::getPlanId, dto.getPlanId());
int delete = byPlanDetailMapper.delete(queryWrapper2);
List<BatchResult> insert = byPlanDetailMapper.insert(byPlanDetail);
return true;
}
public Page<ByPlan> pageQuery(ByPlanPageSo dto) {
LambdaQueryWrapper<ByPlan> queryWrapper = new LambdaQueryWrapper<>();
if(!StringUtils.isBlank(dto.getPlanName())){
queryWrapper.like(ByPlan::getPlanName,dto.getPlanName());
}
queryWrapper.orderByDesc(ByPlan::getPlanDateStart);
Page<ByPlan> byPlanPage = baseMapper.selectPage(dto.getPageSo().toPage(), queryWrapper);
List<ByPlan> records = byPlanPage.getRecords();
Iterator<ByPlan> iterator = records.iterator();
while(iterator.hasNext()){
ByPlan entity = iterator.next();
List<ByPlanDetail> query = byPlanDetailMapper.selectList(entity.getPlanId(),dto);
//如果preDetailName不为空的情况下且details为空那么就去掉这个主数据
if (!StringUtils.isBlank(dto.getPreDetailName()) && query.isEmpty()) {
iterator.remove();
continue;
}
List<ByPlanDetail> details = byPlanDetailMapper.selectList(entity.getPlanId(), null);
entity.setByPlanDetail(details);
//根据用户id查询用户名称
if(entity.getUserId() != null){
SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(entity.getUserId()));
if(sysUser != null){
entity.setUserName(sysUser.getNickName());
}
}
}
return byPlanPage;
}
public boolean saveData(ByPlan dto) {
dto.setId(IdWorker.getId());
List<ByPlanDetail> byPlanDetail = dto.getByPlanDetail();
boolean save = save(dto);
byPlanDetail.forEach(detail -> {
detail.setId(IdWorker.getId());
});
if(save){
byPlanDetailService.saveBatch(byPlanDetail);
}
return true;
}
}