//  预测消费率计算对应型号的过去3年期间有合同的的全国合同修理金额合计
global class ComputeLTYRepairBatch2 implements Database.Batchable<sObject> {
    public String query;
    public String year;
    public Date timeOne ;
    public Date timeTwo;
    public Date timeThr;
    public Date timeFour;
    public List<String> targetList= new List<String>();
    public List<String> asslist= new List<String>();

    global ComputeLTYRepairBatch2() {
    }

    global ComputeLTYRepairBatch2(List<String> targetList) {

        this.query = query;
        this.targetList = targetList;
        this.timeTwo = date.newInstance(
                                        2023,
                                        4,
                                        31
                                        );
        this.timeThr = date.newInstance(
                                        2020,
                                        4,
                                        1
                                        );
        this.timeFour = this.timeTwo.addDays(1);
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {

        system.debug('targetList=='+targetList);
        //查询符合条件的所有设备
        query = 'select id,product2.Asset_Model_No__c,Category4__c ';
        query +=' from asset where IsCompetitorProduct = false AND ( AssetMark__c != \'耗材\' OR Product2.Family != \'ET\') and RecordTypeId !=\'01210000000kOPRAA2\' and  Return_Flag__c = false   ';
        if (targetList.size()>0 && targetList !=null) {
            query += ' and id in :targetList';
        }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<asset> assetList) {
            // 分类计算，分别赋值，第四分类和产品型号
            List<Asset> assList1 = ComputeLRepair(1,assetList);
            List<Asset> assList2 = ComputeLRepair(2,assetList);             
        try {

            if (assList1.size()>0) {
                update assList1;
            }
            if (assList2.size()>0) {
                update assList2;
            }
            
        } catch (Exception e) {
            system.debug('err:::::' + e.getMessage());
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
 
    private  List<Asset> ComputeLRepair(Integer type,list<asset> assetList) {
            List<String> uniqueAssetModelNo = new List<String>();
            List<String> uniqueCategories = new List<String>();
            for (Asset ass: assetList) {
                // 统计第四分类和 产品分类的集合
                if (!uniqueCategories.contains(ass.Category4__c) && ass.Category4__c !=null && type==1) {
                    uniqueCategories.add(ass.Category4__c);
                }
                if (!uniqueAssetModelNo.contains(ass.product2.Asset_Model_No__c) &&ass.product2.Asset_Model_No__c !=null && type==2) {
                    uniqueAssetModelNo.add(ass.product2.Asset_Model_No__c);
                }
            }
            // 产品型号
            if (type==2) {   
                List<Maintenance_Contract_Asset__c>  mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.product2.Asset_Model_No__c from Maintenance_Contract_Asset__c 
                                                            where ((Maintenance_Contract_StarDate__c>= :this.timeThr  and  Maintenance_Contract_StarDate__c < :this.timeFour) 
                                                                or (Contract_End_Date__c>= :this.timeThr and  Contract_End_Date__c < :this.timeFour)) 
                                                            and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.product2.Asset_Model_No__c in :uniqueAssetModelNo];

                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                // 过去3年

                for (Maintenance_Contract_Asset__c mca:mcaList) {
                    if (NotoAssSize.containsKey(mca.Asset__r.product2.Asset_Model_No__c)) {
                        Decimal ms = NotoAssSize.get(mca.Asset__r.product2.Asset_Model_No__c);
                        ms += mca.Maintenance_Contract__r.MonthSize__c;
                        NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,ms);
                    }else{
                        NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,mca.Maintenance_Contract__r.MonthSize__c);
                    }
                }
               
                for (Asset ass : assetList) {
                    // 按照产品型号，更新对应的设备字段（过去3年有合同的修理实绩和，对应型号的设备总数换算为12个月的件数）

                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            if (NotoAssSize.get(ass.product2.Asset_Model_No__c) !=null) {
                                 assOne.MonthSizeAssetModelNo__c = NotoAssSize.get(ass.product2.Asset_Model_No__c);
                            }
                            updateAss.add(assOne);
                }

                return updateAss;
                //第四分类
            }else if (type==1) {
                List<Maintenance_Contract_Asset__c>  mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.Category4__c   from Maintenance_Contract_Asset__c 
                                                            where ((Maintenance_Contract_StarDate__c>= :this.timeThr  and  Maintenance_Contract_StarDate__c < :this.timeFour) 
                                                                or (Contract_End_Date__c>= :this.timeThr and  Contract_End_Date__c < :this.timeFour)) 
                                                            and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.Category4__c in:uniqueCategories];


                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                
                    for (Maintenance_Contract_Asset__c mca:mcaList) {
                        if (NotoAssSize.containsKey(mca.Asset__r.Category4__c)) {
                            Decimal ms = NotoAssSize.get(mca.Asset__r.Category4__c);
                            ms += mca.Maintenance_Contract__r.MonthSize__c;
                            NotoAssSize.put(mca.Asset__r.Category4__c,ms);
                        }else{
                            NotoAssSize.put(mca.Asset__r.Category4__c,mca.Maintenance_Contract__r.MonthSize__c);
                        }
                    }
                    for (Asset ass : assetList) {
                    // 按照第四分类，更新对应的设备字段（过去3年有合同的修理实绩和，对应型号的设备总数换算为12个月的件数）
                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            if (NotoAssSize.get(ass.Category4__c) !=null) {
                                 assOne.MonthSizeCategory4__c = NotoAssSize.get(ass.Category4__c);
                            }
                            updateAss.add(assOne);

                    }
                return updateAss;
            }else{
                return null;
            }
    }

}