【HZERO】数据导入
数据导入
文档整理
通用导入客户端
https://open.hand-china.com/hzero-docs/v1.3/zh/docs/service/import/import/
开放平台
https://open.hand-china.com/document-center/doc/component/174/10206?doc_id=133475&doc_code=3374#使用导入模板管理
客户端配置
代码示例
数据校验
package com.inja.mdm.app.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inja.mdm.domain.entity.MdmMdGoodsReport;
import com.inja.mdm.domain.lov.MdmDataStatus;
import com.inja.mdm.domain.repository.MdmRepository;
import com.inja.mdm.domain.repository.UnitRepository;
import com.inja.mdm.domain.vo.MdmMdGoodsReportVO;
import io.choerodon.core.oauth.DetailsHelper;
import org.hzero.boot.imported.app.service.ValidatorHandler;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidator;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidators;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.util.List;
/**
* description
*
* @author Lenovo-pc 2021/11/05 14:42
*/
@ImportValidators({
@ImportValidator(templateCode = "MDM_MD_GOODS")
})
public class ImportValidatorMdmGoodsReportVO extends ValidatorHandler {
@Autowired
private ObjectMapper objectMapper;
@Autowired
private UnitRepository unitRepository;
@Autowired
private MdmRepository mdmMdGoodsReportRepository;
@Override
public boolean validate(String data) {
MdmMdGoodsReportVO mdmMdGoodsReportVO = new MdmMdGoodsReportVO();
MdmMdGoodsReport optional = new MdmMdGoodsReport();
final Long tenantId = DetailsHelper.getUserDetails().getTenantId();
try {
mdmMdGoodsReportVO = objectMapper.readValue(data, MdmMdGoodsReportVO.class);
} catch (IOException e) {
e.printStackTrace();
}
//新增、编辑数据校验
if(mdmMdGoodsReportVO.getGoodsCode() == null){
//新增
optional.setTenantId(tenantId);
optional.setGoodsName(mdmMdGoodsReportVO.getGoodsName());
optional.setDeleteFlag("N");
String repeatCheck = null;
if (mdmMdGoodsReportVO.getModel() != null) {
repeatCheck = mdmMdGoodsReportVO.getGoodsName() + "+" + mdmMdGoodsReportVO.getGoodsSpec() + "+" + mdmMdGoodsReportVO.getModel() + "+" + mdmMdGoodsReportVO.getUnitIdMeaning();
} else if (mdmMdGoodsReportVO.getModel() == null) {
repeatCheck = mdmMdGoodsReportVO.getGoodsName() + "+" + mdmMdGoodsReportVO.getGoodsSpec() + "+" + mdmMdGoodsReportVO.getUnitIdMeaning();
}
optional.setRepeatCheck(repeatCheck);
MdmMdGoodsReport tempMdm = mdmMdGoodsReportRepository.selectOne(optional);
if(tempMdm != null){
getContext().addErrorMsg("这条数据已经存在");
return false;
}
return true;
} else {
//编辑
optional.setGoodsCode(mdmMdGoodsReportVO.getGoodsCode());
MdmMdGoodsReport tempMdm = mdmMdGoodsReportRepository.selectOne(optional);
if(tempMdm == null){
getContext().addErrorMsg("这条待编辑的数据不存在");
return false;
}
String status = tempMdm.getStatus();
if(!status.equals(MdmDataStatus.UNSUBMITTED.getValue())
&& !status.equals(MdmDataStatus.UNAPPROVE.getValue())){
getContext().addErrorMsg("状态有误这条数据不能更新");
return false;
}
//校验长描述
String repeatCheck = null;
if (mdmMdGoodsReportVO.getModel() != null) {
repeatCheck = mdmMdGoodsReportVO.getGoodsName() + "+" + mdmMdGoodsReportVO.getGoodsSpec() + "+" + mdmMdGoodsReportVO.getModel() + "+" + mdmMdGoodsReportVO.getUnitIdMeaning();
} else if (mdmMdGoodsReportVO.getModel() == null) {
repeatCheck = mdmMdGoodsReportVO.getGoodsName() + "+" + mdmMdGoodsReportVO.getGoodsSpec() + "+" + mdmMdGoodsReportVO.getUnitIdMeaning();
}
optional.setRepeatCheck(repeatCheck);
optional.setGoodsCode(null);
List tempMdmCheck = mdmMdGoodsReportRepository.select(optional);
if (tempMdmCheck.size() == 1 && !tempMdmCheck.get(0).getGoodsCode().equals(mdmMdGoodsReportVO.getGoodsCode())) {
getContext().addErrorMsg("长描述重复不能更新");
return false;
}
return true;
}
}
}
数据导入
package com.inja.mdm.app.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inja.mdm.app.service.MdmMdGoodsReportVOService;
import com.inja.mdm.domain.entity.*;
import com.inja.mdm.domain.lov.MdmDataStatus;
import com.inja.mdm.domain.repository.*;
import com.inja.mdm.domain.vo.MdmMdGoodsReportVO;
import com.inja.mdm.infra.repository.impl.PackagingTypeRepositoryImpl;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.DetailsHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.hzero.boot.imported.app.service.BatchImportHandler;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.hzero.boot.platform.code.builder.CodeRuleBuilder;
import org.hzero.core.base.BaseConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* 商品提报表应用服务默认实现
*
* @author cmj 2021-11-01 09:26:40
* @Autowired private CodeRuleBuilder codeRuleBuilder;
*
* private ObjectM
*/
@ImportService(templateCode = "MDM_MD_GOODS")
@Service
public class MdmMdGoodsReportVOServiceImpl extends BatchImportHandler implements MdmMdGoodsReportVOService {
@Autowired
private MdmRepository mdmGoodsReportRepository;
@Autowired
private UnitRepository unitRepository;
@Autowired
private MaterialClassRepository materialClassRepository;
@Autowired
private TranpricetypeRepository tranpricetypeRepository;
@Autowired
private TaxtypeRepository taxtypeRepository;
@Autowired
private CountryOfOriginRepository countryOfOriginRepository;
@Autowired
private NetContentUnitRepository netContentUnitRepository;
@Autowired
private PackagingTypeRepositoryImpl packagingTypeRepository;
@Autowired
private BrandRepository brandRepository;
private ObjectMapper objectMapper;
public MdmMdGoodsReportVOServiceImpl(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Autowired
private CodeRuleBuilder codeRuleBuilder;
@Override
public Boolean doImport(List data) {
String voStr = BaseConstants.Symbol.LEFT_MIDDLE_BRACE + StringUtils.join(data.toArray(), BaseConstants.Symbol.COMMA)
+ BaseConstants.Symbol.RIGHT_MIDDLE_BRACE;
List MdmMdGoodsReportVOs = null;
try {
MdmMdGoodsReportVOs = objectMapper.readValue(voStr, objectMapper.getTypeFactory().constructParametricType(List.class, MdmMdGoodsReportVO.class));
} catch (IOException e) {
e.printStackTrace();
}
final Long tenantId = DetailsHelper.getUserDetails().getTenantId();
List insertMdmMdGoodsReportVOs = MdmMdGoodsReportVOs.stream()
.filter(T -> T.getGoodsCode() == null)
.collect(Collectors.toList());
List updateMdmMdGoodsReportVOs = MdmMdGoodsReportVOs.stream()
.filter(T -> T.getGoodsCode() != null)
.collect(Collectors.toList());
//新增
if (!insertMdmMdGoodsReportVOs.isEmpty()) {
insertMdmMdGoodsReportVOs.forEach(T -> {
MdmMdGoodsReport mdmMdGoodsReport = new MdmMdGoodsReport();
BeanUtils.copyProperties(T, mdmMdGoodsReport);
mdmMdGoodsReport.setStatus(MdmDataStatus.UNSUBMITTED.getValue());
mdmMdGoodsReport.setTenantId(tenantId);
mdmMdGoodsReport.setDeleteFlag("N");
mdmMdGoodsReport.setGoodsCode(this.codeRuleBuilder.generateCode("MDM.BF.GOODS.EDIT", null));
// 查询商品分类(取物料四级分类)
if (null != T.getGoodClassCodeMeaning()) {
MaterialClass tempMaterialClass = new MaterialClass();
tempMaterialClass.setLevel("4");
tempMaterialClass.setMaterialClassCode(T.getGoodClassCodeMeaning());
List materialClass = materialClassRepository.select(tempMaterialClass);
if (materialClass.isEmpty()) {
throw new CommonException("error.mdm.goods.materialclassnotexist");
}
mdmMdGoodsReport.setGoodClassCode(materialClass.get(0).getMaterialClassCode());
}
// 查询计量单位
if (null == T.getUnitIdMeaning()) {
throw new CommonException("error.mdm.goods.unitnotexist");
}
Unit tempUnit = new Unit();
tempUnit.setUnitName(T.getUnitIdMeaning());
List unit = unitRepository.select(tempUnit);
if (unit.isEmpty()) {
throw new CommonException("error.mdm.goods.unitnotexist");
}
mdmMdGoodsReport.setUnitId(unit.get(0).getUnitId());
// 查询运输计价类别
if (null == T.getTranpricetypeCodeMeaning()) {
throw new CommonException("error.mdm.goods.tranpricetypenotexist");
}
Tranpricetype tempTranpritype = new Tranpricetype();
tempTranpritype.setTranpricetypeName(T.getTranpricetypeCodeMeaning());
List tranpricetype = tranpricetypeRepository.select(tempTranpritype);
if (tranpricetype.isEmpty()) {
throw new CommonException("error.mdm.goods.tranpricetypenotexist");
}
mdmMdGoodsReport.setTranpricetypeCode(tranpricetype.get(0).getTranpricetypeCode());
// 查询物料税类
if (null == T.getMaterialTaxIdMeaning()) {
throw new CommonException("error.mdm.goods.taxtypenotexist");
}
Taxtype tempTaxtype = new Taxtype();
tempTaxtype.setTaxtypeName(T.getMaterialTaxIdMeaning());
List taxtype = taxtypeRepository.select(tempTaxtype);
if (taxtype.isEmpty()) {
throw new CommonException("error.mdm.goods.taxtypenotexist");
}
mdmMdGoodsReport.setMaterialTaxId(taxtype.get(0).getTaxtypeId());
// 查询品牌名称
if (null == T.getBrandIdMeaning()) {
throw new CommonException("error.mdm.goods.brandnotexist");
}
Brand tempBrand = new Brand();
tempBrand.setBrandName(T.getBrandIdMeaning());
List brand = brandRepository.select(tempBrand);
if (brand.isEmpty()) {
throw new CommonException("error.mdm.goods.brandnotexist");
}
mdmMdGoodsReport.setBrandId(brand.get(0).getBrandId());
// 查询原产国
if (null != T.getOriginCountryIdMeaning()) {
CountryOfOrigin tempCountryOfOrigin = new CountryOfOrigin();
tempCountryOfOrigin.setCountryOfOriginName(T.getOriginCountryIdMeaning());
List countryOfOrigin = countryOfOriginRepository.select(tempCountryOfOrigin);
if (countryOfOrigin.isEmpty()) {
throw new CommonException("error.mdm.goods.countryoforiginnotexist");
}
mdmMdGoodsReport.setOriginCountryId(countryOfOrigin.get(0).getCountryOfOriginId());
}
// 查询净含量单位
if (null != T.getNetContentUnitIdMeaning()) {
NetContentUnit tempNetContentUnit = new NetContentUnit();
tempNetContentUnit.setNetContentUnitName(T.getNetContentUnitIdMeaning());
List netContentUnit = netContentUnitRepository.select(tempNetContentUnit);
if (netContentUnit.isEmpty()) {
throw new CommonException("error.mdm.goods.netcontentunitnotexist");
}
mdmMdGoodsReport.setNetContentUnitId(netContentUnit.get(0).getNetContentUnitId());
}
// 查询单品包装类型
if (null != T.getEachPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getEachPackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setEachPackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 查询中包包装类型
if (null != T.getPackPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getPackPackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setEachPackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 查询大包包装类型
if (null != T.getPackPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getCasePackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setCasePackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 商品长描述
String repeatCheck = null;
if (T.getModel() != null) {
repeatCheck = T.getGoodsName() + "+" + T.getGoodsSpec() + "+" + T.getModel() + "+" + T.getUnitIdMeaning();
} else if (T.getModel() == null) {
repeatCheck = T.getGoodsName() + "+" + T.getGoodsSpec() + "+" + T.getUnitIdMeaning();
}
mdmMdGoodsReport.setRepeatCheck(repeatCheck);
// 插入数据
mdmGoodsReportRepository.insertSelective(mdmMdGoodsReport);
});
}
//编辑
if (!updateMdmMdGoodsReportVOs.isEmpty()) {
updateMdmMdGoodsReportVOs.forEach(T -> {
MdmMdGoodsReport tempMdmMdGoodsReport = new MdmMdGoodsReport();
tempMdmMdGoodsReport.setGoodsCode(T.getGoodsCode());
List mdmMdGoodsReportList = mdmGoodsReportRepository.select(tempMdmMdGoodsReport);
MdmMdGoodsReport mdmMdGoodsReport = mdmMdGoodsReportList.get(0);
Long id = mdmMdGoodsReport.getGoodsId();
BeanUtils.copyProperties(T, mdmMdGoodsReport);
mdmMdGoodsReport.setGoodsId(id);
// 查询商品分类(取物料四级分类)
if (null != T.getGoodClassCodeMeaning()) {
MaterialClass tempMaterialClass = new MaterialClass();
tempMaterialClass.setLevel("4");
tempMaterialClass.setMaterialClassName(T.getGoodClassCodeMeaning());
List materialClass = materialClassRepository.select(tempMaterialClass);
if (materialClass.isEmpty()) {
throw new CommonException("error.mdm.goods.materialclassnotexist");
}
mdmMdGoodsReport.setGoodClassCode(materialClass.get(0).getMaterialClassCode());
}
// 查询计量单位
if (null == T.getUnitIdMeaning()) {
throw new CommonException("error.mdm.goods.unitnotexist");
}
Unit tempUnit = new Unit();
tempUnit.setUnitName(T.getUnitIdMeaning());
List unit = unitRepository.select(tempUnit);
if (unit.isEmpty()) {
throw new CommonException("error.mdm.goods.unitnotexist");
}
mdmMdGoodsReport.setUnitId(unit.get(0).getUnitId());
// 查询运输计价类别
if (null == T.getTranpricetypeCodeMeaning()) {
throw new CommonException("error.mdm.goods.tranpricetypenotexist");
}
Tranpricetype tempTranpritype = new Tranpricetype();
tempTranpritype.setTranpricetypeName(T.getTranpricetypeCodeMeaning());
List tranpricetype = tranpricetypeRepository.select(tempTranpritype);
if (tranpricetype.isEmpty()) {
throw new CommonException("error.mdm.goods.tranpricetypenotexist");
}
mdmMdGoodsReport.setTranpricetypeCode(tranpricetype.get(0).getTranpricetypeCode());
// 查询物料税类
if (null == T.getMaterialTaxIdMeaning()) {
throw new CommonException("error.mdm.goods.taxtypenotexist");
}
Taxtype tempTaxtype = new Taxtype();
tempTaxtype.setTaxtypeName(T.getMaterialTaxIdMeaning());
List taxtype = taxtypeRepository.select(tempTaxtype);
if (taxtype.isEmpty()) {
throw new CommonException("error.mdm.goods.taxtypenotexist");
}
mdmMdGoodsReport.setMaterialTaxId(taxtype.get(0).getTaxtypeId());
// 查询品牌名称
if (null != T.getBrandIdMeaning()) {
Brand tempBrand = new Brand();
tempBrand.setBrandName(T.getBrandIdMeaning());
List brand = brandRepository.select(tempBrand);
if (brand.isEmpty()) {
throw new CommonException("error.mdm.goods.brandnotexist");
}
mdmMdGoodsReport.setBrandId(brand.get(0).getBrandId());
}
// 查询原产国
if (null != T.getOriginCountryIdMeaning()) {
CountryOfOrigin tempCountryOfOrigin = new CountryOfOrigin();
tempCountryOfOrigin.setCountryOfOriginName(T.getOriginCountryIdMeaning());
List countryOfOrigin = countryOfOriginRepository.select(tempCountryOfOrigin);
if (countryOfOrigin.isEmpty()) {
throw new CommonException("error.mdm.goods.countryoforiginnotexist");
}
mdmMdGoodsReport.setOriginCountryId(countryOfOrigin.get(0).getCountryOfOriginId());
}
// 查询净含量单位
if (null != T.getNetContentUnitIdMeaning()) {
NetContentUnit tempNetContentUnit = new NetContentUnit();
tempNetContentUnit.setNetContentUnitName(T.getNetContentUnitIdMeaning());
List netContentUnit = netContentUnitRepository.select(tempNetContentUnit);
if (netContentUnit.isEmpty()) {
throw new CommonException("error.mdm.goods.netcontentunitnotexist");
}
mdmMdGoodsReport.setNetContentUnitId(netContentUnit.get(0).getNetContentUnitId());
}
// 查询单品包装类型
if (null != T.getEachPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getEachPackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setEachPackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 查询中包包装类型
if (null != T.getPackPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getPackPackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setEachPackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 查询大包包装类型
if (null != T.getPackPackagingTypeIdMeaning()) {
PackagingType tempPackagingType = new PackagingType();
tempPackagingType.setPackagingTypeName(T.getCasePackagingTypeIdMeaning());
List packagingType = packagingTypeRepository.select(tempPackagingType);
if (packagingType.isEmpty()) {
throw new CommonException("error.mdm.goods.packagingTypenotexist");
}
mdmMdGoodsReport.setCasePackagingTypeId(packagingType.get(0).getPackagingTypeId());
}
// 商品长描述
String repeatCheck = null;
if (T.getModel() != null) {
repeatCheck = T.getGoodsName() + "+" + T.getGoodsSpec() + "+" + T.getModel() + "+" + T.getUnitIdMeaning();
} else if (T.getModel() == null) {
repeatCheck = T.getGoodsName() + "+" + T.getGoodsSpec() + "+" + T.getUnitIdMeaning();
}
mdmMdGoodsReport.setRepeatCheck(repeatCheck);
// 插入数据
mdmGoodsReportRepository.updateByPrimaryKeySelective(mdmMdGoodsReport);
});
}
return true;
}
}
对象序列化
https://blog.csdn.net/mingyuli/article/details/119455324
public class UserBase {
/**
* 用户名
*/
private String userName;
/**
* 年龄
*/
private Integer age;
/**
* 增加时间
*/
private Date addTime;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
}
public class TestMain {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String json1 = "{\"userName\":\"小李飞刀\",\"age\":18,\"addTime\":1591851786568}";
String json2 = "[{\"userName\":\"小李飞刀\",\"age\":18,\"addTime\":123}, {\"userName\":\"小李飞刀2\",\"age\":182,\"addTime\":1234}]";
try {
//1、json字符串转为对象
UserBase userBase1 = objectMapper.readValue(json1, UserBase.class);
UserBase userBase2 = objectMapper.readValue(json1, new TypeReference(){});
System.out.println(userBase1.getUserName());
System.out.println(userBase2.getUserName());
//2、json转为map
Map map = objectMapper.readValue(json1, new TypeReference