/**
|
* 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>();//市
|
List<String> inchargeStaff = 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);
|
}
|
inchargeStaff.add(nObj.Incharge_Staff__c);
|
}
|
// gzw DB202212270703 20230301 备品地址导入改造 start
|
Map<String,user> profileAndRoleMap = new Map<String,user>();
|
for(user re :[select id,Profile.name,Branch__c,UserRole.name from user where id in :inchargeStaff]){
|
profileAndRoleMap.put(re.id, re);
|
}
|
// gzw DB202212270703 20230301 备品地址导入改造 start
|
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 = '';
|
//DB202303246427 LY 20230329 start
|
// 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('山东,济南市');
|
// }
|
if('青岛' == ac.OCM_man_province_txt__c){
|
provinceAndCity = matchupMap.get('山东,青岛市');
|
}else if ('山东' == ac.OCM_man_province_txt__c) {
|
provinceAndCity = matchupMap.get('山东,济南市');
|
//DB202303246427 LY 20230329 end
|
}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]);
|
}
|
}
|
}
|
}
|
}
|
system.debug('falg='+falg);
|
system.debug('cityList='+cityList);
|
system.debug('provinceList='+provinceList);
|
if(falg && provinceList != null && cityList != null && provinceList.size() > 0 && cityList.size() > 0){
|
// gzw DB202212270703 20230301 备品地址导入改造 start
|
// 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 zhj MEBG新方案改造 2022-11-29
|
// + ' 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 ';
|
|
|
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 '
|
+ ' ,Address_Classification__c,Beipin_Center__c '
|
//+ ',Contacts__r.LastName_Encrypted__c,ZipCode_Encrypted__c,Detailed_Address_Encrypted__c,Telephone_Encrypted__c' // PI改造 By Bright 20220407 zhj MEBG新方案改造 2022-11-29
|
+ ' from Address__c '
|
+ ' where ( Address_Classification__c = \'办事处\' and Province_Name__c in :provinceList and City_Name__c in :cityList )'
|
+ ' or Address_Classification__c = \'备品\''
|
+ ' order by Using_Datetime__c desc NULLS LAST ';
|
|
// gzw DB202212270703 20230301 备品地址导入改造 end
|
system.debug('addressSQl='+addressSQl);
|
List<Address__c> addressList = new List<Address__c>();
|
Map<String,Address__c> beipinMap = new Map<String,Address__c>();
|
List<Address__c> addressTempList = Database.query(addressSQl);
|
for (Address__c ad :addressTempList) {
|
if (ad.Address_Classification__c == '备品') {
|
beipinMap.put(ad.Beipin_Center__c,ad);
|
}else{
|
addressList.add(ad);
|
}
|
}
|
//List<Address__c> addressList = Database.query(addressSQl);
|
|
System.debug('进入备品+++++addressTempList ' + addressTempList.size());
|
// if(addressList != null && addressList.size() > 0){
|
if(addressTempList != null && addressTempList.size() > 0){
|
for (Repair__c nObj : newList) {
|
System.debug('进入备品+++++ ');
|
if (nObj.Returns_Product_way__c == '备品中心') {
|
user re = profileAndRoleMap.get(nObj.Incharge_Staff__c);
|
if (re == null) return;
|
String beipCenter = '';
|
if (re.Profile.name.startsWith('2B3')) {
|
beipCenter = '北京备品中心';
|
}else if (re.Profile.name.startsWith('2B2')) {
|
if (re.Branch__c == '北京') {
|
beipCenter = '北京备品中心';
|
}else if (re.Branch__c == '上海') {
|
beipCenter = '华东备品中心';
|
}else if (re.Branch__c == '广州') {
|
beipCenter = '广州备品中心';
|
}
|
}else if (re.UserRole.name == 'CTEC教育本部') {
|
if (re.Branch__c == '北京') {
|
beipCenter = '北京C-TEC';
|
}else if (re.Branch__c == '上海') {
|
beipCenter = '上海C-TEC';
|
}else if (re.Branch__c == '广州') {
|
beipCenter = '广州C-TEC';
|
}
|
}
|
System.debug('进入备品+++++ beipCenter' + beipCenter);
|
|
if(String.isNotBlank(beipCenter)){
|
Address__c addressbeip = beipinMap.get(beipCenter);
|
System.debug('进入备品+++++ addressbeip' + addressbeip.id);
|
//联系人
|
String contactsName = '';
|
String contactsNameEncrypt = '';
|
if(!String.isBlank(addressbeip.Contacts__c)){
|
contactsName = addressbeip.Contacts_Name__c;
|
}
|
//省+市
|
String address = addressbeip.Detailed_Address__c.trim();
|
String cityName = addressbeip.City_Name__c.trim();
|
String ProvinceCity = addressbeip.Province_Name__c.trim()+cityName;
|
//防止详细地址里面带着省份和市
|
if(!address.contains(ProvinceCity)){
|
address = ProvinceCity + address;
|
}
|
//联系人
|
String ContactPerson = '';
|
if(!String.isBlank(addressbeip.Customer__c)){
|
ContactPerson = addressbeip.Customer_Name__c;
|
}
|
nObj.address_Contacts__c=contactsName;
|
nObj.address_ZipCode__c = addressbeip.ZipCode__c;
|
nObj.address_City__c = cityName;
|
nObj.address_Contacts_Name__c = ContactPerson;
|
nObj.address_Telephone__c=addressbeip.Telephone__c;
|
nObj.Detailed_Address__c=address;
|
nObj.Encrypt_Update_Flag__c=true;
|
system.debug('nObj assign and Encrypt_Update_Flag__c set true');
|
|
}
|
}else{
|
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);
|
//DB202303246427 LY 20230329 start
|
// 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('山东,济南市');
|
// }
|
if ('青岛' == ac.OCM_man_province_txt__c) {
|
provinceAndCity = matchupMap.get('山东,青岛市');
|
}else if ('山东' == ac.OCM_man_province_txt__c) {
|
provinceAndCity = matchupMap.get('山东,济南市');
|
//DB202303246427 LY 20230329 end
|
}else{
|
provinceAndCity = matchupMap.get(ac.OCM_man_province_txt__c);
|
}
|
system.debug('provinceAndCity='+provinceAndCity);
|
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 zhj MEBG新方案改造 2022-11-29
|
}
|
//省+市
|
String address = addressList[0].Detailed_Address__c.trim();
|
//String addressEncrypt = addressList[0].Detailed_Address_Encrypted__c; zhj MEBG新方案改造 2022-11-29
|
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 zhj MEBG新方案改造 2022-11-29
|
nObj.address_ZipCode__c = addressList[0].ZipCode__c;
|
//nObj.address_ZipCode_Encrypt__c = addressList[0].ZipCode_Encrypted__c;// 20220407 PI改造 By Bright zhj MEBG新方案改造 2022-11-29
|
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 zhj MEBG新方案改造 2022-11-29
|
nObj.Detailed_Address__c=address;
|
//nObj.Detailed_Address_Encrypt__c=addressEncrypt;// 20220407 PI改造 By Bright zhj MEBG新方案改造 2022-11-29
|
nObj.Encrypt_Update_Flag__c=true;// 20220411 PI改造 By Bright
|
system.debug('nObj assign and Encrypt_Update_Flag__c set true');
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
}
|
}
|
}
|
}
|
}
|
}
|