package com.ruoyi.gas.service.impl; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; import com.ruoyi.common.core.page.PagePlus; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.PageUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.gas.domain.GPressureRegulatingBox; import com.ruoyi.gas.domain.GRegulatorBox; import com.ruoyi.gas.domain.GRegulatorBoxRelation; import com.ruoyi.gas.domain.bo.GPressureRegulatingBoxBo; import com.ruoyi.gas.domain.bo.GPressureRegulatingBoxDetailsBo; import com.ruoyi.gas.domain.bo.GRegulatorBoxBo; import com.ruoyi.gas.domain.vo.GPressureRegulatingBoxDetailsVo; import com.ruoyi.gas.domain.vo.GPressureRegulatingBoxExcelVo; import com.ruoyi.gas.domain.vo.GPressureRegulatingBoxVo; import com.ruoyi.gas.mapper.GPressureRegulatingBoxMapper; import com.ruoyi.gas.service.IGPressureRegulatingBoxDetailsService; import com.ruoyi.gas.service.IGPressureRegulatingBoxService; import com.ruoyi.gas.service.IGRegulatorBoxRelationService; import com.ruoyi.system.mapper.SysDictDataMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import javax.annotation.Resource; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 调压箱管理Service业务层处理 * * @author ruoyi * @date 2024-03-25 */ @Service public class GPressureRegulatingBoxServiceImpl extends ServicePlusImpl implements IGPressureRegulatingBoxService { @Autowired private IGPressureRegulatingBoxDetailsService igPressureRegulatingBoxDetailsService; @Resource private SysDictDataMapper sysDictDataMapper; @Override public Map importVo(List list, Boolean isUpdateSupport, String operName, int n, int successNum) { if (ObjectUtils.isEmpty(list) || list.size() == 0) { throw new ServiceException("导入数据不能为空!"); } Map ret = new HashMap<>(); StringBuilder successMsg = new StringBuilder(); for (int i = n; i < list.size(); i++) { GPressureRegulatingBoxExcelVo vo = list.get(i); try { n++; if (StringUtils.isNotEmpty(vo.getNumber())) { successNum++; GPressureRegulatingBox gPressureRegulatingBox = baseMapper.getByNumber(vo.getNumber(),vo.getName(),vo.getManagementOffice()); // String type = sysDictDataMapper.queryDictTextByKey("tyx_type", vo.getType()); // if (StringUtils.isBlank(type)) { // throw new ServiceException( "第" + (n + 1) + "行类型填写错误,导入失败!"); // } // String getNature = sysDictDataMapper.queryDictTextByKey("tyx_nature", vo.getNature()); // if (StringUtils.isBlank(getNature)) { // throw new ServiceException("第" + (n + 1) + "行性质填写错误,导入失败!"); // } // String getNumberOfRoutes = sysDictDataMapper.queryDictTextByKey("tyx_numberOfRoutes", vo.getNumberOfRoutes()); // if (StringUtils.isBlank(getNumberOfRoutes)) { // throw new ServiceException( "第" + (n + 1) + "行路数填写错误,导入失败!"); // } String getManagementOffice = sysDictDataMapper.queryDictTextByKey("administrative_office", vo.getManagementOffice()); if (StringUtils.isBlank(getManagementOffice)) { throw new ServiceException( "第" + (n + 1) + "行管理所填写错误,导入失败!"); } if (ObjectUtils.isEmpty(gPressureRegulatingBox)) { GPressureRegulatingBox bean = BeanUtil.toBean(vo, GPressureRegulatingBox.class); bean.setId(null); baseMapper.insert(bean); Long id = bean.getId(); GPressureRegulatingBoxDetailsBo bean1 = BeanUtil.toBean(vo, GPressureRegulatingBoxDetailsBo.class); bean1.setRegulatingBoxId(id.toString()); bean1.setId(null); igPressureRegulatingBoxDetailsService.insertByBo(bean1); } else { GPressureRegulatingBox bean = BeanUtil.toBean(vo, GPressureRegulatingBox.class); bean.setId(gPressureRegulatingBox.getId()); baseMapper.updateById(bean); Long id = gPressureRegulatingBox.getId(); GPressureRegulatingBoxDetailsBo bean1 = BeanUtil.toBean(vo, GPressureRegulatingBoxDetailsBo.class); bean1.setRegulatingBoxId(id.toString()); bean1.setId(null); igPressureRegulatingBoxDetailsService.insertByBo(bean1); } }else { throw new ServiceException("第" + (n + 1) + "行编号错误,导入失败!"); } } catch (Exception e) { e.printStackTrace(); throw new ServiceException("第" + (n + 1) + "行格式错误,导入失败!"); } ret.put("bfb", String.format("%.2f", (Double.valueOf((n + 1)) / Double.valueOf(list.size())) * 100)); ret.put("n", n); ret.put("is_success", true); ret.put("successMsg", ""); ret.put("successNum", successNum); return ret; } successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条"); ret.put("bfb", String.format("%.2f", (Double.valueOf((n + 1)) / Double.valueOf(list.size())) * 100)); ret.put("n", n); ret.put("is_success", false); ret.put("successMsg", successMsg.toString()); return ret; } @Override public GPressureRegulatingBoxVo queryById(Long id) { GPressureRegulatingBoxVo voById = getVoById(id); GPressureRegulatingBoxDetailsBo gPressureRegulatingBoxDetailsBo = new GPressureRegulatingBoxDetailsBo(); gPressureRegulatingBoxDetailsBo.setRegulatingBoxId(voById.getId().toString()); TableDataInfo queriedPageList = pressureRegulatingBoxDetailsService.queryPageList(gPressureRegulatingBoxDetailsBo); voById.setPressureRegulatingBoxDetailsVo(queriedPageList.getRows()); return voById; } @Override public List getExcelList() { List excelList = baseMapper.getExcelList(); for (int i = 1; i <= excelList.size(); i++) { excelList.get(i - 1).setId(String.valueOf(i)); } return excelList; } @Autowired private IGPressureRegulatingBoxDetailsService pressureRegulatingBoxDetailsService; @Override public TableDataInfo queryPageList(GPressureRegulatingBoxBo bo) { PagePlus result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo)); result.getRecordsVo().forEach(item -> { GPressureRegulatingBoxDetailsBo gPressureRegulatingBoxDetailsBo = new GPressureRegulatingBoxDetailsBo(); gPressureRegulatingBoxDetailsBo.setRegulatingBoxId(item.getId().toString()); TableDataInfo queriedPageList = pressureRegulatingBoxDetailsService.queryPageList(gPressureRegulatingBoxDetailsBo); item.setPressureRegulatingBoxDetailsVo(queriedPageList.getRows()); }); return PageUtils.buildDataInfo(result); } @Override public List queryList(GPressureRegulatingBoxBo bo) { return listVo(buildQueryWrapper(bo)); } private LambdaQueryWrapper buildQueryWrapper(GPressureRegulatingBoxBo bo) { Map params = bo.getParams(); LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(StringUtils.isNotBlank(bo.getNature()), GPressureRegulatingBox::getNature, bo.getNature()); lqw.eq(StringUtils.isNotBlank(bo.getType()), GPressureRegulatingBox::getType, bo.getType()); lqw.eq(StringUtils.isNotBlank(bo.getNumberOfRoutes()), GPressureRegulatingBox::getNumberOfRoutes, bo.getNumberOfRoutes()); lqw.eq(StringUtils.isNotBlank(bo.getManagementOffice()), GPressureRegulatingBox::getManagementOffice, bo.getManagementOffice()); lqw.like(StringUtils.isNotBlank(bo.getNumber()), GPressureRegulatingBox::getNumber, bo.getNumber()); lqw.like(StringUtils.isNotBlank(bo.getName()), GPressureRegulatingBox::getName, bo.getName()); lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), GPressureRegulatingBox::getLongitude, bo.getLongitude()); lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), GPressureRegulatingBox::getLatitude, bo.getLatitude()); lqw.like(StringUtils.isNotBlank(bo.getInspector()), GPressureRegulatingBox::getInspector, bo.getInspector()); if (!ObjectUtils.isEmpty(bo.getCreateTimes())){ lqw.ge(GPressureRegulatingBox::getCreateTime, bo.getCreateTimes().get(0)); lqw.le(GPressureRegulatingBox::getCreateTime, bo.getCreateTimes().get(1)); } return lqw; } @Autowired private IGRegulatorBoxRelationService regulatorBoxRelationService; @Override @Transactional(rollbackFor = Exception.class) public Boolean insertByBo(GPressureRegulatingBoxBo bo) { GPressureRegulatingBox add = BeanUtil.toBean(bo, GPressureRegulatingBox.class); validEntityBeforeSave(add); boolean flag = save(add); if (flag) { bo.setId(add.getId()); bo.getPressureRegulatingBoxDetailsBo().setRegulatingBoxId(add.getId().toString()); pressureRegulatingBoxDetailsService.insertByBo(bo.getPressureRegulatingBoxDetailsBo()); } return flag; } @Override public Boolean updateByBo(GPressureRegulatingBoxBo bo) { GPressureRegulatingBox update = BeanUtil.toBean(bo, GPressureRegulatingBox.class); validEntityBeforeSave(update); pressureRegulatingBoxDetailsService.updateByBo(bo.getPressureRegulatingBoxDetailsBo()); return updateById(update); } /** * 保存前的数据校验 * * @param entity 实体类数据 */ private void validEntityBeforeSave(GPressureRegulatingBox entity) { //TODO 做一些数据校验,如唯一约束 } @Override public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { if (isValid) { //TODO 做一些业务上的校验,判断是否需要校验 } return removeByIds(ids); } @Override public TableDataInfo myList(GPressureRegulatingBoxBo bo) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); //查询我的 if (StringUtils.isNotBlank(bo.getJobNum())){ QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GRegulatorBoxRelation::getUserId,bo.getJobNum()); List collect = regulatorBoxRelationService.list(queryWrapper).stream().map(GRegulatorBoxRelation::getRegulatorBoxId).collect(Collectors.toList()); lqw.in(GPressureRegulatingBox::getId,collect); } PagePlus result = pageVo(PageUtils.buildPagePlus(), lqw); return PageUtils.buildDataInfo(result); } }