package com.sagacloud.utils.venders;
/*
 * Author: Jxing
 * Create Time: 2018/8/30
 */

import com.alibaba.fastjson.*;
import com.sagacloud.cache.EhCaches;
import com.sagacloud.cache.cachedObject.pojo.VenderInfoMap;
import com.sagacloud.cache.cachedObject.pojo.venders.*;
import com.sagacloud.dict.EqStaticInfo;
//import com.sagacloud.json.JSONValue;
import com.sagacloud.utils.InfoCodeUtil;
import com.sagacloud.utils.OssUtils;

import java.util.Set;


public class VendersInfoUtil {

    /**
     * 添加厂商的信息
     * @param projectId 项目id, 带Pj
     * @param arr 要返回的带信息点的Content
     */
    public static void addVenderInfo(String projectId, JSONArray arr){
        try {
            if(!OssUtils.getProperty("is.use.vender.url").equals("true")){
                return;
            }
            EqStaticInfo eqStaticInfo = new EqStaticInfo(projectId);
            VenderInfoMap venderInfoMap = VenderInfoMap.get();
            for (int i = 0; i < arr.size(); ++i) {
                JSONObject obj = arr.getJSONObject(i);
                if (!isIdSatisfy(obj.getString("id")))
                    continue;
                String type = getType(obj);
                JSONObject infos = obj.getJSONObject("infos");
                if (infos == null || infos.keySet().size() == 0)
                    return;
                // 设备/部件的类型转换为所属的种族类型
                if(type != null && type.length() != 3)
                    type = eqStaticInfo.getEqToFamilyMap().get(type.substring(2));
                complementManufacturer(projectId, infos, venderInfoMap, type);
                complementSupplier(infos, venderInfoMap, projectId);
                complementInsurance(infos, venderInfoMap, projectId);
                complementMaintainance(infos, venderInfoMap, projectId);
            }
        }catch (Exception ignore){
            ignore.printStackTrace();
        }
    }
//    /**
//     * 添加厂商的信息
//     * @param projectId 项目id, 带Pj
//     * @param arr 要返回的带信息点的Content
//     */
//    public static JSONArray addVenderInfo(String projectId, JSONArray arr) {
////        String str = arr.toString();
////        JSONArray array = JSONArray.parseArray(str);
//        addVenderInfo(projectId, arr);
//        str = array.toString();
//        return null;
//    }

    private static String getType(JSONObject singleObj) {
        String id = singleObj.getString("id");
        if(id.startsWith("Eq"))
            return singleObj.getString("category");
        if(id.startsWith("Ec"))
            return singleObj.getString("category");
        if(id.startsWith("Pe"))
            return singleObj.getString("family");
        return null;
    }

