public with sharing class CreateTargetCmp {
    public CreateTargetCmp() {
        
    }
/*
    @AuraEnabled
    public static List<Agency_Hospital_Link__c> getHospitalList(String hospital_name) {
        hospital_name = '%' + hospital_name.trim() + '%'; 
        return [select Hospital_Name_readonly__c, Id, Hospital__c from Agency_Hospital_Link__c where Hospital_Name_readonly__c like :hospital_name];
    }

	@AuraEnabled
    public static Agency_Opportunity__c getEmptyOpp() {
        return [select Id, Agency_Hospital__c,Agency_Hospital__r.Name from Agency_Opportunity__c where Agency_Hospital__c <> null limit 1][0];
    }
*/

    @RemoteAction
    @AuraEnabled
    public static ProductTypes__c getProduct(String id){
        return [select Department_Cateogy__c, OPD_Flg__c, Id, SIS_Flg__c from ProductTypes__c where Id =:id];
    }

    @RemoteAction
    @AuraEnabled
    public static Map<String,Object> getProductList(){
        List<ProductTypes__c> ptList = [select Id, Name, Department_Cateogy__c, OPD_Flg__c, SIS_Flg__c from ProductTypes__c where DeleteFlg__c = false];
        Map<String,Object> resMap = new Map<String,Object>();
        for (ProductTypes__c pt : ptList) {
            Map<String,String> ptMap = new Map<String,String>();
            ptMap.put('label', pt.Name);
            ptMap.put('value', pt.Id);
            if (String.isBlank(pt.Department_Cateogy__c)) continue;

            String[] dcList = pt.Department_Cateogy__c.split(';');

            for (String dc : dcList) {
                List<Map<String,String>> pts;
                if (resMap.containsKey(dc)) {
                    pts = (List<Map<String,String>>)resMap.get(dc);
                } else {
                    pts = new List<Map<String,String>>();
                    Map<String,String> blank = new Map<String,String>();
                    blank.put('label', '');
                    blank.put('value', '');
                    pts.add(blank);
                }
                pts.add(ptMap);
                resMap.put(dc, pts);
            }
            
        }
        return resMap;
    }

    @AuraEnabled
    public static Map<String, String> getHospital(String recordId) {
        List<Agency_Opportunity__c> aoList = [select Id, Agency_Hospital_Target__c, Agency_Hospital_Target__r.Name from Agency_Opportunity__c where Id = :recordId];
        if (aoList.size() > 0) {
            Map<String,String> res = new Map<String, String>();
            res.put('Id', aoList[0].Agency_Hospital_Target__c);
            res.put('Name', aoList[0].Agency_Hospital_Target__r.Name);
            return res;
        } else {
            throw new AuraHandledException('找不到经销商医院');
        }    
    }

    @AuraEnabled
    public static Map<String,Object> getDeptAmount(String hospitalId) {

        Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
        //Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('目标').getRecordTypeId();

        List<Agency_Opportunity__c> accList2 = [select Id, Department_Cateogy__c, Product_Category__c, Product_Category__r.Name,Aim_Price__c 
                                                    from Agency_Opportunity__c 
                                                    where Agency_Hospital_Target__c = :hospitalId and RecordTypeId = :rtId and Is_OPDSIS_Target__c != true];
        //Agency_Hospital_Link__c ahl = [select Id, Hospital__c, Name, TargetHospital__c from Agency_Hospital_Link__c where Id = :hospitalId];
        //deptAmount.put('target', String.valueOf(ahl.TargetHospital__c));
        Map<String,Object> deptAmount = new Map<String, Object>();
        
        List<Agency_Opportunity__c> aoppOTH = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppBF = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGYN = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGS = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGI = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppURO = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppENT = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppET = new List<Agency_Opportunity__c>();
        for (Agency_Opportunity__c acc2 : accList2) {
            if (acc2.Department_Cateogy__c == 'OTH') {
                aoppOTH.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'BF') {
    			aoppBF.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'GYN') {
    			aoppGYN.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'GS') {
    			aoppGS.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'URO') {
    			aoppURO.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'GI') {
    			aoppGI.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'ENT') {
    			aoppENT.add(acc2);
    		} else if (acc2.Department_Cateogy__c == 'ET') {
    			aoppET.add(acc2);
    		}
        }
        deptAmount.put('OTH', aoppOTH);
        deptAmount.put('BF', aoppBF);
        deptAmount.put('GYN', aoppGYN);
        deptAmount.put('GS', aoppGS);
        deptAmount.put('URO', aoppURO);
        deptAmount.put('GI', aoppGI);
        deptAmount.put('ENT', aoppENT);
        deptAmount.put('ET', aoppET);
        return deptAmount;
    }
    

    @AuraEnabled
    public static string createSFTarget(String hospitalId, Boolean targetHospital, String[] GIProduct, String[] ETProduct, String[] BFProduct, String[] OTHProduct, String[] GSProduct, String[] UROProduct, String[] GYNProduct, String[] ENTProduct, String OCMTerm) {
        System.debug('createSFTarget');
        string returnContent = '';
        if (String.isEmpty(hospitalId)) {
        	throw new AuraHandledException('医院不存在');
        } else {
        	//if (!isNumericOrDecimal(GIAmount) ||!isNumericOrDecimal(ETAmount) ||!isNumericOrDecimal(BFAmount) ||!isNumericOrDecimal(OTHAmount) ||!isNumericOrDecimal(GSAmount) ||!isNumericOrDecimal(UROAmount) ||!isNumericOrDecimal(GYNAmount) ||!isNumericOrDecimal(ENTAmount)) {
        	//	throw new AuraHandledException('请输入正确数据');
        	//}
        	String[] deptTypes = new String[] {'Department_Class_OTH', 'Department_Class_BF', 'Department_Class_GYN', 'Department_Class_GS', 'Department_Class_URO', 'Department_Class_GI', 'Department_Class_ENT', 'Department_Class_ET'};
        	List<RecordType> deptRects = LightningUtil.getAccRecordType(deptTypes);
        	Map<String, String> deptRectMap = new Map<String, String>();
	        for (RecordType rect : deptRects) {
	            deptRectMap.put(rect.Id, rect.DeveloperName);
	        }

	        Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;

	        Set<String> recordIds = deptRectMap.keySet();
	        Agency_Hospital_Link__c ahl = [select Id, Hospital__c, Name from Agency_Hospital_Link__c where Id = :hospitalId];
        	//if (targetHospital == null) targetHospital = false;
         //   ahl.TargetHospital__c = targetHospital;
        	//update ahl;

        	List<Account> accList = LightningUtil.getDeptListByRecordType(ahl.Hospital__c, recordIds);
        	List<Agency_Opportunity__c> upsertList = new List<Agency_Opportunity__c>();

            List<ProductTypes__c> productList = [select Id, Name, OPD_Flg__c, SIS_Flg__c, Name2__c from ProductTypes__c where DeleteFlg__c = false];
            Map<ID, ProductTypes__c> productMap = new Map<ID, ProductTypes__c>( productList );
            //Map<String, Agency_Opportunity__c> upsertMap = new Map<String, Agency_Opportunity__c>();
            //for (ProductTypes__c product : productList) {
            //    if (product.OPD_Flg__c) {
            //        Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, null, rtId, OCMTerm, hospitalId, ahl.Name+' OPD');
            //        aopp.Product_Category__c = product.Id;
            //        aopp.TargetInputKey__c = hospitalId+':OPD:'+OCMTerm+':'+product.Name2__c;
            //        upsertMap.put(aopp.TargetInputKey__c, aopp);
            //        //upsertList.add(aopp);
            //    }
            //    if (product.SIS_Flg__c) {
            //        Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, null, rtId, OCMTerm, hospitalId, ahl.Name+' SIS');
            //        aopp.Product_Category__c = product.Id;
            //        aopp.TargetInputKey__c = hospitalId+':SIS:'+OCMTerm+':'+product.Name2__c;
            //        upsertMap.put(aopp.TargetInputKey__c, aopp);
            //        //upsertList.add(aopp);
            //    }
            //}
            //upsertList.addAll(upsertMap.values());

        	for (Account acc : accList) {
        		String recordType = deptRectMap.get(String.valueOf(acc.RecordTypeId));
       // 		Agency_Opportunity__c aopp = new Agency_Opportunity__c();
       // 		aopp.Hospital_Target__c = ahl.Hospital__c;
       // 		aopp.Department_Class__c = acc.Id;
    			//aopp.RecordTypeId = rtId;
    			//aopp.OCM_Term__c = OCMTerm;
    			//aopp.TargetInputKey__c = hospitalId+':'+acc.Id+':'+aopp.OCM_Term__c;
    			//aopp.Agency_Hospital_Target__c = hospitalId;
        		String pc1 = '';
                String pc2 = '';
                String pc3 = '';
                
                Integer pc4_1 = 0;
                Integer pc4_2 = 0;
                Integer pc4_3 = 0;
                String dc = '';
                returnContent += ' = 0';
                if (recordType == 'Department_Class_OTH') {
        			//aopp.Department_Cateogy__c = 'OTH';
                    dc = 'OTH';
                    pc1 = OTHProduct[0];
                    pc2 = OTHProduct[1];
                    pc3 = OTHProduct[2];

                    pc4_1 = Integer.valueOf(OTHProduct[3]);
                    pc4_2 = Integer.valueOf(OTHProduct[4]);
                    pc4_3 = Integer.valueOf(OTHProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(OTHAmount);
        		} else if (recordType == 'Department_Class_BF') {
        			dc = 'BF';
        			//aopp.Amount__c = Decimal.valueOf(BFAmount);
        		    pc1 = BFProduct[0];
                    pc2 = BFProduct[1];
                    pc3 = BFProduct[2];

                    pc4_1 = Integer.valueOf(BFProduct[3]);
                    pc4_2 = Integer.valueOf(BFProduct[4]);
                    pc4_3 = Integer.valueOf(BFProduct[5]);
                } else if (recordType == 'Department_Class_GYN') {
        			dc = 'GYN';
                    pc1 = GYNProduct[0];
                    pc2 = GYNProduct[1];
                    pc3 = GYNProduct[2];
                    pc4_1 = Integer.valueOf(GYNProduct[3]);
                    pc4_2 = Integer.valueOf(GYNProduct[4]);
                    pc4_3 = Integer.valueOf(GYNProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(GYNAmount);
        		} else if (recordType == 'Department_Class_GS') {
        			dc = 'GS';
                    pc1 = GSProduct[0];
                    pc2 = GSProduct[1];
                    pc3 = GSProduct[2];

                    pc4_1 = Integer.valueOf(GSProduct[3]);
                    pc4_2 = Integer.valueOf(GSProduct[4]);
                    pc4_3 = Integer.valueOf(GSProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(GSAmount);
        		} else if (recordType == 'Department_Class_URO') {
        			dc = 'URO';
                    pc1 = UROProduct[0];
                    pc2 = UROProduct[1];
                    pc3 = UROProduct[2];

                    pc4_1 = Integer.valueOf(UROProduct[3]);
                    pc4_2 = Integer.valueOf(UROProduct[4]);
                    pc4_3 = Integer.valueOf(UROProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(UROAmount);
        		} else if (recordType == 'Department_Class_GI') {
        			dc = 'GI';
                    pc1 = GIProduct[0];
                    pc2 = GIProduct[1];
                    pc3 = GIProduct[2];

                    pc4_1 = Integer.valueOf(GIProduct[3]);
                    pc4_2 = Integer.valueOf(GIProduct[4]);
                    pc4_3 = Integer.valueOf(GIProduct[5]);
                    returnContent += '  pc4_2 ='+pc4_2 + ' pc4_3 ='+pc4_3;
        			//aopp.Amount__c = Decimal.valueOf(GIAmount);
        		} else if (recordType == 'Department_Class_ENT') {
        			dc = 'ENT';
                    pc1 = ENTProduct[0];
                    pc2 = ENTProduct[1];
                    pc3 = ENTProduct[2];

                    pc4_1 = Integer.valueOf(ENTProduct[3]);
                    pc4_2 = Integer.valueOf(ENTProduct[4]);
                    pc4_3 = Integer.valueOf(ENTProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(ENTAmount);
        		} else if (recordType == 'Department_Class_ET') {
        			dc = 'ET';
                    pc1 = ETProduct[0];
                    pc2 = ETProduct[1];
                    pc3 = ETProduct[2];

                    pc4_1 = Integer.valueOf(ETProduct[3]);
                    pc4_2 = Integer.valueOf(ETProduct[4]);
                    pc4_3 = Integer.valueOf(ETProduct[5]);
        			//aopp.Amount__c = Decimal.valueOf(ETAmount);
        		}
                //if (String.isBlank(aopp.Product_Category1__c)) {
                //    if (String.isBlank(aopp.Product_Category2__c)) {
                //        aopp.Product_Category1__c = aopp.Product_Category3__c;
                //    } else {
                //        aopp.Product_Category1__c = aopp.Product_Category2__c;
                //        aopp.Product_Category2__c = aopp.Product_Category3__c;
                //    }
                //    aopp.Product_Category3__c = null; 
                //}
        		//aopp.Name = '目标::'+aopp.OCM_Term__c+':'+acc.Name;
                if (String.isNotBlank(pc1)) {
                    ProductTypes__c pt = productMap.get(pc1);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc1;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_1 != 0) {
                        aopp.Aim_Price__c = pc4_1;
                    }
                    upsertList.add(aopp);

                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc1;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_1 != 0) {
                            aopp_opd.Aim_Price__c = pc4_1;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc1;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_1 != 0) {
                            aopp_sis.Aim_Price__c = pc4_1;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
                if (String.isNotBlank(pc2)) {
                    ProductTypes__c pt = productMap.get(pc2);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc2;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_2 != 0) {
                        aopp.Aim_Price__c = pc4_2;
                    }
                    upsertList.add(aopp);

                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc2;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_2 != 0) {
                            aopp_opd.Aim_Price__c = pc4_2;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc2;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_2 != 0) {
                            aopp_sis.Aim_Price__c = pc4_2;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
                if (String.isNotBlank(pc3)) {
                    ProductTypes__c pt = productMap.get(pc3);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc3;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_3 != 0) {
                        aopp.Aim_Price__c = pc4_3;
                    }
                    upsertList.add(aopp);

                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc3;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_3 != 0) {
                            aopp_opd.Aim_Price__c = pc4_3;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc3;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_3 != 0) {
                            aopp_sis.Aim_Price__c = pc4_3;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
        		//upsertList.add(aopp);
        	}
            Datetime nowDate = Datetime.now();
            if (upsertList.size() > 0) {
                LightningUtil.upsertAgencyOpportunity(upsertList);
            }

            List<Agency_Opportunity__c> delList = [select Id from Agency_Opportunity__c where RecordTypeId = :rtId and Agency_Hospital_Target__c = :hospitalId and LastModifiedDate < :nowDate];
            if (delList.size() > 0) {
                delete delList;
            }
        }
        return returnContent;
    }

    private static Agency_Opportunity__c setAgencyOpp(String hospitalTarget, String departmentClass, String rtId, String OCMTerm, String hospitalId, String accName){
        Agency_Opportunity__c aopp = new Agency_Opportunity__c();
        aopp.Hospital_Target__c = hospitalTarget;
        aopp.Department_Class__c = departmentClass;
        aopp.RecordTypeId = rtId;
        aopp.OCM_Term__c = OCMTerm;
        aopp.Agency_Hospital_Target__c = hospitalId;
        aopp.Name = '目标::'+OCMTerm+':'+accName;
        return aopp;
    }
/*
    private static Boolean isNumericOrDecimal(String s){
        Boolean ReturnValue;
        try{
            Decimal.valueOf(s);
            ReturnValue = TRUE; 
        } catch (Exception e) {
            ReturnValue = FALSE;
        }
        return ReturnValue;
    }
*/
}