高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@RestResource(urlMapping='/OFSHpLeave/*')
global with sharing class OFSHpLeaveRest {
    @HttpPost
    global static void doPost(Id Eid) {
        system.debug('OFSHpLeaveRest.start');
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        
        String jsonResponse;
        
        List<Event> elist = [select 
                                  id, ActivityDate, OwnerId, Subject, whatid__c, EventC_ID__c, NextEventC_ID__c, AppCdId__c, SyncCreatedDate__c, 
                                  StartDateTime, EndDateTime, DurationInMinutes, Main_Visit_Location__c, Activity_Type2__c, IsScheduled__c, BeforeActivityDate__c,
                                  Purpose_Type__c, Location, Related_Opportunity1__c, Related_Service1__c, Related_Opportunity1_ID__c, Related_Service1_ID__c, 
                                  WS_flg__c, HPLeaveFlg__c, EndDateTime_org__c, StartDateTime_org__c
                            from Event where id = :Eid];
        if(elist.size()<=0){
            //TODO:
            //error message:no event or wrong Id 
            res.statusCode = 200;
            //jsonResponse = '{"status": "Failure", "Message": "No matching event was found"}';
            jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage +'": "No matching event was found"}';
            res.responseBody = blob.valueOf(jsonResponse);
               return;
        }else{
            if(elist[0].HPLeaveFlg__c == false){
                if(elist[0].StartDateTime_org__c==null && elist[0].EndDateTime_org__c==null){
                    elist[0].StartDateTime_org__c = elist[0].StartDateTime;
                    elist[0].EndDateTime_org__c = elist[0].EndDateTime;
                }
                elist[0].EndDateTime = Datetime.now();
                elist[0].DurationInMinutes = Integer.valueOf((elist[0].EndDateTime.getTime() - elist[0].StartDateTime.getTime()) / 1000 / 60);
                elist[0].HPLeaveFlg__c = true;
            } else {
                //操作重複
                res.statusCode = 200;
                jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage +'": "'+ System.Label.OFSErrorDuplication +'"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            }
            try{
                update elist[0];
                system.debug('OFSHpLeaveRest.DurationInMinutes:' + elist[0].DurationInMinutes);
                res.statusCode = 200;
                //jsonResponse = '{"status": "Success", "Message": "' + elist[0].Id + '"}';
                jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorSuccess +'", "'+ System.Label.OFSErrorMessage +'": "'+ elist[0].Id +'"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            }catch ( Exception ex ) {
                //TODO:
                //error message:cannot update exception
                res.statusCode = 200;
                //jsonResponse = '{"status": "Failure", "Message": "' + ex + '"}';
                jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage +'": "'+ ex +'"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            }
        }        
    }
}