企业端-小程序接口和调试修改

This commit is contained in:
79493 2022-12-15 09:11:31 +08:00
parent ebf8ca0158
commit ee11246d87
10 changed files with 281 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.rzyc.mapper.ent;
import com.rzyc.model.EasyExcel.EasyExcelInList;
import com.rzyc.model.ent.InListItem;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
@ -23,4 +24,9 @@ public interface InListItemMapper extends BaseMapper<InListItem> {
/*通过行业查询清单信息*/
List<InListItem> findByIndustryId(@Param("industryId") String industryId);
/**
*
*
* */
int insertItem(List<EasyExcelInList>easyExcelInLists);
}

View File

@ -3,6 +3,7 @@ package com.rzyc.mapper.ent;
import com.rzyc.model.ent.BaseInClass;
import com.rzyc.model.ent.InList;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@ -19,4 +20,12 @@ import java.util.List;
public interface InListMapper extends BaseMapper<InList> {
/*查询所有*/
List<InList> findAll();
/**
* 插入多条清单
* @param list
* @return int
* */
int insertList(@Param("list") List<InList>list);
}

View File

@ -0,0 +1,21 @@
package com.rzyc.model.EasyExcel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
public class EasyExcelInList {
private String industry;
private String listName;
private String listItem;
private String listContent;
private Integer standard;
private Integer frequency;
private String remark;
private String industryId;
private String listId;
}

View File

