/**
 * wangweipeng    2021/07/27
 * 新增修理时，默认查找到对应的直返收货地址
 * 根据修理上选中医院的 OCM_man_province_txt__c 值，去匹配直返地址的省或市
 */
public without sharing class RepairBeforeInsertHandler {

    public Map<String,String>  matchupMap;

    public RepairBeforeInsertHandler() {
        //map的键对应医院表上ocsm管理省值
        //map的值对应地址表上类型为办事处的 省+','+市
        matchupMap = new Map<String,String>();
        matchupMap.put('黑龙江','黑龙江,哈尔滨市');
        matchupMap.put('青海','青海省,西宁市');
        matchupMap.put('陕西','陕西省,西安市');
        matchupMap.put('重庆','重庆市,渝中区');
        matchupMap.put('贵州','贵州省,贵阳市');
        matchupMap.put('福建','福建省,福州市');
        matchupMap.put('甘肃','甘肃省,兰州市');
        matchupMap.put('湖南','湖南省,长沙市');
        matchupMap.put('湖北','湖北省,武汉市');
        matchupMap.put('深圳','广东省,深圳市');
        matchupMap.put('广东','广东省,广州市');
        //matchupMap.put('海南','');//没有
        matchupMap.put('浙江','浙江省,杭州市');
        matchupMap.put('河南','河南省,郑州市');
        matchupMap.put('河北','河北省,石家庄市');
        matchupMap.put('沈阳','辽宁省,沈阳市');
        matchupMap.put('大连','辽宁省,大连市');
        matchupMap.put('江西','江西省,南昌市');
        matchupMap.put('江苏','江苏省,南京市');
        matchupMap.put('新疆','新疆维吾尔自治区,乌鲁木齐市');
        matchupMap.put('广西','广西自治区,南宁市');
        matchupMap.put('山西','山西省,太原市');
        matchupMap.put('山东,济南市','山东省,济南市');
        matchupMap.put('山东,青岛市','山东省,青岛市');
        matchupMap.put('安徽','安徽省,合肥市');
        matchupMap.put('宁夏','宁夏回族自治区,银川市');
        matchupMap.put('天津','天津市,河西区');
        matchupMap.put('四川/西藏','四川省,成都市');
        matchupMap.put('吉林','吉林省,长春市');
        matchupMap.put('北京','北京市,朝阳区');
        matchupMap.put('内蒙古','内蒙古自治区,呼和浩特市');
        matchupMap.put('云南','云南省,昆明市');
        //add        wangweipeng                 2021/12/27         start
        matchupMap.put('上海','上海市,徐汇区');
        //add        wangweipeng                 2021/12/27         end
    }

    //新建修理表的时候新增直返地址信息
    public void beforeInsertValue(List<Repair__c> newList,List<Repair__c> oldList) {
        boolean falg = false;
        boolean falg2 = true;
        //存放查到的医院id
        List<String> HospitalId = new List<String>();
        List<String> provinceList = new List<String>();//省
        List<String> cityList = new List<String>();//市
        for (Repair__c nObj : newList) {
            //医院的CSM管理省(文本) 值不为空，并且收货地址不为空
            if(nObj.Hospital__c != null && nObj.address_Contacts__c == null && nObj.address_Telephone__c == null && nObj.address_Contacts_Name__c == null && nObj.address_City__c == null && nObj.Detailed_Address__c == null){
                HospitalId.add(nObj.Hospital__C);
            }
        }
        if(HospitalId != null && HospitalId.size() > 0){
            String HospitalSQl = 'SELECT id,OCM_man_province_txt__c,FieldCity_Master_Name__c from Account where id in :HospitalId';
            List<Account> accountList = Database.query(HospitalSQl);
            for (Repair__c nObj : newList) {
                if(nObj.Hospital__c != null && nObj.address_Contacts__c == null && nObj.address_Telephone__c == null && nObj.address_Contacts_Name__c == null && nObj.address_City__c == null && nObj.Detailed_Address__c == null){
                    for(Account ac : accountList){
                        if(nObj.Hospital__c == ac.id){
                            String provinceAndCity = '';
                            if('山东' == ac.OCM_man_province_txt__c){
                                if('烟台市' == ac.FieldCity_Master_Name__c || '威海市' == ac.FieldCity_Master_Name__c || '日照市' == ac.FieldCity_Master_Name__c 
                                    || '青岛市' == ac.FieldCity_Master_Name__c || '潍坊市' == ac.FieldCity_Master_Name__c){
                                    provinceAndCity = matchupMap.get('山东,青岛市');
                                }else{
                                    provinceAndCity = matchupMap.get('山东,济南市');
                                }
                            }else{
                                provinceAndCity = matchupMap.get(ac.OCM_man_province_txt__c);
                            }
                             if(provinceAndCity != null){
                                falg = true;
                                provinceList.add(provinceAndCity.split(',')[0]);
                                cityList.add(provinceAndCity.split(',')[1]);
                            }
                        }
                    }
                }
            }
            if(falg && provinceList != null && cityList != null && provinceList.size() > 0 && cityList.size() > 0){
                String addressSQl = 'SELECT ID,Customer__c,Customer_Name__c,Contacts__c,Contacts_Name__c,Telephone__c,Province__c,Province_Name__c,City__c,City_Name__c,Detailed_Address__c,ZipCode__c'
                    			+ ',Contacts__r.LastName_Encrypted__c,ZipCode_Encrypted__c,Detailed_Address_Encrypted__c,Telephone_Encrypted__c' // PI改造 By Bright 20220407
                                + ' from Address__c  '
                                + ' where Address_Classification__c =  \'办事处\' and Province_Name__c in :provinceList and City_Name__c in :cityList '
                                + ' order by Using_Datetime__c desc NULLS LAST ';
				system.debug('addressSQl='+addressSQl);
                List<Address__c> addressList = Database.query(addressSQl);
                
                if(addressList != null && addressList.size() > 0){
                    system.debug(addressList[0]);
                    for (Repair__c nObj : newList) {
                        if(nObj.Hospital__c != null && nObj.address_Contacts__c == null && nObj.address_Telephone__c == null && nObj.address_Contacts_Name__c == null && nObj.address_City__c == null && nObj.Detailed_Address__c == null){
                            //由于为了减少select ,所以获取我们配置好的map，根据保有设备上医院的 OCSM管理省(文本) 的值，获取对应的地址上的省和市
                            for(Account ac : accountList){
                                if(nObj.Hospital__c == ac.id){
                                    String provinceAndCity = '';
                                    matchupMap.get(ac.OCM_man_province_txt__c);
                                    if('山东' == ac.OCM_man_province_txt__c){
                                        if('烟台市' == ac.FieldCity_Master_Name__c || '威海市' == ac.FieldCity_Master_Name__c || '日照市' == ac.FieldCity_Master_Name__c 
                                            || '青岛市' == ac.FieldCity_Master_Name__c || '潍坊市' == ac.FieldCity_Master_Name__c){
                                            provinceAndCity = matchupMap.get('山东,青岛市');
                                        }else{
                                            provinceAndCity = matchupMap.get('山东,济南市');
                                        }
                                    }else{
                                        provinceAndCity = matchupMap.get(ac.OCM_man_province_txt__c);
                                    }
                                    if(provinceAndCity != null){//不能为空
                                        //地址表上省和市不能为空
                                        if(addressList[0].Province_Name__c != null && addressList[0].City_Name__c!= null){
                                            //拼接一个字符串方便对比
                                            String pAc = addressList[0].Province_Name__c + ',' + addressList[0].City_Name__c;
                                            if(provinceAndCity.equals(pAc)){
                                                //联系人
                                                String contactsName = '';
                                                String contactsNameEncrypt = '';// 20220407 PI改造 By Bright
                                                if(!String.isBlank(addressList[0].Contacts__c)){
                                                    contactsName = addressList[0].Contacts_Name__c;
                                                    contactsNameEncrypt = addressList[0].Contacts__r.LastName_Encrypted__c;// 20220407 PI改造 By Bright
                                                }
                                                //省+市
                                                String address = addressList[0].Detailed_Address__c.trim();
                                                String addressEncrypt = addressList[0].Detailed_Address_Encrypted__c;
                                                String cityName = addressList[0].City_Name__c.trim();
                                                String ProvinceCity = addressList[0].Province_Name__c.trim()+cityName;
                                                //防止详细地址里面带着省份和市
                                                if(!address.contains(ProvinceCity)){
                                                    address = ProvinceCity + address;
                                                }
                                                //联系人
                                                String ContactPerson = '';
                                                if(!String.isBlank(addressList[0].Customer__c)){
                                                    ContactPerson = addressList[0].Customer_Name__c;
                                                }
                                                nObj.address_Contacts__c=contactsName;
                                                nObj.address_Contacts_Encrypt__c=contactsNameEncrypt;// 20220407 PI改造 By Bright
                                                nObj.address_ZipCode__c = addressList[0].ZipCode__c;
                                                nObj.address_ZipCode_Encrypt__c = addressList[0].ZipCode_Encrypted__c;// 20220407 PI改造 By Bright
                                                nObj.address_City__c = cityName;
                                                nObj.address_Contacts_Name__c = ContactPerson;
                                                nObj.address_Telephone__c=addressList[0].Telephone__c;
                                                nObj.address_Telephone_Encrypt__c=addressList[0].Telephone_Encrypted__c;// 20220407 PI改造 By Bright
                                                nObj.Detailed_Address__c=address;
                                                nObj.Detailed_Address_Encrypt__c=addressEncrypt;// 20220407 PI改造 By Bright
                                                nObj.Encrypt_Update_Flag__c=true;// 20220411 PI改造 By Bright
                                                system.debug('nObj assign and Encrypt_Update_Flag__c set true');
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}