|
@@ -0,0 +1,238 @@
|
|
|
+package com.kmall.admin.service.impl.haikong;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.google.common.collect.ImmutableBiMap;
|
|
|
+import com.kmall.admin.dto.StockChangeDto;
|
|
|
+import com.kmall.admin.dto.StockChangeRecordDto;
|
|
|
+import com.kmall.admin.entity.StoreEntity;
|
|
|
+import com.kmall.admin.entity.haikong.StockChangeRecordEntity;
|
|
|
+import com.kmall.admin.fromcomm.entity.SysUserEntity;
|
|
|
+import com.kmall.admin.haikong.constant.Constants;
|
|
|
+import com.kmall.admin.service.GoodsService;
|
|
|
+import com.kmall.admin.service.ProductStoreRelaService;
|
|
|
+import com.kmall.admin.service.StoreService;
|
|
|
+import com.kmall.admin.service.haikong.StockChangeRecordService;
|
|
|
+import com.kmall.admin.utils.ShiroUtils;
|
|
|
+import com.kmall.common.utils.MapBeanUtil;
|
|
|
+import com.kmall.common.utils.R;
|
|
|
+import com.kmall.common.utils.RRException;
|
|
|
+import com.kmall.common.utils.ValidatorUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.aop.framework.AopContext;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import com.kmall.admin.dao.haikong.StockChangeDao;
|
|
|
+import com.kmall.admin.entity.haikong.StockChangeEntity;
|
|
|
+import com.kmall.admin.service.haikong.StockChangeService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Service实现类
|
|
|
+ *
|
|
|
+ * @author emato
|
|
|
+ * @email admin@qhdswl.com
|
|
|
+ * @date 2021-12-29 14:46:49
|
|
|
+ */
|
|
|
+@Service("stockChangeService")
|
|
|
+public class StockChangeServiceImpl implements StockChangeService {
|
|
|
+ @Autowired
|
|
|
+ private StockChangeDao stockChangeDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StockChangeRecordService stockChangeRecordService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StoreService storeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductStoreRelaService productStoreRelaService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GoodsService goodsService;
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(StockChangeServiceImpl.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public StockChangeEntity queryObject(String id) {
|
|
|
+ return stockChangeDao.queryObject(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<StockChangeEntity> queryList(Map<String, Object> map) {
|
|
|
+ return stockChangeDao.queryList(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int queryTotal(Map<String, Object> map) {
|
|
|
+ return stockChangeDao.queryTotal(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int save(StockChangeEntity stockChange) {
|
|
|
+ return stockChangeDao.save(stockChange);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int update(StockChangeEntity stockChange) {
|
|
|
+ return stockChangeDao.update(stockChange);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int delete(String id) {
|
|
|
+ String[] ids = {id};
|
|
|
+ stockChangeRecordService.deleteBatchByParentIds(ids);
|
|
|
+ return stockChangeDao.delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int deleteBatch(String[]ids) {
|
|
|
+ stockChangeRecordService.deleteBatchByParentIds(ids);
|
|
|
+ return stockChangeDao.deleteBatch(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void uploadExcel(List<StockChangeRecordDto> stockChangeRecordDtos, StockChangeDto stockChangeDto) {
|
|
|
+ if (!CollectionUtils.isEmpty(stockChangeRecordDtos) && Objects.nonNull(stockChangeDto)) {
|
|
|
+ List<StockChangeRecordEntity> stockChangeRecordEntities = new ArrayList<>();
|
|
|
+
|
|
|
+ String id = stockChangeDto.getId();
|
|
|
+ String merchSn = stockChangeDto.getMerchSn();
|
|
|
+ String thirdMerchSn = stockChangeDto.getThirdMerchSn();
|
|
|
+ Integer storeId = stockChangeDto.getStoreId();
|
|
|
+ StoreEntity storeEntity = storeService.queryObject(storeId);
|
|
|
+ String type = stockChangeDto.getType();
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ log.error("导入出入库单时,出入库单单号为空!");
|
|
|
+ throw new RRException("出入库单单号不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(merchSn)) {
|
|
|
+ log.error("导入出入库单时,商户编号为空!");
|
|
|
+ throw new RRException("商户编号不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(thirdMerchSn)) {
|
|
|
+ log.error("导入出入库单时,第三方商户编号为空!");
|
|
|
+ throw new RRException("第三方商户编号不能为空!");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(storeId)) {
|
|
|
+ log.error("导入出入库单时,门店编号为空!");
|
|
|
+ throw new RRException("门店编号不能为空!");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(storeEntity)) {
|
|
|
+ log.error("门店id对应门店不存在!");
|
|
|
+ throw new RRException(String.format("门店编号【%s】对应门店不存在!", storeId));
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(type)) {
|
|
|
+ log.error("导入出入库单时,类型为空!");
|
|
|
+ throw new RRException("类型不能为空!");
|
|
|
+ }
|
|
|
+ Date createTime = new Date();
|
|
|
+ stockChangeRecordDtos.forEach(stockChangeRecordDto -> {
|
|
|
+ Map<String, Object> valideDate = MapBeanUtil.fromObject(stockChangeRecordDto);
|
|
|
+ // 校验excel传入的数据
|
|
|
+ ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
|
|
|
+ builder.put("sku", "商品SKU");
|
|
|
+ builder.put("number", "出入库数量");
|
|
|
+
|
|
|
+ R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
|
|
|
+ if (Integer.valueOf(r.get("code").toString()) != 0) {
|
|
|
+ throw new RRException(r.get("msg").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ StockChangeRecordEntity stockChangeRecordEntity = new StockChangeRecordEntity();
|
|
|
+ BeanUtils.copyProperties(stockChangeRecordDto, stockChangeRecordEntity);
|
|
|
+
|
|
|
+ stockChangeRecordEntity.setCreateTime(createTime);
|
|
|
+ stockChangeRecordEntity.setModifyTime(createTime);
|
|
|
+ stockChangeRecordEntity.setThirdMerchSn(thirdMerchSn);
|
|
|
+ stockChangeRecordEntity.setMerchSn(merchSn);
|
|
|
+ stockChangeRecordEntity.setType(type);
|
|
|
+ stockChangeRecordEntity.setStoreId(storeId);
|
|
|
+ stockChangeRecordEntity.setParentId(id);
|
|
|
+ stockChangeRecordEntities.add(stockChangeRecordEntity);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 先不修改库存,等审核成功后再进行修改
|
|
|
+ stockChangeRecordService.saveBatchByOrderSubmit(stockChangeRecordEntities);
|
|
|
+ SysUserEntity userEntity = ShiroUtils.getUserEntity();
|
|
|
+
|
|
|
+ StockChangeEntity stockChangeEntity = new StockChangeEntity();
|
|
|
+ BeanUtils.copyProperties(stockChangeDto, stockChangeEntity);
|
|
|
+ DateTime parse = DateUtil.parse(stockChangeDto.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ stockChangeEntity.setTime(parse);
|
|
|
+ stockChangeEntity.setAudit(Constants.AuditStatus.WAIT_AUDIT.getType());
|
|
|
+ stockChangeEntity.setStore(storeEntity.getStoreName());
|
|
|
+ stockChangeEntity.setMerch(storeEntity.getMerchName());
|
|
|
+ stockChangeEntity.setThirdMerch(stockChangeEntity.getThirdMerch());
|
|
|
+ stockChangeEntity.setCreateBy(userEntity.getUsername());
|
|
|
+ stockChangeEntity.setCreateTime(createTime);
|
|
|
+ stockChangeEntity.setModifyBy(userEntity.getUsername());
|
|
|
+ stockChangeEntity.setModifyTime(createTime);
|
|
|
+
|
|
|
+ ((StockChangeService)(AopContext.currentProxy())).save(stockChangeEntity);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新审核状态
|
|
|
+ *
|
|
|
+ * @param ids 要更新的数据
|
|
|
+ * @param type 状态
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateAuditStatus(String[] ids, String type) {
|
|
|
+
|
|
|
+ // 更新库存
|
|
|
+ List<StockChangeEntity> inChangeEntities = new ArrayList<>();
|
|
|
+ List<StockChangeEntity> outChangeEntities = new ArrayList<>();
|
|
|
+ // 审核成功才需要更新库存
|
|
|
+ if (Constants.AuditStatus.AUDIT_SUCCESS.getType().equals(type)) {
|
|
|
+ List<StockChangeEntity> stockChangeEntities = queryListByIds(ids);
|
|
|
+
|
|
|
+ stockChangeEntities.forEach(stockChangeEntity -> {
|
|
|
+ String type1 = stockChangeEntity.getType();
|
|
|
+ if (Constants.StockChangeType.item_0.getType().equals(type1)
|
|
|
+ || Constants.StockChangeType.item_1.getType().equals(type1)) {
|
|
|
+ inChangeEntities.add(stockChangeEntity);
|
|
|
+ } else if (Constants.StockChangeType.item_3.getType().equals(type1)
|
|
|
+ || Constants.StockChangeType.item_4.getType().equals(type1)) {
|
|
|
+ outChangeEntities.add(stockChangeEntity);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(inChangeEntities)) {
|
|
|
+ List<String> stringList = inChangeEntities.stream().map(StockChangeEntity::getId).collect(Collectors.toList());
|
|
|
+ List<StockChangeRecordEntity> stockChangeRecordEntities = stockChangeRecordService.queryListByParentIds(stringList);
|
|
|
+ productStoreRelaService.updateStockNumberByStorage(stockChangeRecordEntities);
|
|
|
+ goodsService.updateStockNumberByStorage(stockChangeRecordEntities);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(outChangeEntities)) {
|
|
|
+ List<String> stringList = inChangeEntities.stream().map(StockChangeEntity::getId).collect(Collectors.toList());
|
|
|
+ List<StockChangeRecordEntity> stockChangeRecordEntities = stockChangeRecordService.queryListByParentIds(stringList);
|
|
|
+ productStoreRelaService.updateStockNumberByOutbound(stockChangeRecordEntities);
|
|
|
+ goodsService.updateStockNumberByOutbound(stockChangeRecordEntities);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ stockChangeDao.updateAuditStatus(ids, type);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<StockChangeEntity> queryListByIds(String[] ids) {
|
|
|
+ return stockChangeDao.queryListByIds(ids);
|
|
|
+ }
|
|
|
+}
|