商贸型企业 Java 收货 + 入库 + 生成付款单

package cn.hybn.erp.modular.system.service.impl;

import cn.hybn.erp.core.common.page.LayuiPageFactory;
import cn.hybn.erp.core.common.page.LayuiPageInfo;
import cn.hybn.erp.modular.system.base.BaseStatus;
import cn.hybn.erp.modular.system.base.Order;
import cn.hybn.erp.modular.system.base.Status;
import cn.hybn.erp.modular.system.entity.*;
import cn.hybn.erp.modular.system.mapper.GrnOrderMapper;
import cn.hybn.erp.modular.system.model.params.GrnOrderParam;
import cn.hybn.erp.modular.system.model.params.PurchaseOrderParam;
import cn.hybn.erp.modular.system.model.result.GrnOrderResult;
import cn.hybn.erp.modular.system.service.*;
import cn.hybn.erp.modular.system.utility.Katrina_CJ_Utils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.sun.xml.internal.bind.v2.TODO;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Katrina_CJ
 * @since 2019-03-16
 */
@Service
public class GrnOrderServiceImpl extends ServiceImpl<GrnOrderMapper, GrnOrder> implements GrnOrderService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private GrnOrderDetailService grnOrderDetailService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private InventoryTransactionService inventoryTransactionService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private PaymentOrderService paymentOrderService;

    @Override
    public void add(GrnOrderParam param) {
        GrnOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GrnOrderParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(GrnOrderParam param) {
        GrnOrder oldEntity = getOldEntity(param);
        GrnOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GrnOrderResult findBySpec(GrnOrderParam param) {
        return null;
    }

    @Override
    public List<GrnOrderResult> findListBySpec(GrnOrderParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GrnOrderParam param) {
        Page pageContext = getPageContext();
        QueryWrapper<GrnOrder> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void createGrnOrder(PurchaseOrderParam param) {
        GrnOrder grnOrder = new GrnOrder();
        grnOrder.setGrnOrderStatus(Status.CREATE.getStatus());
        grnOrder.setOrderId(param.getPoId());
        grnOrder.setGrnOrderCreateTime(new Date());
        QueryWrapper<GrnOrder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("grn_order_id").last("limit 1");
        GrnOrder grnId = this.getOne(wrapper);
        //如果当前数据库没有订单,手动赋值
        if (Objects.isNull(grnId)) {
            grnId = new GrnOrder();
            grnId.setOrderId((long) 0);
        }
        //创建订单编号
        LoggerFactory.getLogger(GrnOrderServiceImpl.class).info("DEBUG:->" + String.valueOf(grnId.getOrderId()));
        String serial = Katrina_CJ_Utils.createOrderSerial(Order.RKD_, grnId.getOrderId());
        grnOrder.setGrnOrderSerial(serial);
        //填充入库单明细
//        QueryWrapper<PurchaseDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("order_po_id", param.getPoId());
        //当前采购单明细
//        List<PurchaseDetail> details = purchaseDetailService.list(queryWrapper);

        this.getBaseMapper().insert(grnOrder);
//        details.forEach(pd -> {
//            GrnOrderDetail grnOrderDetail = new GrnOrderDetail();
//            grnOrderDetail.setGrnOrderId(grnOrder.getGrnOrderId());
//            grnOrderDetail.setGrnDetailIsGift(pd.getOrderIsGifts());
//            grnOrderDetail.setGrnDetailProductId(pd.getOrderProductId());
//            grnOrderDetail.setGrnDetailUnit(pd.getOrderUnit());
//            grnOrderDetail.setGrnDetailGoods(new BigDecimal(0));
//            grnOrderDetailService.getBaseMapper().insert(grnOrderDetail);
//        });
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void getGoods(GrnOrderParam grnOrderParam) {
        GrnOrder grnOrder = new GrnOrder();
        grnOrder.setGrnOrderId(grnOrderParam.getGrnOrderId());
        grnOrder.setGrnOrderAgent(grnOrderParam.getGrnOrderAgent());
        grnOrder.setGrnOrderAgentDept(grnOrderParam.getGrnOrderAgentDept());
        grnOrder.setGrnOrderGetGoodsTime(new Date());
        grnOrder.setGrnOrderRemark(grnOrderParam.getGrnOrderRemark());
        grnOrder.setGrnOrderStatus(Status.FINISH.getStatus());

        List<GrnOrderDetail> grnOrderDetails = JSONObject.parseArray(grnOrderParam.getGrnOrderDetail(), GrnOrderDetail.class);

        //更新入库单

        this.getBaseMapper().updateById(grnOrder);

        //更新入库单详情
        grnOrderDetails.forEach(g -> {
//            grnOrderDetail.setGrnDetailIsGift(pd.getOrderIsGifts());
//            grnOrderDetail.setGrnDetailProductId(pd.getOrderProductId());
//            grnOrderDetail.setGrnDetailUnit(pd.getOrderUnit());
//            grnOrderDetail.setGrnDetailGoods(new BigDecimal(0));
            g.setGrnDetailProductId(g.getGrnDetailProductId());
            g.setGrnDetailIsGift(g.getGrnDetailIsGift());
            g.setGrnOrderId(grnOrder.getGrnOrderId());

            grnOrderDetailService.getBaseMapper().insert(g);
        });

        //查询入库单详情
        QueryWrapper<GrnOrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grn_order_id", grnOrder.getGrnOrderId());


        //更新库存和库存事务
        List<GrnOrderDetail> details = grnOrderDetailService.list(queryWrapper);


        //获取采购单数据
        GrnOrder grn = this.getById(grnOrder.getGrnOrderId());
        PurchaseOrder purchaseOrder = purchaseOrderService.getById(grn.getOrderId());
        //更新采购单数据
        QueryWrapper<PurchaseDetail> purchaseDetailQueryWrapper = new QueryWrapper<>();
        purchaseDetailQueryWrapper.eq("order_po_id", purchaseOrder.getPoId());
        List<PurchaseDetail> purchaseDetails = purchaseDetailService.list(purchaseDetailQueryWrapper);

        //移除无入库的明细
        details.removeIf(d -> Objects.isNull(d.getGrnDetailWarehouse()));

        details.forEach(d -> {
            QueryWrapper<Inventory> inventoryQueryWrapper = new QueryWrapper<>();
            inventoryQueryWrapper.eq("inventory_product_id", d.getGrnDetailProductId());
            List<Inventory> inventories = inventoryService.list(inventoryQueryWrapper);

            //如果无当前产品,新增库存
            if (inventories.size() == 0) {
                //新库存
                Inventory inventory = new Inventory();
                inventory.setInventoryAmount(d.getGrnDetailGoods());
                inventory.setInventoryAvailableLocation(d.getGrnDetailGoods());
                inventory.setInventoryIn(d.getGrnDetailGoods());
                inventory.setInventoryLow(new BigDecimal(0));
                inventory.setInventoryProductId(d.getGrnDetailProductId());
                inventory.setInventoryMax(new BigDecimal(0));
                inventory.setInventoryWarehouseId(d.getGrnDetailWarehouse());
                inventory.setInventoryUnit(d.getGrnDetailUnit());
                inventory.setInventoryCreateTime(new Date());
                inventory.setInventoryOnWay(new BigDecimal(0));
                inventory.setInventoryBatch(d.getGrnDetailBatch());
                inventory.setInventoryProductTime(d.getGrnDetailProductTime());
                inventoryService.getBaseMapper().insert(inventory);

                //新库存事务
                InventoryTransaction transaction = new InventoryTransaction();
                transaction.setGrnTransactionInventoryId(inventory.getInventoryId());
                transaction.setGrnTransactionIsGift(d.getGrnDetailIsGift());
                transaction.setGrnTransactionOrderSerial(grnOrderParam.getGrnOrderSerial());
                transaction.setGrnTransactionBatch(d.getGrnDetailBatch());
                transaction.setGrnTransactionType(BaseStatus.RK.getDesc());
                transaction.setGrnTransactionProductId(d.getGrnDetailProductId());
                transaction.setGrnTransactionGrnOrderId(grnOrder.getGrnOrderId());
                transaction.setGrnTransactionHappenNumber(d.getGrnDetailGoods());
                transaction.setGrnTransactionProductionOfDate(d.getGrnDetailProductTime());
                transaction.setGrnTransactionProductBarcode(d.getGrnDetailBarcode());

                inventoryTransactionService.getBaseMapper().insert(transaction);

            } else {
                inventories.forEach(inventory -> {
                    //库位一致
                    if (inventory.getInventoryWarehouseId().equals(d.getGrnDetailWarehouse())) {
                        //单位一致
                        if (inventory.getInventoryUnit().equals(d.getGrnDetailUnit())) {
                            //更新库存数量
                            inventory.setInventoryAmount(inventory.getInventoryAmount().add(d.getGrnDetailGoods()));

                            inventoryService.getBaseMapper().updateById(inventory);
                        } else {
                            //新库存
                            Inventory newInventory = new Inventory();
                            newInventory.setInventoryAmount(d.getGrnDetailGoods());
                            newInventory.setInventoryAvailableLocation(d.getGrnDetailGoods());
                            newInventory.setInventoryIn(d.getGrnDetailGoods());
                            newInventory.setInventoryLow(new BigDecimal(0));
                            newInventory.setInventoryProductId(d.getGrnDetailProductId());
                            newInventory.setInventoryMax(new BigDecimal(0));
                            newInventory.setInventoryWarehouseId(d.getGrnDetailWarehouse());
                            newInventory.setInventoryUnit(d.getGrnDetailUnit());
                            newInventory.setInventoryCreateTime(new Date());
                            newInventory.setInventoryOnWay(new BigDecimal(0));
                            newInventory.setInventoryBatch(d.getGrnDetailBatch());
                            newInventory.setInventoryProductTime(d.getGrnDetailProductTime());
                            inventoryService.getBaseMapper().insert(newInventory);

                            //新库存事务
                            InventoryTransaction transaction = new InventoryTransaction();
                            transaction.setGrnTransactionInventoryId(newInventory.getInventoryId());
                            transaction.setGrnTransactionIsGift(d.getGrnDetailIsGift());
                            transaction.setGrnTransactionOrderSerial(grnOrderParam.getGrnOrderSerial());
                            transaction.setGrnTransactionBatch(d.getGrnDetailBatch());
                            transaction.setGrnTransactionType(BaseStatus.RK.getDesc());
                            transaction.setGrnTransactionProductId(d.getGrnDetailProductId());
                            transaction.setGrnTransactionGrnOrderId(grnOrder.getGrnOrderId());
                            transaction.setGrnTransactionHappenNumber(d.getGrnDetailGoods());
                            transaction.setGrnTransactionProductionOfDate(d.getGrnDetailProductTime());
                            transaction.setGrnTransactionProductBarcode(d.getGrnDetailBarcode());
                            inventoryTransactionService.getBaseMapper().insert(transaction);
                        }
                    } else {
                        //新库存
                        Inventory newInventory = new Inventory();
                        newInventory.setInventoryAmount(d.getGrnDetailGoods());
                        newInventory.setInventoryAvailableLocation(d.getGrnDetailGoods());
                        newInventory.setInventoryIn(d.getGrnDetailGoods());
                        newInventory.setInventoryLow(new BigDecimal(0));
                        newInventory.setInventoryProductId(d.getGrnDetailProductId());
                        newInventory.setInventoryMax(new BigDecimal(0));
                        newInventory.setInventoryWarehouseId(d.getGrnDetailWarehouse());
                        newInventory.setInventoryUnit(d.getGrnDetailUnit());
                        newInventory.setInventoryCreateTime(new Date());
                        newInventory.setInventoryOnWay(new BigDecimal(0));
                        newInventory.setInventoryBatch(d.getGrnDetailBatch());
                        newInventory.setInventoryProductTime(d.getGrnDetailProductTime());
                        inventoryService.getBaseMapper().insert(newInventory);

                        //新库存事务
                        InventoryTransaction transaction = new InventoryTransaction();
                        transaction.setGrnTransactionInventoryId(newInventory.getInventoryId());
                        transaction.setGrnTransactionIsGift(d.getGrnDetailIsGift());
                        transaction.setGrnTransactionOrderSerial(grnOrderParam.getGrnOrderSerial());
                        transaction.setGrnTransactionBatch(d.getGrnDetailBatch().longValue());
                        transaction.setGrnTransactionType(BaseStatus.RK.getDesc());
                        transaction.setGrnTransactionProductId(d.getGrnDetailProductId());
                        transaction.setGrnTransactionGrnOrderId(grnOrder.getGrnOrderId());
                        transaction.setGrnTransactionHappenNumber(d.getGrnDetailGoods());
                        transaction.setGrnTransactionProductionOfDate(d.getGrnDetailProductTime());
                        transaction.setGrnTransactionProductBarcode(d.getGrnDetailBarcode());
                        inventoryTransactionService.getBaseMapper().insert(transaction);
                    }
                });
            }
        });

        //TODO
        //生成付款单
        QueryWrapper<PaymentOrder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("payment_order_id").last("limit 1");
        PaymentOrder serial = paymentOrderService.getOne(wrapper);
        if (Objects.isNull(serial)) {
            serial = new PaymentOrder();
            serial.setPaymentOrderId((long) 0);
        }
        String paySerial = Katrina_CJ_Utils.createOrderSerial(Order.FKD_, serial.getPaymentOrderId());
        PaymentOrder paymentOrder = new PaymentOrder();
        paymentOrder.setPaymentOrderSerial(paySerial);
        paymentOrder.setPaymentOrderCreateTime(new Date());
        paymentOrder.setPaymentOrderStatus(Status.EXECUTION.getStatus());


        //更新采购单
        //当前采购单总数
        purchaseOrder.setPoSumNumbers(new BigDecimal(0));

        details.forEach(d -> {
            if (purchaseOrder.getPoStatus().equals(Status.EXECUTION.getStatus()) || purchaseOrder.getPoStatus().equals(Status.TERMINATION.getStatus())) {
                purchaseDetails.forEach(pd -> {
                    if (pd.getOrderProductId().equals(d.getGrnDetailProductId())) {
                        //更新已完成数量
                        pd.setOrderFinishNumber(pd.getOrderFinishNumber().add(d.getGrnDetailGoods()));
                        int flag = pd.getOrderNumbers().compareTo(pd.getOrderFinishNumber());
                        if (flag == 0) {
                            //已完成
                            pd.setOrderPendingNumber(new BigDecimal(0));
                        } else if (flag < 0) {
                            //超收
                            pd.setOrderPendingNumber(new BigDecimal(0));
                        } else {
                            //更新未完成数量
                            pd.setOrderPendingNumber(pd.getOrderNumbers().subtract(pd.getOrderFinishNumber()));
                        }
                        paymentOrder.setPaymentAmountsPayable(pd.getOrderPrice().multiply(d.getGrnDetailGoods()));
                        purchaseOrder.setPoSumNumbers(purchaseOrder.getPoSumNumbers().add(pd.getOrderFinishNumber()));
                        purchaseDetailService.getBaseMapper().updateById(pd);
                    }
                });
            } else {
                throw new RuntimeException("订单状态异常!");
            }
        });

        PurchaseOrder purchaseOrder_old = purchaseOrderService.getById(purchaseOrder.getPoId());

        int flag = purchaseOrder.getPoSumNumbers().compareTo(purchaseOrder_old.getPoSumNumbers());


        //订单完成
        if (flag == 0 || flag > 0) {
            //订单完成
            purchaseOrder_old.setPoStatus(Status.FINISH.getStatus());
            purchaseOrderService.getBaseMapper().updateById(purchaseOrder_old);
        }
        paymentOrder.setPaymentGrnOrder(grnOrder.getGrnOrderId());
        //新增付款单
        paymentOrderService.getBaseMapper().insert(paymentOrder);
    }

    private Serializable getKey(GrnOrderParam param) {
        return param.getGrnOrderId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GrnOrder getOldEntity(GrnOrderParam param) {
        return this.getById(getKey(param));
    }


    private GrnOrder getEntity(GrnOrderParam param) {
        GrnOrder entity = new GrnOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public Page<Map<String, Object>> list(String condition, String date_1, String date_2, String status) {
        Page productPage = LayuiPageFactory.defaultPage();
        return baseMapper.order_list(productPage, condition, date_1, date_2, status);
    }


}

  

posted @ 2019-04-12 10:48  杨超杰  阅读(568)  评论(1编辑  收藏  举报