    // 补充维修商信息点
    private static void complementMaintainance(JSONObject infos, VenderInfoMap venderInfoMap, String projectId) {
        String venderId = infos.getString("DPMaintainerID");
        if(venderId == null || !venderInfoMap.getMaintainanceMap().containsKey(venderId))
            return;
        VendersEntity vendersEntity = venderInfoMap.getMaintainanceMap().get(venderId);
        if((infos.get("Maintainer") == null || infos.getString("Maintainer").length() == 0) && (vendersEntity.getName() != null && vendersEntity.getName().length() != 0)){
            infos.put("Maintainer", vendersEntity.getName());
        }
        if((infos.get("MaintainerWeb") == null || infos.getString("MaintainerWeb").length() == 0) && (vendersEntity.getWebsite() != null && vendersEntity.getWebsite().length() != 0)){
            infos.put("MaintainerWeb", vendersEntity.getWebsite());
        }
        if(vendersEntity.getContactMap().containsKey(projectId)){
            ContactEntity contactEntity = vendersEntity.getContactMap().get(projectId);
            if((infos.get("MaintainerContactor") == null || infos.getString("MaintainerContactor").length() == 0) && contactEntity.getName() != null && contactEntity.getName().length() != 0){
                infos.put("MaintainerContactor", contactEntity.getName());
            }
            if((infos.get("MaintainerPhone") == null || infos.getString("MaintainerPhone").length() == 0) && contactEntity.getPhone() != null && contactEntity.getPhone().length() != 0){
                infos.put("MaintainerPhone", contactEntity.getPhone());
            }
            if((infos.get("MaintainerEmail") == null || infos.getString("MaintainerEmail").length() == 0) && contactEntity.getEmail() != null && contactEntity.getEmail().length() != 0){
                infos.put("MaintainerEmail", contactEntity.getEmail());
            }
            if((infos.get("MaintainerFax") == null || infos.getString("MaintainerFax").length() == 0) && contactEntity.getFax() != null && contactEntity.getFax().length() != 0){
                infos.put("MaintainerFax", contactEntity.getFax());
            }
        }
    }
    // 补充保险商信息点
    private static void complementInsurance(JSONObject infos, VenderInfoMap venderInfoMap, String projectId) {
        String venderId = infos.getString("DPInsurerID");
        String warrantyNum = infos.getString("InsuranceNum");
        // 保险商信息
        complementInsuranceVenderInfo(infos, venderInfoMap, projectId, venderId);
        // 保单信息
        if(warrantyNum != null && venderInfoMap.getWarrantyMap().containsKey(warrantyNum)){
            WarrantyEntity warrantyEntity = venderInfoMap.getWarrantyMap().get(warrantyNum);
            if((infos.get("InsuranceFile") == null || infos.getString("InsuranceFile").length() == 0) && warrantyEntity.getContractFile() != null && warrantyEntity.getContractFile().length() != 0){
                infos.put("InsuranceFile", warrantyEntity.getContractFile());
            }
            // 如果信息点中只填了保单号, 没有保险商id, 则尝试找厂商id, 然后补充信息点.
            if(venderId == null || venderId.length() == 0){
                venderId = venderInfoMap.getRelWarrantyInsuranceMap().get(warrantyNum);
                complementInsuranceVenderInfo(infos, venderInfoMap, projectId, venderId);
            }
        }
    }
    // 保险商信息
    private static void complementInsuranceVenderInfo(JSONObject infos, VenderInfoMap venderInfoMap, String projectId, String venderId) {
        if(venderId != null && venderInfoMap.getInsuranceMap().containsKey(venderId)){
            VendersEntity vendersEntity = venderInfoMap.getInsuranceMap().get(venderId);
            if((infos.get("Insurer") == null || infos.getString("Insurer").length() == 0) && (vendersEntity.getName() != null && vendersEntity.getName().length() != 0)){
                infos.put("Insurer", vendersEntity.getName());
            }
            if((infos.get("InsurerWeb") == null || infos.getString("InsurerWeb").length() == 0) && (vendersEntity.getWebsite() != null && vendersEntity.getWebsite().length() != 0)){
                infos.put("InsurerWeb", vendersEntity.getWebsite());
            }
            if(vendersEntity.getContactMap().containsKey(projectId)){
                ContactEntity contactEntity = vendersEntity.getContactMap().get(projectId);
                if((infos.get("InsurerContactor") == null || infos.getString("InsurerContactor").length() == 0) && contactEntity.getName() != null && contactEntity.getName().length() != 0){
                    infos.put("InsurerContactor", contactEntity.getName());
                }
                if((infos.get("InsurerPhone") == null || infos.getString("InsurerPhone").length() == 0) && contactEntity.getPhone() != null && contactEntity.getPhone().length() != 0){
                    infos.put("InsurerPhone", contactEntity.getPhone());
                }
                if((infos.get("InsurerEmail") == null || infos.getString("InsurerEmail").length() == 0) && contactEntity.getEmail() != null && contactEntity.getEmail().length() != 0){
                    infos.put("InsurerEmail", contactEntity.getEmail());
                }
                if((infos.get("InsurerFax") == null || infos.getString("InsurerFax").length() == 0) && contactEntity.getFax() != null && contactEntity.getFax().length() != 0){
                    infos.put("InsurerFax", contactEntity.getFax());
                }
            }
        }
    }

