@isTest
private class ProductMonthlySalesCountCopyBatchTest
{
  static testMethod void test1() {

    Account hospital = new Account();
    hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName != 'HP' limit 1].id;
    hospital.Name = 'test hospital';
    insert hospital;
    Contact ctt=new Contact(RecordTypeId='01210000000QfWd');
    ctt.AccountId=hospital.Id;
    ctt.FirstName='名';
    ctt.LastName = '姓';
    insert ctt;

    // 产品一
    Product2 pro1 = new Product2(Name='产品一',IsActive=true,Family='SP',
    Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
    Fixture_Model_No_T__c = 'n01',
    ProductMonthlySalesFlag__c=true,
    ProductCode_Ext__c='pc01',Manual_Entry__c=false,
    StorageStatus__c = '',StorageStatusNo__c = 22,
    WhiteSpace__c = true,Product_Status__c='正常销售',
    SFDA_Approbated_Status__c='有效',SFDA_Status__c='有効',
    M_BC_Stop_manufacure__c=date.today().addMonths(+10));
    insert pro1;

    // Product2 pro1=new Product2();
    // pro1.Name='产品一';
    // pro1.Manual_Entry__c=false;
    // pro1.M_BC_Stop_manufacure__c=date.today().addMonths(+10);
    // pro1.SFDA_Status__c='有効';
    // pro1.ProductMonthlySalesFlag__c=true;
    // insert pro1;

    // 产品二
    Product2 pro2 = new Product2(Name='产品二',IsActive=true,Family='SP',
    Fixture_Model_No__c='n02',Serial_Lot_No__c='S/N tracing',
    Fixture_Model_No_T__c = 'n02',
    ProductMonthlySalesFlag__c=true,
    Correspond_Code__c=pro1.Id,
    ProductCode_Ext__c='pc02',Manual_Entry__c=false,
    StorageStatus__c = '',StorageStatusNo__c = 22,
    WhiteSpace__c = true,Product_Status__c='正常销售',
    SFDA_Approbated_Status__c='有效',SFDA_Status__c='有効',
    M_BC_Stop_manufacure__c=date.today().addMonths(-1));
    insert pro2;

    // Product2 pro2=new Product2();
    // pro2.Name='产品二';
    // pro2.Manual_Entry__c=false;
    // pro2.ProductCode=pro1.Id;
    // pro2.Correspond_Code__c=pro1.Id;
    // pro2.M_BC_Stop_manufacure__c=date.today().addMonths(-1);
    // pro2.SFDA_Status__c='有効';
    // pro2.ProductMonthlySalesFlag__c=true;
    // insert pro2;

    // 产品三

    Product2 pro3 = new Product2(Name='产品三',IsActive=true,Family='SP',
    Fixture_Model_No__c='n03',Serial_Lot_No__c='S/N tracing',
    Correspond_Code__c=pro2.Id,
    Fixture_Model_No_T__c = 'n03',
    ProductMonthlySalesFlag__c=true,
    ProductCode_Ext__c='pc03',Manual_Entry__c=false,
    StorageStatus__c = '',StorageStatusNo__c = 22,
    WhiteSpace__c = true,Product_Status__c='正常销售',
    SFDA_Approbated_Status__c='有效',SFDA_Status__c='有効',
    M_BC_Stop_manufacure__c=date.today().addMonths(-1));
    insert pro3;

    // Product2 pro3=new Product2();
    // pro3.Name='产品二';
    // pro3.Manual_Entry__c=false;
    // pro3.ProductCode=pro2.Id;
    // pro3.Correspond_Code__c=pro2.Id;
    // pro3.M_BC_Stop_manufacure__c=date.today().addMonths(-1);
    // pro3.SFDA_Status__c='有効';
    // pro3.ProductMonthlySalesFlag__c=true;

    // insert pro3;

