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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
global without sharing class AssetMaintainHeaderWebService {
    public AssetMaintainHeaderWebService() {
 
    }
    @AuraEnabled
    WebService static String submitApply(String amhId) {
        List<AssetMaintainHeader__c> amhList = [select id, Status__c
                                            from AssetMaintainHeader__c
                                            where id = :amhId
                                            FOR UPDATE];
        if (amhList.size() <= 0) {
            return '维护单不存在。';
        }
        AssetMaintainHeader__c amh = amhList[0];
        if(amh.Status__c != '草案中'){
            return '请确认维护单状态,已经提交过的申请,不能重复提交';
        }
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amhId, AssetMaintainManualBatch.Operation.FROZEN).errorList;
        if(errList.size()>0){
            return String.join(errList,'\n');
        }
        amh.Status__c = '填写完毕';
        amh.Submit_Person__c = UserInfo.getUserId();
        Savepoint sp = Database.setSavepoint();
        try {
            List<AssetMaintainDetail__c> amdList = [
                SELECT Asset__c
                     , MaintainCount__c
                  FROM AssetMaintainDetail__c
                 WHERE AssetMaintainHeader__c =: amh.Id
            ];
            setMaintainConuntToAsset(amdList);
            update amh;
        }
        catch (Exception ex) {
            Database.rollback(sp);
            return ex.getMessage(); 
        }
        return '1';
    }
    @AuraEnabled
    WebService static String abandon(String amhId) {
        Savepoint sp = Database.setSavepoint();
        try {
            List<AssetMaintainHeader__c> amhList = [select id
                                                         , Status__c
                                                         , Batch_Processing__c
                                                from AssetMaintainHeader__c
                                                where id = :amhId
                                                FOR UPDATE];
            if (amhList.size() <= 0) {
                return '维护单不存在。';
            }
            AssetMaintainHeader__c amh = amhList[0];
            if(amh.Status__c != '已批准'){
                return '请确认维护单状态,只有已批准的申请才可以废弃';
            }
            if(amh.Batch_Processing__c) {
                return '此维护单有Batch正在执行,请稍候';
            }
            amh.Batch_Processing__c = true;
            update amh;
            Boolean needCheck = true;
            AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amhId, needCheck, AssetMaintainManualBatch.Operation.ABANDON);
            Database.executeBatch(amBatch, Integer.valueOf(System.Label.AssetMaintainManualBatchSize));
           
        }
        catch (Exception ex) {
            Database.rollback(sp);
            return ex.getMessage();
        }
        return '1';
    }
    //20231027  ymh添加注释  lighting  lwc调用
    @AuraEnabled
    public static Boolean applyPermission()
    {
        Schema.DescribeSobjectResult schemaMap = Schema.describeSObjects(new String[]{'TransferApply__c'})[0];
        return schemaMap.isCreateable();
    }
     //20231027  ymh添加注释  lighting  lwc调用
    @AuraEnabled
    WebService static String deleteApply(String amhId) {
        List<AssetMaintainHeader__c> amhList = [select id, Status__c
                                            from AssetMaintainHeader__c
                                            where id = :amhId
                                            FOR UPDATE];
 
        if (amhList.size() <= 0) {
            return '维护单不存在。';
        }
 
        AssetMaintainHeader__c amh = amhList[0];
        if (amh.Status__c == '申请中') {
            return '需要调回/驳回申请后再删除';
        }
        else if (amh.Status__c != '草案中') {
            return '维护单状态为【' + amh.Status__c + '】,不可以删除';
        }
        else {
            Savepoint sp = Database.setSavepoint();
            try {
                delete amh;
            } catch (Exception ex) {
                Database.rollback(sp);
                return ex.getMessage();
            }
        }
 
        return '1';
    }
    /**
    @description 把维护数写到asset
    */
    static private void setMaintainConuntToAsset(List<AssetMaintainDetail__c> amdList){
        Set<Id> assetIdSet = new Set<Id>();
        for (AssetMaintainDetail__c amd: amdList) {
            assetIdSet.add(amd.Asset__c);
        }
        Map<Id, Asset> assetMap = new Map<Id, Asset>([
            SELECT Id
                 , MaintainCount_For_Processing__c
              FROM Asset
             WHERE Id IN:assetIdSet
               FOR UPDATE
        ]);
        for (AssetMaintainDetail__c amd: amdList) {
            if(assetMap.containsKey(amd.Asset__c)) {
                Asset ass = assetMap.get(amd.Asset__c);
                ass.MaintainCount_For_Processing__c = intValueOf(ass.MaintainCount_For_Processing__c) + intValueOf(amd.MaintainCount__c);
                assetMap.put(ass.Id, ass);
            }
        }
        Oly_TriggerHandler.bypass('AssetHandler');
        update assetMap.values();
        Oly_TriggerHandler.clearBypass('AssetHandler');
    }
    static private Integer intValueOf(Decimal d) {
        if(d == null || d < 0) {
            return 0;
        }
        return Integer.valueOf(d);
    }
}