package com.ruoyi.aftersalesservice.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aftersalesservice.dto.AfterSalesNearExpiryImportDto;
import com.ruoyi.aftersalesservice.mapper.AfterSalesNearExpiryMapper;
import com.ruoyi.aftersalesservice.pojo.AfterSalesNearExpiry;
import com.ruoyi.aftersalesservice.service.AfterSalesNearExpiryService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* 临期售后管理接口实现类
*
*
* @author deslrey
* @version 1.0
* @since 2026/03/02 14:46
*/
@Slf4j
@Service
public class AfterSalesNearExpiryServiceImpl extends ServiceImpl implements AfterSalesNearExpiryService {
@Override
public void add(AfterSalesNearExpiry entity) {
if (entity == null) {
throw new ServiceException("添加失败,数据不能为空");
}
if (entity.getProductName() == null || entity.getProductName().trim().isEmpty()) {
throw new ServiceException("产品名称不能为空");
}
if (entity.getBatchNumber() == null || entity.getBatchNumber().trim().isEmpty()) {
throw new ServiceException("产品批号不能为空");
}
if (entity.getExpireDate() == null) {
throw new ServiceException("临期日期不能为空");
}
entity.setId(null);
entity.setStatus(1);
entity.setDisposeUserId(null);
entity.setDisposeNickName(null);
entity.setDisDate(null);
entity.setCreateTime(LocalDateTime.now());
entity.setCreateUser(SecurityUtils.getLoginUser().getUserId());
entity.setUpdateUser(null);
entity.setUpdateTime(null);
entity.setTenantId(SecurityUtils.getLoginUser().getTenantId());
int result = baseMapper.insert(entity);
if (result <= 0) {
throw new ServiceException("新增失败");
}
}
@Override
public void update(AfterSalesNearExpiry entity) {
if (entity == null || entity.getId() == null) {
throw new ServiceException("更新失败,数据不完整");
}
entity.setStatus(2);
entity.setUpdateUser(SecurityUtils.getLoginUser().getUserId());
entity.setUpdateTime(LocalDateTime.now());
int result = baseMapper.updateById(entity);
if (result <= 0) {
throw new ServiceException("更新失败");
}
}
@Override
public void delete(Long[] ids) {
if (ids == null || ids.length == 0) {
throw new ServiceException("请选择要删除的数据");
}
int result = baseMapper.deleteBatchIds(Arrays.asList(ids));
if (result <= 0) {
throw new ServiceException("删除失败");
}
}
@Override
public IPage listPage(Page page, AfterSalesNearExpiry entity) {
return baseMapper.listPage(page, entity);
}
@Override
public AjaxResult importData(MultipartFile file) {
List rows;
try {
ExcelUtil util = new ExcelUtil<>(AfterSalesNearExpiryImportDto.class);
rows = util.importExcel(file.getInputStream());
} catch (Exception e) {
log.error("临期售后管理导入-解析excel失败", e);
return AjaxResult.error("解析Excel失败:" + e.getMessage());
}
if (CollectionUtils.isEmpty(rows)) {
return AjaxResult.error("导入文件无数据");
}
int success = 0;
List errors = new ArrayList<>();
for (int i = 0; i < rows.size(); i++) {
AfterSalesNearExpiryImportDto row = rows.get(i);
int rowNo = i + 2; // 首行为表头
try {
if (StringUtils.isEmpty(trim(row.getProductName()))) {
errors.add("第" + rowNo + "行:临期产品名称为空");
continue;
}
if (StringUtils.isEmpty(trim(row.getBatchNumber()))) {
errors.add("第" + rowNo + "行:产品批号为空");
continue;
}
if (row.getExpireDate() == null) {
errors.add("第" + rowNo + "行:临期日期为空");
continue;
}
if (row.getStockQuantity() == null) {
errors.add("第" + rowNo + "行:库存数量为空");
continue;
}
if (StringUtils.isEmpty(trim(row.getCustomerName()))) {
errors.add("第" + rowNo + "行:客户名称为空");
continue;
}
AfterSalesNearExpiry entity = new AfterSalesNearExpiry();
entity.setProductName(trim(row.getProductName()));
entity.setBatchNumber(trim(row.getBatchNumber()));
entity.setExpireDate(row.getExpireDate());
entity.setStockQuantity(row.getStockQuantity());
entity.setCustomerName(trim(row.getCustomerName()));
entity.setContactPhone(trim(row.getContactPhone()));
entity.setDisRes(row.getDisRes());
add(entity);
success++;
} catch (Exception e) {
log.error("临期售后管理导入第{}行失败", rowNo, e);
errors.add("第" + rowNo + "行:" + e.getMessage());
}
}
AjaxResult result = AjaxResult.success();
result.put("totalCount", rows.size());
result.put("successCount", success);
result.put("failCount", rows.size() - success);
result.put("errors", errors);
result.put("msg", "导入完成:成功 " + success + " 条,失败 " + (rows.size() - success) + " 条");
return result;
}
private String trim(String value) {
return value == null ? null : value.trim();
}
}