高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public without sharing class ConsumableOtherArrDetController {
    // 订单 ID
    private String orderId = '';
    public List<Consumable_Orderdetails__c> otherArrDetIifo { get; set; }
    public ConsumableOtherArrDetController() {
        orderId = ApexPages.currentPage().getParameters().get('Id');
        otherArrDetIifo = new List<Consumable_Orderdetails__c>();
    }
 
    // 画面初始化
    public void init() {
        List<showRecords> arrDetIifo = new List<showRecords>();
        List<showRecords> notArrDetIifo = new List<showRecords>();
        List<showRecords> allArrDetIifo = new List<showRecords>();
        // List<Consumable_order_details2__c> conList = [SELECT id,Bar_Code__c FROM Consumable_order_details2__c WHERE Dealer_Arrive__c = true AND Consumable_order_minor__c = :orderId];
        List<Consumable_order_details2__c> conList = [SELECT id,Bar_Code__c FROM Consumable_order_details2__c WHERE Consumable_order_minor__c = :orderId];
        Map<String,String> srtMap = new Map<String,String>();
        for(Consumable_order_details2__c con : conList){
            srtMap.put(con.Bar_Code__c, con.Id);
        }
        List<String> str = new List<String>();
        for(String s : srtMap.keySet()){
            str.add(srtMap.get(s));
        }
        List<AggregateResult> arrDetList = [SELECT Asset_Model_No__c prodModel,count(Id) recordCount FROM Consumable_order_details2__c
                        WHERE Id =: str AND Dealer_Arrive__c = true
                        GROUP BY Asset_Model_No__c];
        for(Integer i = 0 ; i< arrDetList.size();i++){
            arrDetIifo.add(new showRecords(arrDetList[i]));
        }
 
        List<AggregateResult> notArrDetList = [SELECT Asset_Model_No__c prodModel,count(Id) recordCount FROM Consumable_order_details2__c
                        WHERE Dealer_Arrive__c = false
                        AND Consumable_order_minor__c = :orderId
                        GROUP BY Asset_Model_No__c];
        for(Integer i = 0 ; i< notArrDetList.size();i++){
            notArrDetIifo.add(new showRecords(notArrDetList[i]));
        }
 
        // List<AggregateResult> allArrDetList = [SELECT Asset_Model_No__c prodModel,count(Id) recordCount FROM Consumable_order_details2__c
        //                 WHERE Id =: str
        //                 GROUP BY Asset_Model_No__c];
        List<Consumable_Orderdetails__c> allArrDetList = [SELECT Asset_Model_No__c,Consumable_count__c FROM Consumable_Orderdetails__c
                        WHERE Consumable_order__c =: orderId];
        // for(Integer i = 0 ; i< allArrDetList.size();i++){
        //     allArrDetIifo.add(new showRecords(notArrDetList[i]));
        // }
        Map<String,Decimal> arrDetMap = new Map<String,Decimal>();
        for(showRecords arr : arrDetIifo){
            arrDetMap.put(arr.prodModel, arr.recordCount);
        }
        for(showRecords notarr : notArrDetIifo){
            if(arrDetMap.containsKey(notarr.prodModel)){
                arrDetMap.put(notarr.prodModel, arrDetMap.get(notarr.prodModel)+notarr.recordCount);
            }else{
                arrDetMap.put(notarr.prodModel, notarr.recordCount);
            }
        }
        List<String> AssetModelNoEdList = new List<String>();//20200904 ljh add 
        for(Consumable_Orderdetails__c allarr : allArrDetList){
            for(String promodel : arrDetMap.keySet()){
                if(allarr.Asset_Model_No__c == promodel){
                    if((allarr.Consumable_count__c - arrDetMap.get(promodel)) > 0){
                        allarr.Consumable_count__c = (allarr.Consumable_count__c - arrDetMap.get(promodel));
                        otherArrDetIifo.add(allarr);
                    }
                    AssetModelNoEdList.add(promodel);//20200904 ljh add 
                }
            }
        }
        //20200904 ljh add  start
        for(Consumable_Orderdetails__c allarr1 : allArrDetList){
            if(!AssetModelNoEdList.contains(allarr1.Asset_Model_No__c)){
                otherArrDetIifo.add(allarr1);
            }
        }
        //20200904 ljh add  end
        // for(Consumable_Orderdetails__c allarr : allArrDetIifo){
        //     for(String promodel : arrDetMap.keySet()){
        //         if(allarr.prodModel == promodel){
        //             if((allarr.recordCount - arrDetMap.get(promodel)) >= 0){
        //                 allarr.recordCount = (allarr.recordCount - arrDetMap.get(promodel));
        //                 otherArrDetIifo.add(allarr);
        //             }
        //         }
        //     }
        // }
    }
 
    // Data Bean
    class showRecords implements Comparable {
 
        public Decimal recordCount { get; set; }
        public String prodModel { get; set; }
 
 
        public showRecords(AggregateResult e) {
            recordCount =Integer.valueOf(e.get('recordCount'));
            prodModel = String.valueOf(e.get('prodModel'));
        }
        // 排序
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }
}