|
@@ -0,0 +1,366 @@
|
|
|
+package org.springblade.business.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springblade.business.dto.*;
|
|
|
+import org.springblade.business.entity.TrialContainerClassification;
|
|
|
+import org.springblade.business.mapper.TrialContainerClassificationMapper;
|
|
|
+import org.springblade.business.service.ITrialContainerClassificationService;
|
|
|
+import org.springblade.business.utils.BeanFieldUtils;
|
|
|
+import org.springblade.business.utils.ExcelUtils;
|
|
|
+import org.springblade.business.vo.TrialContainerClassificationPageVO;
|
|
|
+import org.springblade.business.vo.TrialContainerClassificationVO;
|
|
|
+import org.springblade.common.utils.FileUtils;
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
+import org.springblade.core.tool.utils.BeanUtil;
|
|
|
+import org.springblade.core.tool.utils.DateUtil;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class TrialContainerClassificationServiceImpl
|
|
|
+ extends BaseServiceImpl<TrialContainerClassificationMapper, TrialContainerClassification>
|
|
|
+ implements ITrialContainerClassificationService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TrialContainerClassification classificationDetail(Long id) {
|
|
|
+ return baseMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean classificationSubmit(TrialContainerClassificationDTO obj) {
|
|
|
+ if (ObjectUtil.isEmpty(obj.getId())) {
|
|
|
+ obj.setId(SnowFlakeUtil.getId());
|
|
|
+ //新增容器分类信息
|
|
|
+ TrialContainerClassification beanObj = BeanUtil.copyProperties(obj, TrialContainerClassification.class);
|
|
|
+ if (beanObj != null) {
|
|
|
+ beanObj.setFieldId(null);
|
|
|
+ beanObj.setFieldName(null);
|
|
|
+ beanObj.setFieldType(null);
|
|
|
+ beanObj.setContainerId(null);
|
|
|
+ beanObj.setContainerInitTabName("u_trial_container_" + obj.getId());
|
|
|
+ obj.setContainerInitTabName(beanObj.getContainerInitTabName());
|
|
|
+ baseMapper.insert(beanObj);
|
|
|
+
|
|
|
+ //新增容器分类字段信息
|
|
|
+ List<TrialContainerClassification> initFieldsData = new ArrayList<>();
|
|
|
+ if (obj.getFieldList().size() > 0) {
|
|
|
+ int filedNumber = 1;
|
|
|
+ for (TrialContainerClassificationFieldBean bean : obj.getFieldList()) {
|
|
|
+ TrialContainerClassification beanObjField = BeanUtil.copyProperties(bean, TrialContainerClassification.class);
|
|
|
+ if (beanObjField != null) {
|
|
|
+ beanObjField.setFieldKey("key_" + filedNumber++);
|
|
|
+ beanObjField.setContractId(obj.getContractId());
|
|
|
+ beanObjField.setContainerId(obj.getId());
|
|
|
+ beanObjField.setFieldId(SnowFlakeUtil.getId());
|
|
|
+ beanObjField.setContainerInitTabName(beanObj.getContainerInitTabName());
|
|
|
+ baseMapper.insert(beanObjField);
|
|
|
+ initFieldsData.add(beanObjField);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增容器对应实体表信息
|
|
|
+ this.addTrialContainerInitTab(obj, initFieldsData);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //修改
|
|
|
+ //获取存在的字段List
|
|
|
+ List<TrialContainerClassification> filedList = baseMapper.selectList(Wrappers.<TrialContainerClassification>query().lambda().eq(TrialContainerClassification::getContainerId, obj.getId()));
|
|
|
+
|
|
|
+ TrialContainerClassification trialContainerClassification;
|
|
|
+ if (filedList.size() > 0) {
|
|
|
+ Collections.reverse(filedList);
|
|
|
+ trialContainerClassification = filedList.stream().findFirst().orElse(filedList.get(filedList.size() - 1));
|
|
|
+ } else {
|
|
|
+ trialContainerClassification = new TrialContainerClassification();
|
|
|
+ trialContainerClassification.setFieldKey("key_0");
|
|
|
+ }
|
|
|
+
|
|
|
+ int keyName = Integer.parseInt(trialContainerClassification.getFieldKey().split("_")[1]) + 1;
|
|
|
+
|
|
|
+ //修改容器名称
|
|
|
+ this.update(Wrappers.<TrialContainerClassification>update().lambda().set(TrialContainerClassification::getContainerName, obj.getContainerName()).eq(TrialContainerClassification::getId, obj.getId()));
|
|
|
+
|
|
|
+ if (filedList.size() == 0) {
|
|
|
+ for (TrialContainerClassificationFieldBean bean : obj.getFieldList()) {
|
|
|
+ //不存在fieldId,证明不存在该字段,做新增处理
|
|
|
+ keyName = this.alterTabFiled(obj, keyName, bean);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //判断字段List是否发生改变,修改对应字段信息
|
|
|
+ for (TrialContainerClassificationFieldBean beanOut : obj.getFieldList()) {
|
|
|
+ if (ObjectUtil.isEmpty(beanOut.getFieldId())) {
|
|
|
+ //不存在fieldId,证明不存在该字段,做新增处理
|
|
|
+ keyName = this.alterTabFiled(obj, keyName, beanOut);
|
|
|
+ } else {
|
|
|
+ //存在fieldId,证明存在该字段,做修改处理
|
|
|
+ for (TrialContainerClassification beanInDB : filedList) {
|
|
|
+ if (beanOut.getFieldId().equals(beanInDB.getFieldId())) {
|
|
|
+ if (ObjectUtil.isEmpty(beanOut.getFieldName()) || ObjectUtil.isEmpty(beanOut.getFieldType())) {
|
|
|
+ throw new ServiceException("字段名、类型不能为空,请重新填写");
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(beanOut.getFieldName()) && ObjectUtil.isNotEmpty(beanInDB.getFieldName())
|
|
|
+ && !beanOut.getFieldName().equals(beanInDB.getFieldName())) {
|
|
|
+ //修改字段基础名称,不修改实体表字段名
|
|
|
+ this.update(Wrappers.<TrialContainerClassification>update().lambda().set(TrialContainerClassification::getFieldName, beanOut.getFieldName()).eq(TrialContainerClassification::getFieldId, beanOut.getFieldId()));
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(beanOut.getFieldType()) && ObjectUtil.isNotEmpty(beanInDB.getFieldType())
|
|
|
+ && !beanOut.getFieldType().equals(beanInDB.getFieldType())) {
|
|
|
+ //修改字段类型,同步修改实体表字段类型
|
|
|
+ this.update(Wrappers.<TrialContainerClassification>update().lambda().set(TrialContainerClassification::getFieldType, beanOut.getFieldType()).eq(TrialContainerClassification::getFieldId, beanOut.getFieldId()));
|
|
|
+ //判断实体表字段下是否存在数据,存在则不允许修改类型
|
|
|
+ Integer result = baseMapper.selectDataByTabName(obj.getContainerInitTabName(), beanOut.getFieldKey());
|
|
|
+ if (result > 0) {
|
|
|
+ throw new ServiceException("当前字段下存在数据,不允许修改类型");
|
|
|
+ }
|
|
|
+ if (result == 0) {
|
|
|
+ baseMapper.updateFiledType(obj.getContainerInitTabName(), beanOut.getFieldKey(), BeanFieldUtils.getFieldType(beanOut.getFieldType()), BeanFieldUtils.getFieldLength(beanOut.getFieldType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int alterTabFiled(TrialContainerClassificationDTO obj, int keyName, TrialContainerClassificationFieldBean bean) {
|
|
|
+ TrialContainerClassification beanObj = BeanUtil.copyProperties(bean, TrialContainerClassification.class);
|
|
|
+ if (beanObj != null) {
|
|
|
+ beanObj.setContainerId(obj.getId());
|
|
|
+ beanObj.setFieldId(SnowFlakeUtil.getId());
|
|
|
+ beanObj.setFieldKey("key_" + keyName++);
|
|
|
+ beanObj.setContainerInitTabName(obj.getContainerInitTabName());
|
|
|
+ baseMapper.insert(beanObj);
|
|
|
+ //追加实体表字段
|
|
|
+ baseMapper.alterTabFiled(obj.getContainerInitTabName(), beanObj.getFieldKey(), BeanFieldUtils.getFieldType(beanObj.getFieldType()), BeanFieldUtils.getFieldLength(beanObj.getFieldType()));
|
|
|
+ }
|
|
|
+ return keyName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addTrialContainerInitTab(TrialContainerClassificationDTO obj, List<TrialContainerClassification> initFieldsData) {
|
|
|
+ if (obj.getFieldList().size() == 0) {
|
|
|
+ baseMapper.addTrialContainerInitTab(obj.getContainerInitTabName());
|
|
|
+ } else {
|
|
|
+ String sql = this.createSQL(initFieldsData);
|
|
|
+ baseMapper.addTrialContainerInitTabBySql(obj.getContainerInitTabName(), sql);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createSQL(List<TrialContainerClassification> initFieldsData) {
|
|
|
+ StringBuilder sbr = new StringBuilder();
|
|
|
+ for (TrialContainerClassification bean : initFieldsData) {
|
|
|
+ sbr.append(bean.getFieldKey()).append(" ").append(BeanFieldUtils.getFieldType(bean.getFieldType())).append("(").append(BeanFieldUtils.getFieldLength(bean.getFieldType())).append(") ,\n");
|
|
|
+ }
|
|
|
+ return String.valueOf(sbr);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<TrialContainerClassificationVO> classificationList(Long contractId) {
|
|
|
+ List<TrialContainerClassification> resultList = baseMapper.selectList(Wrappers.<TrialContainerClassification>lambdaQuery().eq(TrialContainerClassification::getContractId, contractId));
|
|
|
+ List<TrialContainerClassification> containerList = resultList.stream().filter(f -> ObjectUtil.isNotEmpty(f.getContainerName())).collect(Collectors.toList());
|
|
|
+ List<TrialContainerClassification> fieldList = resultList.stream().filter(f -> ObjectUtil.isEmpty(f.getContainerName())).collect(Collectors.toList());
|
|
|
+ Map<String, List<TrialContainerClassification>> result = containerList.stream().collect(Collectors.groupingBy(TrialContainerClassification::getContainerInitTabName));
|
|
|
+ List<TrialContainerClassificationVO> responseList = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, List<TrialContainerClassification>> listEntry : result.entrySet()) {
|
|
|
+ for (TrialContainerClassification trialContainerClassification : listEntry.getValue()) {
|
|
|
+ List<TrialContainerClassification> collect = fieldList.stream().filter(f -> f.getContainerId().equals(trialContainerClassification.getId())).collect(Collectors.toList());
|
|
|
+
|
|
|
+ TrialContainerClassificationVO vo = new TrialContainerClassificationVO();
|
|
|
+ vo.setId(trialContainerClassification.getId());
|
|
|
+ vo.setContractId(trialContainerClassification.getContractId());
|
|
|
+ vo.setContainerName(trialContainerClassification.getContainerName());
|
|
|
+ vo.setContainerInitTabName(trialContainerClassification.getContainerInitTabName());
|
|
|
+
|
|
|
+ List<TrialContainerClassificationFieldBean> beans = new ArrayList<>();
|
|
|
+ for (TrialContainerClassification obj : collect) {
|
|
|
+ beans.add(BeanUtil.copyProperties(obj, TrialContainerClassificationFieldBean.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ vo.setFieldList(beans);
|
|
|
+ responseList.add(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseList.stream().sorted(Comparator.comparing(TrialContainerClassificationVO::getId)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<HashMap<Object, Object>> dataPage(TrialContainerClassificationPageVO obj) {
|
|
|
+ TrialContainerClassification trialContainerClassification = baseMapper.selectById(obj.getId());
|
|
|
+ List<HashMap<Object, Object>> hashMaps = baseMapper.selectTabDataAll(trialContainerClassification.getContainerInitTabName());
|
|
|
+ Page<HashMap<Object, Object>> page = new Page<>(obj.getCurrent(), obj.getSize(), hashMaps.size());
|
|
|
+ long size = page.getSize();
|
|
|
+ long current = (page.getCurrent() - 1) * size;
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(obj.getQueryValue()) && StringUtils.isNotEmpty(obj.getFieldKey())) {
|
|
|
+ //模糊查询
|
|
|
+ hashMaps = baseMapper.selectLikeByFieldKey(trialContainerClassification.getContainerInitTabName(), obj.getQueryValue(), obj.getFieldKey(), current, size);
|
|
|
+ int row = baseMapper.selectCountByField(trialContainerClassification.getContainerInitTabName(), obj.getQueryValue(), obj.getFieldKey());
|
|
|
+ page.setTotal(row);
|
|
|
+ } else {
|
|
|
+ //全部查询
|
|
|
+ hashMaps = baseMapper.selectLikeByFieldKey2(trialContainerClassification.getContainerInitTabName(), current, size);
|
|
|
+ }
|
|
|
+ return page.setRecords(hashMaps);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean dataSubmit(TrialContainerDataDTO obj) {
|
|
|
+ if (obj.getBeanList().size() == 0) {
|
|
|
+ throw new ServiceException("数据不能为空");
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isEmpty(obj.getId())) {
|
|
|
+ //新增
|
|
|
+ StringBuilder key = new StringBuilder();
|
|
|
+ StringBuilder value = new StringBuilder();
|
|
|
+ for (TrialContainerDataBean bean : obj.getBeanList()) {
|
|
|
+ key.append(bean.getFieldKey()).append(",");
|
|
|
+ value.append("'").append(ObjectUtil.isNotEmpty(bean) ? bean.getFieldValue() : null).append("',");
|
|
|
+ }
|
|
|
+ String fieldKeys = key.substring(0, key.length() - 1);
|
|
|
+ String fieldValues = value.substring(0, value.length() - 1).replace("'null'", "null");
|
|
|
+
|
|
|
+ return baseMapper.insertData(obj.getContainerInitTabName(), SnowFlakeUtil.getId(), DateUtil.format(obj.getFieldCalibrationTime(), "yyyy-MM-dd"), fieldKeys, fieldValues) > 0;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //修改
|
|
|
+ StringBuilder keys = new StringBuilder();
|
|
|
+ for (TrialContainerDataBean bean : obj.getBeanList()) {
|
|
|
+ keys.append(bean.getFieldKey()).append("='").append(ObjectUtil.isNotEmpty(bean) ? bean.getFieldValue() : null).append("',");
|
|
|
+ }
|
|
|
+ String values = "," + keys.substring(0, keys.length() - 1);
|
|
|
+
|
|
|
+ return baseMapper.updateData(obj.getContainerInitTabName(), obj.getId(), DateUtil.format(obj.getFieldCalibrationTime(), "yyyy-MM-dd"), values) > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean dataRemove(Long id, String containerInitTabName) {
|
|
|
+ return baseMapper.removeData(id, containerInitTabName) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean dataImportExcel(String containerInitTabName, MultipartFile file) throws IOException {
|
|
|
+ File convert = FileUtils.convert(file);
|
|
|
+ String path = convert.getPath();
|
|
|
+ try {
|
|
|
+ List<List<Map<String, Object>>> lists = ExcelUtils.importExcel(path, 1, 1, 1);
|
|
|
+ //获取第一行title
|
|
|
+ List<Object> titleName = new ArrayList<>();
|
|
|
+ for (List<Map<String, Object>> list : lists) {
|
|
|
+ for (Map<String, Object> row : list) {
|
|
|
+ for (Map.Entry<String, Object> col : row.entrySet()) {
|
|
|
+ titleName.add(col.getValue());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据title对应fieldName、构造fieldKeys
|
|
|
+ List<TrialContainerClassification> fieldBeans = baseMapper.selectList(Wrappers.<TrialContainerClassification>lambdaQuery()
|
|
|
+ .eq(TrialContainerClassification::getContainerInitTabName, containerInitTabName)
|
|
|
+ .isNotNull(TrialContainerClassification::getFieldId)
|
|
|
+ );
|
|
|
+ List<String> collect = titleName.stream().map(String::valueOf).collect(Collectors.toList());
|
|
|
+ StringBuilder keys = new StringBuilder();
|
|
|
+ for (TrialContainerClassification fieldBean : fieldBeans) {
|
|
|
+ for (String name : collect) {
|
|
|
+ if (fieldBean.getFieldName().equals(name)) {
|
|
|
+ keys.append(fieldBean.getFieldKey()).append(",");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String fieldKeys = keys.substring(0, keys.length() - 1);
|
|
|
+
|
|
|
+ //解析构造插入数据
|
|
|
+ for (List<Map<String, Object>> list : lists) {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ if (i == 0) {
|
|
|
+ //跳过title
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ StringBuilder values = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> col : list.get(i).entrySet()) {
|
|
|
+ values.append("'").append(ObjectUtil.isNotEmpty(col.getValue()) ? col.getValue() : null).append("',");
|
|
|
+ }
|
|
|
+ //fieldValues
|
|
|
+ String fieldValues = values.substring(0, values.length() - 1).replace("'null'", "null");
|
|
|
+
|
|
|
+ //插入一行的数据
|
|
|
+ baseMapper.insertData(containerInitTabName, SnowFlakeUtil.getId(), DateUtil.format(new Date(), "yyyy-MM-dd"), fieldKeys, fieldValues);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("导入时发生了异常,请检查excel模板格式是否与当前容器字段相对应");
|
|
|
+ } finally {
|
|
|
+ File file2 = new File(path);
|
|
|
+ if (file2.isFile() && file2.exists()) {
|
|
|
+ if (file2.delete()) {
|
|
|
+ System.gc();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String dataVerificationNo(TrialContainerDataBean bean, Long id, String containerInitTabName) {
|
|
|
+ List<HashMap<Object, Object>> hashMaps = baseMapper.selectTabDataAll(containerInitTabName);
|
|
|
+ List<String> values = new ArrayList<>();
|
|
|
+ Map<Long, String> beanMap = new HashMap<>();
|
|
|
+ for (HashMap<Object, Object> hashMap : hashMaps) {
|
|
|
+ Long beanId = null;
|
|
|
+ String beanNumber = null;
|
|
|
+ for (Map.Entry<Object, Object> map : hashMap.entrySet()) {
|
|
|
+ if (("id").equals(map.getKey())) {
|
|
|
+ beanId = (Long) map.getValue();
|
|
|
+ }
|
|
|
+ if (bean.getFieldKey().equals(map.getKey())) {
|
|
|
+ beanNumber = String.valueOf(map.getValue());
|
|
|
+ values.add(String.valueOf(map.getValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (beanId != null && beanNumber != null) {
|
|
|
+ beanMap.put(beanId, beanNumber);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isEmpty(id)) {
|
|
|
+ String obj = values.stream().filter(f -> f.equals(bean.getFieldValue())).findAny().orElse(null);
|
|
|
+ if (obj != null) {
|
|
|
+ return "编号已存在,请重新输入";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Map.Entry<Long, String> map = beanMap.entrySet().stream().filter(f -> f.getKey().equals(id)).findAny().orElse(null);
|
|
|
+ assert map != null;
|
|
|
+ if (!map.getValue().equals(bean.getFieldValue())) {
|
|
|
+ String obj = values.stream().filter(f -> f.equals(bean.getFieldValue())).findAny().orElse(null);
|
|
|
+ if (obj != null) {
|
|
|
+ return "编号已存在,请重新输入";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "编号可以使用";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|