liyanqi
2022-09-13 42c81d1538e3c08c84a789ffafd9a7deafa1ff24
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
72
package com.ycl.service.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.store.UmsStoreInfo;
import com.ycl.enums.common.ResultCode;
import com.ycl.exception.ApiException;
import com.ycl.mapper.store.UmsStoreInfoMapper;
import com.ycl.service.store.UmsStoreInfoService;
import com.ycl.utils.ExcelUtils;
import com.ycl.vo.store.StoreInfoExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 UmsStoreInfoServiceImpl extends ServiceImpl<UmsStoreInfoMapper, UmsStoreInfo> implements UmsStoreInfoService {
 
    @Override
    public Page<UmsStoreInfo> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<UmsStoreInfo> page = new Page<>(pageSize, pageNum);
        LambdaQueryWrapper<UmsStoreInfo> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(UmsStoreInfo::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<UmsStoreInfo> infos = new ArrayList<>();
            for (StoreInfoExcelVo data : datas) {
                UmsStoreInfo info = new UmsStoreInfo();
                BeanUtils.copyProperties(data,info);
                infos.add(info);
            }
            BeanUtils.copyProperties(datas,infos);
            saveBatch(infos);
            return true;
        } catch (Exception e) {
            throw new ApiException(ResultCode.FAILED);
        }
    }
}