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.StoreInfo; import com.ycl.enums.common.ResultCode; import com.ycl.exception.ApiException; import com.ycl.mapper.store.StoreInfoMapper; import com.ycl.service.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; /** *

* 服务实现类 *

* * @author lyq * @since 2022-09-08 */ @Service @Slf4j public class StoreInfoServiceImpl extends ServiceImpl implements StoreInfoService { @Override public Page list(String keyword, Integer pageSize, Integer pageNum) { Page page = new Page<>(pageSize, pageNum); LambdaQueryWrapper 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 datas = ExcelUtils.getExcelModelData(file.getInputStream(), StoreInfoExcelVo.class); log.info("读取到{}条数据", datas.size()); List 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); } } }