高章伟
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
public without sharing class DiscountProductApplicationController {
 
    //获取经销商信息
    public Agency_Hospital_Link__c ahl { get; set; }
    //public Account acc { get; set; }
    //特价申请
    public DiscountProductApplication__c dpa { get; set; }
    //新建页面发送的经销商ID
    public String accountId;
    //编辑页面发送的经销商ID
    public String dpaId;
    //经销商名
    public String AimsAccount { get; set; }
    //医院id
    public String AimsAccountId { get; set; }
    //页面请求明细展示构造类
    public List<DiscountProductApplicationDetail> DiscountProductApplicationDetailList { get; set; }
    //查询用请求明细
    public List<DiscountProductApplicationDetail__c> dpadlList;
    //更新用请求明细
    public List<DiscountProductApplicationDetail__c> dpadList;
    //经销商医院
    public List<Agency_Hospital_Link__c> ahlList;
    //public String RawAccountc{ get; set; }
    //全部检索list
    public List<String> dpadIdList;
    //text1,text1是检索输入参数
    public String text1 { get; set; }
    public String text2 { get; set; }
    public String soql;
    public Boolean ApplicationStatus;
    public Integer unCheckedDpadCount { 
        get {
            return DiscountProductApplicationDetailList == null ? 0 : DiscountProductApplicationDetailList.size();
        }
    }
    //add by rentx 2021-2-20
    public Boolean isPage {get;set;}
    public String tempVal {get;set;}
    public String rawAccountId {get;set;}
    public String rawAccountName {get;set;}
    //add by rentx 2021-2-20
 
    public DiscountProductApplicationController(ApexPages.StandardController stdController) {
        //新建页面发送的经销商ID
        accountId = ApexPages.currentPage().getParameters().get('accId');
        //编辑页面发送的经销商ID
        dpaId = ApexPages.currentPage().getParameters().get('Id');
         DiscountProductApplicationDetailList = new List<DiscountProductApplicationDetail>();
         dpadIdList = new List<String>();
    }
    //add by rentx 2021-2-22
    //add by rentx 2021-2-22
 
    //查询初期数据
    public void init(){
        if (String.isBlank(accountId) && String.isBlank(dpaId)) {
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请选择经销商'));
            dpa = new DiscountProductApplication__c();
            isPage = true;
        }else{
            isPage = false;
        }
        String message;
        //String RawAccount;
        if(!String.isBlank(accountId)){
            dpa = new DiscountProductApplication__c();
            dpa.RawAccount__c = accountId;
            rawAccountId = accountId;
            // List<Account> accList = [select id,Name from Account where id = :accountId ];
            // if(accList.size()>0){
                // this.accountName = accList[0].Name;
            // }
        }
        if(!String.isBlank(dpaId)){
            List<DiscountProductApplication__c> dpaList =[select RawAccount__c from DiscountProductApplication__c where id =: dpaId];
            dpa = new DiscountProductApplication__c();
            if(dpaList.size()>0){
                dpa = dpaList.get(0);
            }
            accountId = dpa.RawAccount__c;
        }
        
        if((String.isBlank(dpaId) && !String.isBlank(accountId)) || isPage == true){
            //新建特价申请
            //dpa = new DiscountProductApplication__c();
            //dpain = true;
            // List<Account> accList = [select id,name from Account where id = :accountId ];
            
            List<Product2__c> Product2List = new List<Product2__c>();
            soql = this.makeSoql1('','');
            Product2List = Database.query(soql);
            if(Product2List.size()>=1){
                for(Product2__c Pro2 : Product2List){
                    DiscountProductApplicationDetail dpad = 
                    new DiscountProductApplicationDetail(Pro2);
                    DiscountProductApplicationDetailList.add(dpad);
                }
            }
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  acc.name));
        }else if(!String.isBlank(dpaId)){
            //查询已存在的特价申请
            List<DiscountProductApplication__c> dpaList = [select id,name,ApplicationStatus__c,RawAccount__c,RawAccount__r.name,AimsAccount__c,AimsAccount__r.name from DiscountProductApplication__c where id = :dpaId ];
            if(dpaList.size()>0){
                dpa = dpaList[0];
            }
            String dpadId = dpa.Id;
            AimsAccountId = dpa.AimsAccount__c;
            AimsAccount = dpa.AimsAccount__r.name;
            if(String.isBlank(AimsAccountId)){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '医院没有选择'));
            }
            //ahl = new Agency_Hospital_Link__c();
            //ahl.Agency__c = dpa.RawAccount__c;
            List<DiscountProductApplicationDetail__c> dpadList = new List<DiscountProductApplicationDetail__c>();
            //申请明细查询
            dpadList = [select id,name,DiscountProductApplication__c,ProductDiscount__c,Product2__c,Product2__r.name,Product2__r.name__c,Product2__r.OT_CODE_Text__c,
            Product2__r.SFDA_Status__c,Product2__r.Intra_Trade_List_RMB__c,Product2__r.Foreign_Trade_List_US__c,
            Product2__r.Category2__c,Product2__r.Category3__c,Product2__r.Category4__c,Product2__r.Category5__c 
            from DiscountProductApplicationDetail__c where DiscountProductApplication__c =: dpadId];
            
            //赋值给临时class类
            for(DiscountProductApplicationDetail__c dpadc:dpadList){
                
                DiscountProductApplicationDetail dpad = 
                new DiscountProductApplicationDetail(dpadc);
                    DiscountProductApplicationDetailList.add(dpad);
            }
        }
    }
    //保存更新
    public PageReference savesaveDiscountProduct(){
        Integer FLG = 0;
        Integer Count = 0;
        for(DiscountProductApplicationDetail dpadl: DiscountProductApplicationDetailList){
            FLG = FLG + 1;
            if(dpadl.check == false){ 
                Count = Count + 1;
            }
        }
        if(Count == FLG){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择要维护的特价产品'));
            return null;
        }
 
 
        if(!String.isBlank(AimsAccountId) && !String.isBlank(dpa.RawAccount__c)){
            
            dpa.AimsAccount__c = AimsAccountId;
            dpa.RawAccount__c = dpa.RawAccount__c;
            dpa.ApplicationStatus__c = '草案中';
            //add by rentx start 2021-01-22 设置记录类型
            dpa.RecordTypeId = [select Id from RecordType where IsActive = true and DeveloperName = 'HospitalSpecials'].Id;
            //add by rentx end 2021-01-22 设置记录类型
 
            upsert dpa;
        } else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '医院没有选择'));
            return null;
        }
        dpadlList = new List<DiscountProductApplicationDetail__c>();
        List<DiscountProductApplicationDetail__c> dpadDeleteList = new List<DiscountProductApplicationDetail__c>();
        for(DiscountProductApplicationDetail dpadl: DiscountProductApplicationDetailList){
            //特价产品申请明细    
            DiscountProductApplicationDetail__c dpad = new DiscountProductApplicationDetail__c();
            //更新
            if(!String.isBlank(dpadl.dp.id)){
                if(dpadl.check == true && dpadl.dp.ProductDiscount__c != null){
                    //dpad.DiscountProductApplication__c = dpadUpsert.id;
                    dpad.id = dpadl.dp.id;
                    
                    dpad.ProductDiscount__c = dpadl.dp.ProductDiscount__c;
                    dpad.Product2__c = dpadl.Pr2.id;
                    dpadlList.add(dpad);
                }else{
                    dpad.id = dpadl.dp.id;
                    dpadDeleteList.add(dpad);
                }
                
            }else if(dpadl.check == true){
                //新规
                //特价产品申请
                dpad.DiscountProductApplication__c = dpa.id;
                dpad.ProductDiscount__c = dpadl.dp.ProductDiscount__c;
                dpad.Product2__c = dpadl.Pr2.id;
                dpadlList.add(dpad);
              
            }
            //产品价格输入警告  为null是删除
            // else if (dpadl.check == true && dpadl.dp.ProductDiscount__c == null) {
                //throw new ControllerUtil.myException('选中的产品特价不可为空');
 
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '选中的产品特价不可为空'));
                //dpadlFalg = false;
                // return null;
            // }
        }
        upsert dpadlList;
        delete dpadDeleteList;
        dpaId = dpa.id;
        return ProductApplicationEdit();  
    }
 
    // 进入特价申请页面
    public PageReference ProductApplicationEdit() {
        PageReference pg;
        pg = new PageReference('/' + dpaId);
        return pg;
    }
 
    //查询全部商品检索
    /*public void  searchAllProduct2(){
 
 
        List<DiscountProductApplicationDetail> dpadNewList = new List<DiscountProductApplicationDetail>();
        for (DiscountProductApplicationDetail dpad : DiscountProductApplicationDetailList) {
            if (dpad.check == true) {
                dpadNewList.add(dpad);
                dpadIdList.add(dpad.pr2.id);
            }
        }
 
        List<Product2__c> Product2FalseList = new List<Product2__c>();
        soql = this.makeSoql1(text1,text2);
        Product2FalseList = Database.query(soql);
 
        if(Product2FalseList.size() >=1 ){
            for(Product2__c Pro2False : Product2FalseList){
                DiscountProductApplicationDetail dpad = 
                new DiscountProductApplicationDetail(Pro2False);
                dpad.check = false;
                dpadNewList.add(dpad);
                
            }
        }
        DiscountProductApplicationDetailList = new List<DiscountProductApplicationDetail>();
        for(DiscountProductApplicationDetail dpad : dpadNewList){
            DiscountProductApplicationDetailList.add(dpad);
        }
   }*/
   public void beforeSearch(){}
   public void  searchAllProduct2(){
    if (String.isBlank(AimsAccountId)) {
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请选择医院'));
 
    }
 
        //查询该经销商医院下的所有特价产品
        // gzw 修复数据源查询问题
        List<hospitalprice__c> hpList = [Select id,product__c,mPrice__c from hospitalprice__c where account__c = :dpa.RawAccount__c and hospital__c = :AimsAccountId];
        // List<hospitalprice__c> hpList = [Select id,product__c,mPrice__c from hospitalprice__c where hospital__c = :AimsAccountId];
        Map<Id,Decimal> pdMap = new Map<Id,Decimal>();
        if (hpList != null && hpList.size() > 0) {
            for (hospitalprice__c hp : hpList) {
                pdMap.put(hp.product__c, hp.mPrice__c);
            }
        }
 
        List<DiscountProductApplicationDetail> dpadNewList = new List<DiscountProductApplicationDetail>();
        for (DiscountProductApplicationDetail dpad : DiscountProductApplicationDetailList) {
            if (dpad.check == true) {
                dpadNewList.add(dpad);
                dpadIdList.add(dpad.pr2.id);
            }
        }
        //查询所有产品
        List<Product2__c> Product2FalseList = new List<Product2__c>();
        soql = this.makeSoql1(text1,text2);
        Product2FalseList = Database.query(soql);
        List<DiscountProductApplicationDetail> dpadNewList1 = new List<DiscountProductApplicationDetail>();
 
        if(Product2FalseList.size() >=1 ){
            for(Product2__c Pro2False : Product2FalseList){
                DiscountProductApplicationDetail dpad = 
                new DiscountProductApplicationDetail(Pro2False);
                dpad.check = false;
                dpadNewList1.add(dpad);
            }
        }
        DiscountProductApplicationDetailList = new List<DiscountProductApplicationDetail>();
        for(DiscountProductApplicationDetail dpad : dpadNewList){
            DiscountProductApplicationDetailList.add(dpad);
        }
        for(DiscountProductApplicationDetail dpad : dpadNewList1){
            if (pdMap.containsKey(dpad.Pr2.Id)) {
                dpad.dismoney = pdMap.get(dpad.Pr2.Id);
                dpad.dp.ProductDiscount__c = pdMap.get(dpad.Pr2.Id);
            }
            DiscountProductApplicationDetailList.add(dpad);
        }
   }
 
    //拼接检索产品检索soql
    private String makeSoql1(String text1,String text2) {
 
        soql = 'select id,name,OT_CODE_Text__c,SFDA_Status__c,Intra_Trade_List_RMB__c,Foreign_Trade_List_US__c,Category2__c,Category3__c,Category4__c,Category5__c,Name__c from Product2__c where Pro2_Dealer_ENG__c = true and id not in:dpadIdList';
        soql += ' and Estimation_Entry_Possibility__c = \'○\'';
        if (!String.isBlank(text1)) {
            soql += ' and Name__c like \'%' + text1 + '%\' ';
        }
        if (!String.isBlank(text2)) {
            soql += ' and OT_CODE_Text__c like \'%' + text2 + '%\' ';
        }
        return soql;
    }
 
    //页面显示产品明细构造类
    class DiscountProductApplicationDetail {
        public Product2__c Pr2 { get; set; }
        public DiscountProductApplicationDetail__c dp { get; set; }
        public Boolean check { get; set; }
        public Decimal dismoney {get;set;}
        public DiscountProductApplicationDetail(){
 
        }
        public DiscountProductApplicationDetail(Product2__c Pro2){
            Pr2 = Pro2;
            dp = new DiscountProductApplicationDetail__c();
            check = false;
        }
        public DiscountProductApplicationDetail(DiscountProductApplicationDetail__c dpadc){
            dp = dpadc;
            pr2 = dpadc.Product2__r;
            check = true;
            dismoney = dpadc.ProductDiscount__c;
        }
    }
 
}