高章伟
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
public class SelectAssetExtension {
 
    private String targetHospitalId { get; set; }
 
    private String targetContractId { get; set; }
 
    // 引数のサービス契約の情報を格納する。
    public Maintenance_Contract__c contract { get; set; }
 
    // 病院配下の納入機器の情報を格納する。
    //public List<Asset> assetRecords { get; set; }//20191220 modify
    //public static  List<Asset> assetRecords  = new List<Asset>();
    public List<List<Asset>> assetLists { get; set; }
 
    public Integer getProductCount() {
        //return this.assetRecords.size();
        Integer listSize = assetLists.size();
 
        Integer pCount = 0;
        if (listSize > 0) {
            for (Integer i = 0; i < listSize; i++) {
                List<Asset> aList = assetLists.get(i);
                pCount += aList.size();
            }
        }
        return pCount;
    }
 
    // 選択済みの納入機器の
    public List<Maintenance_Contract_Asset__c> selectedAsset { get; set; }
    public List<Maintenance_Contract_Asset_Estimate__c> selectedAsset2 { get; set; }
    //public List<Maintenance_Contract_Asset_Estimate__c> selectedAsset2{ get; set; }
    public String sortKey { get; set; }
    public String preSortKey { get; set; }
    public Boolean sortOrderAsc { get; set; }
 
    public String[] sortOrder { get; set; }
 
    public String[] columus = new String[] { 'asset.name',
            'asset.SerialNumber',
            'asset.Department_Name__c',
            'asset.Installation_Site__c',
            'asset.Posting_Date__c',
            'asset.Asset_Owner__c'
                                           };
 
    public void init() {
 
        List<Asset> assetRecords  = new List<Asset>();//20191220 add
        // サービス契約の情報を取得
        try {
            this.contract = [SELECT Id, Name, Management_Code__c, Hospital__c, Contract_Start_Date__c, Contract_End_Date__c FROM Maintenance_Contract__c WHERE Id = :this.targetContractId][0];
            this.targetHospitalId = this.contract.Hospital__c;
        } catch ( System.ListException le) {
            this.contract = new Maintenance_Contract__c();
        }
 
        // 納入機器の情報を取得
        if ( this.targetHospitalId == null || ( this.targetHospitalId.length() != 15 && this.targetHospitalId.length() != 18)) {
            //this.assetRecords  = new List<Asset>();
        } else {
            // HWAG-BHD66X start
            assetRecords = [SELECT Id, Name, CheckBox__c, SerialNumber,TransferToOther__c    /*临时用*/,
                            Department_Name__c, Installation_Site__c, Posting_Date__c,
                            Asset_Owner__c,    Product2Id
                            // Gzw 20200826 保存上一期合同临时用 start
                            , Remarks__c
                            // Gzw 20200826 保存上一期合同临时用 end
                            // fxk 2021/9/17 点检对象临时用 star
                            // , Freeze_sign__c
                            // fxk 2021/9/17 点检对象临时用 end
                            FROM Asset
                            WHERE Hospital__c = :this.targetHospitalId
                                                AND ( AssetMark__c != '耗材' OR Product2.Family != 'ET' )
                                                ORDER BY Department_Name__c, Posting_Date__c, SerialNumber];
            system.debug('assetRecords123456789' + assetRecords);
            // HWAG-BHD66X end
        }
 
        // 選択済みの納入機器情報を取得
        this.selectedAsset = [SELECT Id, Name, Check_object__c, Maintenance_Contract__c, LastMContract__c, Asset__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c = :this.targetContractId];
        system.debug('selectedAsset==========>'+selectedAsset);
        //Maintenance_Contract_Asset_Estimate__c
        this.selectedAsset2 = [SELECT Id, Name,Product_Manual__c, Check_object__c,Maintenance_Contract_Estimate__r.Maintenance_Contract__c,Asset__c,Asset__r.product2Id 
        FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__r.Maintenance_Contract__c = :this.targetContractId];
        system.debug('selectedAsset2==========>'+selectedAsset2);
        //SELECT Id, Name, Check_object__c, Asset__c FROM Maintenance_Contract_Asset_Estimate__c WHERE     Maintenance_Contract_Estimate__r.Maintenance_Contract__c = 'a0H1m000002shhL'
        // 選択済みのものにチェックを付ける
        Boolean selected = false;
        for ( Asset local  : assetRecords) {
            selected = false;
            local.Remarks__c = null;
            local.TransferToOther__c = null;
            // local.Freeze_sign__c = false;
            for ( Maintenance_Contract_Asset__c selectedLocal : this.selectedAsset) {
                if ( local.Id == selectedLocal.Asset__c) {
                    local.CheckBox__c = true;
                    local.Remarks__c = selectedLocal.LastMContract__c;
                    local.TransferToOther__c = selectedLocal.Check_object__c;
                    //占用一个复选框字段用于展示“点检对象”
                    // fxk 2021/9/17 点检对象临时用 star
                    // local.Freeze_sign__c = selectedLocal.Check_object__c;
                    // fxk 2021/9/17 点检对象临时用 end
                    selected = true;
                    break;
                }
            }
            for(Maintenance_Contract_Asset_Estimate__c selectedLocal : this.selectedAsset2){
                if(local.Product2Id == selectedLocal.Product_Manual__c){
                    //local.TransferToOther__c = true;
                    local.TransferToOther__c = selectedLocal.Check_object__c;
                    system.debug('TransferToOther__c============>'+local.TransferToOther__c);
                }
                
            }
            if ( selected == true) {
                continue;
            }
        }
        // List<Asset> assetRecordsv = new List<Asset>(); 
        // for(Asset local : assetRecords){
        //      local.Internal_Asset_photo__c = false;
        //      for(Maintenance_Contract_Asset_Estimate__c equipment : this.selectedAsset2){
        //         if(local.Id == equipment.Maintenance_Contract_Estimate__r.Maintenance_Contract__c){
        //             local.Internal_Asset_photo__c = equipment.Check_object__c;
        //         }
        //     }
        //     assetRecordsv.add(local);
        // }
        this.sortKey = '0';
        this.preSortKey = '0';
        this.sortOrderAsc = true;
        this.sortOrder = new String[6];
        this.sortOrder = new String[] { '↓', ' ', ' ', ' ', ' ', ' ',' '};
        
        //system.debug('assetRecordsv            '+assetRecordsv);
        this.cutListSize(assetRecords);
    }
 