@ -56,7 +56,7 @@ public class InList implements Serializable {
@TableField("modify_by")
private String modifyBy;
@ApiModelProperty(value = "父级id")
@ApiModelProperty(value = "父级id(行业id)")
@TableField("industry_id")
private String industryId;

View File

@ -29,6 +29,10 @@
item_basis, item_proof, item_law, sort_id, del_state,create_time, create_by, modify_time, modify_by
</sql>
<insert id="insertItem">
</insert>
<select id="SelectContents" resultMap="BaseResultMap">
select item_content from in_list_item;
</select>

View File

@ -19,10 +19,23 @@
list_id, name, sort_id,del_state, create_time, create_by, modify_time, modify_by
</sql>
<!--查询清单列-->
<select id="findAll" resultMap="BaseResultMap">
select * from in_list il order by il.sort_id asc
</select>
<insert id="insertList">
insert into in_list (list_id, name, del_state, create_by,create_time,industry_id)values
<foreach collection="list" item="list" separator=",">
(#{list.listId},
#{list.name},
1,
#{list.createBy},
#{list.createTime},
#{list.industryId})
</foreach>
</insert>
</mapper>

View File

@ -23,7 +23,6 @@ import com.rzyc.model.dto.SparePartDto;
import com.rzyc.model.ent.InEntList;
import com.rzyc.model.ent.SysEnterprise;
import com.rzyc.utils.easyexcel.EasyExcelInList;
import com.rzyc.utils.easyexcel.InListListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -0,0 +1,137 @@
package com.rzyc.utils.easyexcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.common.utils.RandomNumber;
import com.rzyc.mapper.ent.BaseInClassMapper;
import com.rzyc.mapper.ent.InListItemMapper;
import com.rzyc.mapper.ent.InListMapper;
import com.rzyc.model.EasyExcel.EasyExcelInList;
import com.rzyc.model.ent.BaseInClass;
import com.rzyc.model.ent.InList;
import lombok.extern.slf4j.Slf4j;
import org.graalvm.compiler.core.common.util.IntList;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 模板的读取类
*
* @author Jiaju Zhuang
*/
// 有个很重要的点 DemoDataListener 不能被spring管理要每次读取excel都要new,然后里面用到spring可以构造方法传进去
@Slf4j
public class InListListener implements ReadListener<EasyExcelInList> {
/**
* 每隔5条存储数据库实际使用中可以100条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 100;
/**
* 缓存的数据
*/
private List<EasyExcelInList> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
private List<InList>queryList = new ArrayList<>();
private String listName = null;
private String industry = null;
private String listId = null;
private String industryId = null;
private InListMapper inListMapper;
private InListItemMapper inListItemMapper;
private BaseInClassMapper baseInClassMapper;
public InListListener(InListMapper inListMapper, InListItemMapper inListItemMapper, BaseInClassMapper baseInClassMapper) {
this.inListMapper = inListMapper;
this.inListItemMapper = inListItemMapper;
this.baseInClassMapper = baseInClassMapper;
}
/**
* 这个每一条数据解析都会来调用
*
* @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(EasyExcelInList data, AnalysisContext context) {
log.info("解析到一条数据:{}", JSON.toJSONString(data));
if (data.getListName() != null){
BaseInClass base = baseInClassMapper.selectByName(data.getListName());
industryId = base.getBaseinclassid();
listName = data.getListName();
InList inList = new InList();
inList.setName(data.getListName());
listId = RandomNumber.getUUid();
inList.setListId(RandomNumber.getUUid());
inList.setIndustryId(base.getBaseinclassid());
inList.setCreateBy("系统导入");
inList.setCreateTime(new Date());
queryList.add(inList);
}
if (data.getIndustry() != null){
industry = data.getIndustry();
}
data.setListName(listName);
data.setIndustry(industry);
data.setListId(listId);
data.setIndustryId(industryId);
cachedDataList.add(data);
// 达到BATCH_COUNT了需要去存储一次数据库防止数据几万条数据在内存容易OOM
if (cachedDataList.size() >= BATCH_COUNT) {
saveListItem();
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
// Contain contain = new Contain();
// BeanUtils.copyProperties(data,contain);
// contain.getMoreRows().add(new MoreRow(data.getListItem(),data.getListContent(),data.getStandard(),data.getFrequency(),data.getRemark()));
// contains.add(contain);
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
//这里也要保存数据确保最后遗留的数据也存储到数据库
saveList();
log.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveListItem() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
//插入清单项
inListItemMapper.insertItem(cachedDataList);
log.info("存储数据库成功!");
}
/**
* 加上存储数据库
*/
private void saveList() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
int result = inListMapper.insertList(queryList);
//清空list
queryList = new ArrayList<>();
log.info("存储数据库成功!");
}
}

View File

@ -0,0 +1,54 @@
package com.rzyc.utils.easyexcel;
import com.alibaba.excel.EasyExcel;
import com.rzyc.mapper.ent.BaseInClassMapper;
import com.rzyc.mapper.ent.InListItemMapper;
import com.rzyc.mapper.ent.InListMapper;
import com.rzyc.model.EasyExcel.EasyExcelInList;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 读的常见写法
*
* @author Jiaju Zhuang
*/
@Ignore
@Slf4j
public class ReadTest {
@Autowired
InListMapper inListMapper;
@Autowired
InListItemMapper inListItemMapper;
@Autowired
BaseInClassMapper baseInClassMapper;
@Test
public void repeatedRead() {
String fileName = "导入模板.xlsx";
// 读取全部sheet
// 这里需要注意 DemoDataListener的doAfterAllAnalysed 会在每个sheet读取完毕后调用一次然后所有sheet都会往同一个DemoDataListener里面写
EasyExcel.read(fileName, EasyExcelInList.class, new InListListener(inListMapper,inListItemMapper,baseInClassMapper)).doReadAll();
// 读取部分sheet
// fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
//
// // 写法1
// try (ExcelReader excelReader = EasyExcel.read(fileName).build()) {
// // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener
// ReadSheet readSheet1 =
// EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new DemoDataListener()).build();
// ReadSheet readSheet2 =
// EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new DemoDataListener()).build();
// // 这里注意 一定要把sheet1 sheet2 一起传进去不然有个问题就是03版的excel 会读取多次浪费性能
// excelReader.read(readSheet1, readSheet2);
// }
}
}

View File

@ -0,0 +1,36 @@
package com.rzyc.utils.easyexcel;
import java.io.File;
import java.io.InputStream;
public class TestFileUtil {
public static InputStream getResourcesFileInputStream(String fileName) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName);
}
public static String getPath() {
return TestFileUtil.class.getResource("/").getPath();
}
public static File createNewFile(String pathName) {
File file = new File(getPath() + pathName);
if (file.exists()) {
file.delete();
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
return file;
}
public static File readFile(String pathName) {
return new File(getPath() + pathName);
}
public static File readUserHomeFile(String pathName) {
return new File(System.getProperty("user.home") + File.separator + pathName);
}
}