liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
//耗材销售状态
//每月跑
global class ConsumableTargetManageBatch1 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date tcsEndDate;
    public Date tcsStartDate;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public Boolean hisFlag;
    global ConsumableTargetManageBatch1(Date startDate,Date endDate,Boolean hisFlag) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.hisFlag=hisFlag;
        this.tcsStartDate=startDate;
        this.tcsEndDate=endDate;
        this.currentMonth=startDate;
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = year+1;
        }
        queryStr = 'SELECT ';
        queryStr += ' FY_SaleStatus_Formula_'+FYear+'__c,';
        queryStr += ' BillLostWarning__c,Thisyear_SaleStatus_formula__c,Consumable_Category1__c,Hospital__c,Consumable_Category2__c,Consumable_Category3__c,SnapshotMonth__c FROM TrackConsumableSalesForecast__c WHERE Consumable_Category3__c!= \'\' AND recordType.Name=\'目标耗材\' And SnapshotMonth__c = :tcsStartDate ';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<TrackConsumableSalesForecast__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 5, 1)){
            FYear = year+1;
        }
        String  OCSMYear= 'FY'+FYear;
 
 
        Set<String> ctmKeySet=new Set<String>();
        Map<String,List<TrackConsumableSalesForecast__c>> tcsMap = new Map<String,List<TrackConsumableSalesForecast__c>>();
 
        for(TrackConsumableSalesForecast__c tcs:scope){
            String key = tcs.Consumable_Category2__c+'-'+tcs.Consumable_Category3__c+'-'+tcs.Hospital__c+'-'+tcs.Consumable_Category1__c;
            ctmKeySet.add(tcs.Hospital__c+'-'+tcs.Consumable_Category2__c+'-'+tcs.Consumable_Category3__c+'-'+OCSMYear);
            if(tcsMap.containsKey(key)){
                tcsMap.get(key).add(tcs);
            }else{
                List<TrackConsumableSalesForecast__c> tcsList = new List<TrackConsumableSalesForecast__c>();
                tcsList.add(tcs);
                tcsMap.put(key,tcsList);
            }
        }
        //List<ConsumableTargetManage__c> ctmList= [SELECT  ConsumableTargetManageKey__c,Hospital__r.Name,productType__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear AND ConsumableTargetManageKey__c in :ctmKeySet];
        String sql='SELECT    ConsumableTargetManageKey__c,Hospital__r.Name,productType__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear AND ConsumableTargetManageKey__c in :ctmKeySet';
 
        ApexPages.StandardSetController con= new ApexPages.StandardSetController(Database.getQueryLocator(sql));
        List<ConsumableTargetManage__c> ctmList=(List<ConsumableTargetManage__c>) con.getRecords();
        for(ConsumableTargetManage__c ctm:ctmList){
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ctm.Hospital__c+'-'+ctm.productType__c;
            if(tcsMap.containsKey(key)){
                List<TrackConsumableSalesForecast__c> tcsList=tcsMap.get(key);
                String statu='';
                Date lastDate=Date.newInstance(FYear-1, 4, 1);
                for(TrackConsumableSalesForecast__c tcs:tcsList){
                    if(tcs.SnapshotMonth__c>=lastDate){
                        lastDate=tcs.SnapshotMonth__c;
                        ctm.ConsumableSalesState__c=(String)tcs.get('FY_SaleStatus_Formula_'+FYear+'__c');
                        ctm.BillLostWarning__c=(tcs.BillLostWarning__c==1);
                    }
                }
            }
        }
 
        if(ctmList.size()>0){
            update ctmList;
        }
 
 
    }
 
    global void finish(Database.BatchableContext BC) {
        //if(this.hisFlag){
        //    Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate,this.hisFlag), 200);
        //}else{
        //    Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate), 200);
        //}
        //20240131 lwt 耗材销量分析维度修改 start
        if(this.hisFlag){
            Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100);
            Integer year = currentMonth.year();
            Integer month = currentMonth.month();
            Date nextMonth;
            if(month == 12){
                nextMonth = Date.newInstance(year+1, 1, 1);
            }else {
                nextMonth = Date.newInstance(year, month+1, 1);
            }
            if(currentMonth < endDate){
                Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate,this.hisFlag), 200);
            }
        //20240131 lwt 耗材销量分析维度修改 end
        }else{
            Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100);
            Integer year = currentMonth.year();
            Integer month = currentMonth.month();
            Date nextMonth;
            if(month == 12){
                nextMonth = Date.newInstance(year+1, 1, 1);
            }else {
                nextMonth = Date.newInstance(year, month+1, 1);
            }
            if(currentMonth < endDate){
                Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate), 200);
            }
        }
    }
}