19626
2023-09-09 e14d6d0619330cad423f06493e3aa2371faa2a8f
force-app/main/default/classes/Add_Report.cls
@@ -1,409 +1,411 @@
global class Add_Report {
   // TODO reportのidで検索じゃなく、event_id__cで検索
   WebService static String addReportPr(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4,String visitor5, String description, String reportDate){
      String rt = selectRecordType(recordType);
      List<Account> aList = selectAccount(aId);
  // TODO reportのidで検索じゃなく、event_id__cで検索
  WebService static String addReportPr(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4,String visitor5, String description, String reportDate){
    String rt = selectRecordType(recordType);
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
         if (rList.size() > 0) {
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
      if (rList.size() > 0) {
        return rList.get(0).id;
      }
    }
      Report__c r = new Report__c();
    Report__c r = new Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Department__c = a.Id;
         r.Department_Class_Ref__c = a.Department_Class__r.Id;
         r.Hospital_Reference__c = a.Hospital__r.Id;
      } else {
         r.Manual_Name__c = aId;
      }
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Department__c = a.Id;
      r.Department_Class_Ref__c = a.Department_Class__r.Id;
      r.Hospital_Reference__c = a.Hospital__r.Id;
    } else {
      r.Manual_Name__c = aId;
    }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.RecordTypeId = rt;
      r.Event_Id__c = eventId;
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
      if(visitor1 != null && visitor1 != ''){
         r.Practitioner1__c = visitor1;
      }
      if(visitor2 != null && visitor2 != ''){
         r.Practitioner2__c = visitor2;
      }
      if(visitor3 != null && visitor3 != ''){
         r.Practitioner3__c = visitor3;
      }
      if(visitor4 != null && visitor4 != ''){
         r.Practitioner4__c = visitor4;
      }
      if(visitor5 != null && visitor5 != ''){
         r.Practitioner5__c = visitor5;
      }
      r.Comment__c = description;
      upsert r;
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.RecordTypeId = rt;
    r.Event_Id__c = eventId;
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
    if(visitor1 != null && visitor1 != ''){
      r.Practitioner1__c = visitor1;
    }
    if(visitor2 != null && visitor2 != ''){
      r.Practitioner2__c = visitor2;
    }
    if(visitor3 != null && visitor3 != ''){
      r.Practitioner3__c = visitor3;
    }
    if(visitor4 != null && visitor4 != ''){
      r.Practitioner4__c = visitor4;
    }
    if(visitor5 != null && visitor5 != ''){
      r.Practitioner5__c = visitor5;
    }
    r.Comment__c = description;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
   WebService static String addReportOP(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate){
      return addReportOPWithEvaluationPDF(repOwnerId, reportId, dailyReportId, eventId, recordType, aId, visitor1, visitor2, visitor3, visitor4, visitor5, opp1, opp2, opp3, opp4, opp5, reportDate, '', '', '', '');
   }
   WebService static String addReportOP2(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate, String sTime, String eTime){
      return addReportOPWithEvaluationPDF(repOwnerId, reportId, dailyReportId, eventId, recordType, aId, visitor1, visitor2, visitor3, visitor4, visitor5, opp1, opp2, opp3, opp4, opp5, reportDate, '', '', sTime, eTime);
   }
   @AuraEnabled
   WebService static String addReportOPWithEvaluationPDF(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate, String evaluationPDFNumber, String pro1, String sTime, String eTime) {
      String rt = selectRecordType(recordType);
      List<Account> aList = selectAccount(aId);
  WebService static String addReportOP(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate){
    return addReportOPWithEvaluationPDF(repOwnerId, reportId, dailyReportId, eventId, recordType, aId, visitor1, visitor2, visitor3, visitor4, visitor5, opp1, opp2, opp3, opp4, opp5, reportDate, '', '', '', '');
  }
  WebService static String addReportOP2(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate, String sTime, String eTime){
    return addReportOPWithEvaluationPDF(repOwnerId, reportId, dailyReportId, eventId, recordType, aId, visitor1, visitor2, visitor3, visitor4, visitor5, opp1, opp2, opp3, opp4, opp5, reportDate, '', '', sTime, eTime);
  }
  @AuraEnabled
  WebService static String addReportOPWithEvaluationPDF(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String opp1, String opp2, String opp3, String opp4, String opp5, String reportDate, String evaluationPDFNumber, String pro1, String sTime, String eTime) {
    String rt = selectRecordType(recordType);
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Opportunity1__c, Opportunity2__c, Opportunity3__c, Opportunity4__c, Opportunity5__c from Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      //康康p课题需要修改字段类型 后续跟进(Opportunity5__c变为文本类型字段)
    //   List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Opportunity1__c, Opportunity2__c, Opportunity3__c, Opportunity4__c, Opportunity5__c from Report__c where id =:reportId];
    //   if(rList.size() > 0){
    //     return rList.get(0).id;
    //   }
    }
      Report__c r = new Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Department__c = a.Id;
         r.Department_Class_Ref__c = a.Department_Class__r.Id;
         r.Hospital_Reference__c = a.Hospital__r.Id;
      } else {
         r.Manual_Name__c = aId;
      }
    Report__c r = new Report__c();
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Department__c = a.Id;
      r.Department_Class_Ref__c = a.Department_Class__r.Id;
      r.Hospital_Reference__c = a.Hospital__r.Id;
    } else {
      r.Manual_Name__c = aId;
    }
      if (String.isBlank(dailyReportId) == false) r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.RecordTypeId = rt;
      if (String.isBlank(eventId) == false) r.Event_Id__c = eventId;
      r.Evaluation_PDF_number__c = evaluationPDFNumber;
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
      if(visitor1 != null && visitor1 != ''){
         r.Practitioner1__c = visitor1;
      }
      if(visitor2 != null && visitor2 != ''){
         r.Practitioner2__c = visitor2;
      }
      if(visitor3 != null && visitor3 != ''){
         r.Practitioner3__c = visitor3;
      }
      if(visitor4 != null && visitor4 != ''){
         r.Practitioner4__c = visitor4;
      }
      if(visitor5 != null && visitor5 != ''){
         r.Practitioner5__c = visitor5;
      }
    if (String.isBlank(dailyReportId) == false) r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.RecordTypeId = rt;
    if (String.isBlank(eventId) == false) r.Event_Id__c = eventId;
    r.Evaluation_PDF_number__c = evaluationPDFNumber;
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
    if(visitor1 != null && visitor1 != ''){
      r.Practitioner1__c = visitor1;
    }
    if(visitor2 != null && visitor2 != ''){
      r.Practitioner2__c = visitor2;
    }
    if(visitor3 != null && visitor3 != ''){
      r.Practitioner3__c = visitor3;
    }
    if(visitor4 != null && visitor4 != ''){
      r.Practitioner4__c = visitor4;
    }
    if(visitor5 != null && visitor5 != ''){
      r.Practitioner5__c = visitor5;
    }
      List<String> oppIdList = new List<String>();
      if(opp1 != null && opp1 != ''){
         oppIdList.add(opp1);
      }
      if(opp2 != null && opp2 != ''){
         oppIdList.add(opp2);
      }
      if(opp3 != null && opp3 != ''){
         oppIdList.add(opp3);
      }
      if(opp4 != null && opp4 != ''){
         oppIdList.add(opp4);
      }
      if(opp5 != null && opp5 != ''){
         oppIdList.add(opp5);
      }
    List<String> oppIdList = new List<String>();
    if(opp1 != null && opp1 != ''){
      oppIdList.add(opp1);
    }
    if(opp2 != null && opp2 != ''){
      oppIdList.add(opp2);
    }
    if(opp3 != null && opp3 != ''){
      oppIdList.add(opp3);
    }
    if(opp4 != null && opp4 != ''){
      oppIdList.add(opp4);
    }
    if(opp5 != null && opp5 != ''){
      oppIdList.add(opp5);
    }
      if(oppIdList.size() > 0){
         if(opp1 != null && opp1 != ''){
            r.Opportunity1__c = opp1;
         }
         if(opp2 != null && opp2 != ''){
            r.Opportunity2__c = opp2;
         }
         if(opp3 != null && opp3 != ''){
            r.Opportunity3__c = opp3;
         }
         if(opp4 != null && opp4 != ''){
            r.Opportunity4__c = opp4;
         }
         if(opp5 != null && opp5 != ''){
            r.Opportunity5__c = opp5;
         }
         r.Opportunity_Situation__c = '引合発生';
      }
      else{
         r.Opportunity_Situation__c = '引合無';
      }
      if (String.isBlank(pro1) == false) {
         r.Product1__c = pro1;
      }
    if(oppIdList.size() > 0){
      if(opp1 != null && opp1 != ''){
        r.Opportunity1__c = opp1;
      }
      if(opp2 != null && opp2 != ''){
        r.Opportunity2__c = opp2;
      }
      if(opp3 != null && opp3 != ''){
        r.Opportunity3__c = opp3;
      }
    //康康p课题需要修改字段类型 后续跟进
      if(opp4 != null && opp4 != ''){
        r.Opportunity4__c = opp4;
      }
    //   if(opp5 != null && opp5 != ''){
    //     r.Opportunity5__c = opp5;
    //   }
      r.Opportunity_Situation__c = '引合発生';
    }
    else{
      r.Opportunity_Situation__c = '引合無';
    }
    if (String.isBlank(pro1) == false) {
      r.Product1__c = pro1;
    }
      if (sTime != null && sTime != '') {
         r.Operation_From__c = datetime.parse(sTime);
      }
      if (eTime != null && eTime != '') {
         r.Operation_To__c = datetime.parse(eTime);
      }
      upsert r;
    if (sTime != null && sTime != '') {
      r.Operation_From__c = datetime.parse(sTime);
    }
    if (eTime != null && eTime != '') {
      r.Operation_To__c = datetime.parse(eTime);
    }
    upsert r;
      if (String.isBlank(eventId) == false) updateEvent(r.id,eventId,recordType);
    if (String.isBlank(eventId) == false) updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
   WebService static String addReportNT(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String sTime, String eTime, String mainPlace){
      String rt = selectRecordType(recordType);
      List<Account> aList = selectAccount(aId);
  WebService static String addReportNT(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String visitor2, String visitor3, String visitor4, String visitor5, String sTime, String eTime, String mainPlace){
    String rt = selectRecordType(recordType);
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, From__c, To__c, Location__c from Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, From__c, To__c, Location__c from Report__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
      Report__c r = new Report__c();
    Report__c r = new Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Department__c = a.Id;
         r.Department_Class_Ref__c = a.Department_Class__r.Id;
         r.Hospital_Reference__c = a.Hospital__r.Id;
      } else {
         r.Manual_Name__c = aId;
      }
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Department__c = a.Id;
      r.Department_Class_Ref__c = a.Department_Class__r.Id;
      r.Hospital_Reference__c = a.Hospital__r.Id;
    } else {
      r.Manual_Name__c = aId;
    }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.RecordTypeId = rt;
      r.Event_Id__c = eventId;
      if(visitor1 != null && visitor1 != ''){
         r.Practitioner1__c = visitor1;
      }
      if(visitor2 != null && visitor2 != ''){
         r.Practitioner2__c = visitor2;
      }
      if(visitor3 != null && visitor3 != ''){
         r.Practitioner3__c = visitor3;
      }
      if(visitor4 != null && visitor4 != ''){
         r.Practitioner4__c = visitor4;
      }
      if(visitor5 != null && visitor5 != ''){
         r.Practitioner5__c = visitor5;
      }
      if(sTime != null && sTime != ''){
         //sTime = sTime.substring(0,16);
         r.From__c = datetime.parse(sTime);
      }
      if(eTime != null && eTime != ''){
         //eTime = eTime.substring(0,16);
         r.To__c = datetime.parse(eTime);
      }
      r.Location__c = mainPlace;
      upsert r;
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.RecordTypeId = rt;
    r.Event_Id__c = eventId;
    if(visitor1 != null && visitor1 != ''){
      r.Practitioner1__c = visitor1;
    }
    if(visitor2 != null && visitor2 != ''){
      r.Practitioner2__c = visitor2;
    }
    if(visitor3 != null && visitor3 != ''){
      r.Practitioner3__c = visitor3;
    }
    if(visitor4 != null && visitor4 != ''){
      r.Practitioner4__c = visitor4;
    }
    if(visitor5 != null && visitor5 != ''){
      r.Practitioner5__c = visitor5;
    }
    if(sTime != null && sTime != ''){
      //sTime = sTime.substring(0,16);
      r.From__c = datetime.parse(sTime);
    }
    if(eTime != null && eTime != ''){
      //eTime = eTime.substring(0,16);
      r.To__c = datetime.parse(eTime);
    }
    r.Location__c = mainPlace;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
   WebService static String addReportOn(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String v1Str, String startHour, String startMin, String endHour, String endMin, String description){
      List<Account> aList = selectAccount(aId);
  WebService static String addReportOn(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String v1Str, String startHour, String startMin, String endHour, String endMin, String description){
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<On_Call__c> oList = [select id, Daily_Report__c, Event_Id__c, segment__c from On_Call__c where id =:reportId];
         if(oList.size() > 0){
            return oList.get(0).id;
         }
      }
      On_Call__c o = new On_Call__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         o.segment__c = a.Id;
         // 病院ID(18桁)差込み
         o.Hospital__c = a.Hospital__r.Id;
      }
    if(reportId != null && reportId != ''){
      List<On_Call__c> oList = [select id, Daily_Report__c, Event_Id__c, segment__c from On_Call__c where id =:reportId];
      if(oList.size() > 0){
        return oList.get(0).id;
      }
    }
    On_Call__c o = new On_Call__c();
    if(aList.size() != 0){
      Account a = aList.get(0);
      o.segment__c = a.Id;
      // 病院ID(18桁)差込み
      o.Hospital__c = a.Hospital__r.Id;
    }
      o.Daily_Report__c = dailyReportId;
      o.OwnerId = repOwnerId;
      o.Event_Id__c = eventId;
      o.Responsible_Person_HP__c = v1Str;
      if(String.isBlank(description) == false){
         if(description.length() > 255){
            o.Customer_Problem_c__c = description.substring(0, 255);
         } else {
            o.Customer_Problem_c__c = description;
         }
    o.Daily_Report__c = dailyReportId;
    o.OwnerId = repOwnerId;
    o.Event_Id__c = eventId;
    o.Responsible_Person_HP__c = v1Str;
    if(String.isBlank(description) == false){
      if(description.length() > 255){
        o.Customer_Problem_c__c = description.substring(0, 255);
      } else {
        o.Customer_Problem_c__c = description;
      }
      }
      Date rDate = date.parse(reportDate);
      o.From_c__c = Datetime.newInstance(rDate.year(), rDate.month(), rDate.day(), Integer.valueOf(startHour), Integer.valueOf(StartMin), 0);
      o.To_c__c = Datetime.newInstance(rDate.year(), rDate.month(), rDate.day(), Integer.valueOf(endHour), Integer.valueOf(endMin), 0);
    }
    Date rDate = date.parse(reportDate);
    o.From_c__c = Datetime.newInstance(rDate.year(), rDate.month(), rDate.day(), Integer.valueOf(startHour), Integer.valueOf(StartMin), 0);
    o.To_c__c = Datetime.newInstance(rDate.year(), rDate.month(), rDate.day(), Integer.valueOf(endHour), Integer.valueOf(endMin), 0);
      upsert o;
      updateEvent(o.id,eventId,recordType);
      return o.id;
   }
   WebService static String addReportAc(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
      List<Account> aList = selectAccount(aId);
    upsert o;
    updateEvent(o.id,eventId,recordType);
    return o.id;
  }
  WebService static String addReportAc(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Visit_Report__c> rList = [select id, Daily_Report__c, Event_Id__c, Hospital_Reference__c, Department_Class__c, Department__c, Date__c from Visit_Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<Visit_Report__c> rList = [select id, Daily_Report__c, Event_Id__c, Hospital_Reference__c, Department_Class__c, Department__c, Date__c from Visit_Report__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
      Visit_Report__c r = new Visit_Report__c();
    Visit_Report__c r = new Visit_Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Reference__c = a.Hospital__r.Id;
         r.Department_Class__c = a.Department_Class__r.Id;
         r.Department__c = a.Id;
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Reference__c = a.Hospital__r.Id;
      r.Department_Class__c = a.Department_Class__r.Id;
      r.Department__c = a.Id;
//        } else {
//            r.Manual_Name__c = aId;
      }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.Event_Id__c = eventId;
      r.Evaluate_And_Problem__c = description;
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
    }
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.Event_Id__c = eventId;
    r.Evaluate_And_Problem__c = description;
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
      upsert r;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
   WebService static String addClaim(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
      List<Account> aList = selectAccount(aId);
    return r.id;
  }
  WebService static String addClaim(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<QIS_Report__c> rList = [select id, Daily_Report__c, Event_Id__c, Hospital_Department__c, Date__c from QIS_Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<QIS_Report__c> rList = [select id, Daily_Report__c, Event_Id__c, Hospital_Department__c, Date__c from QIS_Report__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
      QIS_Report__c r = new QIS_Report__c();
    QIS_Report__c r = new QIS_Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Department__c = a.Id;
         r.Department_Class__c = a.Department_Class__r.Id;
         r.Hospital__c = a.Hospital__r.Id;
      }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.Event_Id__c = eventId;
      //r.Department_Class_Ref__c = a.Department_Class__r.Id;
      //r.Hospital_Reference__c = a.Hospital__r.Id;
      r.problem_detail__c = description;
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
      r.Generation_Source__c = '日报';
      r.Name = '*';
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Department__c = a.Id;
      r.Department_Class__c = a.Department_Class__r.Id;
      r.Hospital__c = a.Hospital__r.Id;
    }
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.Event_Id__c = eventId;
    //r.Department_Class_Ref__c = a.Department_Class__r.Id;
    //r.Hospital_Reference__c = a.Hospital__r.Id;
    r.problem_detail__c = description;
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
    r.Generation_Source__c = '日报';
    r.Name = '*';
      upsert r;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
   // MarketCondition or Competitor
   WebService static String addReportEx(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String reportDate, String description){
      String rt = selectRecordType(recordType);
      List<Account> aList = selectAccount(aId);
  // MarketCondition or Competitor
  WebService static String addReportEx(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String visitor1, String reportDate, String description){
    String rt = selectRecordType(recordType);
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
      Report__c r = new Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Department__c = a.Id;
         r.Department_Class_Ref__c = a.Department_Class__r.Id;
         r.Hospital_Reference__c = a.Hospital__r.Id;
      }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.RecordTypeId = rt;
      r.Event_Id__c = eventId;
      if (String.isBlank(visitor1) == false) {
         r.Person_In_Charge__c = visitor1;
      }
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
      r.Comment__c = description;
    if(reportId != null && reportId != ''){
      List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
    Report__c r = new Report__c();
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Department__c = a.Id;
      r.Department_Class_Ref__c = a.Department_Class__r.Id;
      r.Hospital_Reference__c = a.Hospital__r.Id;
    }
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.RecordTypeId = rt;
    r.Event_Id__c = eventId;
    if (String.isBlank(visitor1) == false) {
      r.Person_In_Charge__c = visitor1;
    }
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
    r.Comment__c = description;
      upsert r;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
   WebService static String addReportIm(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
      String rt = selectRecordType(recordType);
      List<Account> aList = selectAccount(aId);
  WebService static String addReportIm(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
    String rt = selectRecordType(recordType);
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<Report__c> rList = [select id, Daily_Report__c, RecordTypeId, Event_Id__c, Hospital_Department__c, Department_Class_Ref__c, Hospital_Reference__c, Date__c, Practitioner1__c, Practitioner2__c, Practitioner3__c, Practitioner4__c, Practitioner5__c, Comment__c from Report__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
      Report__c r = new Report__c();
    Report__c r = new Report__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital_Reference__c = a.Hospital__r.Id;
      }
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital_Reference__c = a.Hospital__r.Id;
    }
      r.Daily_Report__c = dailyReportId;
      r.OwnerId = repOwnerId;
      r.RecordTypeId = rt;
      r.Event_Id__c = eventId;
      if(reportDate != null && reportDate != ''){
         r.Date__c = date.parse(reportDate);
      }
      r.Comment__c = description;
    r.Daily_Report__c = dailyReportId;
    r.OwnerId = repOwnerId;
    r.RecordTypeId = rt;
    r.Event_Id__c = eventId;
    if(reportDate != null && reportDate != ''){
      r.Date__c = date.parse(reportDate);
    }
    r.Comment__c = description;
      upsert r;
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
    return r.id;
  }
//    // Maintenance_Contract
//    WebService static String addMC(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description){
@@ -439,127 +441,127 @@
//        return r.id;
//    }
   // Rental_Apply
   WebService static String addRA(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description,String followOpps){
      List<Account> aList = selectAccount(aId);
  // Rental_Apply
  WebService static String addRA(String repOwnerId, String reportId, String dailyReportId, String eventId, String recordType, String aId, String reportDate, String description,String followOpps){
    List<Account> aList = selectAccount(aId);
      if(reportId != null && reportId != ''){
         List<Rental_Apply__c> rList = [select id, Daily_Report__c, Event_Id__c from Rental_Apply__c where id =:reportId];
         if(rList.size() > 0){
            return rList.get(0).id;
         }
      }
    if(reportId != null && reportId != ''){
      List<Rental_Apply__c> rList = [select id, Daily_Report__c, Event_Id__c from Rental_Apply__c where id =:reportId];
      if(rList.size() > 0){
        return rList.get(0).id;
      }
    }
      Rental_Apply__c r = new Rental_Apply__c();
    Rental_Apply__c r = new Rental_Apply__c();
      if(aList.size() != 0){
         Account a = aList.get(0);
         r.Hospital__c = a.Hospital__r.Id;
         r.Strategic_dept__c = a.Department_Class__r.Id;
         r.Account__c = a.Id;
      }
      r.Daily_Report__c = dailyReportId;
      r.Event_Id__c = eventId;
      r.Demo_purpose_text__c = description;
      r.Request_return_day__c = Date.today();
      r.Person_In_Charge__c = repOwnerId;
      r.applyUser__c = repOwnerId;
      if(followOpps!=null&&followOpps!=''){
         r.Follow_UP_Opp__c = followOpps;
      }
      upsert r;
    if(aList.size() != 0){
      Account a = aList.get(0);
      r.Hospital__c = a.Hospital__r.Id;
      r.Strategic_dept__c = a.Department_Class__r.Id;
      r.Account__c = a.Id;
    }
    r.Daily_Report__c = dailyReportId;
    r.Event_Id__c = eventId;
    r.Demo_purpose_text__c = description;
    r.Request_return_day__c = Date.today();
    r.Person_In_Charge__c = repOwnerId;
    r.applyUser__c = repOwnerId;
    if(followOpps!=null&&followOpps!=''){
      r.Follow_UP_Opp__c = followOpps;
    }
    upsert r;
      updateEvent(r.id,eventId,recordType);
    updateEvent(r.id,eventId,recordType);
      return r.id;
   }
   WebService static String selectRecordType(String recordTypeName){
      List<RecordType> rtList = [select id from RecordType where IsActive = true and DeveloperName =:recordTypeName];
      return rtList.get(0).id;
   }
    return r.id;
  }
  WebService static String selectRecordType(String recordTypeName){
    List<RecordType> rtList = [select id from RecordType where IsActive = true and DeveloperName =:recordTypeName];
    return rtList.get(0).id;
  }
   WebService static List<Account> selectAccount(String aId){
      List<Account> aList = new List<Account>();
      if (aId != null && aId.startsWith('001')) {
         aList = [select Id, Department_Class__r.Id, Hospital__r.Id from Account where Id =:aId];
      }
      return aList;
   }
  WebService static List<Account> selectAccount(String aId){
    List<Account> aList = new List<Account>();
    if (aId != null && aId.startsWith('001')) {
      aList = [select Id, Department_Class__r.Id, Hospital__r.Id from Account where Id =:aId];
    }
    return aList;
  }
   WebService static void updateEvent(String rId, String eventId, String recordType){
      Event__c e = [
         select id,Maintenance_Contract_ID__c, Asset_Manual_ID__c, Rental_Apply_ID__c,
               Product_Description_Id__c, OPD_ID__c, NTC_ID__c, ReportAccompanied_ID__c, CityStatus_ID__c, Improvement_ID__c, Conflict_ID__c, Claim_ID__c
           from Event__c where id =:eventId
      ];
  WebService static void updateEvent(String rId, String eventId, String recordType){
    Event__c e = [
      select id,Maintenance_Contract_ID__c, Asset_Manual_ID__c, Rental_Apply_ID__c,
           Product_Description_Id__c, OPD_ID__c, NTC_ID__c, ReportAccompanied_ID__c, CityStatus_ID__c, Improvement_ID__c, Conflict_ID__c, Claim_ID__c
        from Event__c where id =:eventId
    ];
//        if(recordType == 'Maintenance_Contract'){
//            e.Maintenance_Contract_ID__c = rId;
//        }
      if(recordType == 'Rental_Apply'){
         e.Rental_Apply_ID__c = rId;
      }
      if(recordType == 'Asset_Manual'){
         e.Asset_Manual_ID__c = rId;
      }
      if(recordType == 'Product_Explanation'){
         e.Product_Description_Id__c = rId;
      }
      else if(recordType == 'OPD' || recordType == 'SIS'){
         e.OPD_ID__c = rId;
      }
      else if(recordType == 'NTC'){
         e.NTC_ID__c = rId;
      }
      else if(recordType == 'On_Call'){
         e.OnCall_ID__c = rId;
      }
      else if(recordType == 'Visit_With'){
         e.ReportAccompanied_ID__c = rId;
      }
      else if(recordType == 'MarketCondition'){
         e.CityStatus_ID__c = rId;
      }
      else if(recordType == 'Improvement'){
         e.Improvement_ID__c = rId;
      }
      else if(recordType == 'Competitor'){
         e.Conflict_ID__c = rId;
      }
      else if(recordType == 'Complaint'){
         e.Claim_ID__c = rId;
      }
    if(recordType == 'Rental_Apply'){
      e.Rental_Apply_ID__c = rId;
    }
    if(recordType == 'Asset_Manual'){
      e.Asset_Manual_ID__c = rId;
    }
    if(recordType == 'Product_Explanation'){
      e.Product_Description_Id__c = rId;
    }
    else if(recordType == 'OPD' || recordType == 'SIS'){
      e.OPD_ID__c = rId;
    }
    else if(recordType == 'NTC'){
      e.NTC_ID__c = rId;
    }
    else if(recordType == 'On_Call'){
      e.OnCall_ID__c = rId;
    }
    else if(recordType == 'Visit_With'){
      e.ReportAccompanied_ID__c = rId;
    }
    else if(recordType == 'MarketCondition'){
      e.CityStatus_ID__c = rId;
    }
    else if(recordType == 'Improvement'){
      e.Improvement_ID__c = rId;
    }
    else if(recordType == 'Competitor'){
      e.Conflict_ID__c = rId;
    }
    else if(recordType == 'Complaint'){
      e.Claim_ID__c = rId;
    }
      update e;
   }
   WebService static Product2 getProduct2(String a){
      Product2 p2 = [select Id, Name, Intra_Trade_Cost_RMB__c, Intra_Trade_List_RMB__c FROM Product2 Where Id =:a];
      return p2;
   }
    update e;
  }
  WebService static Product2 getProduct2(String a){
    Product2 p2 = [select Id, Name, Intra_Trade_Cost_RMB__c, Intra_Trade_List_RMB__c FROM Product2 Where Id =:a];
    return p2;
  }
   WebService static Product2 getProduct2US(String a){
      Product2 p2 = [select Id, Name, Foreign_Trade_Cost_US__c, Foreign_Trade_List_US__c FROM Product2 Where Id =:a];
      return p2;
   }
   WebService static String getReportId(String oId, String rDate){
      List<Daily_Report__c> rtList = [select Id from Daily_Report__c where OwnerId =:oId and Reported_Date__c =:date.parse(rDate)];
      return rtList.get(0).id;
   }
   WebService static String checkAccountMatch(String name, String id){
      List<Account> accList = [select Id from Account where Name = :name and Id = :id];
      if (accList.size() > 0) {
         return 'OK';
      }
      return 'NG';
   }
   WebService static Account getAccountForJs(String id){
      return [select Id, Name, Department_Class__r.Name, Department_Class__r.Id,
            Hospital__r.Name, Hospital__r.Id, Hospital__r.OCM_Category__c
            from Account where Id = :id];
   }
  WebService static Product2 getProduct2US(String a){
    Product2 p2 = [select Id, Name, Foreign_Trade_Cost_US__c, Foreign_Trade_List_US__c FROM Product2 Where Id =:a];
    return p2;
  }
  WebService static String getReportId(String oId, String rDate){
    List<Daily_Report__c> rtList = [select Id from Daily_Report__c where OwnerId =:oId and Reported_Date__c =:date.parse(rDate)];
    return rtList.get(0).id;
  }
  WebService static String checkAccountMatch(String name, String id){
    List<Account> accList = [select Id from Account where Name = :name and Id = :id];
    if (accList.size() > 0) {
      return 'OK';
    }
    return 'NG';
  }
  WebService static Account getAccountForJs(String id){
    return [select Id, Name, Department_Class__r.Name, Department_Class__r.Id,
        Hospital__r.Name, Hospital__r.Id, Hospital__r.OCM_Category__c
        from Account where Id = :id];
  }
}