    // TODO
    List<Product2> pro=[select id,ProductCode,Correspond_Code__c,Correspond_ProductCode__c,M_BC_Stop_manufacure__c,ProductMonthlySalesFlag__c from Product2 where M_BC_Stop_manufacure__c !=null and SFDA_Status__c!='停止'];
    // system.assertEquals(3,pro.size());

    //保有设备一
    Asset aset1=new Asset();
    aset1.name='保有设备一';
    aset1.IsCompetitorProduct=false;
    aset1.Loaner_accsessary__c=true;
    // aset1.RecordTypeId ='01210000000kOPR';
    aset1.Product2Id=pro1.Id;
    aset1.Posting_Date__c=date.today().addMonths(-5);
    aset1.AccountId=hospital.Id;
    aset1.ContactId=ctt.Id;
    aset1.IF_Information_From__c=true;
    aset1.RecordTypeId=Schema.SObjectType.Asset.getRecordTypeInfosByDeveloperName().get('HPAsset').getRecordTypeId();
    insert aset1;

    //保有设备二
    Asset aset2=new Asset();
    aset2.name='保有设备二';
    aset2.IsCompetitorProduct=false;
    aset2.Loaner_accsessary__c=false;
    aset2.RecordTypeId ='01210000000kOPM';
    aset2.Product2Id=pro2.Id;
    aset2.Posting_Date__c=date.today().addMonths(-20);
    aset2.AccountId=hospital.Id;
    aset2.ContactId=ctt.Id;
    aset2.IF_Information_From__c=true;
    aset2.SerialNumber='序列号2';
    insert aset2;

    //保有设备三
    Asset aset3=new Asset();
    aset3.name='保有设备三';
    aset3.IsCompetitorProduct=false;
    aset3.Loaner_accsessary__c=false;
    aset3.RecordTypeId ='01210000000kOPM';
    aset3.Product2Id=pro2.Id;
    aset3.Posting_Date__c=date.today().addMonths(-7);
    aset3.AccountId=hospital.Id;
    aset3.ContactId=ctt.Id;
    aset3.IF_Information_From__c=true;
    aset3.Product_Serial_No__c='N4510030:9ZK(testtest)';
    aset3.SerialNumber='序列号3';
    insert aset3;

    //保有设备四
    Asset aset4=new Asset();
    aset4.name='保有设备四';
    aset4.IsCompetitorProduct=false;
    aset4.Loaner_accsessary__c=false;
    aset4.RecordTypeId ='01210000000kOPM';
    aset4.Product2Id=pro2.Id;
    aset4.Posting_Date__c=date.today().addMonths(-7);
    aset4.AccountId=hospital.Id;
    aset4.ContactId=ctt.Id;
    aset4.IF_Information_From__c=true;
    aset4.Product_Serial_No__c='N4510030:9ZK(testtest)';
    aset4.SerialNumber='序列号4';
    insert aset4;

    List<Asset> assetlog=[select Id,Product2Id,Posting_Date__c,RecordType.DeveloperName from Asset where Posting_Date__c!=null and RecordType.DeveloperName !='Shipment' and Posting_Date__c!=1900-01-01 order by Posting_Date__c desc];
    System.assertEquals(assetlog.size(), 4);

    // 创建月销量表
    ProductMonthlySales__c prd1=new ProductMonthlySales__c();
    prd1.GenerationMonths__c=5;
    prd1.MonthSales__c=20;
    prd1.Earliest_Shipping__c=date.today().addMonths(-100);
    prd1.CurrentDate__c=date.today().addMonths(-1);
    prd1.Name__c='创建月销量表';
    prd1.ProductM__c=pro1.Id;
    prd1.Product2Model__c='产品型号';
    insert prd1;

    List<String> a=new List<String>();
    a.add(pro1.id);
    a.add(pro3.id);
    Database.executeBatch(new ProductMonthlySalesCountCopyBatch());
    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(Date.valueOf('2023-03-31')));
    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(true));

    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(a));
    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(pro1.id));

    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(a,true));
    Database.executeBatch(new ProductMonthlySalesCountCopyBatch(pro1.id,true));
  }
}