public class lexConsumabletargetformController {
    @AuraEnabled
    public static  List<ConsumableTargetManage__c> selectAllConsumableTargetManagecopy(String fiscalYearValue){
        Integer year = Integer.valueOf(fiscalYearValue.substring(2))+1;
        String fiscalYearValue1 = 'FY' + year;
        System.debug('fiscalYearValue1'+fiscalYearValue1);
        List<ConsumableTargetManage__c> contm = [select Id,
                                                Hospital__r.Id,
                                                Hospital__r.Name,
                                                H_Salesdepartment_HP__c,
                                                StrategicDepartment__r.Id,
                                                StrategicDepartment__r.Name,
                                                Category3__c,
                                                Category4__c,
                                                lastDosageNumTotal__c,
                                                lastDosageAmountTotal__c,
                                                TargetNumber__c,
                                                CreatedDate_month__c,
                                                PlannedNewOpeningTime__c,
                                                NewConfirmation_New__c,
                                                BillLostWarning__c,
                                                ConsumableSalesState__c,
                                                SpecialistVisitsNum4__c,
                                                SpecialistVisitsNum5__c,
                                                SpecialistVisitsNum6__c,
                                                SpecialistVisitsNum7__c,
                                                SpecialistVisitsNum8__c,
                                                SpecialistVisitsNum9__c,
                                                SpecialistVisitsNum10__c,
                                                SpecialistVisitsNum11__c,
                                                SpecialistVisitsNum12__c,
                                                SpecialistVisitsNum1__c,
                                                SpecialistVisitsNum2__c,
                                                SpecialistVisitsNum3__c,
                                                SpecialistVisits_ZD__c,
                                                Reality_IMS_Num4__c,
                                                Reality_IMS_Num5__c,
                                                Reality_IMS_Num6__c,
                                                Reality_IMS_Num7__c,
                                                Reality_IMS_Num8__c,
                                                Reality_IMS_Num9__c,
                                                Reality_IMS_Num10__c,
                                                Reality_IMS_Num11__c,
                                                Reality_IMS_Num12__c,
                                                Reality_IMS_Num1__c,
                                                Reality_IMS_Num2__c,
                                                Reality_IMS_Num3__c,
                                                Reality_IMS_RMB4__c,
                                                Reality_IMS_RMB5__c,
                                                Reality_IMS_RMB6__c,
                                                Reality_IMS_RMB7__c,
                                                Reality_IMS_RMB8__c,
                                                Reality_IMS_RMB9__c,
                                                Reality_IMS_RMB10__c,
                                                Reality_IMS_RMB11__c,
                                                Reality_IMS_RMB12__c,
                                                Reality_IMS_RMB1__c,
                                                Reality_IMS_RMB2__c,
                                                Reality_IMS_RMB3__c,
                                                Actual_year_number__c,
                                                Actual_year_amount__c,
                                                Aggregate_YTD_number_g__c,
                                                Aggregate_YTD_amount_g__c,
                                                Actual_Usage_Num4__c,
                                                Actual_Usage_Num5__c,
                                                Actual_Usage_Num6__c,
                                                Actual_Usage_Num7__c,
                                                Actual_Usage_Num8__c,
                                                Actual_Usage_Num9__c,
                                                Actual_Usage_Num10__c,
                                                Actual_Usage_Num11__c,
                                                Actual_Usage_Num12__c,
                                                Actual_Usage_Num1__c,
                                                Actual_Usage_Num2__c,
                                                Actual_Usage_Num3__c,
                                                Actual_Usage_RMB4__c,
                                                Actual_Usage_RMB5__c,
                                                Actual_Usage_RMB6__c,
                                                Actual_Usage_RMB7__c,
                                                Actual_Usage_RMB8__c,
                                                Actual_Usage_RMB9__c,
                                                Actual_Usage_RMB10__c,
                                                Actual_Usage_RMB11__c,
                                                Actual_Usage_RMB12__c,
                                                Actual_Usage_RMB1__c,
                                                Actual_Usage_RMB2__c,
                                                Actual_Usage_RMB3__c,
                                                DealerActivities__c,
                                                ConsumableTargetManageKey__c,
                                                Before_AdmissionHospitale_Volve__c,
                                                Progress_AdmissionHospitale_Volve__c,
                                                After_AdmissionHospitale_Volve__c,
                                                IssuesAndPlans__c
                                                 from ConsumableTargetManage__c
                                                  where Hospital__c != null
                                                 and (OCM_Year__c =:fiscalYearValue or OCM_Year__c =:fiscalYearValue1) order by Hospital__c];
        return contm;
    }
    //检索所有符合条件的耗材目标管理数据
    @AuraEnabled
    public static  List<ConsumableTargetManage__c> selectAllConsumableTargetManage(String fiscalYearValue,String productTypeValue){
        List<ConsumableTargetManage__c> contm = [select Id,
                                                Hospital__r.Id,
                                                Hospital__r.Name,
                                                H_Salesdepartment_HP__c,
                                                StrategicDepartment__r.Id,
                                                StrategicDepartment__r.Name,
                                                Category3__c,
                                                Category4__c,
                                                lastDosageNumTotal__c,
                                                lastDosageAmountTotal__c,
                                                TargetNumber__c,
                                                CreatedDate_month__c,
                                                PlannedNewOpeningTime__c,
                                                NewConfirmation_New__c,
                                                BillLostWarning__c,
                                                ConsumableSalesState__c,
                                                SpecialistVisitsNum4__c,
                                                SpecialistVisitsNum5__c,
                                                SpecialistVisitsNum6__c,
                                                SpecialistVisitsNum7__c,
                                                SpecialistVisitsNum8__c,
                                                SpecialistVisitsNum9__c,
                                                SpecialistVisitsNum10__c,
                                                SpecialistVisitsNum11__c,
                                                SpecialistVisitsNum12__c,
                                                SpecialistVisitsNum1__c,
                                                SpecialistVisitsNum2__c,
                                                SpecialistVisitsNum3__c,
                                                SpecialistVisits_ZD__c,
                                                Reality_IMS_Num4__c,
                                                Reality_IMS_Num5__c,
                                                Reality_IMS_Num6__c,
                                                Reality_IMS_Num7__c,
                                                Reality_IMS_Num8__c,
                                                Reality_IMS_Num9__c,
                                                Reality_IMS_Num10__c,
                                                Reality_IMS_Num11__c,
                                                Reality_IMS_Num12__c,
                                                Reality_IMS_Num1__c,
                                                Reality_IMS_Num2__c,
                                                Reality_IMS_Num3__c,
                                                Reality_IMS_RMB4__c,
                                                Reality_IMS_RMB5__c,
                                                Reality_IMS_RMB6__c,
                                                Reality_IMS_RMB7__c,
                                                Reality_IMS_RMB8__c,
                                                Reality_IMS_RMB9__c,
                                                Reality_IMS_RMB10__c,
                                                Reality_IMS_RMB11__c,
                                                Reality_IMS_RMB12__c,
                                                Reality_IMS_RMB1__c,
                                                Reality_IMS_RMB2__c,
                                                Reality_IMS_RMB3__c,
                                                Actual_year_number__c,
                                                Actual_year_amount__c,
                                                Aggregate_YTD_number_g__c,
                                                Aggregate_YTD_amount_g__c,
                                                Actual_Usage_Num4__c,
                                                Actual_Usage_Num5__c,
                                                Actual_Usage_Num6__c,
                                                Actual_Usage_Num7__c,
                                                Actual_Usage_Num8__c,
                                                Actual_Usage_Num9__c,
                                                Actual_Usage_Num10__c,
                                                Actual_Usage_Num11__c,
                                                Actual_Usage_Num12__c,
                                                Actual_Usage_Num1__c,
                                                Actual_Usage_Num2__c,
                                                Actual_Usage_Num3__c,
                                                Actual_Usage_RMB4__c,
                                                Actual_Usage_RMB5__c,
                                                Actual_Usage_RMB6__c,
                                                Actual_Usage_RMB7__c,
                                                Actual_Usage_RMB8__c,
                                                Actual_Usage_RMB9__c,
                                                Actual_Usage_RMB10__c,
                                                Actual_Usage_RMB11__c,
                                                Actual_Usage_RMB12__c,
                                                Actual_Usage_RMB1__c,
                                                Actual_Usage_RMB2__c,
                                                Actual_Usage_RMB3__c,
                                                DealerActivities__c,
                                                ConsumableTargetManageKey__c,
                                                Before_AdmissionHospitale_Volve__c,
                                                Progress_AdmissionHospitale_Volve__c,
                                                After_AdmissionHospitale_Volve__c,
                                                IssuesAndPlans__c
                                                 from ConsumableTargetManage__c
                                                  where Hospital__c != null
                                                 and productType__c =:productTypeValue
                                                 and OCM_Year__c =:fiscalYearValue order by Hospital__c];
        return contm;
    }
    //检索目标ET-ASP价格
    @AuraEnabled
    public static Map<String,ET_ASP__c> selectETASP(String ETASPKeyListStr){
        Set<String> ETASPKeyset = (Set<String>)JSON.deserialize(ETASPKeyListStr,Set<String>.class);
        List<String> ETASPKeyList = new List<String>(ETASPKeyset); 
        List<ET_ASP__c> listName = [select Id,Salesdepartment__c,yearly__c,ConsumCategory3__c,ASP_year__c,ET_ASP_KEY__c from ET_ASP__c where ET_ASP_KEY__c in:ETASPKeyList];
        Map<String,ET_ASP__c> ETASPMap = new Map<String,ET_ASP__c>();
        if(listName.size()>0){
            for (ET_ASP__c ETASP : listName) {
                ETASPMap.put(ETASP.ET_ASP_KEY__c,ETASP);
            }
        }
        return ETASPMap;
    }
    //新开创建
    @AuraEnabled
    public static String SubmitApprovals(String SubmitManageApprovedata){
        Savepoint sp = Database.setSavepoint();
        try {
            List<ConsumableTargetManage__c> SubmitApproveDataList = (List<ConsumableTargetManage__c>)JSON.deserialize(SubmitManageApprovedata,List<ConsumableTargetManage__c>.class); 
            List<ConsumableTargetManage__c> SubmitDataList = new List<ConsumableTargetManage__c>();
            for (ConsumableTargetManage__c SubmitApproveData : SubmitApproveDataList) {
                ConsumableTargetManage__c saveApproveData = new ConsumableTargetManage__c();
                saveApproveData.Category3__c = SubmitApproveData.Category3__c;
                saveApproveData.Category4__c = SubmitApproveData.Category4__c;
                saveApproveData.TargetNumber__c = SubmitApproveData.TargetNumber__c;
                saveApproveData.Hospital__c = SubmitApproveData.Hospital__c;
                saveApproveData.StrategicDepartment__c = SubmitApproveData.StrategicDepartment__c;
                saveApproveData.PlannedNewOpeningTime__c = Date.valueOf(SubmitApproveData.PlannedNewOpeningTime__c);
                // saveApproveData.NewConfirmation__c = true;
                saveApproveData.OCM_Year__c = SubmitApproveData.OCM_Year__c;
                if(saveApproveData.Category3__c=='ENG耗材'){
                    saveApproveData.productType__c = '外科耗材';
                }else {
                    saveApproveData.productType__c = 'ET耗材';
                }
                saveApproveData.ConsumableTargetManageKey__c = SubmitApproveData.Hospital__c+'-'+SubmitApproveData.Category3__c+'-'+SubmitApproveData.Category4__c+'-'+SubmitApproveData.OCM_Year__c;
                SubmitDataList.add(saveApproveData);
            }
            upsert SubmitDataList ConsumableTargetManageKey__c;
            return 'success';
        }catch(DmlException de) {
            Database.rollback(sp);
            return de.getLineNumber()+'行'+de.getDmlMessage(0);
        }catch(Exception e) {
            Database.rollback(sp);
            return e.getLineNumber()+'行'+e.getMessage();
        }
    }
    //新开保存
    @AuraEnabled
    public static String saveIms(String Managesavedata,String fiscalYearValue){
        Savepoint sp = Database.setSavepoint();
        try {
            List<ConsumableData> SubmitApproveDataList = (List<ConsumableData>)JSON.deserialize(Managesavedata,List<ConsumableData>.class); 
            List<ConsumableTargetManage__c> SubmitDataList = new List<ConsumableTargetManage__c>();
            for (ConsumableData SubmitApproveData : SubmitApproveDataList) {
                ConsumableTargetManage__c saveApproveData = new ConsumableTargetManage__c();
                saveApproveData.Id = SubmitApproveData.id;
                saveApproveData.ConsumableTargetManageKey__c = SubmitApproveData.consumableTargetManageKey;
                saveApproveData.ConsumableTargetManageKey__c = SubmitApproveData.hospital+'-'+SubmitApproveData.category3+'-'+SubmitApproveData.category4+'-'+fiscalYearValue;
                // if(SubmitApproveData.actualUsageNum1 !=null){
                    saveApproveData.Actual_Usage_Num1__c = SubmitApproveData.actualUsageNum1;
                // }
                // if(SubmitApproveData.actualUsageNum2 !=null){
                    saveApproveData.Actual_Usage_Num2__c = SubmitApproveData.actualUsageNum2;
                // }
                // if(SubmitApproveData.actualUsageNum3 !=null){
                    saveApproveData.Actual_Usage_Num3__c = SubmitApproveData.actualUsageNum3;
                // }
                // if(SubmitApproveData.actualUsageNum4 !=null){
                    saveApproveData.Actual_Usage_Num4__c = SubmitApproveData.actualUsageNum4;
                // }
                // if(SubmitApproveData.actualUsageNum5 !=null){
                    saveApproveData.Actual_Usage_Num5__c = SubmitApproveData.actualUsageNum5;
                // }
                // if(SubmitApproveData.actualUsageNum6 !=null){
                    saveApproveData.Actual_Usage_Num6__c = SubmitApproveData.actualUsageNum6;
                // }
                // if(SubmitApproveData.actualUsageNum7 !=null){
                    saveApproveData.Actual_Usage_Num7__c = SubmitApproveData.actualUsageNum7;
                // }
                // if(SubmitApproveData.actualUsageNum8 !=null){
                    saveApproveData.Actual_Usage_Num8__c = SubmitApproveData.actualUsageNum8;
                // }
                // if(SubmitApproveData.actualUsageNum9 !=null){
                    saveApproveData.Actual_Usage_Num9__c = SubmitApproveData.actualUsageNum9;
                // }
                // if(SubmitApproveData.actualUsageNum10 !=null){
                    saveApproveData.Actual_Usage_Num10__c = SubmitApproveData.actualUsageNum10;
                // }
                // if(SubmitApproveData.actualUsageNum11 !=null){
                    saveApproveData.Actual_Usage_Num11__c = SubmitApproveData.actualUsageNum11;
                // }
                // if(SubmitApproveData.actualUsageNum12 !=null){
                    saveApproveData.Actual_Usage_Num12__c = SubmitApproveData.actualUsageNum12;
                // }
                saveApproveData.IssuesAndPlans__c = SubmitApproveData.issuesAndPlans;
                SubmitDataList.add(saveApproveData);
            }
            update SubmitDataList;
            return 'success';
        }catch(DmlException de) {
            Database.rollback(sp);
            return de.getLineNumber()+'行'+de.getDmlMessage(0);
        }catch(Exception e) {
            Database.rollback(sp);
            return e.getLineNumber()+'行'+e.getMessage();
        }
    }
    //根据医院检索战略科室
    @AuraEnabled
    public static List<Account> selectSDepartmentByHospital(String HospitalId){
        List<Account> StrategicDepartment = [select Id, Name from Account where ParentId =:HospitalId];
        return StrategicDepartment;
    }
    //检索省
    @AuraEnabled
    public static List<Address_Level__c> selectState_Master(){
        List<Address_Level__c> AddressLevel = [select Id, Name from Address_Level__c];
        return AddressLevel;
    }  
    //更具省检索市
    @AuraEnabled
    public static List<Address_Level2__c> selectCityMasterByStateMaste(String StateMaste){
        List<Address_Level2__c> AddressLevel2 = [select Id, Name from Address_Level2__c where Address_Level__r.Id =:StateMaste ];
        return AddressLevel2;
    }  
    //检索满足条件的战略科室（筛选器筛数据）
    @AuraEnabled
    public static  List<ConsumableTargetManage__c> selectConsumableTargetManageByCondition(String fiscalYearValue,String productTypeValue,String customerTypeValue,String salesdepartment_HPValue,String state_MasterValue,String city_MasterValue,String hospitalId1){
        String searchSql = 'select Id,Hospital__r.Id,Hospital__r.Name,H_Salesdepartment_HP__c,StrategicDepartment__r.Id,StrategicDepartment__r.Name,Category3__c,Category4__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,TargetNumber__c,CreatedDate_month__c,PlannedNewOpeningTime__c,NewConfirmation_New__c,BillLostWarning__c,ConsumableSalesState__c,'
                        + 'SpecialistVisitsNum4__c,SpecialistVisitsNum5__c,SpecialistVisitsNum6__c,SpecialistVisitsNum7__c, SpecialistVisitsNum8__c, SpecialistVisitsNum9__c, SpecialistVisitsNum10__c, SpecialistVisitsNum11__c, SpecialistVisitsNum12__c, SpecialistVisitsNum1__c, SpecialistVisitsNum2__c, SpecialistVisitsNum3__c,SpecialistVisits_ZD__c,'
                        + 'Reality_IMS_Num4__c,Reality_IMS_Num5__c,Reality_IMS_Num6__c,Reality_IMS_Num7__c,Reality_IMS_Num8__c,Reality_IMS_Num9__c,Reality_IMS_Num10__c,Reality_IMS_Num11__c,Reality_IMS_Num12__c,Reality_IMS_Num1__c,Reality_IMS_Num2__c,Reality_IMS_Num3__c,'
                        + 'Reality_IMS_RMB4__c,Reality_IMS_RMB5__c,Reality_IMS_RMB6__c,Reality_IMS_RMB7__c,Reality_IMS_RMB8__c,Reality_IMS_RMB9__c,Reality_IMS_RMB10__c,Reality_IMS_RMB11__c,Reality_IMS_RMB12__c,Reality_IMS_RMB1__c,Reality_IMS_RMB2__c,Reality_IMS_RMB3__c,'
                        + 'Actual_year_number__c,Actual_year_amount__c,Aggregate_YTD_number_g__c,Aggregate_YTD_amount_g__c,'
                        + 'Actual_Usage_Num4__c,Actual_Usage_Num5__c,Actual_Usage_Num6__c,Actual_Usage_Num7__c,Actual_Usage_Num8__c,Actual_Usage_Num9__c,Actual_Usage_Num10__c,Actual_Usage_Num11__c,Actual_Usage_Num12__c,Actual_Usage_Num1__c,Actual_Usage_Num2__c,Actual_Usage_Num3__c,'
                        + 'Actual_Usage_RMB4__c,Actual_Usage_RMB5__c,Actual_Usage_RMB6__c,Actual_Usage_RMB7__c,Actual_Usage_RMB8__c,Actual_Usage_RMB9__c,Actual_Usage_RMB10__c,Actual_Usage_RMB11__c,Actual_Usage_RMB12__c,Actual_Usage_RMB1__c,Actual_Usage_RMB2__c,Actual_Usage_RMB3__c,'
                        + 'DealerActivities__c,Before_AdmissionHospitale_Volve__c,Progress_AdmissionHospitale_Volve__c,After_AdmissionHospitale_Volve__c,ConsumableTargetManageKey__c,IssuesAndPlans__c '
                        + 'FROM ConsumableTargetManage__c where Hospital__c != null';
        String whereSql = '';
        if(productTypeValue != null && productTypeValue != ''){
            whereSql += ' and productType__c = \'' + productTypeValue + '\'';
        }
        if(fiscalYearValue != null && fiscalYearValue != ''){
            whereSql += ' and OCM_Year__c = \'' + fiscalYearValue + '\'';
        }
        if(customerTypeValue != null && customerTypeValue != ''){
            if(customerTypeValue == '新开'){
                whereSql += ' and ConsumableSalesState__c = \'新开\'';
            }else if(customerTypeValue == '存量'){
                whereSql += ' and ConsumableSalesState__c = \'存量\'';
            }
        }
        if(salesdepartment_HPValue != null && salesdepartment_HPValue != ''){
            whereSql += ' and H_Salesdepartment_HP__c = \'' + salesdepartment_HPValue + '\'';
        }
        if(state_MasterValue != null && state_MasterValue != ''){
            whereSql += ' and H_OCM_man_province_HP__c = \'' + state_MasterValue + '\'';
        }
        if(city_MasterValue != null && city_MasterValue != ''){
            whereSql += ' and H_City_Master__c = \'' + city_MasterValue + '\'';
        }
        if(hospitalId1 != null && hospitalId1 != ''){
            whereSql += ' and Hospital__r.Id = \'' + hospitalId1 + '\'';
        }
        searchSql = searchSql + whereSql;
        searchSql += ' order by Hospital__c';
        System.debug('searchSql:  '+searchSql);
        List<ConsumableTargetManage__c> contm = Database.query(searchSql);
        return contm;
    }
    //通过API获取字段label
    // @AuraEnabled
    // public static List<String> getFieldsLabel(String objstr, String fld,String fld2  ){
    //     String soqlQuery = 'SELECT  ' + fld + '.Label ' + fld2 + '.Label FROM ConsumableTargetManage__c';
    //     List<SObject> results = Database.query(soqlQuery);
    //     List<String> fieldLabel = new List<String>();
    //     for (SObject record : results) {
    //         fieldLabel.add(record.get('Label').toString());
    //     }
    //     return fieldLabel;
    // }
    //检索下拉列表值
    @AuraEnabled
    public static List<Map<String,String>> getPicklistValues(String objstr, String fld ,Boolean ifempty){
        List<Map<String,String>> options = new List<Map<String,String>>();
        Map<String,String> space = new Map<String,String>();
        if(ifempty){
            space.put('label', '--无--');
            space.put('value', '');
            options.add(space);
        }

        Schema.sObjectType objType = Schema.getGlobalDescribe().get(objstr);
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
        list<Schema.PicklistEntry> values = fieldMap.get(fld).getDescribe().getPickListValues();
        system.debug(objstr + '=' + values);
        for (Schema.PicklistEntry a : values)
        {
            if (!a.isActive()) continue;
            Map<String,String> ses = new Map<String,String>();
            ses.put('label', a.getLabel());
            ses.put('value', a.getValue());
            options.add(ses);
        }
        return options;
    }
    public class ConsumableData {
        String id;
        String consumableTargetManageKey;
        String hospital;
        String category3;
        String category4;
        String issuesAndPlans;
        Integer actualUsageNum1;
        Integer actualUsageNum2;
        Integer actualUsageNum3;
        Integer actualUsageNum4;
        Integer actualUsageNum5;
        Integer actualUsageNum6;
        Integer actualUsageNum7;
        Integer actualUsageNum8;
        Integer actualUsageNum9;
        Integer actualUsageNum10;
        Integer actualUsageNum11;
        Integer actualUsageNum12;
    }
}