高章伟
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
public with sharing class SubmitForApprovalController {
    //维修合同失单数据
    public Lost_Report__c lostData {
        get;
        set;
    }
    //ID作为SQL的条件
    public String Id {
        get;
        set;
    }
    public boolean flag {
        get;
        set;
    }
    public Boolean canseePage {
        get;
        set;
    }
    public String mcId {
        get;
        set;
    }
    //构造器
    public SubmitForApprovalController() {
        //从地址栏获取ID
        Id = System.currentPageReference().getParameters().get('id');
        mcId = System.currentPageReference().getParameters().get('mcId');
        // 维修合同失单数据
        lostData = new Lost_Report__c();
        flag = false;
    }
    public SubmitForApprovalController(ApexPages.StandardController controller) {
        this();
    }
    //初始化方法
    public void init() {
        if (String.isNotBlank(Id)) {
            canseePage = false;
            List < Lost_Report__c > lostDataList = new List < Lost_Report__c > ();
            String lostReportSql = lostOrder(Id);
            lostDataList = Database.query(lostReportSql);
            if (lostDataList.size() > 0) {
                lostData = lostDataList[0];
                mcId = lostData.Maintenance_Contract__c;
            }
            if (lostData.Status__c == '提交' || lostData.Status__c == '审批中' || lostData.Status__c == '批准') {
                canseePage = true;
                return;
            }
        } else {
            lostData.Maintenance_Contract__c = mcId;
            lostData.Status__c = '草案中';
            // insert lostData;
        }
    }
    //保存方法
    public PageReference save() {
        flag = false;
        try {
            Boolean resultSaveFlag = dataInspect();
            if (!resultSaveFlag) {
                ControllerUtil.lostOrder(lostData);
                flag = true;
            }
        } catch (Exception e) {
            ApexPages.addmessages(e);
            return null;
        }
        return null;
    }
    //提交审批方法
    public PageReference submit() {
        save();
        
        lostData.Status__c = '提交';
        try {
            ControllerUtil.lostOrder(lostData);
            flag = true;
        } catch (Exception e) {
            ApexPages.addmessages(e);
            return null;
        }
        return null;
    }
    //数据检查警告
    public Boolean dataInspect() {
        boolean result = false;
        if (lostData.Specific_Reasons__c == null) {
            lostData.Specific_Reasons__c.addError('请选择你的具体原因');
            result = true;
        }
        if (lostData.To_Where__c == null) {
            lostData.To_Where__c.addError('请选择您的去向');
            result = true;
 
        }
        if (lostData.Specific_Reasons__c == '其他(手写)') {
            if (String.isBlank(lostData.Other_Reasons__c)) {
                lostData.Other_Reasons__c.addError('具体原因为"其他"时,应详细说明!');
                result = true;
            }
        } else {
            lostData.Other_Reasons__c = null;
        }
        if (lostData.To_Where__c == '医院选择第三方') {
            if (String.isBlank(lostData.Third_Party_Company__c)) {
                lostData.Third_Party_Company__c.addError('去向为"第三方公司",应详细说明第三方名称');
                result = true;
            }
            if (lostData.Third_Party_Contract_Price__c == null) {
                lostData.Third_Party_Company__c.addError('去向为"第三方公司",应详细说明第三方成交价格');
                result = true;
            }
        } else {
            lostData.Third_Party_Company__c = null;
            lostData.Third_Party_Contract_Price__c = null;
        }
        if (lostData.To_Where__c == '其他(手写)') {
            if (String.isBlank(lostData.Other__c)) {
                lostData.Third_Party_Company__c.addError('去向为"其他"应详细说明!');
                result = true;
            }
        } else {
            lostData.Other__c = null;
        }
        return result;
    }
    //查询具体原因的sql
    public String lostOrder(String Id) {
        String lostReportSql = 'SELECT id,Status__c,Other_Reasons__c,Other__c,Third_Party_Company__c,Third_Party_Contract_Price__c,To_Where__c,Specific_Reasons__c,Maintenance_Contract__c from Lost_Report__c where Id = \'' + Id + '\'';
        return lostReportSql;
    }
}