global class RenewTargetToAsotBatch implements Database.Batchable<sObject> {
    public String query;
    private BatchIF_Log__c iflog;
    public Date toDayTime = Date.today();
    public String OCSM_Period_half;
    public String OCSM_Period;
    public List < String > accList;
    global RenewTargetToAsotBatch() {
        this.query = query;
        OCSM_Period = 'FY'+(toDayTime.year()+1);
    }

    global RenewTargetToAsotBatch(List < String > accList) {
        this.query = query;
        OCSM_Period = 'FY'+(toDayTime.year()+1);
        this.accList = accList;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        if (toDayTime.month() >= 4 && toDayTime.month() <= 9) {
            OCSM_Period_half = '1H';
        }else{
            OCSM_Period_half = '2H';
        }
        if (accList != null && accList.size() > 0) {
            query ='select id,name,IF_Renewalrate_Target_Asset__c,Hospital__c,Product2.ServiceCategory__c '
                    + 'from asset  where IF_Renewalrate_Target_Asset__c= \'1\' and id In :accList';
        }else{
            query = 'select id,name,IF_Renewalrate_Target_Asset__c,Hospital__c,Product2.ServiceCategory__c '
                    +' from asset  where IF_Renewalrate_Target_Asset__c= \'1\'';
        }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Asset> assList) {
        system.debug('执行execute');
        Set<String> accIds = new Set<String>();
        for(Asset ass1:assList){
            accIds.add(ass1.Hospital__c);
        } 
        Set<String> accIdSet = new Set<String>();
        List<Account_Service_Of_Target__c> targetObjs = new List<Account_Service_Of_Target__c>(); 
        Map<String,Account_Service_Of_Target__c> targetMap = new Map<String,Account_Service_Of_Target__c>();
        for(Account_Service_Of_Target__c target:[SELECT Id ,Target_Rigid_Mirror_2__c,Target_Soft_Mirror_2__c,Target_Correlation_Lightsource__c,Account_HP__c,OCSM_Period_half__c 
                                                FROM Account_Service_Of_Target__c 
                                                WHERE OCSM_Period_half__c = :OCSM_Period_half 
                                                AND OCSM_Period__c = :OCSM_Period
                                                AND Account_HP__c IN:accIds]){
            targetMap.put(target.Account_HP__c,target);
        }
        for(Asset ass:assList){
            // 避免重复
            if(!accIdSet.contains(ass.Hospital__c)){
                // 如果查询到Account_Service_Of_Target__c直接取出来用
                if(targetMap.containskey(ass.Hospital__c)){
                    Account_Service_Of_Target__c asot = targetMap.get(ass.Hospital__c);
                    asot.Renew_Target_Soft_Mirror_2__c = 0;
                    asot.Renew_Target_Rigid_Mirror_2__c = 0;
                    asot.Renew_Target_Correlation_Lightsource__c = 0;
                }else{
                    // 如果没有查询到Account_Service_Of_Target__c，新建
                    Account_Service_Of_Target__c asot = new Account_Service_Of_Target__c();
                    asot.Account_HP__c = ass.Hospital__c;
                    asot.Coverage_Target_Account__c = true;
                    asot.Renew_Target_Soft_Mirror_2__c = 0;
                    asot.Renew_Target_Rigid_Mirror_2__c = 0;
                    asot.Renew_Target_Correlation_Lightsource__c = 0;
                    asot.OCSM_Period__c = OCSM_Period; 
                    asot.OCSM_Period_half__c = OCSM_Period_half;
                    targetMap.put(ass.Hospital__c,asot);
                }
                accIdSet.add(ass.Hospital__c);
            }

            if (ass.Product2.ServiceCategory__c == '硬性镜') {
                targetMap.get(ass.Hospital__c).Renew_Target_Soft_Mirror_2__c ++;
            }
            if(ass.Product2.ServiceCategory__c =='软性镜'){
                targetMap.get(ass.Hospital__c).Renew_Target_Rigid_Mirror_2__c ++;
            }
            if (ass.Product2.ServiceCategory__c == '周边') {
                targetMap.get(ass.Hospital__c).Renew_Target_Correlation_Lightsource__c ++;
            }

        }
        UpSert targetMap.values();
    }

    global void finish(Database.BatchableContext BC) {
        
    }
}