|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import com.yoho.message.sdk.utils.DateUtils;
|
|
|
import com.yoho.product.dal.ProductProfitMapper;
|
|
|
import com.yoho.product.model.ProductProfit;
|
|
|
import com.yoho.ufo.dal.model.ProductProfitImportItem;
|
|
|
import com.yoho.ufo.exception.PlatformException;
|
|
|
import com.yoho.ufo.service.IProductProfitService;
|
|
|
import com.yoho.ufo.util.DateUtil;
|
|
|
import com.yohobuy.ufo.model.common.ApiResponse;
|
|
|
import com.yohobuy.ufo.model.common.PageResponseBO;
|
|
|
import com.yohobuy.ufo.model.request.product.ProductProfitReqBo;
|
|
|
import com.yohobuy.ufo.model.response.productProfit.ProductProfitRespBO;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class ProductProfitServiceImpl implements IProductProfitService {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ProductProfitServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
ProductProfitMapper productProfitMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private BatchService batchService;
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<PageResponseBO<ProductProfitRespBO>> getProductProfitPage(ProductProfitReqBo reqBo) {
|
|
|
int count = productProfitMapper.selectCount(reqBo.getProductId());
|
|
|
if( count == 0){
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
List<ProductProfit> doList = productProfitMapper.selectByCondition(reqBo);
|
|
|
List<ProductProfitRespBO> boList = new ArrayList<>();
|
|
|
for(ProductProfit productProfit : doList){
|
|
|
ProductProfitRespBO bo = new ProductProfitRespBO();
|
|
|
bo.setId(productProfit.getId());
|
|
|
bo.setProductId(productProfit.getProductId());
|
|
|
bo.setProfitRate(String.format("%.2f", productProfit.getProfitRate().multiply(BigDecimal.valueOf(100))) + '%');
|
|
|
bo.setProfitMaxPrice('¥' + String.valueOf(productProfit.getProfitMaxPrice()));
|
|
|
bo.setProfitMinPrice('¥' + String.valueOf(productProfit.getProfitMinPrice()));
|
|
|
bo.setAddUserName(productProfit.getAddUserName());
|
|
|
bo.setEditUserName(productProfit.getEditUserName());
|
|
|
bo.setCreateTime(DateUtil.int2DateStr(productProfit.getCreateTime(), DateUtil.DATE_TIME_FORMAT));
|
|
|
bo.setUpdateTime(DateUtil.int2DateStr(productProfit.getUpdateTime(), DateUtil.DATE_TIME_FORMAT));
|
|
|
boList.add(bo);
|
|
|
}
|
|
|
PageResponseBO<ProductProfitRespBO> page = new PageResponseBO<>(count, boList, reqBo.getPage(), reqBo.getRows());
|
|
|
return new ApiResponse<>(page);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<ProductProfitRespBO> getProductProfitDetail(int id) {
|
|
|
ProductProfit productProfit = productProfitMapper.selectById(id);
|
|
|
ProductProfitRespBO respBO = new ProductProfitRespBO();
|
|
|
respBO.setId(productProfit.getId());
|
|
|
respBO.setProductId(productProfit.getProductId());
|
|
|
respBO.setProfitRate(String.format("%.2f", productProfit.getProfitRate().multiply(BigDecimal.valueOf(100))));
|
|
|
respBO.setProfitMinPrice(String.valueOf(productProfit.getProfitMinPrice()));
|
|
|
respBO.setProfitMaxPrice(String.valueOf(productProfit.getProfitMaxPrice()));
|
|
|
return new ApiResponse<>(respBO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> deleteProductProfit(int id) {
|
|
|
//判断该skn是否存在
|
|
|
productProfitMapper.updateDelStatus(id);
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> addOrUpdate(ProductProfitReqBo productProfitReqBo, String operatorName) {
|
|
|
ProductProfit productProfit = new ProductProfit();
|
|
|
productProfit.setProductId(productProfitReqBo.getProductId());
|
|
|
productProfit.setProfitRate(new BigDecimal(productProfitReqBo.getProfitRate()).divide(BigDecimal.valueOf(100)));
|
|
|
productProfit.setProfitMaxPrice(new BigDecimal(productProfitReqBo.getProfitMaxPrice()));
|
|
|
productProfit.setProfitMinPrice(new BigDecimal(productProfitReqBo.getProfitMinPrice()));
|
|
|
//新增
|
|
|
if(productProfitReqBo.getId() == 0){
|
|
|
//判断该商品是否已经配置, 不可重复添加
|
|
|
ProductProfit existProfit = productProfitMapper.selectByProductId(productProfitReqBo.getProductId());
|
|
|
if(existProfit != null && !existProfit.getId().equals(productProfitReqBo.getId())){
|
|
|
return new ApiResponse<>(400, "该商品已配置,不可重复配置");
|
|
|
}
|
|
|
productProfit.setAddUserName(operatorName);
|
|
|
productProfit.setCreateTime(DateUtils.getCurrentTimeSeconds());
|
|
|
productProfitMapper.insert(productProfit);
|
|
|
}
|
|
|
//修改
|
|
|
else{
|
|
|
productProfit.setId(productProfitReqBo.getId());
|
|
|
productProfit.setEditUserName(operatorName);
|
|
|
productProfit.setUpdateTime(DateUtils.getCurrentTimeSeconds());
|
|
|
productProfitMapper.update(productProfit);
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Integer> batchImportFromXls(MultipartFile file, String operatorName) throws Exception {
|
|
|
|
|
|
List<ProductProfitImportItem> items = batchService.parseData(file, "ProductProfits", ProductProfitImportItem.class);
|
|
|
if(CollectionUtils.isEmpty(items)){
|
|
|
throw new PlatformException("导入的excel数据为空!", 400);
|
|
|
}
|
|
|
if(items.size() > 500){
|
|
|
throw new PlatformException("导入的excel数据不能超过500条!", 400);
|
|
|
}
|
|
|
//查询导入的数据中,那些已经保存过,获取那些已经保存过的记录的创建时间和创建人
|
|
|
List<Integer> productIdList = new ArrayList<>();
|
|
|
items.forEach(productProfitImportItem -> productIdList.add(productProfitImportItem.getProductId()));
|
|
|
List<ProductProfit> productProfits = productProfitMapper.selectByProductIds(productIdList);
|
|
|
Map<Integer, ProductProfit> productProfitMap = productProfits.stream().collect(Collectors.toMap(ProductProfit :: getProductId, ProductProfit -> ProductProfit));
|
|
|
//将导入数据插入到表中
|
|
|
ProductProfit productProfitExist;
|
|
|
int currentTime = DateUtils.getCurrentTimeSeconds();
|
|
|
List<ProductProfit> productProfitList = new ArrayList<>();
|
|
|
Set<Integer> productIdSet = new HashSet<>();
|
|
|
for(ProductProfitImportItem item : items){
|
|
|
if(productIdSet.contains(item.getProductId())) continue;
|
|
|
ProductProfit productProfit = new ProductProfit();
|
|
|
productProfit.setProductId(item.getProductId());
|
|
|
productProfit.setProfitRate(new BigDecimal(item.getProfitRate()).divide(BigDecimal.valueOf(100)));
|
|
|
productProfit.setProfitMaxPrice(new BigDecimal(item.getProfitMaxPrice()));
|
|
|
productProfit.setProfitMinPrice(new BigDecimal(item.getProfitMinPrice()));
|
|
|
productProfitExist = productProfitMap.get(item.getProductId());
|
|
|
productProfit.setCreateTime(productProfitExist != null ? productProfitExist.getCreateTime() : currentTime);
|
|
|
productProfit.setAddUserName(productProfitExist != null ? productProfitExist.getAddUserName() : operatorName);
|
|
|
productProfit.setEditUserName(operatorName);
|
|
|
productProfit.setUpdateTime(currentTime);
|
|
|
productProfitList.add(productProfit);
|
|
|
productIdSet.add(item.getProductId());
|
|
|
}
|
|
|
int result = productProfitMapper.insertBatchProductProfit(productProfitList);
|
|
|
return new ApiResponse<>(result);
|
|
|
}
|
|
|
} |
...
|
...
|
|