@IsTest
private class ET_Product_ScoreTableHistory_Batch3Test {

    /*******************************************************************************************************
     * @description   testデータ作成        
     */
    private static void setup() {
        // OLY_OCM-644
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;

        //Account 经销商 recordtypeID
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();

        //Account 医院 recordtypeID
        Id HPRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();

        // 省
        Address_Level__c al = new Address_Level__c(Name = '東京', Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999');
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c(Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999', Level1_Name__c = '東京',
                Name = '渋谷区', Level2_Code__c = 'CN-9999', Level2_Sys_No__c = '9999999', Address_Level__c = al.Id);
        insert al2;

        Account AccountHP = new Account(Name = 't_e_s_tHP001', AgentCode_Ext__c = '9999900', RecordTypeId = HPRecordTypeId,
                Is_Active__c = '有効', Attribute_Type__c = '卫生部', Speciality_Type__c = '综合医院',
                Grade__c = '一级', OCM_Category__c = 'SLTV', Is_Medical__c = '医疗机构',
                State_Master__c = al.Id, City_Master__c = al2.Id, Town__c = '东京', OCM_man_province_txt__c = '北京');
        insert AccountHP;

        Account AccountAgent1 = new Account(Name = 't_e_s_tAgent001', RecordTypeId = AgencyRecordTypeId);
        insert AccountAgent1;

        Account AccountAgent2 = new Account(Name = 't_e_s_tAgent002', RecordTypeId = AgencyRecordTypeId, ParentId = AccountHP.Id);
        insert AccountAgent2;

        Product2 prod01 = new Product2(
                Name = 'Test01',
                ProductCode = 'Test01',
                Asset_Model_No__c = 'Test01',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true,
                Family = 'ET',
                Category2__c = '耗材',
                Category3__c = 'ERCP',
                Category4__c = '导丝',
                Category5__c = 'Visiglide35'
        );
        Product2 prod02 = new Product2(
                Name = 'T_est002',
                ProductCode = 'T_estCode002',
                Category2__c = '耗材',
                Category3__c = 'ERCP',
                Category4__c = '导丝',
                Category5__c = 'Visiglide35',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est02',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod03 = new Product2(
                Name = 'T_est003',
                ProductCode = 'T_estCode003',
                Category2__c = '耗材',
                Category3__c = 'Capsule',
                Category4__c = 'CP打印机',
                Category5__c = 'CP打印机',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est03',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod04 = new Product2(
                Name = 'T_est004',
                ProductCode = 'T_estCode004',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est04',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod05 = new Product2(
                Name = 'T_est005',
                ProductCode = 'T_estCode005',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 6,
                Asset_Model_No__c = 'T_est05',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod06 = new Product2(
                Name = 'T_est006',
                ProductCode = 'T_estCode006',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 5,
                Asset_Model_No__c = 'T_est06',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 [] pd2s = new List<Product2>{
                prod01, prod02, prod03, prod04, prod05, prod06
        };
        insert pd2s;

        List<Product2__c> prod2cs = new List<Product2__c>();
        for(Product2 p: pd2s) {
            Product2__c pdc = new Product2__c(
                    Name=p.Name,
                    OT_CODE_Text__c=p.ProductCode,
                    Product2__c = p.Id
            );
            prod2cs.add(pdc);
        }
        insert prod2cs;

        System.runAs(new User(Id = Userinfo.getUserId())) {
            // PCL
            Opportunity opp01 = new Opportunity(
                    AccountId = AccountAgent2.Id,
                    Opportunity_Category__c = 'GI',
                    Strategic_department_new__c = '消化科',
                    SAP_Province__c = '辽宁省',
                    Name = 't_e_s_t_opp',
                    Purchase_Reason__c = '新设',
                    Fund_Basis__c = '政府资金',
                    Purchase_Type__c = '一般询价',
                    Sales_Method__c = '院内招标',
                    Trade__c = '内贸',
                    Promise_Class__c = '内贸',
                    Sales_Root__c = '販売店',//niwu changed
                    Close_Forecasted_Date__c = Date.today().addDays(50),
                    CloseDate = Date.today(),
                    Hospital__c = AccountHP.Id,
                    StageName = '引合',
                    SAP_Send_OK__c = true
            );
            insert opp01;

            Consumable_order__c [] coList = new List<Consumable_order__c>();
            for(Integer i=0;i<pd2s.size();i++) {
                coList.add(
                        new Consumable_order__c(
                                Name='T_e_s_tOrder'+i,
                                Order_ForHospital__c = AccountHP.Id,
                                Dealer_Info__c = AccountAgent2.Id,
                                Opportunity__c = opp01.Id,
                                SummonsForDirction__c = '直接销售给医院'
                        )
                );
            }

            insert coList;
            Consumable_Orderdetails__c [] codList = new List<Consumable_Orderdetails__c>();
            for(Integer i=0;i<coList.size();i++) {
                codList.add(
                        new Consumable_Orderdetails__c(
                                Name='T_e_s_t'+i,
                                Consumable_order__c = coList[i].Id,
                                RecordTypeId = System.Label.RT_ConOrderDetail1_Order,
                                Consumable_count__c = 1,
                                Consumable_product__c = prod2cs[i].Id
                        )
                );
            }

            insert codList;
            Consumable_order_details2__c [] cod2List = new List<Consumable_order_details2__c>();
            for(Integer i=0;i<coList.size();i++) {
                cod2List.add(
                        new Consumable_order_details2__c(
                                Name = 'T_e_s_t_cod2_'+i,
                                Consumable_order_minor__c = coList[i].Id,
                                Consumable_Sale_order__c = coList[i].Id,
                                Deliver_date__c =  Date.newInstance(Date.today().year(),Date.today().month(),1),
                                Consumable_product__c = prod2cs[i].Id,
                                Asset_Model_No__c = 'T_e_s_t01',
                                Intra_Trade_List_RMB__c = 100 * (i + 1),
                                Box_Piece__c = '盒',
                                IF_Information_From__c = TRUE
                        )
                );
            }

            insert cod2List;

            //产品得分表
    //        ET_Product_ScoreTable_Batch bat = new ET_Product_ScoreTable_Batch();
    //        Database.executeBatch(bat);
            Product_Score_Table__c [] pstList = new List<Product_Score_Table__c>();
            Product_Score_Table__c pst01 = new Product_Score_Table__c(
            OCM_Term__c = '154P',
            Plan_Category3__c = 'ERCP',
            Category3__c = 'ERCP',
            Category4__c = '导丝');
            pstList.add(pst01);
            Product_Score_Table__c pst02 = new Product_Score_Table__c(
                    OCM_Term__c = '154P',
                    Plan_Category3__c = 'Capsule',
                    Category3__c = 'Capsule',
                    Category4__c = 'CP打印机'
                    );
            pstList.add(pst02);

            Product_Score_Table__c pst03 = new Product_Score_Table__c(
                    OCM_Term__c = '154P',
                    Plan_Category3__c = 'Capsule',
                    Category3__c = '基幹',
                    Category4__c = '清洗刷'
            );
            pstList.add(pst03);

            for(Product_Score_Table__c pst: pstList) {
                pst.UniqueKey__c = pst.OCM_Term__c+':'+pst.Category3__c+':'+pst.Category4__c;
            }
            insert pstList;

        }

    }

    /*
     * @description   直销实绩
     */
    private static testMethod void ET_Product_ScoreTableHistory_Batch3_01() {
        setup();

        Account AccountHP = [SELECT Id, Name, Management_Code__c, OCM_man_province_txt__c FROM Account WHERE Name = 't_e_s_tHP001'];
        Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002'];

        Test.startTest();
        ET_Product_ScoreTableHistory_Batch3 bat = new ET_Product_ScoreTableHistory_Batch3 ();
        Id JobId = Database.executeBatch(bat);
        Test.stopTest();
//        Product_Score_Table_History__c psth = [SELECT Shipping_date__c,Qty__c,UniqueKey__c FROM Product_Score_Table_History__c WHERE UniqueKey__c='H:150P:T_e_s_t_HP001:ERCP:导丝'];
        Product_Score_Table_History__c [] psthList = [SELECT Id,Shipping_date__c,Sales_Amount__c,Qty__c,UniqueKey__c,Product_Score_Table__r.UniqueKey__c FROM Product_Score_Table_History__c];
        System.assertNotEquals('000000000000000',String.valueOf(JobId));
        String uniqueKey_1 = 'U:154P:' + Date.today().month() + ':東京:' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':ERCP:导丝'; 
        String uniqueKey_2 = 'U:154P:' + Date.today().month() + ':東京:' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':基幹:清洗刷';
        String uniqueKey_3 = 'U:154P:' + Date.today().month() + ':東京:' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':Capsule:CP打印机';
    //    System.assertEquals(3, psthList.size()); niwu 注释
        for(Product_Score_Table_History__c psth: psthList) {
            if (psth.UniqueKey__c == uniqueKey_1) {
                System.assertEquals(265.49,psth.Sales_Amount__c);
                System.assertEquals(2,psth.Qty__c);
                System.assertEquals('154P:ERCP:导丝',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            } else if (psth.UniqueKey__c == uniqueKey_2) {
                System.assertEquals(1327.43,psth.Sales_Amount__c);
                System.assertEquals(3,psth.Qty__c);
                System.assertEquals('154P:基幹:清洗刷',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            } else if (psth.UniqueKey__c == uniqueKey_3) {
                System.assertEquals(265.49,psth.Sales_Amount__c);
                System.assertEquals(1,psth.Qty__c);
                System.assertEquals('154P:Capsule:CP打印机',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            }
            
        }

    }
    /*******************************************************************************************************
    *@description 添加额外数据数据
    */
    private static testMethod void ET_Product_ScoreTableHistory_Batch3_02() {
        setup();

        Test.startTest();
        Account AccountHP = [SELECT Id, Name, Management_Code__c, OCM_man_province_txt__c FROM Account WHERE Name = 't_e_s_tHP001'];
        Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002'];

        Id PSTH_RecordTypeId = Schema.SObjectType.Product_Score_Table_History__c.getRecordTypeInfosByDeveloperName().get('Company').getRecordTypeId();

        //历史资料
        Product2 p = [SELECT Plan_Term__c,Category3__c,Category4__c,CreatedDate FROM Product2 WHERE Name = 'Test01'];
        String month = String.valueOf(p.CreatedDate.month());
        String term = p.Plan_Term__c.left(4);
        String uniqueKey = 'U:' + term + ':' + month + ':' + AccountHP.OCM_man_province_txt__c + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':' + p.Category3__c + ':' + p.Category4__c;
        String pstKey = term + ':' + p.Category3__c + ':' + p.Category4__c;
        // Product_Score_Table__c pst = [SELECT Id FROM Product_Score_Table__c WHERE UniqueKey__c = :pstKey];
        Product_Score_Table_History__c psth01 = new Product_Score_Table_History__c();
        //UniqueKey__c :  (151PA/151PB)计画财年:第三分类 :新4.5分类 :经销商 :医院
        psth01.UniqueKey__c = uniqueKey;
        psth01.Hospital__c = AccountHP.Id;
        psth01.OCM_Province_Text__c = AccountHP.OCM_man_province_txt__c;
        psth01.Province_From_Consume__c = TRUE;
        // psth01.Product_Score_Table__c = pst.Id;
        psth01.Sales_Amount__c = 67;
        psth01.Shipping_date__c = Date.newInstance(Date.today().year(), Date.today().month(), 1);
        psth01.Agency__c = AccountAgent2.Id;
        psth01.Qty__c = 2;
        psth01.RecordTypeId = PSTH_RecordTypeId;
        insert psth01;


        //call ET_Product_ScoreTableHistory_Batch3
        ET_Product_ScoreTableHistory_Batch3 bat = new ET_Product_ScoreTableHistory_Batch3 ();
        Database.executeBatch(bat);

        Test.stopTest();

        System.runAs(new User(Id = Userinfo.getUserId())) {
        //     Product_Score_Table_History__c psth = [SELECT Sales_Amount__c,Qty__c,Shipping_date__c FROM Product_Score_Table_History__c WHERE UniqueKey__c = :uniqueKey];
        //     System.assertEquals(265.49, psth.Sales_Amount__c);
        //     System.assertEquals(2, psth.Qty__c);
            Date today = Date.today();
        //     System.assertEquals(Date.newInstance(today.year(), today.month(), 1), psth.Shipping_date__c);

        }

    }
}