高章伟
2022-03-10 1312ba82d4c880bdb5357d28e0d4af5b285f610f
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
global class InquiryVerifyBatch  implements Schedulable,Database.Batchable<SObject>,Database.Stateful {
    public String query;
 
    public Date start_date;
 
    global InquiryVerifyBatch() {
        // if(Test.isRunningTest()){
            this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate = today';
        // }else{
            // this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No__c like \'%01\' and CreatedDate = today';
        // }
        
    }
 
    global InquiryVerifyBatch(Integer year, Integer month) {
        if (year == null) {
            year = 2021;
        }
        if (month == null) {
            month = 4;
        }
        start_date = Date.newInstance(year, month, 1);
 
        this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate >= :start_date';
    }
 
    public void execute(SchedulableContext sc) {
        Database.executeBatch(this);
    }
    // global void execute(SchedulableContext sc){Database.executebatch(this);}
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // 获取报价单数据
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<quote> quotes) {
            
       List<Opportunity> os = new List<Opportunity>();
       // 遍历报价单
        for (quote  quo: quotes) {
            Opportunity o = New Opportunity();
            o = quo.opportunity; 
            Date createdDateStr ;
            // 提出报价单生成当日时间
            if (quo.quoteSavedDate__c!=null){
                createdDateStr = quo.quoteSavedDate__c;
            }else{
                createdDateStr = Date.today();
            }
            // 获取报价产品数据,获取第四分类
             List<QuoteLineItem> qlts = [Select Id,Product2.Category4__c from QuoteLineItem where QuoteId  = :quo.Id and Product2.Important_product__c = true];
             List<String> c4  = new List<String>();
             List<String> qdept  = new List<String>();
             // 第四分类放入list
            for (QuoteLineItem qlt : qlts) {
                c4.add(qlt.Product2.Category4__c);
            }
            
              // 一年前的日期
            Date createdDateYear = createdDateStr.addYears(-1);
 
            // 获取备品配套一览明细
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id, product__c, Bollow_Date__c,Category4__c, Rental_Apply__r.Hospital__c 
                                                                from Rental_Apply_Equipment_Set_Detail__c 
                                                                where Rental_Apply__r.Strategic_dept__c = : quo.Opportunity.Department_Class__c 
                                                                and Category4__c IN :c4 
                                                                and Rental_Apply__r.Demo_purpose2__c = '试用(无询价)'
                                                                and Bollow_Date__c >= :createdDateYear order by Bollow_Date__c limit 200 ];
 
 
              for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
                o.WhetherTrySpareParts_3m__c = false;
                o.WhetherTrySpareParts_6m__c = false;
                o.WhetherTrySpareParts_1y__c = false;
                Date bollDate = raesd.Bollow_Date__c;
                if (bollDate != null && bollDate.addMonths(3) > createdDateStr) {
                    o.WhetherTrySpareParts_3m__c = true;
                }
                if (bollDate != null && bollDate.addMonths(6) > createdDateStr && bollDate.addMonths(3) < createdDateStr) {
                    o.WhetherTrySpareParts_6m__c = true;
                }
                if (bollDate != null && bollDate.addYears(1) > createdDateStr && bollDate.addMonths(6) < createdDateStr) {
                    o.WhetherTrySpareParts_1y__c = true;
                }
              }
            os.add(o);
        }
        if (os.size()>0) {
            update os;
            system.debug('执行成功');
        }else{
             system.debug('执行失败');
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        
    }
}