|
@@ -0,0 +1,251 @@
|
|
|
+package com.fjhx.service.order.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.base.BaseEntity;
|
|
|
+import com.fjhx.constants.ExcelImportBusinessType;
|
|
|
+import com.fjhx.constants.SystemConfigKeyConstant;
|
|
|
+import com.fjhx.entity.customer.CustomerInfo;
|
|
|
+import com.fjhx.entity.order.OrderJd;
|
|
|
+import com.fjhx.entity.order.OrderJdDetails;
|
|
|
+import com.fjhx.entity.order.OrderSales;
|
|
|
+import com.fjhx.entity.product.ProductInfo;
|
|
|
+import com.fjhx.mapper.order.OrderJdMapper;
|
|
|
+import com.fjhx.params.order.OrderJdEx;
|
|
|
+import com.fjhx.params.order.OrderJdExcelVo;
|
|
|
+import com.fjhx.params.order.OrderJdVo;
|
|
|
+import com.fjhx.service.excel.ExcelImportLogService;
|
|
|
+import com.fjhx.service.order.OrderJdDetailsService;
|
|
|
+import com.fjhx.service.order.OrderJdService;
|
|
|
+import com.fjhx.service.product.ProductInfoService;
|
|
|
+import com.fjhx.service.system.SystemConfigService;
|
|
|
+import com.fjhx.uitl.code.CodeEnum;
|
|
|
+import com.fjhx.utils.Assert;
|
|
|
+import com.fjhx.utils.RegionClientUtil;
|
|
|
+import com.fjhx.utils.wrapperUtil.IWrapper;
|
|
|
+import com.fjhx.utils.wrapperUtil.KeywordData;
|
|
|
+import org.springblade.core.excel.util.ExcelUtil;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.secure.utils.AuthUtil;
|
|
|
+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.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 销售订单 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author ${author}
|
|
|
+ * @since 2022-12-08
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class OrderJdServiceImpl extends ServiceImpl<OrderJdMapper, OrderJd> implements OrderJdService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExcelImportLogService excelImportLogService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductInfoService productInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderJdDetailsService orderJdDetailsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SystemConfigService systemConfigService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<OrderJdEx> getPage(Map<String, Object> condition) {
|
|
|
+
|
|
|
+ IWrapper<Object> wrapper = IWrapper.getWrapper(condition)
|
|
|
+ .keyword(new KeywordData("oj", OrderSales::getCode), new KeywordData("ci", CustomerInfo::getName))
|
|
|
+ .eq("oj", OrderJd::getStatus)
|
|
|
+ .like("oj", OrderJd::getCode)
|
|
|
+ .like("ci", CustomerInfo::getName, condition.get("customerName"))
|
|
|
+ .eq("oj", OrderJd::getCountryId)
|
|
|
+ .eq("oj", OrderJd::getProvinceId)
|
|
|
+ .eq("oj", OrderJd::getCityId)
|
|
|
+ .ge("oj", OrderJd::getAmountMoney, condition.get("minAmountMoney"))
|
|
|
+ .le("oj", OrderJd::getAmountMoney, condition.get("maxAmountMoney"));
|
|
|
+
|
|
|
+ Page<OrderJdEx> page = baseMapper.getPage(createPage(condition), wrapper);
|
|
|
+
|
|
|
+ // 赋值国省市
|
|
|
+ RegionClientUtil.setEntityRegionName(page.getRecords());
|
|
|
+
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void add(OrderJdVo orderJdVo) {
|
|
|
+ // 订单明细
|
|
|
+ List<OrderJdDetails> orderJdDetailsList = orderJdVo.getOrderJdDetailsList();
|
|
|
+ Assert.notEmpty(orderJdDetailsList, "订单明细不能为空");
|
|
|
+
|
|
|
+ // 获取京东客户id
|
|
|
+ Long customerCustomerId = systemConfigService.getValue(SystemConfigKeyConstant.CUSTOMER_CUSTOMER_ID, Long.class);
|
|
|
+ orderJdVo.setCustomerInfoId(customerCustomerId);
|
|
|
+
|
|
|
+ // 订单时间
|
|
|
+ orderJdVo.setOrderTime(ObjectUtil.defaultIfNull(orderJdVo.getOrderTime(), new Date()));
|
|
|
+
|
|
|
+ // 统计订单金额
|
|
|
+ BigDecimal amountMoney = orderJdDetailsList.stream()
|
|
|
+ .map(item -> item.getPrice().multiply(item.getQuantity())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ orderJdVo.setAmountMoney(amountMoney);
|
|
|
+
|
|
|
+ synchronized (this) {
|
|
|
+ orderJdVo.setCode(CodeEnum.ORDER_JD.getCode(orderJdVo.getCode()));
|
|
|
+ save(orderJdVo);
|
|
|
+ }
|
|
|
+ // 添加订单明细
|
|
|
+ for (OrderJdDetails orderJdDetails : orderJdDetailsList) {
|
|
|
+ orderJdDetails.setOrderJdId(orderJdVo.getId());
|
|
|
+ orderJdDetails.setNotIssuedQuantity(orderJdDetails.getQuantity());
|
|
|
+ }
|
|
|
+ orderJdDetailsService.saveBatch(orderJdDetailsList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void edit(OrderJdVo orderJdVo) {
|
|
|
+ updateById(orderJdVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(OrderJdVo orderJdVo) {
|
|
|
+ removeById(orderJdVo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long excelImport(MultipartFile file) {
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+
|
|
|
+ long flag = IdWorker.getId();
|
|
|
+
|
|
|
+ // 读取excel数据
|
|
|
+ List<OrderJdExcelVo> list = ExcelUtil.read(file, OrderJdExcelVo.class);
|
|
|
+
|
|
|
+ // 获取京东客户id
|
|
|
+ Long customerCustomerId = systemConfigService.getValue(SystemConfigKeyConstant.CUSTOMER_CUSTOMER_ID, Long.class);
|
|
|
+
|
|
|
+ // 验证订单编号是否重复
|
|
|
+ verifyOrderNumber(list);
|
|
|
+
|
|
|
+ // 查询产品编码对应的产品id
|
|
|
+ Map<String, Long> codeIdMap = getCodeIdMap(list);
|
|
|
+
|
|
|
+ // 保存导入记录
|
|
|
+ excelImportLogService.saveLog(flag, ExcelImportBusinessType.JD_ORDER, file);
|
|
|
+
|
|
|
+ // 京东订单表
|
|
|
+ Map<String, OrderJd> map = new HashMap<>();
|
|
|
+
|
|
|
+ // 京东订单明细
|
|
|
+ List<OrderJdDetails> orderJdDetailsList = new ArrayList<>();
|
|
|
+
|
|
|
+ Date date = new Date();
|
|
|
+
|
|
|
+ for (OrderJdExcelVo orderJdExcelVo : list) {
|
|
|
+
|
|
|
+ String code = orderJdExcelVo.getCode();
|
|
|
+ String productCode = orderJdExcelVo.getProductCode();
|
|
|
+ BigDecimal price = orderJdExcelVo.getPrice();
|
|
|
+ BigDecimal quantity = orderJdExcelVo.getQuantity();
|
|
|
+
|
|
|
+ Long productId = codeIdMap.get(productCode);
|
|
|
+ OrderJd orderJd = map.get(code);
|
|
|
+ if (orderJd == null) {
|
|
|
+ orderJd = new OrderJdEx();
|
|
|
+
|
|
|
+ long id = IdWorker.getId();
|
|
|
+ orderJd.setId(id);
|
|
|
+ orderJd.setCustomerInfoId(customerCustomerId);
|
|
|
+ orderJd.setCode(code);
|
|
|
+ orderJd.setAmountMoney(price.multiply(quantity));
|
|
|
+ orderJd.setOrderTime(orderJdExcelVo.getOrderTime());
|
|
|
+ orderJd.setStatus(1);
|
|
|
+ orderJd.setCountryId("China");
|
|
|
+ orderJd.setDetailedAddress(orderJdExcelVo.getDetailedAddress());
|
|
|
+ orderJd.setContacts(orderJdExcelVo.getContacts());
|
|
|
+ orderJd.setPhone(orderJdExcelVo.getPhone());
|
|
|
+ orderJd.setExcelImportId(flag);
|
|
|
+ orderJd.setCreateUser(AuthUtil.getUserId());
|
|
|
+ orderJd.setCreateTime(date);
|
|
|
+ map.put(code, orderJd);
|
|
|
+ } else {
|
|
|
+ orderJd.setAmountMoney(orderJd.getAmountMoney().add(price.multiply(quantity)));
|
|
|
+ }
|
|
|
+
|
|
|
+ OrderJdDetails orderJdDetails = new OrderJdDetails();
|
|
|
+ orderJdDetails.setOrderJdId(orderJd.getId());
|
|
|
+ orderJdDetails.setProductId(productId);
|
|
|
+ orderJdDetails.setQuantity(quantity);
|
|
|
+ orderJdDetails.setPrice(price);
|
|
|
+ orderJdDetails.setNotIssuedQuantity(quantity);
|
|
|
+
|
|
|
+ orderJdDetailsList.add(orderJdDetails);
|
|
|
+ }
|
|
|
+
|
|
|
+ saveBatch(new ArrayList<>(map.values()));
|
|
|
+ orderJdDetailsService.saveBatch(orderJdDetailsList);
|
|
|
+
|
|
|
+ // 保存导入记录
|
|
|
+ excelImportLogService.editLog(start, flag, 1);
|
|
|
+
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证订单编号是否重复
|
|
|
+ */
|
|
|
+ private void verifyOrderNumber(List<OrderJdExcelVo> list) {
|
|
|
+ List<String> codeList = list.stream().map(OrderJdExcelVo::getCode).distinct().collect(Collectors.toList());
|
|
|
+ List<String> existCodeList = listObj(OrderJd::getCode, q -> q.in(OrderJd::getCode, codeList));
|
|
|
+
|
|
|
+ if (existCodeList.size() > 0) {
|
|
|
+ StringJoiner joiner = new StringJoiner(",");
|
|
|
+ existCodeList.forEach(joiner::add);
|
|
|
+
|
|
|
+ // 保存导入记录
|
|
|
+// excelImportLogService.editLog(start, flag, 2);
|
|
|
+ throw new ServiceException("存在相同的采购单号:" + joiner);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取<产品编码,产品id>map
|
|
|
+ */
|
|
|
+ private Map<String, Long> getCodeIdMap(List<OrderJdExcelVo> list) {
|
|
|
+ List<String> productCodeList = list.stream().map(OrderJdExcelVo::getProductCode).distinct().collect(Collectors.toList());
|
|
|
+ Map<String, Long> kv = productInfoService.getKV(
|
|
|
+ ProductInfo::getCode,
|
|
|
+ BaseEntity::getId,
|
|
|
+ q -> q.in(ProductInfo::getCode, productCodeList));
|
|
|
+
|
|
|
+ if (kv.size() != productCodeList.size()) {
|
|
|
+ StringJoiner joiner = new StringJoiner(",");
|
|
|
+
|
|
|
+ for (String productCode : productCodeList) {
|
|
|
+ if (kv.get(productCode) == null) {
|
|
|
+ joiner.add(productCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// // 保存导入记录
|
|
|
+// excelImportLogService.editLog(start, flag, 2);
|
|
|
+ throw new ServiceException("产品表中未找到对应产品编码:" + joiner);
|
|
|
+ }
|
|
|
+ return kv;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|