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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
global class  OpdPlanWebService {
    //按钮调用 SWAG-BUF6J5  20201110 youchang
    @AuraEnabled
    WebService  static String changeTrade(String opdplanId) { //取消opd 和取消备品得窗口调用
        List<Rental_Apply__c> raList = [select Yi_loaner_arranged__c, Shipment_request_Cnt__c from Rental_Apply__c where OPDPlan__c = :opdplanId];
        String rtn = 'OK';
        if (null != raList && raList.size() > 0) {
            Integer yla = 0;
            Integer srct = 0;
            for (Rental_Apply__c ra : raList) {
                yla += Integer.valueOf(ra.Yi_loaner_arranged__c);
                srct += Integer.valueOf(ra.Shipment_request_Cnt__c);
            }
            if (yla > 0) {
                rtn = '备品已经出库,不能取消或延期OPD计划.';
            } else if (srct > 0) {
                rtn = '备品已做出库指示,请联络备品中心取消或延期计划.';
            }
        }
 
        return rtn;
    }
    @AuraEnabled
    WebService  static String changeCampaign(String campaignId) { //学会取消得窗口调用
        List<Rental_Apply__c> raList = [select Yi_loaner_arranged__c, Shipment_request_Cnt__c from Rental_Apply__c where Campaign__c = :campaignId];
        String rtn = 'OK';
 
        //因为报50001得错误。所以取消聚合函数得引用
        // List<AggregateResult> opdList = [select sum(Yi_loaner_arranged__c) yla,sum(Shipment_request_Cnt__c) src from Rental_Apply__c where Campaign__c = :campaignId];
 
        if (null != raList && raList.size() > 0) {
            Integer yla = 0;
            Integer srct = 0;
            for (Rental_Apply__c ra : raList) {
                yla += Integer.valueOf(ra.Yi_loaner_arranged__c);
                srct += Integer.valueOf(ra.Shipment_request_Cnt__c);
            }
            //kk  20231104  学会取消合并  DB202306243540  start
            // if (yla > 0) {
            //     rtn = '备品已经出库,不能取消或延期学会.';
            // } else if (srct > 0) {
            //     rtn = '备品已做出库指示,不能取消或延期学会.';
            // }
            if (yla > 0) {
                rtn = '备品已经出库,不能延期学会.';
            } else if (srct > 0) {
                rtn = '备品已做出库指示,不能延期学会.';
            }
            //kk  20231104  学会取消合并  DB202306243540  end
 
        }
 
        return rtn;
 
    }
 
    @AuraEnabled
    WebService  static String dataCheck(String rentalApplyId) {
        string result = 'OK';
        if (String.isBlank(rentalApplyId)) {
            result = 'opd计划无备品借出申请,无法进行OPD补充申请';
            return result;
        }
        List<String> RAStatusList = new List<String>();
        RAStatusList.add('申请中');
        //20231224 sx add  备品智能化添加状态 优化
        RAStatusList.add('申请中(OPD未通过)');
        RAStatusList.add('已批准');
        RAStatusList.add('已出库指示');
        RAStatusList.add('已下架');
        RAStatusList.add('出库前已检测');
        List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
        raList = [SELECT Id, Name, RA_Status__c, Status__c  FROM Rental_Apply__c
                  WHERE Id = :rentalApplyId AND demo_purpose2__c != '试用(有询价)'
                             AND demo_purpose2__c != '试用(无询价)'
                             AND demo_purpose2__c != '新产品评价'
                             AND Status__c != '申请中'
                             AND Status__c != '申请中(OPD未通过)'
                             AND RA_Status__c not In:RAStatusList limit 1];
        //不满足要求 不能创建 opd计划
        if (raList.size() > 0) {
            Rental_Apply__c ra =  raList[0];
            String status = ra.RA_Status__c;
            //20231224 sx add  备品智能化添加状态 优化
            if (ra.Status__c != '申请中' && ra.Status__c != '申请中(OPD未通过)') {
                result = 'opd计划下备品借出申请 状态 不是 申请中,无法进行OPD补充申请';
            } else if (!RAStatusList.contains(status)) {
                result = 'opd计划下备品借出申请 备品借出状态 不是 申请中、已批准、已出库指示、已下架、出库前已检测,无法进行OPD补充申请';
            }  else {
                result = 'opd计划下备品借出申请 使用目的2 不是 试用(有询价)、试用(无询价)、新产品评价,无法进行OPD补充申请';
            }
        }
 
        return result;
    }
 
    @AuraEnabled
    WebService  static String raesCountCheck(String rentalApplyId) {
        String result = 'OK';
        List<Rental_Apply__c> raLsit = [
                                           SELECT Id,
                                           OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c
                                           from Rental_Apply__c
                                           where SupplementCreated__c = true
                                                   AND OPDPlan__c != null
                                                   AND Id = :rentalApplyId];
        if (raLsit.size() > 0 ) {
            // 获取 补充申请OPD计划创建的 备品借出申请Id 以及 被克隆的备品借出申请Id
            Set<Id> raIdSet = new Set<Id>();
            for (Rental_Apply__c ra : raLsit) {
                Id raId = ra.Id;
                Id cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
                raIdSet.add(raId);
                raIdSet.add(cloneRaId);
            }
 
            //查找 备品借出申请下 已有多少个借出备品配套一览
            AggregateResult[] aggArr = [
                                           SELECT Rental_Apply__c raId, count(Id) cnt
                                           FROM Rental_Apply_Equipment_Set__c
                                           WHERE Rental_Apply__c
                                           In :raIdSet
                                           //AND Cancel_Reason__c != null
                                           group by Rental_Apply__c
                                       ];
            Map<String, Integer> raIdCntMap = new Map<String, Integer>();
 
            for (AggregateResult ar : aggArr ) {
                String raId = String.valueOf(ar.get('raId'));
                Integer cnt = Integer.valueOf(ar.get('cnt'));
                raIdCntMap.put(raId, cnt);
 
            }
            Map<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
            for (Rental_Apply__c ra : raLsit) {
                String raId = ra.Id;
                Integer  raCnt = 0;
                if (raIdCntMap.containskey(raId)) {
                    raCnt = raIdCntMap.get(raId);
                }
                String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
 
                Integer cloneRacnt = 0;
                if (raIdCntMap.containskey(cloneRaId)) {
                    cloneRacnt = raIdCntMap.get(cloneRaId);
                }
                //system.debug('cloneRacnt------>' + cloneRacnt);
                if ( raCnt > cloneRacnt) {
                    raCheckMap.put(raId, true);
                }
 
            }
            if (raCheckMap != null && raCheckMap.size() > 0) {
                result = '借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量';
            }
 
        }
        return result;
    }
 
    @TestVisible public static void test() {
        Integer i = 0;
        
    }
                
}