高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
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
global without sharing class ImportDocTController {
    public List<ImportDocT__c> testList{get; set;}
    public List<testInit> initList{get; set;}
    public List<testInit> newinitList{get; set;}
    public Map<String,ImportDocT__c> testMap{get; set;}
    public List<Map<String,ImportDocT__c>> mapList{get; set;}
    public List<String> strList{get; set;}
    public ImportDocTController() {
        testList = [SELECT Id, Name , code__c from ImportDocT__c];
        testList = new List<ImportDocT__c>();
        initList = new List<testInit>();
        newinitList = new List<testInit>();
    }
    public void init(){
        testMap = new Map<String,ImportDocT__c>();
        testList = [SELECT Id, Name , code__c , Num__c from ImportDocT__c order by code__c desc];
        // for (ImportDocT__c nObj : testList) {
        //     if (testMap==null) {
        //         testMap.put(nObj.code__c, nObj);
        //     }else{
        //         testMap.put(nObj.code__c, nObj);
        //     }
        //     if (testMap.size() > 0) {
        //         mapList.add(testMap);
        //     }
        // }
        // for (ImportDocT__c mObj : testMap) {
        //     strList.add(testMap.get(mObj.code__c).size());
        // }
        Integer n = 0; // 初始化合并行数
        for (Integer i=0;i<testList.size() ;i++ ) {
            testInit init = new testInit();
            if (i==0) {
                init.Name = testList[i].Name;
                init.code = testList[i].code__c;
                init.Num = testList[i].Num__c;
                init.Id = testList[i].Id;
                n = n+1;
                if (i != testList.size()-1) {
                    // 判断第一个与第二个是否一样,不一样赋值1
                    if (testList[i].code__c != testList[i+1].code__c) {
                        init.count = n;
                    }
                }
            }else {
                // 判断与前一个编码是一致
                if (testList[i].code__c == testList[i-1].code__c) {
                    init.Name = testList[i].Name;
                    init.code = testList[i].code__c;
                    init.Num = testList[i].Num__c;
                    init.Id = testList[i].Id;
                    n = n+1;
                    // 判断是不是最后一个
                    if (i != testList.size()-1) {
                        // 不是最后一个,判断与下一个编码一致不一致,不一致直接赋值
                        if (testList[i].code__c != testList[i+1].code__c) {
                            init.count = n;
                        }
                    }
                    // 最后一个直接赋值
                    if (i == testList.size()-1) {
                        init.count = n;
                    }
                }else {
                    // 下一组开始
                    n = 0;
                    init.Name = testList[i].Name;
                    init.code = testList[i].code__c;
                    init.Num = testList[i].Num__c;
                    init.Id = testList[i].Id;
                    n = n+1;
                }
            }
            initList.add(init);
        }
        // 倒序显示
        for (Integer i = initList.size()-1; i>=0 ; i--) {
            testInit init = new testInit();
            init = initList[i];
            newinitList.add(init);
        }
    }
    // 定义内部类,自定义参数
    class testInit{
        public Boolean check{get; set;}
        public String Name{get; set;}
        public String code{get; set;}
        public Decimal Num{get; set;}
        public String Id{get; set;}
        public Integer count{get; set;}
    }
}