package com.ecep.contract.cloud.u8.ctx;

import java.sql.Timestamp;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.model.Inventory;
import com.ecep.contract.model.PurchaseSettlementVoucher;
import com.ecep.contract.model.PurchaseSettlementVoucherItem;

public class PurchaseSettlementVoucherCtx extends AbstractYongYouU8Ctx {
    InventoryCtx inventoryCtx;

    InventoryCtx getInventoryCtx() {
        if (inventoryCtx == null) {
            inventoryCtx = new InventoryCtx();
            inventoryCtx.from(this);
        }
        return inventoryCtx;
    }

    private boolean applyPurchaseSettlementVoucherDetail(PurchaseSettlementVoucher voucher, Map<String, Object> map,
            MessageHolder holder) {
        String code = (String) map.get("cSVCode");
        String vendorCode = (String) map.get("cVenCode");
        String personCode = (String) map.get("cPersonCode");
        double taxRate = (double) map.get("iTaxRate");
        String maker = (String) map.get("cMaker");
        Timestamp date = (Timestamp) map.get("dSVDate");

        String description = (String) map.get("cSVMemo");
        boolean modified = false;
        if (updateText(voucher::getCode, voucher::setCode, code, holder, "编号：")) {
            modified = true;
        }
        if (updateCompanyByVendorCode(voucher::getCompany, voucher::setCompany, vendorCode, holder, "供应商")) {
            modified = true;
        }
        if (updateEmployeeByCode(voucher::getEmployee, voucher::setEmployee, personCode, holder, "业务员")) {
            modified = true;
        }
        if (updateEmployeeByName(voucher::getMaker, voucher::setMaker, maker, holder, "制单人")) {
            modified = true;
        }
        if (updateLocalDate(voucher::getDate, voucher::setDate, date, holder, "日期：")) {
            modified = true;
        }
        if (updateAppendText(voucher::getDescription, voucher::setDescription, description, holder, "描述")) {
            modified = true;
        }

        return modified;
    }

    private boolean applyPurchaseSettlementVoucherItemDetail(PurchaseSettlementVoucherItem item,
            Map<String, Object> map, MessageHolder holder) {
        String inventoryCode = (String) map.get("cInvCode");
        String pivCode = (String) map.get("cPIVCode");
        String accountant = (String) map.get("cbAccounter");
        Integer rdsID = (Integer) map.get("iRdsID");
        double quantity = (double) map.get("iPBVQuantity");
        String description = (String) map.get("cbMemo");
        boolean modified = false;
        if (updateInventory(item::getInventory, item::setInventory, inventoryCode, holder, "商品")) {
            modified = true;
        }
        if (updateEmployeeByName(item::getAccountant, item::setAccountant, accountant, holder, "会计师")) {
            modified = true;
        }
        if (updateNumber(item::getQuantity, item::setQuantity, quantity, holder, "数量")) {
            modified = true;
        }
        if (updateAppendText(item::getDescription, item::setDescription, description, holder, "描述")) {
            modified = true;
        }

        return modified;
    }

    boolean updateInventory(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode,
            MessageHolder holder, String topic) {
        return getInventoryCtx().syncInventoryDetailByCode(getter, setter, inventoryCode, holder, topic);
    }
}
