liyanqi
2022-09-15 34de42f43fd75a31fe0c0d51adf30fb554ad5fa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.ycl.service.platform.store.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.entity.platform.store.StoreInfo;
import com.ycl.enums.common.ResultCode;
import com.ycl.exception.ApiException;
import com.ycl.mapper.platform.store.StoreInfoMapper;
import com.ycl.service.platform.store.StoreInfoService;
import com.ycl.utils.ExcelUtils;
import com.ycl.vo.store.StoreInfoExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyq
 * @since 2022-09-08
 */
@Service
@Slf4j
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {
 
    @Override
    public Page<StoreInfo> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<StoreInfo> page = new Page<>(pageSize, pageNum);
        LambdaQueryWrapper<StoreInfo> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(StoreInfo::getStorename, keyword);
        }
        return page(page, wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addByExcel(MultipartFile file) {
        if (file == null) {
            throw new ApiException(ResultCode.FILE_NOT_FOUND);
        }
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (!(type.equals(".xls") || type.equals(".xlsx"))) {
            throw new ApiException(ResultCode.FILE_TYPE_FAIL);
        }
        try {
            List<StoreInfoExcelVo> datas = ExcelUtils.getExcelModelData(file.getInputStream(), StoreInfoExcelVo.class);
            log.info("读取到{}条数据", datas.size());
            List<StoreInfo> infos = new ArrayList<>();
            for (StoreInfoExcelVo data : datas) {
                StoreInfo info = new StoreInfo();
                BeanUtils.copyProperties(data,info);
                infos.add(info);
            }
            BeanUtils.copyProperties(datas,infos);
            saveBatch(infos);
            return true;
        } catch (Exception e) {
            throw new ApiException(ResultCode.FAILED);
        }
    }
}