高章伟
2022-02-18 650847118307a1c9ae0ada15b7c69bbf5792c54c
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
public with sharing class NewDiagnosisPartController {
 
    public Diagnosis_Part__c diagnosisPart {get; set;}
    public String swoId {get; set;}
    public String diagnosisPartId {get; set;}
 
    public String baseUrl { get; set; }
    public String rtUrl { get; set; }
 
    public Boolean pageClose{ get; set; }
    public Boolean DELIVERED_isCheck{get;set;}
    public Boolean RETURNED_isCheck{get;set;}
 
    public NewDiagnosisPartController() {
        swoId = System.currentPageReference().getParameters().get('swoId');
        diagnosisPartId = System.currentPageReference().getParameters().get('id');
    }
    public NewDiagnosisPartController(ApexPages.StandardController controller) {
        swoId = System.currentPageReference().getParameters().get('swoId');
        diagnosisPartId = System.currentPageReference().getParameters().get('id');
 
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0, path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
    }
 
    public void init() {
        DELIVERED_isCheck = false;
        RETURNED_isCheck = false;
        if (String.isNotBlank(diagnosisPartId)) {
            Schema.DescribeSobjectResult repairPartType = Diagnosis_Part__c.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> repairPart_fields = repairPartType.fields.getMap();
            String soql_repair = 'select ';
            String fields = '';
            for (String field : repairPart_fields.keySet()) {
                if (fields.length() > 0) {
                    fields += ', ';
                }
                fields += field;
            }
            soql_repair += fields;
            soql_repair += ' from Diagnosis_Part__c where Id = \'' + diagnosisPartId + '\'';
            diagnosisPart = Database.query(soql_repair);
            DELIVERED_isCheck = diagnosisPart.DELIVERED__c;
            RETURNED_isCheck = diagnosisPart.RETURNED__c;
        } else {
            diagnosisPart = new Diagnosis_Part__c();
        }
        if (String.isNotBlank(swoId)) {
            diagnosisPart.SWO__c = swoId;
        }
    }
 
    public void save() {
        Boolean isSave = true;
        if(diagnosisPart.DELIVERED__c == false && DELIVERED_isCheck == true){
            diagnosisPart.DELIVERED__c.addError('DELIVERED 不允许取消勾选');
            isSave = false;
        }
        if(diagnosisPart.RETURNED__c == false && RETURNED_isCheck == true){
            diagnosisPart.RETURNED__c.addError('RETURNED 不允许取消勾选');
            isSave = false;
        }
        if(!isSave){
            return;
        }
        String productId = diagnosisPart.Product__c;
        if (String.isNotBlank(productId)) {
            Product2 product = [SELECT Id, Name, Product_ECCode__c, Description FROM Product2 WHERE Id = :productId limit 1];
            diagnosisPart.PART_NUMBER__c = product.Product_ECCode__c;
            diagnosisPart.DESCRIPTION__c = product.Description;
        }
        pageClose = false;
 
        if (diagnosisPart.DELIVERED__c == false  && String.isBlank(diagnosisPart.Id)) {
            Boolean isUpdateSwo = false;
            List<SWO__c> swoList = [select Id,Diagnose_part_not_delievered_1__c,Diagnose_part_not_delievered_2__c,Diagnose_part_not_delievered_3__c from SWO__c where Id=:diagnosisPart.SWO__c];
            System.debug('swoList.size():'+swoList.size());
            if (swoList!=null && swoList.size()!=0) {
                SWO__c swo = swoList[0];
                if(String.isBlank(swo.Diagnose_part_not_delievered_1__c)){
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_1__c = diagnosisPart.PART_NUMBER__c;
                }else if(String.isBlank(swo.Diagnose_part_not_delievered_2__c)){ 
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_2__c = diagnosisPart.PART_NUMBER__c;
                }else if(String.isBlank(swo.Diagnose_part_not_delievered_3__c)){
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_3__c = diagnosisPart.PART_NUMBER__c;
                }
                System.debug('swo.Id:'+swo.Id);
                if(isUpdateSwo) update swo;
            }
        }
 
        upsert diagnosisPart;
        pageClose = true;
        System.debug('id:' + diagnosisPart.Id);
        diagnosisPartId = diagnosisPart.Id;
        return;
    }
 
    public PageReference deleteAction() {
        if (String.isNotBlank(diagnosisPart.Id)) {
            delete diagnosisPart;
        }
        return ReturnSWO();
    }
 
    public PageReference ReturnSWO() {
        String swoId = diagnosisPart.SWO__c;
        String url = '/apex/SWOPage?id=' + swoId + '&type=Diagnosis';
        PageReference  ref = new Pagereference(url);
        ref.setRedirect(true);
         return ref;
    }
}