/**
 * 送货单解析
 */
import extend from '../utils/extend.js';
import analyze from '../utils/analyzeScannedContent.js';

/**
 * 叉板类
 */
class Fork {
    /**
     * 确认标记
     */
    Confirmed = false;

    /**
     * 叉板号
     */
    ForkNumber = 0;

    /**
     * 叉板数量
     */
    ForkQty = 0;

    /**
     * 条码集合
     */
    Barcodes = new Map();
}

/**
 * 完工送货单
 */
class Order {
    /**
     * 订单
     */
    order = null;

    /**
     * 条码集合
     */
    barcodes = new Map();

    /**
     * 提货单号集合
     */
    orderNumbers = new Map();

    /**
     * 叉板数据集合
     */
    forks = new Map();

    /**
     * 叉板数据集合
     */
    currentFork = new Fork();

    /**
     * 提货单行列表
     */
    orderLines = [];

    constructor(order) {
        this.order = order;
        this.init();
    }

    /**
     * 初始化
     */
    init(order) {
        this.order = order;
        this.currentFork = new Fork();
        this.barcodes.clear();
        this.orderNumbers.clear();
        this.forks.clear();
        this.orderLines = [];

        if (extend.isNull(this.order)) {
            return;
        }
        this.orderLines = this.getAllLines();
        this.initBarcodes();
        this.initForks();
    }

