package com.ycl.service.store.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.entity.store.StoreScore;
import com.ycl.enums.common.ResultCode;
import com.ycl.exception.ApiException;
import com.ycl.mapper.store.StoreInfoMapper;
import com.ycl.mapper.store.StoreScoreMapper;
import com.ycl.mapper.video.VideoPointMapper;
import com.ycl.service.store.StoreInfoService;
import com.ycl.utils.ExcelUtils;
import com.ycl.vo.store.StoreInfoExcelVo;
import com.ycl.vo.store.StoreInfoVO;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author lyq
* @since 2022-09-08
*/
@Service
@Slf4j
public class StoreInfoServiceImpl extends ServiceImpl implements StoreInfoService {
@Resource
StoreInfoMapper storeInfoMapper;
@Resource
VideoPointMapper videoPointMapper;
@Resource
StoreScoreMapper storeScoreMapper;
@Override
public Page list(String keyword, Integer pageSize, Integer pageNum,String status) {
Page storeInfoPage = new Page<>();
storeInfoPage.setSize(pageSize);
storeInfoPage.setCurrent(pageNum);
Page page = storeInfoMapper.selectStorePage(storeInfoPage, keyword,status);
page.getRecords().forEach(x -> x.setVideoPoint(videoPointMapper.selectById(x.getVideoId())));
return page;
}
@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);
}
}
@Override
public Page getScoreList(Long storeId, Integer pageSize, Integer pageNum) {
Page storeScorePage = new Page<>();
storeScorePage.setSize(pageSize);
storeScorePage.setCurrent(pageNum);
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().eq(StoreScore::getStoreId, storeId);
Page page = storeScoreMapper.selectPage(storeScorePage, queryWrapper);
return page;
}
}