    //private void cutListSize() {//20191220 modify
    private void cutListSize(List<Asset> assetRecordsv) {
        this.assetLists = new List<List<Asset>>();
        List<Asset> tempList = new List<Asset>();
        //for (Integer i = 0; i < this.assetRecords.size(); i++) {//20191220 modify
        for (Integer i = 0; i < assetRecordsv.size(); i++) {
            //Asset a = this.assetRecords.get(i);//20191220 modify
            Asset a = assetRecordsv.get(i);
            if (Math.mod(i, 1000) == 0) {
                if ( tempList.size() > 0) {
                    assetLists.add(tempList);
                }
                tempList = new List<Asset>();
            }
            tempList.add(a);
        }
        if (tempList.size() > 0) {
            this.assetLists.add(tempList);
        }
 
        //this.assetRecords.clear()//20191220 modify
        assetRecordsv.clear();
    }
 
    //private void mergeList() {//20191220 modify
    private List<Asset> mergeList() {
        List<Asset> assetRecords  = new List<Asset>();//20191220 add
        Integer listSize = this.assetLists.size();
        if (listSize == 1) {
            assetRecords.addAll(this.assetLists.get(0));
        } else {
            for (Integer i = 0; i < listSize; i++) {
                assetRecords.addAll(this.assetLists.get(i));
            }
        }
        return assetRecords;//20191220 add
    }
 
    /**
     * Visaulforceから呼ばれるコンストラクタです。
     */
    public SelectAssetExtension( ApexPages.StandardController controller) {
        this.targetContractId = (String)(ApexPages.currentPage().getParameters().get( 'targetContractId'));
        this.init();
    }
 
