/*
 * @author 李　逸飛
 * @date 2018.11.6
 * @related OLY_OCM-473
 * Test for ET_Product_CategoryPrice_Table_Batch
 */
@IsTest
private class ET_Product_CategoryPrice_Table_BatchTest {
    private static List<Product2> products = new List<Product2>();
    @TestSetup
    private static void setup() {
         products.add(new Product2(Name='test1',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '停止',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test2',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=109597));
         products.add(new Product2(Name='test3',Family='ET',Category2__c='耗材',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='針状刀',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=2<<12));
         products.add(new Product2(Name='test4',Family='ET',Category2__c='耗材',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='針状刀',SFDA_Status__c = '停止',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=255015.33));
         products.add(new Product2(Name='test5',Family='ET',Category2__c='耗材',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=1056.71));
         products.add(new Product2(Name='test6',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '不要',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=1515135));
         products.add(new Product2(Name='test7',Family='ET',Category2__c='耗材',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=5<<20));
         products.add(new Product2(Name='test8',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '不要',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test9',Family='ET',Category2__c='耗材',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '停止',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test10',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '有効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test11',Family='ET',Category2__c='耗材',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='IT刀',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=2300014));
         products.add(new Product2(Name='test12',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=641544));
         products.add(new Product2(Name='test13',Family='ET',Category2__c='耗材',Category3__c='显示器',Category4__c='OEV',Category5__c='SD-14inch',SFDA_Status__c = '失効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test14',Family='ET',Category2__c='耗材',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=114514810));
         products.add(new Product2(Name='test15',Family='ET',Category2__c='耗材',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '失効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test16',Family='ET',Category2__c='耗材',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '有効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=100.19));
         products.add(new Product2(Name='test17',Family='ET',Category2__c='耗材',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '失効（申請無）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test18',Family='ET',Category2__c='耗材',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test19',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=109597));
         products.add(new Product2(Name='test20',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='針状刀',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=2<<12));
         products.add(new Product2(Name='test21',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='針状刀',SFDA_Status__c = '停止',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=255015.33));
         products.add(new Product2(Name='test22',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=1056.71));
         products.add(new Product2(Name='test23',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '不要',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=1515135));
         products.add(new Product2(Name='test24',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=5<<20));
         products.add(new Product2(Name='test25',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '不要',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test26',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '停止',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test27',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '有効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test28',Category3__c='ESD',Category4__c='粘膜切开刀',Category5__c='IT刀',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=2300014));
         products.add(new Product2(Name='test29',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='10mm',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=641544));
         products.add(new Product2(Name='test30',Category3__c='显示器',Category4__c='OEV',Category5__c='SD-14inch',SFDA_Status__c = '失効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test31',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=114514810));
         products.add(new Product2(Name='test32',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '失効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test33',Category3__c='4K系列',Category4__c='腹腔镜/胸腔镜',Category5__c='5.4mm',SFDA_Status__c = '有効（再申請中）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=100.19));
         products.add(new Product2(Name='test34',Category3__c='摄像头适配器',Category4__c='AR',Category5__c='AR',SFDA_Status__c = '失効（申請無）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test35',Category3__c='灌流系统',Category4__c='Hystro-Flow',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294));
         products.add(new Product2(Name='test36',Category3__c='ET',Category4__c='导丝',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294,Asset_Model_No__c='G-240Test'));
         products.add(new Product2(Name='test37',Category3__c='ET',Category4__c='乳头切开刀',Category5__c='乳头切开-三腔',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294,Asset_Model_No__c='G-240Test'));
         products.add(new Product2(Name='test38',Category3__c='ET',Category4__c='注射针（胃镜）',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294,Asset_Model_No__c='NM-20Test'));
         products.add(new Product2(Name='test39',Category3__c='ET',Category4__c='注射针（肠镜）',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294,Asset_Model_No__c='NM-20Test'));
         products.add(new Product2(Name='test40',Category3__c='呼吸科ET',Category4__c='吸引活检针',Category5__c='Hystro-Flow',SFDA_Status__c = '失効（期限内生産済在庫対応）',Dealer_special_Object__c = true,Intra_Trade_List_RMB_1__c=61294,Asset_Model_No__c='NA-U401SXTest'));

         for(Product2 pd: products) {
             pd.put('Intra_Trade_List_RMB_Date1__c',Date.newInstance(1999,9,9));
             pd.put('Intra_Trade_List_RMB_End_Date1__c',Date.newInstance(4000,12,31));
         }
         insert products;

    }
    /*
     *  一般测试
     */
    static testMethod void ET_Product_CategoryPrice_Table_BatchTest_1() {
        ET_Product_CategoryPrice_Table_Batch pctb = new ET_Product_CategoryPrice_Table_Batch();
        Database.executeBatch(pctb);
        Test.startTest();
        System.debug([SELECT COUNT() FROM Product_CategoryPrice_Table__c]);
//        System.assertEquals(4,pcts.size());
        Test.stopTest();
        Product_CategoryPrice_Table__c [] pcts = [SELECT UniqueKey__c,Category4_Avg_Price__c,Product_Amount__c,Product_Total_Price__c FROM Product_CategoryPrice_Table__c];

        for(Product_CategoryPrice_Table__c pct : pcts) {
            switch on pct.UniqueKey__c {
                when '151P:4K系列:腹腔镜/胸腔镜' {
                    System.assertEquals(122336.09,pct.Category4_Avg_Price__c);
                }
                when '151P:灌流系统:Hystro-Flow'{
                    System.assertEquals(1251942.53, pct.Category4_Avg_Price__c);
                }
                when '151P:摄像头适配器:AR' {
                    System.assertEquals(40080553.35, pct.Category4_Avg_Price__c);
                }
                when '151P:ESD:粘膜切开刀' {
                    System.assertEquals(598084.98, pct.Category4_Avg_Price__c);
                }

            }
        }

    }
    /*
     *  当记录被更新
     */
    static testMethod void ET_Product_CategoryPrice_Table_BatchTest_2() {
        Test.startTest();
        ET_Product_CategoryPrice_Table_Batch pctb = new ET_Product_CategoryPrice_Table_Batch();
        Database.executeBatch(pctb);
        Product2 [] listToUpdate = new List<Product2>();
        products = [SELECT Id FROM Product2];
        for(Integer i=5;i<10;i++) {
            listToUpdate.add(products[i]);
            listToUpdate[i-5].put('Intra_Trade_List_RMB_1__c',(Integer)(1919*i*567));
        }
        update listToUpdate;
        Database.executeBatch(pctb);
        Test.stopTest();
        Product_CategoryPrice_Table__c [] pcts = [SELECT Category4_Avg_Price__c,UniqueKey__c FROM Product_CategoryPrice_Table__c];
        System.debug(pcts);
        for(Product_CategoryPrice_Table__c pct: pcts) {
            switch on pct.UniqueKey__c {
                when '151P:4K系列:腹腔镜/胸腔镜' {
                    System.assertEquals(1484726.91,pct.Category4_Avg_Price__c);
                }
                when '151P:灌流系统:Hystro-Flow'{
                    System.assertEquals(3568673.73, pct.Category4_Avg_Price__c);
                }
                when '151P:摄像头适配器:AR' {
                    System.assertEquals(40080553.35, pct.Category4_Avg_Price__c);
                }
                when '151P:ESD:粘膜切开刀' {
                    System.assertEquals(598084.98, pct.Category4_Avg_Price__c);
                }

            }
        }


    }
}