    // 补充供应商信息点
    private static void complementSupplier(JSONObject infos, VenderInfoMap venderInfoMap, String projectId) {
        String venderId = infos.getString("DPSupplierID");
        if(venderId == null || !venderInfoMap.getSupplierMap().containsKey(venderId))
            return;
        VendersEntity vendersEntity = venderInfoMap.getSupplierMap().get(venderId);
        if((infos.get("Supplier") == null || infos.getString("Supplier").length() == 0) && (vendersEntity.getName() != null && vendersEntity.getName().length() != 0)){
            infos.put("Supplier", vendersEntity.getName());
        }
        if((infos.get("SupplierWeb") == null || infos.getString("SupplierWeb").length() == 0) && (vendersEntity.getWebsite() != null && vendersEntity.getWebsite().length() != 0)){
            infos.put("SupplierWeb", vendersEntity.getWebsite());
        }
        if(vendersEntity.getContactMap().containsKey(projectId)){
            ContactEntity contactEntity = vendersEntity.getContactMap().get(projectId);
            if((infos.get("SupplierContactor") == null || infos.getString("SupplierContactor").length() == 0) && contactEntity.getName() != null && contactEntity.getName().length() != 0){
                infos.put("SupplierContactor", contactEntity.getName());
            }
            if((infos.get("SupplierPhone") == null || infos.getString("SupplierPhone").length() == 0) && contactEntity.getPhone() != null && contactEntity.getPhone().length() != 0){
                infos.put("SupplierPhone", contactEntity.getPhone());
            }
            if((infos.get("SupplierEmail") == null || infos.getString("SupplierEmail").length() == 0) && contactEntity.getEmail() != null && contactEntity.getEmail().length() != 0){
                infos.put("SupplierEmail", contactEntity.getEmail());
            }
            if((infos.get("SupplierFax") == null || infos.getString("SupplierFax").length() == 0)&& contactEntity.getFax() != null && contactEntity.getFax().length() != 0){
                infos.put("SupplierFax", contactEntity.getFax());
            }
        }
    }
    // 补充生产商信息点
    private static void complementManufacturer(String pjId, JSONObject infos, VenderInfoMap venderInfoMap, String family) {
        String venderId = infos.getString("DPManufacturerID");
        String modelId = infos.getString("DPSpecificationID");
        // 有型号id的话, 可以补充厂商名, 品牌名, 型号名, 型号信息点
        if(modelId != null && venderInfoMap.getModelMap().containsKey(modelId)){
            ModelEntity modelEntity = venderInfoMap.getModelMap().get(modelId);
            if((infos.get("Specification") == null || infos.getString("Specification").length() == 0) && modelEntity.getName() != null && modelEntity.getName().length() != 0){
                infos.put("Specification", modelEntity.getName());
            }
            String brandId = venderInfoMap.getRelModelBrandMap().get(modelId);
            if(brandId != null && venderInfoMap.getBrandMap().containsKey(brandId)){
                BrandEntity brandEntity = venderInfoMap.getBrandMap().get(brandId);
                if((infos.get("Brand") == null || infos.getString("Brand").length() == 0) && brandEntity.getName() != null && brandEntity.getName().length() != 0){
                    infos.put("Brand", brandEntity.getName());
                }
            }
            if(venderId == null || venderId.length() == 0){
                venderId = venderInfoMap.getRelModelManufacturerMap().get(modelId);
            }
            if(modelEntity.getFamily() != null && modelEntity.getFamily().equals(family)) {
                JSONObject modelInfos = modelEntity.getInfos();
                Set<String> infoSet = InfoCodeUtil.getInfosWithGeneralInfo(pjId, family);
                for (String modelInfoCode : modelInfos.keySet()) {
                    // 如果出现不属于该种族的信息点的话, 跳过
                    if(!infoSet.contains(modelInfoCode))
                        continue;
                    Object value = modelInfos.get(modelInfoCode);
                    if (infos.get(modelInfoCode) != null && infos.getString(modelInfoCode).length() > 0)
                        continue;
                    if (value == null || value.equals(""))
                        continue;
                    infos.put(modelInfoCode, value);
                }
            }
        }

        // 对于生产商, 如果只有厂商id, 只能补充厂家名称.
        if(venderId != null && venderInfoMap.getManufacturerMap().containsKey(venderId)){
            VendersEntity vendersEntity = venderInfoMap.getManufacturerMap().get(venderId);
            if((infos.get("Manufacturer") == null || infos.getString("Manufacturer").length() == 0) && (vendersEntity.getName() != null && vendersEntity.getName().length() != 0)){
                infos.put("Manufacturer", vendersEntity.getName());
            }
        }
    }

    // 是否满足补充厂商信息点的条件
    private static boolean isIdSatisfy(String id) {
        if(id == null)
            return false;
        if(id.startsWith("Eq") || id.startsWith("Pe") || id.startsWith("Ec"))
            return true;
        return false;
    }
}