    /**
     * 保存メソッドです。
     */
    public PageReference save() {
        // Map<String,Boolean> mCAssetEstMap = new Map<String,Boolean>();
        // List<Maintenance_Contract_Asset_Estimate__c> mCAssetEstList = [select Id, Check_Object__c, Maintenance_Contract_Estimate__c, Product_Manual__r.Name 
        //                                                                 from Maintenance_Contract_Asset_Estimate__c 
        //                                                                 where Check_Object__c = true and Maintenance_Contract_Estimate__r.Estimation_Decision__c = true 
        //                                                                 and Maintenance_Contract_Estimate__r.Maintenance_Contract__c =: targetContractId ];
        // for(Maintenance_Contract_Asset_Estimate__c mCAssetEstinfo : mCAssetEstList){
        //     mCAssetEstMap.put(mCAssetEstinfo.Product_Manual__r.Name, mCAssetEstinfo.Check_Object__c);
        // }
        //this.mergeList();//20191220 modify
        List<Asset> assetRecords  = new List<Asset>();//20191220 add
        assetRecords = this.mergeList();
        List<Maintenance_Contract_Asset__c> insertTarget = new List<Maintenance_Contract_Asset__c>();
        Maintenance_Contract_Asset__c target = null;
        for ( Asset input : assetRecords) {
            if ( input.CheckBox__c == true) {
                target = new Maintenance_Contract_Asset__c();
                target.Maintenance_Contract__c = this.targetContractId;
                target.Asset__c = input.Id;
                target.LastMContract__c = input.Remarks__c;
                target.Check_object__c = input.TransferToOther__c;
                // fxk 2021/9/17 点检对象临时用 star
                // target.Check_object__c = input.Freeze_sign__c;
                // fxk 2021/9/17 点检对象临时用 end
                insertTarget.add( target);
            }
        }
 
        // delete and insertする
        if ( this.selectedAsset.size() > 0) {
            delete selectedAsset;
        }
        if ( insertTarget.size() > 0) {
            insert insertTarget;
        }
        system.debug('insertTarget=============>'+insertTarget);
        return cancel();
    }
 
    /**
     * キャンセルメソッドです。
     */
    public PageReference cancel() {
        PageReference ret = null;
        if ( this.targetContractId != null) {
            ret = new PageReference( '/' + this.targetContractId);
        }
        return ret;
    }
 
    /**
     * ソートメソッドです。
     */
    public void sortTable() {
 
        System.debug( '-----:sortTable start!!');
        //this.mergeList();//20191220 modify
        List<Asset> assetRecords  = new List<Asset>();//20191220 add
        assetRecords = this.mergeList();
        // check済み退避
        List<Asset> checkedAssets = new List<Asset>();
        for ( Asset local : assetRecords) {
            if ( local.checkBox__c == true) {
                checkedAssets.add( local);
            }
        }
 
        if ( this.sortKey == this.preSortKey) {
            // 方向が変わるのみ
            this.sortOrderAsc = !this.sortOrderAsc;
            this.sortOrder[Integer.valueOf( this.sortKey)] = ( this.sortOrderAsc == true ? '↓' : '↑');
        } else {
            this.sortOrderAsc = true;
            this.sortOrder[Integer.valueOf( this.preSortKey)] = ' ';
            this.sortOrder[Integer.valueOf( this.sortKey)] = ( this.sortOrderAsc == true ? '↓' : '↑');
        }
        this.preSortKey = this.sortKey;
        // HWAG-BHD66X start
        string sqlstr = 'SELECT Id, Name, CheckBox__c,' +
                        'SerialNumber, Department_Name__c, ' +
                        ' Installation_Site__c, Posting_Date__c, ' +
                        'Asset_Owner__c FROM Asset WHERE Hospital__c = \'' +
                        this.targetHospitalId +
                        '\' AND ( AssetMark__c != \'耗材\' OR Product2.Family != \'ET\' )' +
                        ' ORDER BY ' + this.columus[Integer.valueOf(this.sortKey)]
                        + ' ' + (this.sortOrderAsc == true ?
                                 'ASC NULLS FIRST' : 'DESC NULLS LAST');
        System.debug(sqlstr);
        assetRecords = Database.query(sqlstr);
        // HWAG-BHD66X end
        // 選択済みのものにチェックを付ける
        Boolean selected = false;
        for ( Asset local  : assetRecords) {
            selected = false;
            local.Remarks__c = null;
            for ( Asset selectedLocal : checkedAssets) {
                if ( local.Id == selectedLocal.Id) {
                    local.CheckBox__c = true;
                    local.Remarks__c = selectedLocal.Remarks__c;
 
                    selected = true;
                    break;
                }
            }
            if ( selected == true) {
                continue;
            }
        }
        this.cutListSize(assetRecords);
 
        System.debug( '-----:sortTable end!!');
 
    }
}