public without sharing class ServiceSummaryHandler {
    public static String recordtypeId = '';
    public static void CreateCase(List<ServiceSummary__c> newList, Map<Id, ServiceSummary__c> newMap, List<ServiceSummary__c> oldList, Map<Id, ServiceSummary__c> oldMap) {
        recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Case' and DeveloperName = 'Draft'].id;
        List<Case> upsertCaseList = new List<Case>();
        for (ServiceSummary__c newServiceSummary : newList) {
            ServiceSummary__c oldServiceSummary = oldMap == null ? null : oldMap.get(newServiceSummary.Id);
            if (oldServiceSummary != null) {
                if (newServiceSummary.IndividualCase__c != oldServiceSummary.IndividualCase__c && newServiceSummary.IndividualCase__c) {
                    upsertCaseList.add(CaseDate(newServiceSummary));
                }
            } else  if (newServiceSummary.IndividualCase__c) {
                upsertCaseList.add(CaseDate(newServiceSummary));
            }
            
        }

        if (upsertCaseList.size() > 0 ) {
            upsert upsertCaseList;
        }

    }


    public static Case CaseDate(ServiceSummary__c serviceSummary) {

        Case newCase = new Case();
        newCase.Subject = serviceSummary.ServiceSubject__c; //主题 
        newCase.Description = serviceSummary.ServiceDescription__c; //描述
        newCase.AccountId = serviceSummary.ServiceAccount__c; //服务客户
        newCase.Reason = serviceSummary.ServiceReason__c; //个案原因
        newCase.Origin = serviceSummary.ServiceOrigin__c; //个案来源
        newCase.Priority = serviceSummary.ServicePriority__c; //优先级
        newCase.CreatedDate = Datetime.now(); //开始日期/时间
        newCase.CaseServiceSummary__c = serviceSummary.Id;
        newCase.Status = 'New';
        newCase.ApprovalStatus__c = '草案中';
        newCase.recordtypeId = recordtypeId;

        return newCase;
    }


}