Helius
2021-04-12 4ffb1d977c62bb2c044c7a9a84e41e53f9d8a4c5
zq-erp/src/main/java/com/matrix/system/hive/service/imp/SysOrderServiceImpl.java
@@ -5,6 +5,7 @@
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.app.dto.OrderListDto;
@@ -15,6 +16,7 @@
import com.matrix.system.app.vo.RankingVo;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.common.dao.SysUsersDao;
import com.matrix.system.constance.Dictionary;
@@ -119,6 +121,8 @@
    @Autowired
    private SysInstoreInfoService sysInstoreInfoService;
    @Autowired
    BusParameterSettingsDao parameterSettingsDao;
    @Value("${evn}")
    private String evn;
@@ -372,7 +376,7 @@
            sysOrderFlowDao.deleteByOrderId(id);
            //删除积分
            scoreVipDetailService.removeByBusinessId(null,order.getVipId(), order.getId());
            scoreVipDetailService.removeByBusinessId(order.getVipId(), order.getId());
            // 取消订单
            order.setStatu(Dictionary.ORDER_STATU_YQX);
@@ -617,7 +621,7 @@
        //添加自己的积分
        if(selfScore>0){
                scoreVipDetailService.addScore(null,
                scoreVipDetailService.addScore(
                        vipInfo.getId(),
                        pageOrder.getStaffId(),
                        pageOrder.getShopId(),
@@ -632,7 +636,7 @@
            //推荐注册老带新积分奖励
            SysVipInfo referrerVip = sysVipInfoDao.selectById(vipInfo.getRecommendId());
            if(parentScore>0){
                scoreVipDetailService.addScore(null,
                scoreVipDetailService.addScore(
                        referrerVip.getId(),
                        pageOrder.getStaffId(),
                        pageOrder.getShopId(),
@@ -646,7 +650,7 @@
            if(referrerVip.getRecommendId()!=null){
                SysVipInfo topVipInfo = sysVipInfoDao.selectById(referrerVip.getRecommendId());
                if(topParentScore>0){
                    scoreVipDetailService.addScore(null,
                    scoreVipDetailService.addScore(
                            topVipInfo.getId(),
                            pageOrder.getStaffId(),
                            pageOrder.getShopId(),
@@ -676,7 +680,7 @@
            ShoppingGoods goods = shoppingGoodsDao.selectById(goodsId);
            flow.setFlowContent(goods.getName() + "等" + sourceOrder.getItems().size() + "件产品");
            // 若是退款,则取负数
            if (Dictionary.ORDER_STATU_TK.equals(sourceOrder.getStatu())) {
            if (SysOrder.ORDER_TYPE_REFUND== sourceOrder.getOrderType()) {
                flow.setFlowType(SysOrderFlow.FLOW_TYPE_REFUND);
                flow.setAmount(flow.getAmount().negate());
                flow.setOrderId(sourceOrder.getOldOrderId());
@@ -984,8 +988,6 @@
    }
    @Autowired
    BusParameterSettingsDao parameterSettingsDao;
    /**
     * @param order 新增出库单并更新本店库存
@@ -995,103 +997,110 @@
    @Override
    public void addOutStore(SysOrder order) {
        List<SysOutStoreItem> storeItemList = new ArrayList<>();
        BusParameterSettings manageStockSetting = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WAREHOUSE_MANAGE_STOCK, order.getCompanyId());
        if(AppConstance.IS_Y.equals(manageStockSetting.getParamValue())){
        for (SysOrderItem sysOrderItem : order.getItems()) {
            List<SysOutStoreItem> storeItemList = new ArrayList<>();
            if (ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP.equals(sysOrderItem.getType())) {
            for (SysOrderItem sysOrderItem : order.getItems()) {
                SysOutStoreItem storeItem = new SysOutStoreItem();
                storeItem.setSkuId(sysOrderItem.getGoodsId());
                storeItem.setAmount(Double.valueOf(sysOrderItem.getCount()));
                storeItemList.add(storeItem);
                if (ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP.equals(sysOrderItem.getType())) {
            } else if (ShoppingGoods.SHOPPING_GOODS_TYPE_TC.equals(sysOrderItem.getType())
                    SysOutStoreItem storeItem = new SysOutStoreItem();
                    storeItem.setSkuId(sysOrderItem.getGoodsId());
                    storeItem.setAmount(Double.valueOf(sysOrderItem.getCount()));
                    storeItemList.add(storeItem);
                    || ShoppingGoods.SHOPPING_GOODS_TYPE_ZHK.equals(sysOrderItem.getType())) {
                } else if (ShoppingGoods.SHOPPING_GOODS_TYPE_TC.equals(sysOrderItem.getType())
                List<ShoppingGoodsAssemble> goodsList = new ArrayList<>();
                        || ShoppingGoods.SHOPPING_GOODS_TYPE_ZHK.equals(sysOrderItem.getType())) {
                goodsList.addAll(shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(sysOrderItem.getGoodsId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP));
                    List<ShoppingGoodsAssemble> goodsList = new ArrayList<>();
                if (ShoppingGoods.SHOPPING_GOODS_TYPE_ZHK.equals(sysOrderItem.getType())) {
                    //综合卡处理,中的套餐,中的家居产品
                    List<ShoppingGoodsAssemble> zhkAssemble = shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(sysOrderItem.getGoodsId(), ShoppingGoods.SHOPPING_GOODS_TYPE_TC);
                    zhkAssemble.forEach(item -> {
                        goodsList.addAll(shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(item.getAssembleGoodId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP));
                    goodsList.addAll(shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(sysOrderItem.getGoodsId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP));
                    if (ShoppingGoods.SHOPPING_GOODS_TYPE_ZHK.equals(sysOrderItem.getType())) {
                        //综合卡处理,中的套餐,中的家居产品
                        List<ShoppingGoodsAssemble> zhkAssemble = shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(sysOrderItem.getGoodsId(), ShoppingGoods.SHOPPING_GOODS_TYPE_TC);
                        zhkAssemble.forEach(item -> {
                            goodsList.addAll(shoppingGoodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(item.getAssembleGoodId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP));
                        });
                    }
                    goodsList.forEach(item -> {
                        SysOutStoreItem storeItem = new SysOutStoreItem();
                        storeItem.setSkuId(item.getAssembleGoodId());
                        storeItem.setAmount(Double.valueOf(item.getTotal()));
                        storeItemList.add(storeItem);
                    });
                }
                goodsList.forEach(item -> {
                    SysOutStoreItem storeItem = new SysOutStoreItem();
                    storeItem.setSkuId(item.getAssembleGoodId());
                    storeItem.setAmount(Double.valueOf(item.getTotal()));
                    storeItemList.add(storeItem);
                });
            }
        }
        if (CollectionUtils.isNotEmpty(storeItemList)) {
            Long warehouseId = warehouseDao.findShopWarehouse(order.getShopId()).get(0).getId();
            SysOutStore outStore = new SysOutStore();
            outStore.setOutStoreNo(codeService.getOutStoreCode());
            outStore.setOrderId(order.getId());
            outStore.setShopId(order.getShopId());
            outStore.setShopId(order.getShopId());
            outStore.setStaffId(order.getStaffId());
            outStore.setType(Dictionary.OUT_STORE_JJCPCK);
            outStore.setServiceNo(order.getOrderNo());
            outStore.setTime(new Date());
            outStore.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
            outStore.setCompanyId(order.getCompanyId());
            sysOutStoreDao.insert(outStore);
            if (CollectionUtils.isNotEmpty(storeItemList)) {
                Long warehouseId = warehouseDao.findShopWarehouse(order.getShopId()).get(0).getId();
                SysOutStore outStore = new SysOutStore();
                outStore.setOutStoreNo(codeService.getOutStoreCode());
                outStore.setOrderId(order.getId());
                outStore.setShopId(order.getShopId());
                outStore.setShopId(order.getShopId());
                outStore.setStaffId(order.getStaffId());
                outStore.setType(Dictionary.OUT_STORE_JJCPCK);
                outStore.setServiceNo(order.getOrderNo());
                outStore.setTime(new Date());
                outStore.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
                outStore.setCompanyId(order.getCompanyId());
                sysOutStoreDao.insert(outStore);
            //出库明细,根据批次维度定义
            List<SysOutStoreItem> realOutStoreItemList = new ArrayList<>();
                //出库明细,根据批次维度定义
                List<SysOutStoreItem> realOutStoreItemList = new ArrayList<>();
            storeItemList.forEach(outStoreItem -> {
                storeItemList.forEach(outStoreItem -> {
                //设置出库主键
                outStoreItem.setOutStoreId(outStore.getId());
                    //设置出库主键
                    outStoreItem.setOutStoreId(outStore.getId());
                //计算库存总数是否满足本次扣减的需求
                List<SysStoreInfo> stores = storeInfoDao.selectStoInfoBySku(outStoreItem.getSkuId(), warehouseId);
                double sum = stores.stream().mapToDouble(item -> item.getStoreTotal()).sum();
                if (sum < outStoreItem.getAmount()) {
                    ShoppingGoods sysGoods = shoppingGoodsDao.selectById(outStoreItem.getSkuId());
                    if (sysGoods != null) {
                        throw new GlobleException("出库失败:【" + sysGoods.getName() + "库存不足】");
                    //计算库存总数是否满足本次扣减的需求
                    List<SysStoreInfo> stores = storeInfoDao.selectStoInfoBySku(outStoreItem.getSkuId(), warehouseId);
                    double sum = stores.stream().mapToDouble(item -> item.getStoreTotal()).sum();
                    if (sum < outStoreItem.getAmount()) {
                        ShoppingGoods sysGoods = shoppingGoodsDao.selectById(outStoreItem.getSkuId());
                        if (sysGoods != null) {
                            throw new GlobleException("出库失败:【" + sysGoods.getName() + "库存不足】");
                    } else {
                        throw new GlobleException("出库失败没有找到出库产品");
                        } else {
                            throw new GlobleException("出库失败没有找到出库产品");
                        }
                    }
                }
                //循环获取所有批次产品,并扣减库存
                Double number = outStoreItem.getAmount();
                for (SysStoreInfo storeInfo : stores) {
                    Double oldStoreTotal = storeInfo.getStoreTotal();
                    Double surplus = storeInfo.getStoreTotal() - number;
                    //更新库存
                    storeInfo.setStoreTotal(surplus < 0 ? 0 : surplus);
                    //循环获取所有批次产品,并扣减库存
                    Double number = outStoreItem.getAmount();
                    for (SysStoreInfo storeInfo : stores) {
                        Double oldStoreTotal = storeInfo.getStoreTotal();
                        Double surplus = storeInfo.getStoreTotal() - number;
                        //更新库存
                        storeInfo.setStoreTotal(surplus < 0 ? 0 : surplus);
                    //每次扣减库存都创建一个出库记录
                    SysOutStoreItem sysOutStoreItem = new SysOutStoreItem();
                    BeanUtils.copyProperties(outStoreItem, sysOutStoreItem);
                    sysOutStoreItem.setStoreId(storeInfo.getId());
                    sysOutStoreItem.setAmount(oldStoreTotal - storeInfo.getStoreTotal());
                    realOutStoreItemList.add(sysOutStoreItem);
                        //每次扣减库存都创建一个出库记录
                        SysOutStoreItem sysOutStoreItem = new SysOutStoreItem();
                        BeanUtils.copyProperties(outStoreItem, sysOutStoreItem);
                        sysOutStoreItem.setStoreId(storeInfo.getId());
                        sysOutStoreItem.setAmount(oldStoreTotal - storeInfo.getStoreTotal());
                        realOutStoreItemList.add(sysOutStoreItem);
                    storeInfoDao.update(storeInfo);
                    //扣除后剩余库存大于0则跳出扣除,否则剩余数量的负数的绝对值就是再次扣减的数量
                    if (surplus > 0) {
                        break;
                    } else {
                        number = Math.abs(surplus);
                        storeInfoDao.update(storeInfo);
                        //扣除后剩余库存大于0则跳出扣除,否则剩余数量的负数的绝对值就是再次扣减的数量
                        if (surplus > 0) {
                            break;
                        } else {
                            number = Math.abs(surplus);
                        }
                    }
                }
            });
            sysOutStoreItemDao.batchInsert(realOutStoreItemList);
                });
                sysOutStoreItemDao.batchInsert(realOutStoreItemList);
            }
        }else{
            LogUtil.debug("不管理库存");
        }
@@ -1322,6 +1331,7 @@
        order.setRemark(cardUser.getCardName());
        order.setStaffId(user.getSuId());
        order.setIsCross(2 + "");
        order.setOrderType(SysOrder.ORDER_TYPE_SEAL);
        order.setStatu(Dictionary.ORDER_STATU_YFK);
        order.setCompanyId(user.getCompanyId());
        sysOrderDao.insert(order);
@@ -1342,6 +1352,7 @@
        orderItem.setPrice(czVo.getBjmoney());
        orderItem.setStatus(Dictionary.ORDER_STATU_YFK);
        orderItem.setAchieveList(czVo.getAchaeveList());
        orderItem.setShoppingGoods(shoppingGoods);
        orderItemDao.insert(orderItem);
        List<SysOrderItem> items = new ArrayList<>();
        items.add(orderItem);
@@ -1362,9 +1373,12 @@
        moneyCardUseFlowDao.insert(moneyCardUseFlow);
        order.setFlows(czVo.getFlows());
        addOrderFlow(order);
        // 添加员工业绩
        achieveNewService.addAchaeveByOrder(order);
        return order;
    }
@@ -1434,7 +1448,6 @@
        sysOrder.setPayTime(now);
        sysOrder.setOrderNo(codeService.getRefundOrderNo());
        sysOrder.setZkTotal(-sysOrder.getZkTotal());
        sysOrder.setOrderType(SysOrder.ORDER_TYPE_REFUND);
        //新增订单
        sysOrderDao.insert(sysOrder);
        //插入明细
@@ -1459,7 +1472,7 @@
        //退款退套餐退项目
        refundProjUse(sysOrder);
        //删除积分
        scoreVipDetailService.removeByBusinessId(null,oldOrder.getVipId(), oldOrder.getId());
        scoreVipDetailService.removeByBusinessId(oldOrder.getVipId(), oldOrder.getId());
        // 设置业绩
        achieveNewService.addAchaeveByOrder(sysOrder);
@@ -1636,10 +1649,14 @@
            shoppingGoodsDao.update(goods);
        }
        // 家居产品退库存
        if (CollectionUtils.isNotEmpty(returnGoodsList)) {
            refundInstore(returnGoodsList);
        BusParameterSettings manageStockSetting = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WAREHOUSE_MANAGE_STOCK, sysOrder.getCompanyId());
        if(AppConstance.IS_Y.equals(manageStockSetting.getParamValue())){
            // 家居产品退库存
            if (CollectionUtils.isNotEmpty(returnGoodsList)) {
                refundInstore(returnGoodsList);
            }
        }
    }
    /**