@isTest
public class DealerConsumableManageBatch2Test {
    @isTest
    static void Test1(){
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
        StaticParameter.EscapeOppandStaTrigger = true;

        List < RecordType > rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '病院'
        ];
        if (rectCo.size() == 0) {
            return;
        }
        List < RecordType > rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '戦略科室分類 呼吸科'
        ];
        if (rectSct.size() == 0) {
            return;
        }
        List < RecordType > rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '診療科 消化科'
        ];
        if (rectDpt.size() == 0) {
            return;
        }
        //签收单最终用户记录类型
        List < RecordType > recordTypeList = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '戦略科室分類 呼吸科'
        ];
        if (recordTypeList.size() == 0) {
            return;
        }
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '广东省';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        upsert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '广东省';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        upsert al2;
        // 产品
        Product2 prd = new Product2();
        prd.ProductCode_Ext__c = 'N2656630';
        prd.ProductCode = 'N2656630';
        prd.Name = 'N2656630';
        prd.Manual_Entry__c = false;
        prd.ENG_New__c = 'ENG2';
        upsert prd;
        //客户医院
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        section.State_Master__c = al.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name = '*';
        depart.Department_Name__c = 'NFM008TestDepart';
        depart.ParentId = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c = company.Id;
        upsert depart;
        //签收单最终用户记录类型
        Account recordType = new Account();
        recordType.RecordTypeId = recordTypeList[0].Id;
        recordType.Name = '*';
        recordType.Department_Class_Label__c = '消化科';
        recordType.ParentId = company.Id;
        recordType.Hospital_Department_Class__c = company.Id;
        upsert recordType;
        RecordType recHanbaiten = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='販売店' limit 1];
        Account sellerA = new Account(Name = '販売店');
        sellerA.RecordTypeId = recHanbaiten.id;
        sellerA.Hospital__c = company.Id;
        sellerA.ParentId = depart.Id;
        sellerA.Tax_Practice_No__c = '001';
        sellerA.Sales_Shop_Class__c = '特約販売店（区域）';
        sellerA.Business_Authorization_No__c = '001';
        sellerA.Tax_Practice_Expiration_Date__c =  Date.today().addDays(20);
        sellerA.Business_Paper_Expiration_Date__c = Date.today().addDays(20);
        insert sellerA;
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'
            and DeveloperName = 'Opportunity'
        ];
        //询价
        Opportunity opp = new Opportunity(Name = 'testOpp1', StageName = '引合', CloseDate = Date.today(), AccountId = depart.Id, Sales_Root__c = '販売店', Competitor__c = 'A', Click_Close_Date__c = null, RecordType = rectOpp);
        opp.Opportunity_Category__c = 'ET';
        opp.Agency1__c = sellerA.Id;
        opp.SAP_Province__c = '广东省';
        upsert opp;
        Opportunity opp1 = new Opportunity(Name = 'testOpp1', StageName = '引合', CloseDate = Date.today(), AccountId = depart.Id, Sales_Root__c = '販売店', Competitor__c = 'A', Click_Close_Date__c = null, RecordType = rectOpp);
        opp1.Opportunity_Category__c = 'ENG';
        opp1.Agency1__c = sellerA.Id;
        opp1.SAP_Province__c = '广东省';
        upsert opp1;
        //注残
        Statu_Achievements__c Sac = new Statu_Achievements__c(name = 'zhucan_one', Opportunity__c = opp.id, DeliveryDate__c = Date.today(), ContractNO__c = 'ContractNO1', ContractAmount__c = 100);
        
        insert Sac;
        Statu_Achievements__c Sac1 = new Statu_Achievements__c(name = 'zhucan_one', Opportunity__c = opp1.id, DeliveryDate__c = Date.today(), ContractNO__c = 'ContractNO1', ContractAmount__c = 100);
        
        insert Sac1;
        //保有设备
        Asset assnew = new Asset(Asset_Owner__c = 'Olympus');
        assnew.Backorder__c = Sac.Id;
        // assnew.Order_number__c = Sac.Id;
        assnew.RecordTypeId = System.Label.Asset_RecordType;
        assnew.SerialNumber = '22K';
        assnew.Name = 'NA-201SX-4021:一次性使用吸引活检针';
        assnew.Product2Id = prd.Id;
        assnew.Hospital__c = company.Id;
        assnew.Department_Class__c = section.Id;
        assnew.AccountId = depart.Id;
        assnew.Asset_Owner__c = '医院资产';
        assnew.Quantity = 1;
        assnew.Status = '有库存';
        assnew.Manage_type__c = '个体管理';
        assnew.Internal_asset_location__c = '北京 备品中心';
        assnew.Loaner_accsessary__c = true;
        assnew.Delete_Flag__c = false;
        assnew.Freeze_sign__c = false;
        assnew.Out_of_wh__c = 0;
        assnew.CIC_call_back_check__c = true;
        assnew.Product_Serial_No__c = 'N2656630:22K(KVWMX)';
        assnew.TracingCode__c = 'KVWMX'; // ※
        assnew.Posting_Date__c = Date.today();
        
        upsert assnew;
        
        //发货DN
        Statu_Achievements_DN__c statuAchievementsDN = new Statu_Achievements_DN__c();
        statuAchievementsDN.Statu_Achievements__c = Sac.Id;
        statuAchievementsDN.DeliveryDate__c = Date.valueOf('2023-12-30');
        statuAchievementsDN.Name = '2021082049';
        statuAchievementsDN.endUser__c = recordType.Id;
        upsert statuAchievementsDN;
        //发货DN明细
        Statu_Achievements_DN_details__c statuAchievementsDNDetails = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails.Statu_Achievements_DN__c = statuAchievementsDN.Id;
        statuAchievementsDNDetails.Name = 'z1c200000292wse';
        statuAchievementsDNDetails.asset__c = assnew.Id;
        statuAchievementsDNDetails.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails.TracingCode_Raw__c = 'KVWMX'; // ※
        statuAchievementsDNDetails.Statu_Achievements__c = Sac.Id;
        statuAchievementsDNDetails.NetWorthNoProviston__c = 1000000;
        upsert statuAchievementsDNDetails;
        Statu_Achievements_DN__c statuAchievementsDN1 = new Statu_Achievements_DN__c();
        statuAchievementsDN1.Statu_Achievements__c = Sac1.Id;
        statuAchievementsDN1.DeliveryDate__c =  Date.valueOf('2023-12-30');
        statuAchievementsDN1.Name = '2021082049';
        statuAchievementsDN1.endUser__c = recordType.Id;
        insert statuAchievementsDN1;
        Statu_Achievements_DN_details__c statuAchievementsDNDetails1 = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails1.Statu_Achievements_DN__c = statuAchievementsDN1.Id;
        statuAchievementsDNDetails1.Name = 'z1c200000292wse';
        statuAchievementsDNDetails1.asset__c = assnew.Id;
        statuAchievementsDNDetails1.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails1.TracingCode_Raw__c = 'KVWMX'; // ※
        statuAchievementsDNDetails1.Statu_Achievements__c = Sac1.Id;
        statuAchievementsDNDetails1.NetWorthNoProviston__c = 1000000;
        insert statuAchievementsDNDetails1;
        List<Statu_Achievements_DN_details__c> aa = [SELECT id,Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c,asset__r.Product2.ConsumCategory2__c,NetWorthNoProviston__c,DeliveryDate__c,Statu_Achievements__c,Statu_Achievements__r.Opportunity__c,Statu_Achievements__r.Opportunity__r.Agency1__c,Statu_Achievements__r.Opportunity_Category__c FROM Statu_Achievements_DN_details__c where Id =:statuAchievementsDNDetails.Id];
        system.debug('aa+'+aa);
        Integer year = Date.today().addDays(-70).year();
        Integer FYear = year;
        if(Date.today().addDays(-70)>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        String keys= aa[0].Statu_Achievements__r.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+aa[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
        Dealer_ConsumableManage__c dc = new Dealer_ConsumableManage__c();
        dc.TMS_Key__c = keys;
        dc.ET_ENG__c = 'ET';
        dc.FY__c = OCSMYear;
        dc.Dealer_Name__c = sellerA.Id;
        dc.FY_S_1__c = 50;
        dc.FY_S_2__c = 50;
        dc.FY_S_3__c = 50;
        dc.FY_S_4__c = 50;
        dc.FY_S_5__c = 50;
        dc.FY_S_6__c = 50;
        dc.FY_S_7__c = 50;
        dc.FY_S_8__c = 50;
        dc.FY_S_9__c = 50;
        dc.FY_S_10__c = 50;
        dc.FY_S_11__c = 50;
        dc.FY_S_12__c = 50;
        insert dc;
        // String keys1= aa[0].Statu_Achievements__r.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+aa[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
        // Dealer_ConsumableManage__c dc1 = new Dealer_ConsumableManage__c();
        // dc1.TMS_Key__c = keys1;
        // dc1.ET_ENG__c = 'ET';
        // dc1.FY__c = OCSMYear;
        // dc1.Dealer_Name__c = sellerA.Id;
        // dc1.FY_S_1__c = 50;
        // dc1.FY_S_2__c = 50;
        // dc1.FY_S_3__c = 50;
        // dc1.FY_S_4__c = 50;
        // dc1.FY_S_5__c = 50;
        // dc1.FY_S_6__c = 50;
        // dc1.FY_S_7__c = 50;
        // dc1.FY_S_8__c = 50;
        // dc1.FY_S_9__c = 50;
        // dc1.FY_S_10__c = 50;
        // dc1.FY_S_11__c = 50;
        // dc1.FY_S_12__c = 50;
        // insert dc1;
        Test.StartTest();
		Database.executeBatch( new DealerConsumableManageBatch2(Date.today().addDays(-70),Date.today().addDays(50),false),10);
		Test.stopTest();
    }
    @isTest
    static void Test2(){
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
        StaticParameter.EscapeOppandStaTrigger = true;

        List < RecordType > rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '病院'
        ];
        if (rectCo.size() == 0) {
            return;
        }
        List < RecordType > rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '戦略科室分類 呼吸科'
        ];
        if (rectSct.size() == 0) {
            return;
        }
        List < RecordType > rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '診療科 消化科'
        ];
        if (rectDpt.size() == 0) {
            return;
        }
        //签收单最终用户记录类型
        List < RecordType > recordTypeList = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '戦略科室分類 呼吸科'
        ];
        if (recordTypeList.size() == 0) {
            return;
        }
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '广东省';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        upsert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '广东省';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        upsert al2;
        // 产品
        Product2 prd = new Product2();
        prd.ProductCode_Ext__c = 'N2656630';
        prd.ProductCode = 'N2656630';
        prd.Name = 'N2656630';
        prd.Manual_Entry__c = false;
        prd.ENG_New__c = 'ENG2';
        upsert prd;
        //客户医院
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        section.State_Master__c = al.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name = '*';
        depart.Department_Name__c = 'NFM008TestDepart';
        depart.ParentId = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c = company.Id;
        upsert depart;
        //签收单最终用户记录类型
        Account recordType = new Account();
        recordType.RecordTypeId = recordTypeList[0].Id;
        recordType.Name = '*';
        recordType.Department_Class_Label__c = '消化科';
        recordType.ParentId = company.Id;
        recordType.Hospital_Department_Class__c = company.Id;
        upsert recordType;
        RecordType recHanbaiten = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='販売店' limit 1];
        Account sellerA = new Account(Name = '販売店');
        sellerA.RecordTypeId = recHanbaiten.id;
        sellerA.Hospital__c = company.Id;
        sellerA.ParentId = depart.Id;
        sellerA.Tax_Practice_No__c = '001';
        sellerA.Sales_Shop_Class__c = '特約販売店（区域）';
        sellerA.Business_Authorization_No__c = '001';
        sellerA.Tax_Practice_Expiration_Date__c =  Date.today().addDays(20);
        sellerA.Business_Paper_Expiration_Date__c = Date.today().addDays(20);
        insert sellerA;
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'
            and DeveloperName = 'Opportunity'
        ];
        //询价
        Opportunity opp = new Opportunity(Name = 'testOpp1', StageName = '引合', CloseDate = Date.today(), AccountId = depart.Id, Sales_Root__c = '販売店', Competitor__c = 'A', Click_Close_Date__c = null, RecordType = rectOpp);
        opp.Opportunity_Category__c = 'ET';
        opp.Agency1__c = sellerA.Id;
        opp.SAP_Province__c = '广东省';
        upsert opp;
        Opportunity opp1 = new Opportunity(Name = 'testOpp1', StageName = '引合', CloseDate = Date.today(), AccountId = depart.Id, Sales_Root__c = '販売店', Competitor__c = 'A', Click_Close_Date__c = null, RecordType = rectOpp);
        opp1.Opportunity_Category__c = 'ENG';
        opp1.Agency1__c = sellerA.Id;
        opp1.SAP_Province__c = '广东省';
        upsert opp1;
        //注残
        Statu_Achievements__c Sac = new Statu_Achievements__c(name = 'zhucan_one', Opportunity__c = opp.id, DeliveryDate__c = Date.today(), ContractNO__c = 'ContractNO1', ContractAmount__c = 100);
        
        insert Sac;
        Statu_Achievements__c Sac1 = new Statu_Achievements__c(name = 'zhucan_one', Opportunity__c = opp1.id, DeliveryDate__c = Date.today(), ContractNO__c = 'ContractNO1', ContractAmount__c = 100);
        
        insert Sac1;
        //保有设备
        Asset assnew = new Asset(Asset_Owner__c = 'Olympus');
        assnew.Backorder__c = Sac.Id;
        // assnew.Order_number__c = Sac.Id;
        assnew.RecordTypeId = System.Label.Asset_RecordType;
        assnew.SerialNumber = '22K';
        assnew.Name = 'NA-201SX-4021:一次性使用吸引活检针';
        assnew.Product2Id = prd.Id;
        assnew.Hospital__c = company.Id;
        assnew.Department_Class__c = section.Id;
        assnew.AccountId = depart.Id;
        assnew.Asset_Owner__c = '医院资产';
        assnew.Quantity = 1;
        assnew.Status = '有库存';
        assnew.Manage_type__c = '个体管理';
        assnew.Internal_asset_location__c = '北京 备品中心';
        assnew.Loaner_accsessary__c = true;
        assnew.Delete_Flag__c = false;
        assnew.Freeze_sign__c = false;
        assnew.Out_of_wh__c = 0;
        assnew.CIC_call_back_check__c = true;
        assnew.Product_Serial_No__c = 'N2656630:22K(KVWMX)';
        assnew.TracingCode__c = 'KVWMX'; // ※
        assnew.Posting_Date__c = Date.today();
        
        upsert assnew;
        
        //发货DN
        Statu_Achievements_DN__c statuAchievementsDN = new Statu_Achievements_DN__c();
        statuAchievementsDN.Statu_Achievements__c = Sac.Id;
        statuAchievementsDN.DeliveryDate__c = Date.valueOf('2023-12-30');
        statuAchievementsDN.Name = '2021082049';
        statuAchievementsDN.endUser__c = recordType.Id;
        upsert statuAchievementsDN;
        //发货DN明细
        Statu_Achievements_DN_details__c statuAchievementsDNDetails = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails.Statu_Achievements_DN__c = statuAchievementsDN.Id;
        statuAchievementsDNDetails.Name = 'z1c200000292wse';
        statuAchievementsDNDetails.asset__c = assnew.Id;
        statuAchievementsDNDetails.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails.TracingCode_Raw__c = 'KVWMX'; // ※
        statuAchievementsDNDetails.Statu_Achievements__c = Sac.Id;
        statuAchievementsDNDetails.NetWorthNoProviston__c = 1000000;
        upsert statuAchievementsDNDetails;
        Statu_Achievements_DN__c statuAchievementsDN1 = new Statu_Achievements_DN__c();
        statuAchievementsDN1.Statu_Achievements__c = Sac1.Id;
        statuAchievementsDN1.DeliveryDate__c =  Date.valueOf('2023-12-30');
        statuAchievementsDN1.Name = '2021082049';
        statuAchievementsDN1.endUser__c = recordType.Id;
        insert statuAchievementsDN1;
        Statu_Achievements_DN_details__c statuAchievementsDNDetails1 = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails1.Statu_Achievements_DN__c = statuAchievementsDN1.Id;
        statuAchievementsDNDetails1.Name = 'z1c200000292wse';
        statuAchievementsDNDetails1.asset__c = assnew.Id;
        statuAchievementsDNDetails1.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails1.TracingCode_Raw__c = 'KVWMX'; // ※
        statuAchievementsDNDetails1.Statu_Achievements__c = Sac1.Id;
        statuAchievementsDNDetails1.NetWorthNoProviston__c = 1000000;
        insert statuAchievementsDNDetails1;
        List<Statu_Achievements_DN_details__c> aa = [SELECT id,Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c,asset__r.Product2.ConsumCategory2__c,NetWorthNoProviston__c,DeliveryDate__c,Statu_Achievements__c,Statu_Achievements__r.Opportunity__c,Statu_Achievements__r.Opportunity__r.Agency1__c,Statu_Achievements__r.Opportunity_Category__c FROM Statu_Achievements_DN_details__c where Id =:statuAchievementsDNDetails.Id];
        system.debug('aa+'+aa);
        Integer year = Date.today().addDays(-70).year();
        Integer FYear = year;
        if(Date.today().addDays(-70)>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        String keys= aa[0].Statu_Achievements__r.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+aa[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
        Dealer_ConsumableManage__c dc = new Dealer_ConsumableManage__c();
        dc.TMS_Key__c = keys;
        dc.ET_ENG__c = 'ET';
        dc.FY__c = OCSMYear;
        dc.Dealer_Name__c = sellerA.Id;
        dc.FY_S_1__c = 50;
        dc.FY_S_2__c = 50;
        dc.FY_S_3__c = 50;
        dc.FY_S_4__c = 50;
        dc.FY_S_5__c = 50;
        dc.FY_S_6__c = 50;
        dc.FY_S_7__c = 50;
        dc.FY_S_8__c = 50;
        dc.FY_S_9__c = 50;
        dc.FY_S_10__c = 50;
        dc.FY_S_11__c = 50;
        dc.FY_S_12__c = 50;
        insert dc;
        String keys1= aa[0].Statu_Achievements__r.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+aa[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
        Dealer_ConsumableManage__c dc1 = new Dealer_ConsumableManage__c();
        dc1.TMS_Key__c = keys1;
        dc1.ET_ENG__c = 'ET';
        dc1.FY__c = OCSMYear;
        dc1.Dealer_Name__c = sellerA.Id;
        dc1.FY_S_1__c = 50;
        dc1.FY_S_2__c = 50;
        dc1.FY_S_3__c = 50;
        dc1.FY_S_4__c = 50;
        dc1.FY_S_5__c = 50;
        dc1.FY_S_6__c = 50;
        dc1.FY_S_7__c = 50;
        dc1.FY_S_8__c = 50;
        dc1.FY_S_9__c = 50;
        dc1.FY_S_10__c = 50;
        dc1.FY_S_11__c = 50;
        dc1.FY_S_12__c = 50;
        insert dc1;
        Test.StartTest();
		Database.executeBatch( new DealerConsumableManageBatch2(Date.today().addDays(-70),Date.today().addDays(50),false),10);
		Test.stopTest();
    }
}