@RestResource(urlMapping='/OFSRepairCreate/*')
global with sharing class OFSRepairCreateRest {
    // 日付は全部 yyyy-MM-ddの形式
    @HttpPost
    global static void doPost(String hpId, String accId, String astId, String Counter_personId, String Maintenance_ContractId,
            String On_site_repair, String SalesOfficeCode_selection, String Counter_person_phone, String Work_location_select,
            String PaperRepairRequestNo, String Failure_Occurrence_Date, String Contacted_day, String On_Site_Repair_Order_Date,
            String Repair_Detail, String Accsessory, String Accserrory_other, String Department_Class, String Send_To_RC_Date) 
            {
        system.debug('OFSRepairCreateRest.start');
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        
        String jsonResponse;
        Repair__c rpr = new Repair__c(
            Hospital__c = hpId,
            Account__c = accId,
            Department_Class__c = Department_Class,
            Send_To_RC_Date__c = String.isBlank(Send_To_RC_Date) ? null : Date.valueOf(Send_To_RC_Date),
            Delivered_Product__c = astId,
            // TODO 科室ではないくFSE 主担当でしょう、FSE_owner__c でしょう
            Counter_person__c = String.isBlank(Counter_personId) ? null : Counter_personId,
            Maintenance_Contract__c = String.isBlank(Maintenance_ContractId) ? null : Maintenance_ContractId,
            On_site_repair__c = On_site_repair,
            SalesOfficeCode_selection__c = SalesOfficeCode_selection,
            Counter_person_phone__c = Counter_person_phone,
            Work_location_select__c = Work_location_select,
            PaperRepairRequestNo__c = PaperRepairRequestNo,
            Failure_Occurrence_Date__c = String.isBlank(Failure_Occurrence_Date) ? null : Date.valueOf(Failure_Occurrence_Date),
            Contacted_day__c = String.isBlank(Contacted_day) ? null : Date.valueOf(Contacted_day),
            On_Site_Repair_Order_Date__c = String.isBlank(On_Site_Repair_Order_Date) ? null : Date.valueOf(On_Site_Repair_Order_Date),
            Repair_Detail__c = Repair_Detail,
            Accsessory__c = Accsessory,
            Accserrory_other__c = Accserrory_other
        );
        try{
            insert rpr;
            res.statusCode = 200;
            jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorSuccess +'", "'+ System.Label.OFSErrorMessage +'": "'+ rpr.Id +'", "repairCreate":"1"}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }catch ( Exception ex ) {
            //error message:cannot update exception
            res.statusCode = 200;
            jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage +'": "'+ ex +'"}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
    }

}