|
@@ -1,10 +1,27 @@
|
|
|
package com.dt.common.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
|
+import cn.hutool.poi.excel.sax.handler.RowHandler;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.dt.common.domain.Child;
|
|
|
import com.dt.common.domain.FoodRecord;
|
|
|
+import com.dt.common.domain.User;
|
|
|
+import com.dt.common.mapper.ChildMapper;
|
|
|
+import com.dt.common.mapper.UserMapper;
|
|
|
import com.dt.common.service.FoodRecordService;
|
|
|
import com.dt.common.mapper.FoodRecordMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* @author sunday
|
|
@@ -12,9 +29,109 @@ import org.springframework.stereotype.Service;
|
|
|
* @createDate 2025-07-01 16:33:35
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class FoodRecordServiceImpl extends ServiceImpl<FoodRecordMapper, FoodRecord>
|
|
|
implements FoodRecordService{
|
|
|
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ChildMapper childMapper;
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ static AtomicInteger num=new AtomicInteger(0);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void leadIn(InputStream inputStream) {
|
|
|
+ List<FoodRecord> list=new ArrayList<>();
|
|
|
+ ExcelUtil.readBySax(inputStream, -1, createRowHandler(list));
|
|
|
+ if(!CollectionUtils.isEmpty(list)){
|
|
|
+ batchAddFoodRecord(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void batchAddFoodRecord(List<FoodRecord> list){
|
|
|
+
|
|
|
+ for (FoodRecord foodRecord : list){
|
|
|
+ num.getAndIncrement();
|
|
|
+ try {
|
|
|
+ addFoodRecord(foodRecord);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("第"+num.get()+"行数据插入失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
|
|
|
+ public void addFoodRecord(FoodRecord foodRecord){
|
|
|
+ if (foodRecord!=null){
|
|
|
+ this.baseMapper.insert(foodRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private RowHandler createRowHandler(List<FoodRecord> foodRecordList){
|
|
|
+ return new RowHandler() {
|
|
|
+ @Override
|
|
|
+ public void handle(int i, long l, List<Object> list) {
|
|
|
+ if(l>0){
|
|
|
+ String childName = list.get(0).toString();
|
|
|
+ QueryWrapper<Child> wrapper=new QueryWrapper<>();
|
|
|
+ wrapper.eq("name",childName);
|
|
|
+ Child child = childMapper.selectOne(wrapper);
|
|
|
+ if(child==null){
|
|
|
+ log.warn("第{}行数据被跳过:未找到姓名为【{}】的幼儿", l, childName);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long id = child.getId();
|
|
|
+ Date recordTime = (Date) list.get(1);
|
|
|
+
|
|
|
+ Map<String,Integer> map=new HashMap<>();//将食谱名称映射成数字
|
|
|
+ map.put("早餐",0);
|
|
|
+ map.put("上午加餐",1);
|
|
|
+ map.put("午餐",2);
|
|
|
+ map.put("下午加餐",3);
|
|
|
+ map.put("晚餐",4);
|
|
|
+
|
|
|
+ String goodName = list.get(2).toString();//食谱名称
|
|
|
+ Integer mealType = map.get(goodName);//食谱名称对应的数字
|
|
|
+ if(mealType==null){
|
|
|
+ mealType=5;
|
|
|
+ }
|
|
|
+ String foodContent =list.get(3).toString();
|
|
|
+ String amount = list.get(4).toString();
|
|
|
+ String remark=list.get(5).toString();
|
|
|
+ String username = list.get(6).toString();
|
|
|
+ QueryWrapper<User> userWrapper=new QueryWrapper<>();
|
|
|
+ userWrapper.eq("username",username);
|
|
|
+ User user = userMapper.selectOne(userWrapper);
|
|
|
+ if (user==null){
|
|
|
+ log.warn("第{}行数据被跳过:未找到名称为【{}】的记录人", l, username);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long recorderId = user.getId();
|
|
|
+ FoodRecord foodRecord=new FoodRecord();
|
|
|
+ foodRecord.setChildId(id);
|
|
|
+ foodRecord.setRecordTime(recordTime);
|
|
|
+ foodRecord.setMealType(mealType);
|
|
|
+ foodRecord.setFoodContent(foodContent);
|
|
|
+ foodRecord.setAmount(amount);
|
|
|
+ foodRecord.setRemark(remark);
|
|
|
+ foodRecord.setRecorderId(recorderId);
|
|
|
+ foodRecordList.add(foodRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|