|
@@ -1,26 +1,36 @@
|
|
|
package cn.iocoder.yudao.module.museum.service.museuminfo;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
|
|
|
import cn.iocoder.yudao.module.museum.controller.admin.museuminfo.vo.*;
|
|
|
import cn.iocoder.yudao.module.museum.dal.database.museuminfo.MuseumInfoDO;
|
|
|
import cn.iocoder.yudao.module.museum.dal.mysql.museuminfo.MuseumInfoMapper;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
import com.mzt.logapi.context.LogRecordContext;
|
|
|
import com.mzt.logapi.service.impl.DiffParseFunction;
|
|
|
import com.mzt.logapi.starter.annotation.LogRecord;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.singleton;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_USERNAME_EXISTS;
|
|
|
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;
|
|
|
import static cn.iocoder.yudao.module.museum.enums.social.LogRecordConstants.*;
|
|
|
|
|
|
+
|
|
|
@Service
|
|
|
public class MuseumInfoServiceImpl implements MuseumInfoService {
|
|
|
|
|
@@ -34,7 +44,7 @@ public class MuseumInfoServiceImpl implements MuseumInfoService {
|
|
|
@Override
|
|
|
@LogRecord(type = MUSEUM_TYPE, subType = CREATE_SUB_TYPE, bizNo = "{{#sampleId.id}}",
|
|
|
success = CREATE_SUB_TYPE_SUCCESS)
|
|
|
- public Long createMuseumInfo(MuseumInfoSaveVO saveVO) {
|
|
|
+ public Integer createMuseumInfo(MuseumInfoSaveVO saveVO) {
|
|
|
MuseumInfoDO sampleId = BeanUtils.toBean(saveVO, MuseumInfoDO.class);
|
|
|
museumInfoMapper.insert(sampleId);
|
|
|
LogRecordContext.putVariable("sampleId", sampleId );
|
|
@@ -109,6 +119,13 @@ public class MuseumInfoServiceImpl implements MuseumInfoService {
|
|
|
public MuseumInfoDO selectMuseumInfoBySampleId (String sampleId) {
|
|
|
return museumInfoMapper.selectMuseumInfoBySampleId(sampleId);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 查询所有标本
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<MuseumInfoDO> selectMuseumInfo () {
|
|
|
+ return museumInfoMapper.selectMuseumInfo();
|
|
|
+ }
|
|
|
/**
|
|
|
* 查询标本类型
|
|
|
*/
|
|
@@ -169,6 +186,118 @@ public class MuseumInfoServiceImpl implements MuseumInfoService {
|
|
|
public Integer MuseumInfoByOrigin(Integer year, Integer origin) {
|
|
|
return museumInfoMapper.countMuseumInfoByOrigin(year,origin);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 批量导入用户
|
|
|
+ *
|
|
|
+ * @param importSamples 导入用户列表
|
|
|
+ * @param isUpdateSupport 是否支持更新
|
|
|
+ * @return 导入结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
|
|
+ public MuseuminfoImportRespVO importSampleList(List<MuseuminfoImportExcelVO> importSamples, boolean isUpdateSupport) {
|
|
|
+ // 2. 遍历,逐个创建 or 更新
|
|
|
+ MuseuminfoImportRespVO respVO = MuseuminfoImportRespVO.builder().createSampleIds(new ArrayList<>())
|
|
|
+ .updateSampleIds(new ArrayList<>()).failureSampleIds(new LinkedHashMap<>()).build();
|
|
|
+ importSamples.forEach(importSample -> {
|
|
|
+
|
|
|
+ try {// 2.1.2 校验,判断是否有不符合的原因
|
|
|
+ museumInfoForCreateOrUpdate(null,importSample.getSampleId() );
|
|
|
+ } catch (ServiceException ex) {
|
|
|
+ respVO.getFailureSampleIds().put(importSample.getSampleId(), ex.getMessage());
|
|
|
+ }
|
|
|
+ // 2.2.1 判断如果不存在,在进行插入
|
|
|
+ MuseumInfoDO existSample = museumInfoMapper.selectMuseumInfoBySampleId(importSample.getSampleId());
|
|
|
+ if (existSample == null) {
|
|
|
+ Integer SampleType = transformSampleType(importSample.getSampleTypeC());
|
|
|
+ importSample.setSampleType(SampleType);
|
|
|
+ MuseumInfoDO newSample = BeanUtils.toBean(importSample, MuseumInfoDO.class);
|
|
|
+ museumInfoMapper.insert(newSample);
|
|
|
+ respVO.getCreateSampleIds().add(importSample.getSampleId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 2.2.2 如果存在,判断是否允许更新
|
|
|
+ if (!isUpdateSupport) {
|
|
|
+ respVO.getFailureSampleIds().put(importSample.getSampleId(), SAMPLE_ID_EXISTS.getMsg());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //todo允许更新
|
|
|
+// try {// 2.1.2 校验,判断是否有不符合的原因
|
|
|
+// MuseuminfoForCreateOrUpdate(existSample.getId(),importSample.getSampleId() );
|
|
|
+// } catch (ServiceException ex) {
|
|
|
+// respVO.getFailureSampleIds().put(importSample.getSampleId(), ex.getMessage());
|
|
|
+// }
|
|
|
+
|
|
|
+ Integer SampleType = transformSampleType(importSample.getSampleTypeC());
|
|
|
+ importSample.setSampleType(SampleType);
|
|
|
+ MuseumInfoDO updateSample = BeanUtils.toBean(importSample, MuseumInfoDO.class);
|
|
|
+ updateSample.setId(existSample.getId());
|
|
|
+ updateSample.setSampleType(SampleType);
|
|
|
+ museumInfoMapper.updateById(updateSample);
|
|
|
+ respVO.getUpdateSampleIds().add(importSample.getSampleId());
|
|
|
+ });
|
|
|
+
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MuseumInfoDO museumInfoForCreateOrUpdate(Integer id, String sampleId) {
|
|
|
+ // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确
|
|
|
+ return DataPermissionUtils.executeIgnore(() -> {
|
|
|
+ // 校验标本存在
|
|
|
+ MuseumInfoDO sample = museumInfoExists(id);
|
|
|
+ // 校验标本编号唯一
|
|
|
+ museumInfoSampleIdUnique(id, sampleId);
|
|
|
+ // 转换标本类型
|
|
|
+ return sample;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer transformSampleType(String sampleType) {
|
|
|
+ if (sampleType==null){
|
|
|
+ return null;
|
|
|
+ }else if (sampleType.equals("矿石")){
|
|
|
+ return 1;
|
|
|
+ }else if (sampleType.equals("岩石")){
|
|
|
+ return 2;
|
|
|
+ }else if (sampleType.equals("矿物")){
|
|
|
+ return 3;
|
|
|
+ }else if (sampleType.equals("陨石")){
|
|
|
+ return 4;
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException("无效的标本类型: " + sampleType);
|
|
|
+ }
|
|
|
+
|
|
|
+ MuseumInfoDO museumInfoExists(Integer id) {
|
|
|
+ if (id == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ MuseumInfoDO sample = museumInfoMapper.selectMuseumInfoById(id);
|
|
|
+ if (sample == null) {
|
|
|
+ throw exception(SAMPLE_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ return sample;
|
|
|
+ }
|
|
|
+
|
|
|
+ void museumInfoSampleIdUnique(Integer id, String sampleId) {
|
|
|
+ if (sampleId==null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ MuseumInfoDO sample = museumInfoMapper.selectMuseumInfoBySampleId(sampleId);
|
|
|
+ //todo 正常
|
|
|
+ if (sample == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // TODO sample不为空
|
|
|
+ if (id == null) {
|
|
|
+ throw exception(SAMPLE_ID_EXISTS);
|
|
|
+ }
|
|
|
+ if (!sample.getId().equals(Integer.valueOf(id))) {
|
|
|
+ throw exception(SAMPLE_ID_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|