    /**
     * 设置二维码
     */
    initBarcodes() {
        if (extend.isNull(this.order)) {
            return;
        }

        this.barcodes.clear();
        this.orderNumbers.clear();

        if (extend.isArray(this.order.Orders)) {
            for (let i in this.order.Orders) {
                this.orderNumbers.set(this.order.Orders[i].OrderNumber, this.order.Orders[i].OrderNumber);
                if (extend.isArray(this.order.Orders[i].Lines)) {
                    for (let j in this.order.Orders[i].Lines) {
                        if (extend.isArray(this.order.Orders[i].Lines[j].Barcodes)) {
                            for (let k in this.order.Orders[i].Lines[j].Barcodes) {
                                this.barcodes.set(this.order.Orders[i].Lines[j].Barcodes[k].Barcode, this.order
                                    .Orders[i].Lines[
                                        j].Barcodes[k]);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置叉板
     */
    initForks() {
        if (extend.isNull(this.order)) {
            return;
        }
        this.forks.clear();

        for (let barcode of this.barcodes) {
            let forkNumber = barcode.ForkNumber;

            if (this.forks.has(forkNumber)) {
                let fork = this.forks.get(forkNumber);
                fork.Barcodes.set(barcode.Barcode, barcode);
            } else {
                let fork = new Fork();
                fork.ForkNumber = forkNumber;
                this.forks.set(forkNumber, fork);
                fork.Barcodes.set(barcode.Barcode, barcode);
            }
        }

        // 设置当前叉板
        this.currentFork.Barcodes.clear();
        this.currentFork.Confirmed = this.order.CurrentFork.Confirmed;
        this.currentFork.ForkNumber = this.order.CurrentFork.ForkNumber;
        this.currentFork.ForkQty = this.order.CurrentFork.ForkQty;
    }

    /**
     * 同步数据
     */
    async () {
        this.asyncBarcodes();
        this.asyncOrderNumbers();
    }

    /**
     * 同步二维码
     */
    asyncBarcodes() {
        if (extend.isNull(this.order)) {
            return;
        }
        if (extend.isArray(this.order.Orders)) {
            for (let i in this.order.Orders) {
                if (extend.isArray((this.order.Orders[i].Lines))) {
                    for (let j in this.order.Orders[i].Lines) {
                        let barcodes = [];
                        for (let [key, value] of this.barcodes) {
                            if (value.PickupOrderLineId === this.order.Orders[i].Lines[j].PickupOrderLineId) {
                                barcodes.push(value);
                            }
                        }
                        this.order.Orders[i].Lines[j].Barcodes = barcodes;
                    }
                }
            }
        }
    }

    /**
     * 同步提货单号列表
     */
    asyncOrderNumbers() {
        if (extend.isNull(this.order)) {
            return;
        }
        if (extend.isArray(this.order.Orders)) {
            for (let i in this.order.Orders) {
                this.order.OrderNumbers.push(this.order.Orders[i].OrderNumber);
            }
        }
    }

    /**
     * 设置当前叉数
     */
    setCurrentFork() {
        if (extend.isNull(this.order)) {
            return;
        }
        this.order.CurrentFork.Confirmed = this.currentFork.Confirmed;
        this.order.CurrentFork.ForkNumber = this.currentFork.ForkNumber;
        this.order.CurrentFork.ForkQty = this.currentFork.ForkQty;
    }

    /**
     * 当前叉数
     */
    get currentForkQty() {
        let qty = 0;
        if (extend.isObject(this.order) && extend.isObject(this.order.CurrentFork)) {
            qty = this.order.CurrentFork.ForkQty || 0;
        }
        return qty;
    }

    /**
     * 当前叉数
     */
    set currentForkQty(value) {
        if (extend.isNumber(value) && extend.isObject(this.order)) {
            this.order.CurrentFork.ForkQty = value;
        }
    }

    /**
     * 叉板数
     */
    get forkQty() {
        let qty = 0;
        if (extend.isObject(this.order)) {
            qty = this.order.PickupedForkQty || 0;
        }
        return qty;
    }

    /**
     * 当前叉数
     */
    set forkQty(value) {
        if (extend.isNumber(value) && extend.isObject(this.order)) {
            this.order.PickupedForkQty = value;
        }
    }

    /**
     * 送货单总数量
     */
    get quantity() {
        let qty = 0;
        for (let i in this.orderLines) {
            qty += this.orderLines[i].Quantity;
        }
        return qty;
    }

    /**
     * 送货扫描确认数量
     */
    get scanedQty() {
        let qty = 0;
        for (let i in this.orderLines) {
            qty += this.orderLines[i].YSMSL;
        }
        return qty;
    }

    /**
     * 剩余扫描数量
     */
    get remainQty() {
        let qty = 0;
        qty = this.quantity - this.scanedQty;
        return qty;
    }

    /**
     * 获取当前叉
     */
    getCurrentFork() {
        let max = 0;
        let fork = null;
        if (this.forks.size > 0) {
            for (let [key, value] of this.forks) {
                if (key > max) {
                    max = key;
                }
            }
        }
        if (max > 0) {
            fork = this.forks.get(max);
        }
        return fork;
    }

    /**
     * 获取已解析二维码数据列表
     */
    getBarcodeRows() {
        let res = [];
        let lines = this.orderLines;
        for (let i in lines) {
            for (let j in lines[i].Barcodes) {
                let row = analyze.parse(lines[i].Barcodes[j].Barcode);
                res.push(row);
            }
        }
        this.sortBySerialNumber(res);
        return res;
    }

    /**
     * 根据流水码排序
     */
    sortBySerialNumber(rows) {
        if (!extend.isArray(rows)) {
            rows = [];
        }
        for (let i in rows) {
            for (let j in rows) {
                if (rows[i].SerialNumber < rows[j].SerialNumber) {
                    let temp = rows[i];
                    rows[i] = rows[j];
                    rows[j] = temp;
                    temp = null;
                }
            }
        }
        return rows;
    }

    /**
     * 获取提货单列表
     */
    getOrderNumbers() {
        return extend.mapValuesToArray(this.orderNumbers);
    }

    /**
     * 获取全部提货单列表
     */
    getAllLines() {
        let lines = [];
        if (extend.isObject(this.order) && extend.isArray(this.order.Orders)) {
            for (let i in this.order.Orders) {
                if (extend.isArray(this.order.Orders[i].Lines)) {
                    for (let j in this.order.Orders[i].Lines) {
                        if (this.order.Orders[i].Lines[j].PickupOrderId === this.order.Orders[i].PickupOrderId) {
                            lines.push(this.order.Orders[i].Lines[j]);
                        }
                    }
                }
            }
        }
        return lines;
    }

    /**
     * 获取板式产品行
     * @param {string} inventoryNumber 料号
     * @returns {Array} 行列表
     */
    get1Lines(inventoryNumber) {
        let res = [];
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber) {
                res.push(lines[i]);
            }
        }
        return res;
    }

    /**
     * 获取定制产品行
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次号
     * @returns {Array} 行列表
     */
    get2Lines(inventoryNumber, batchNumber) {
        let res = [];
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber && lines[i].OrderTotalNumber === batchNumber) {
                res.push(lines[i]);
            }
        }
        return res;
    }

    /**
     * 获取板式产品总数
     * @param {string} inventoryNumber 料号
     * @returns {number} 板式产品总数
     */
    get1InventoryQty(inventoryNumber) {
        let res = 0;
        let lines = this.get1Lines(inventoryNumber);
        for (let i in lines) {
            res += lines[i].Quantity;
        }
        return res;
    }

    /**
     * 获取定制产品总数
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次号
     * @returns {number} 定制产品总数
     */
    get2InventoryQty(inventoryNumber, batchNumber) {
        let res = 0;
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber && lines[i].OrderTotalNumber === batchNumber) {
                res += lines[i].Quantity;
            }
        }
        return res;
    }

    /**
     * 获取板式产品已扫描数
     * @param {string} inventoryNumber 料号
     */
    get1InventoryScanedQty(inventoryNumber) {
        let res = 0;
        let lines = this.get1Lines(inventoryNumber);
        for (let i in lines) {
            res += lines[i].YSMSL;
        }
        return res;
    }

    /**
     * 获取定制产品总数
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次号
     */
    get2InventoryScanedQty(inventoryNumber, batchNumber) {
        let res = 0;
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber && lines[i].OrderTotalNumber === batchNumber) {
                res += lines[i].YSMSL;
            }
        }
        return res;
    }

    /**
     * 获取板式产品列表
     * @param {string} inventoryNumber 料号
     */
    get1Inventorys(inventoryNumber) {
        let res = [];
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber && lines[i].Quantity > lines[i].YSMSL) {
                res.push(lines[i]);
            }
        }
        return res;
    }

    /**
     * 获取定制产品列表
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次号
     */
    get2Inventorys(inventoryNumber, batchNumber) {
        let res = [];
        let lines = this.orderLines;
        for (let i in lines) {
            if (lines[i].InventoryNumber === inventoryNumber && lines[i].OrderTotalNumber === batchNumber) {
                res.push(lines[i]);
            }
        }
        return res;
    }

    /**
     * 获取未扫描列表
     */
    getUnScanedLines() {
        let res = [];
        for (let i in this.orderLines) {
            if (this.orderLines[i].YSMSL < this.orderLines[i].Quantity) {
                res.push(this.orderLines[i]);
            }
        }
        return res;
    }
}

export default Order;