|
@@ -1,10 +1,25 @@
|
|
package com.dt.common.service.impl;
|
|
package com.dt.common.service.impl;
|
|
|
|
|
|
|
|
+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.core.toolkit.CollectionUtils;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
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.FoodRecord;
|
|
|
|
+import com.dt.common.mapper.ChildMapper;
|
|
import com.dt.common.service.FoodRecordService;
|
|
import com.dt.common.service.FoodRecordService;
|
|
import com.dt.common.mapper.FoodRecordMapper;
|
|
import com.dt.common.mapper.FoodRecordMapper;
|
|
|
|
+import com.dt.user.mapper.UserMapper;
|
|
|
|
+import com.dt.user.pojo.User;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author sunday
|
|
* @author sunday
|
|
@@ -12,10 +27,105 @@ import org.springframework.stereotype.Service;
|
|
* @createDate 2025-07-01 16:33:35
|
|
* @createDate 2025-07-01 16:33:35
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class FoodRecordServiceImpl extends ServiceImpl<FoodRecordMapper, FoodRecord>
|
|
public class FoodRecordServiceImpl extends ServiceImpl<FoodRecordMapper, FoodRecord>
|
|
- implements FoodRecordService{
|
|
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
-}
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|