高章伟
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
global class NewQuoteEntrytheBatch implements Database.Batchable<sObject> {
    public String query;
 
    global NewQuoteEntrytheBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
 
        String quoteLN = '%01';
        query='select Id,Opportunity.Department_Class__c,CreatedDate,OpportunityId from Quote where Quote_No__c like :quoteLN and CreatedDate=TODAY';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Quote> qu) {
        Date createdDateStr;      
         //获取已经生成的询价单
        List<Opportunity> opplist = New List<Opportunity>();
        Opportunity o=new Opportunity();
        List<Opportunity> os=new List<Opportunity>();
        System.debug('this is the point ------------------------------------------!');
        //遍历每一个报价单
        for(Quote oneQuote:qu){
           o=oneQuote.opportunity;
           //获取报价时间
           if (oneQuote.id != null && oneQuote.CreatedDate != null ) {
           // createdDateStr = [select id,CreatedDate from Quote where id = :quoId][0].CreatedDate.date();
           createdDateStr = oneQuote.CreatedDate.date();
           } else {
             createdDateStr = Date.today();
           }
           // 一年前的日期
           Date createdDateYear = createdDateStr.addYears(-1);
           System.debug('----------创建的时间-----------'+createdDateYear);
           //获取报价行信息
           list<QuoteLineItem> qli=[select id,Product2Id,Product2.Category4__c from QuoteLineItem where QuoteId=:oneQuote.id];
           List<String> c4=new List<String>();
           for(QuoteLineItem quit:qli){
             c4.add(quit.Product2.Category4__c);
           }
            System.debug('this is the point2 ------------------------------------------!');
            System.debug('报价行第四分类'+c4);
            System.debug('战略科室'+o.Department_Class__c);
           //获取明细
           List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id, product__c, Bollow_Date__c, Rental_Apply__r.Hospital__c  from Rental_Apply_Equipment_Set_Detail__c
                where Rental_Apply__r.Strategic_dept__c = : o.Department_Class__c and Bollow_Date__c >= :createdDateYear
                 and Category4__c In :c4 and Rental_Apply__r.demo_purpose2__c='试用(无询价)'
                order by Bollow_Date__c];
           System.debug('raesdList'+raesdList[0]);
           Map<String, Date> ProOfDate = new Map<String, Date>();
             for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
             ProOfDate.put(raesd.product__c, raesd.Bollow_Date__c);
           }
           System.debug('-----------proofdate---------:'+ProOfDate);
           o.WhetherTrySpareParts_3m__c = false;
           o.WhetherTrySpareParts_6m__c = false;
           o.WhetherTrySpareParts_1y__c = false;
 
 
 
           //获取一次询价中的每一个物品的询价
           //list<OpportunityLineItem> ols=[select Id__c from OpportunityLineItem where OpportunityId =:o.id];
           //System.debug('-----ols-------'+ols);
           //for (OpportunityLineItem oppitem : ols) {
             //if (ProOfDate.containsKey(oppitem.Id__c)) {
 
 
 
               for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
               System.debug('---------进入了目标循环---------------------------------');
               //Date bollDate = ProOfDate.get(oppitem.Id__c);
               Date bollDate = raesd.Bollow_Date__c;
               System.debug('---------time-------------:'+bollDate);
               System.debug('---------time2-------------:'+createdDateStr);
               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;
               }
               System.debug('o1:'+o.WhetherTrySpareParts_3m__c);
               System.debug('o2:'+o.WhetherTrySpareParts_6m__c);
               System.debug('o3:'+o.WhetherTrySpareParts_1y__c);
           }
 
             //}
             os.add(o);
           //}
        }
        if(os.size()>0){
          try{
            update os;
              System.debug('update successed');
             }
          catch(DmlException e){
                System.debug('系统错误');
             }
        }else{
            System.debug('没有数据');
        }       
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}