高章伟
2022-03-18 a0bc3e3af59aed35334bdc38256a7a1dfe8aecf3
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
public with sharing class CM_SearchContactServiceController {
    public Contact newCon { get; set; }
    public Contact searchCon { get; set; }
    public List<LineInfo> lineInfoList { get; set; }
    public String conId { get; set; }
 
    public String openLine { get; set; }
    private String accountId;
    private String nowValue;
 
    public CM_SearchContactServiceController() {
        openLine = Apexpages.currentPage().getParameters().get('line');
        accountId = Apexpages.currentPage().getParameters().get('acc');
        nowValue = Apexpages.currentPage().getParameters().get('now');
    }
 
    public void init() {
        searchCon = new Contact();
        if (nowValue != null && nowValue != '') {
            searchCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                                FirstName, LastName
                           from Contact where Id = :nowValue];
            searchCon.Search_LastName__c = searchCon.LastName;
            searchCon.Search_FirstName__c = searchCon.FirstName;
        }
 
        searchContact();
 
        newCon = new Contact();
        newCon.AccountId = accountId;
 
        return;
    }
 
 
    public PageReference searchContact() {
        String searchStr = 'select Id, Name, Department__c, Type__c, AccountName__c, Supplement__c, Phone ';
         searchStr += ', City__c , State__c '; //2018/11/19 HWAG-B399RW 读取省和市
        searchStr += '        from Contact ';
        searchStr += '       where Isactive__c = \'有效\' ';
        searchStr += '         and AccountId = :accountId ';
        if (searchCon.Search_LastName__c != null && searchCon.Search_LastName__c != '') {
            searchStr += '     and LastName like \'%' + searchCon.Search_LastName__c + '%\'';
        }
        if (searchCon.Search_FirstName__c != null && searchCon.Search_FirstName__c != '') {
            searchStr += '     and FirstName like \'%' + searchCon.Search_FirstName__c + '%\'';
        }
        system.debug('=====searchStr:' + searchStr);
 
        List<Contact> searchResult = Database.query(searchStr);
 
        lineInfoList = new List<LineInfo>();
        Integer line = 0;
        for (Contact con : searchResult) {
            line += 1;
            LineInfo li = new LineInfo(line, con);
            lineInfoList.add(li);
        }
 
        editClear();
 
        return null;
    }
 
    public PageReference editContact() {
        if (conId != null && conId != '') {
            newCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                             FirstName, LastName
                        from Contact where Id = :conId];
            newCon.Search_LastName__c = newCon.LastName;
            newCon.Search_FirstName__c = newCon.FirstName;
        }
 
        return null;
    }
 
    public PageReference saveNew()  {
        if (newCon.Search_LastName__c == null || newCon.Search_LastName__c == '') {
            newCon.Search_LastName__c.addError('必须填写。');
            return null;
        }
        newCon.LastName = newCon.Search_LastName__c;
        newCon.FirstName = newCon.Search_FirstName__c;
        upsert newCon;
 
        searchCon.Search_LastName__c = newCon.LastName;
        searchCon.Search_FirstName__c = newCon.FirstName;
        searchContact();
 
        return null;
    }
 
    public PageReference editClear()  {
        newCon = new Contact();
        newCon.AccountId = accountId;
 
        return null;
    }
 
    class LineInfo {
        public Integer lineNo { get; set; }
        public Contact con { get; set; }
 
        public LineInfo(Integer in_line) {
            lineNo = in_line;
            con = new Contact();
        }
 
        public LineInfo(Integer in_line, Contact in_con) {
            lineNo = in_line;
            con = in_con;
        }
    }
}