高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public with sharing class RentalApplyJumpController {
    /*--------- private ---------*/
    public Id objId {get; set;}            // 申请单ID
    private String raesIds {get; set;}      // 被选中的一览记录Id
    public String saveStatus {get; set;}    // 插队结果: 'ok'--成功
    /*--------- public(画面表示用) ---------*/
    public List<LineInfo> infoList {get; set;}   // 符合插队条件的被选中的一览记录
    public Id RaId {get; private set;}   // 申请单Id
    public Integer infoListSize { 
        get {
            return infoList == null ? 0 : infoList.size();
        }
    }
    public String errorMessage;
    List<Rental_Apply_Equipment_Set_Detail__c> raesdList;
    public RentalApplyJumpController() {
        this.objId = ApexPages.currentPage().getParameters().get('objId');
        this.raesIds = ApexPages.currentPage().getParameters().get('raesIds');
        RaId = this.objId;
        raesdList = new List<Rental_Apply_Equipment_Set_Detail__c>();
    }
    public void init() {
        infoList = new List<LineInfo>();
        try{
            if (String.isBlank(this.objId)) {
                throw new ControllerUtil.myException('请设置备品借出申请的Id');
            }
            if (String.isBlank(this.raesIds)) {
                throw new ControllerUtil.myException('请设置备品借出申请一览的Id');
            }
            check();
            for(Rental_Apply_Equipment_Set_Detail__c rd:raesdList){
                LineInfo lf = new LineInfo(rd);
                infoList.add(lf);
            }
        } catch (ControllerUtil.myException me) {
            System.debug('myException caught when init: ' + me.getMessage());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, me.getMessage()));
            errorMessage = me.getStackTraceString();
            return;
        } catch (Exception e) {
            System.debug('Exception caught when init: ' + e.getStackTraceString());
            ApexPages.addMessages(e);
            errorMessage = e.getStackTraceString();
            return;
        }
 
    }
    public PageReference saveJump() {
        check();
        List<Rental_Apply_Equipment_Set_Detail__c> updList = new List<Rental_Apply_Equipment_Set_Detail__c>();
        // 明细行check
        Integer cntSelect = 0;
        Boolean hasError = false;
        for (LineInfo line : infoList) {
            if (line.isSelect == true) {
                if (String.isBlank(line.raesd.jumpReason__c)) {
                    line.raesd.jumpReason__c.addError('请输入插队原因');
                    hasError = true;
                }
                cntSelect += 1;
            }
        }
        if (cntSelect == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '未选择插队的明细。'));
            hasError = true;
        }
        if (hasError == true) {
            return null;
        }
        // 保存逻辑
        for (LineInfo line : infoList) {
            if (line.isSelect == true) {
                Rental_Apply_Equipment_Set_Detail__c upd = new Rental_Apply_Equipment_Set_Detail__c();
                upd = line.raesd;
                // upd.Allow_Adjust_Queue_Flag__c = line.raesd.Allow_Adjust_Queue_Flag__c;
                upd.Allow_Adjust_Queue_Flag__c = true;
                upd.jumpReason__c = line.raesd.jumpReason__c;
                updList.add(upd);
            }
        }
        FixtureUtil.withoutUpdate(updList);
        saveStatus = 'ok';
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '保存完了。'));
        // 发送邮件
        return null;
    }
    public void check(){
        // 判断是否存在状态和Id和父申请单都符合的选中一览记录
        String[] ids = raesIds.split(',');
        // 分单条件之一览状态--'待分配', '排队中', 
        // '暂定分配'
        Set<String> statusRaesSet = new Set<String>{FixtureUtil.raesStatusMap.get(FixtureUtil.RaesStatus.Dai_Fen_Pei.ordinal()),
                FixtureUtil.raesStatusMap.get(FixtureUtil.RaesStatus.Pai_Dui_Zhong.ordinal())
                // ,FixtureUtil.raesStatusMap.get(FixtureUtil.RaesStatus.Zan_Ding_Fen_Pei.ordinal())
                };
        raesdList = [
                SELECT Id, Name,RAESD_Status__c,Rental_Apply__c,Fixture_Model_No__c,Rental_Apply_Equipment_Set__c,
                 Allow_Adjust_Queue_Flag__c,jumpReason__c 
                FROM Rental_Apply_Equipment_Set_Detail__c
                WHERE  Is_Body__c = true 
                AND Rental_Apply_Equipment_Set__c in :ids
                AND Rental_Apply__c = :this.objId
                AND RAESD_Status__c in :statusRaesSet];
        if(raesdList.size() != ids.size()) {
            throw new ControllerUtil.myException('已选一览状态不符合插队条件,请选择明细状态都是 待分配与排队中的一览');
        }
    }
    class LineInfo {
        // 选择
        public boolean isSelect { get; set; }
        // 借出备品set一览 明细
        public Rental_Apply_Equipment_Set_Detail__c raesd { get; set; }
        public LineInfo(Rental_Apply_Equipment_Set_Detail__c r) {
            isSelect = false;
            raesd = r;
        }
    }
}