@isTest
private class LostCancelReportHandlerTest {


    @isTest static void test_method_one() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 recodetype');
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 recodetype');
        }
        List<RecordType> rectLost = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Lost_cancel_report__c' and DeveloperName IN ('PCL_Lost_report', 'PCL_Cancel_report') order by DeveloperName desc];
        if (rectLost.size() == 0) {
            throw new ControllerUtil.myException('not found Lost_cancel_report recodetype');
        }

        // insert
        Account company1 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name         = '病院テスト1';
        insert company1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = : company1.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        insert depart1;

        // 商談
        Opportunity opp1 = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today(),
                                           Hospital__c = company1.Id,
                                           Department_Class__c = dc1s[0].Id,
                                           AccountId = depart1.Id
                                           );
        insert opp1;

        // 失单
        Lost_cancel_report__c lcr1 = new Lost_cancel_report__c(
            Opportunity__c = opp1.Id,
            RecordTypeId = rectLost[0].Id
            );
        insert lcr1;

        // 20220720 ljh add start 
        lcr1.Report_Status__c = '申请中';
        update lcr1;
        // 20220720 ljh add edn
        //test
        lcr1.Report_Status__c = '批准';
        update lcr1;

        Opportunity opptest = [SELECT Id, Lost_Cancel_Report__c FROM Opportunity WHERE Id =: opp1.Id];

        // System.assertEquals(lcr1.Id, opptest.Lost_Cancel_Report__c);
    }

    // add tcm 20211212 start
    @isTest static void test_method_two() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 recodetype');
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 recodetype');
        }
        List<RecordType> rectLost = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Lost_cancel_report__c' and DeveloperName IN ('PCL_Lost_report', 'PCL_Cancel_report') order by DeveloperName desc];
        if (rectLost.size() == 0) {
            throw new ControllerUtil.myException('not found Lost_cancel_report recodetype');
        }

        // insert
        Account company1 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        insert company1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = : company1.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId= dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c= company1.Id;
        insert depart1;

        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name = '*111';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId= dc1s[0].Id;
        depart2.Department_Class__c = dc1s[0].Id;
        depart2.Hospital__c= company1.Id;
        insert depart2;

        // 询价
        Opportunity opp1 = new Opportunity(Name='opp1', StageName='oppName', CloseDate=Date.today(),Hospital__c = company1.Id,Department_Class__c = dc1s[0].Id,AccountId = depart1.Id);
        insert opp1;

        // 失单
        Lost_cancel_report__c lcr1 = new Lost_cancel_report__c(Opportunity__c = opp1.Id,RecordTypeId = rectLost[0].Id);
        insert lcr1;


        //失单品牌1
        PCLLostBrand__c pcllb1=new PCLLostBrand__c();
        pcllb1.LostPrice__c=1332;
        pcllb1.Lost_Reason_Sub__c='价格';   //原因次
        pcllb1.Lost_reason_main__c='价格';     //原因主
        pcllb1.Lost_By_Company__c='蛇牌';       //品牌(主)
        pcllb1.Agency__c=depart1.Id;            //经销商
        pcllb1.Lost_cancel_report__c=lcr1.Id;

        //失单品牌2
        PCLLostBrand__c pcllb2=pcllb1.clone();
        pcllb2.Lost_By_Company__c='其他';       //品牌(主)
        pcllb2.Lost_By_Company_Mannual__c='失单品牌';   //手动品牌
        pcllb2.Agency__c=depart2.Id;            //经销商
        pcllb2.AgencyMannual__c='手动经销商';
        insert new List<PCLLostBrand__c> {pcllb1,pcllb2};

       
        // 产品
        Product2 pro1 = new Product2(Name='name01', Brand_Name__c='蛇牌',Category5__c='竞争对手',IsActive=true,Family='SP',Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',Fixture_Model_No_T__c = 'n01',ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        Product2 pro2 = new Product2(Name='name02', Brand_Name__c='其他',Category5__c='竞争对手',IsActive=true,Family='SP2',Fixture_Model_No__c='n02',Serial_Lot_No__c='S/N tracing',Fixture_Model_No_T__c = 'n02',ProductCode_Ext__c='pc02',Manual_Entry__c=false);
        // Product2 pro3 = new Product2(Name='name03',Lost_By_Company__c='其他',IsActive=true,Family='SP3',Fixture_Model_No__c='n03',Serial_Lot_No__c='S/N tracing',Fixture_Model_No_T__c = 'n03',ProductCode_Ext__c='pc03',Manual_Entry__c=false);
         if(NFMUtil.isSandbox()){
                pro1.RecordTypeId = '0129D000001NacD';
                pro2.RecordTypeId = '0129D000001NacD';
            } else {
                pro1.RecordTypeId = '01210000000aMAE';
                pro2.RecordTypeId = '01210000000aMAE';
            }

        insert new List<Product2> {pro1,pro2};




        //失单型号
        PCLLostProduct__c pcllp1=new PCLLostProduct__c();
        pcllp1.Quantity__c=2;
        pcllp1.LostBrandName__c='蛇牌';
        pcllp1.ProductClass__c='主机';
        pcllp1.ProductCategory__c='4K';
        pcllp1.LostProduct__c=pro1.Id;     //产品Id
        pcllp1.LostProductMannual__c='失单对手型号（手动）';   //失单对手型号（手动）
        pcllp1.PCLLostBrand__c=pcllb1.Id;     //询价失单品牌

        PCLLostProduct__c pcllp2=pcllp1.clone();
        pcllp2.LostBrandName__c='其他';
        pcllp2.ProductClass__c='主机';
        pcllp2.ProductCategory__c='4K';
        pcllp2.LostProduct__c=pro2.Id;     //产品Id
        pcllp2.PCLLostBrand__c=pcllb2.Id;     //询价失单品牌

        // PCLLostProduct__c pcllp3=pcllp1.clone();
        // pcllp3.LostBrandName__c='其他';
        // pcllp3.LostProduct__c=pro3.Id;     //产品Id
        // pcllp3.PCLLostBrand__c=pcllb2.Id;     //询价失单品牌

        insert new List<PCLLostProduct__c> {pcllp1,pcllp2};


        //test
        lcr1.Report_Status__c = '批准';
        update lcr1;

    }
    // add tcm 20211212 end
    // 20221206 ljh start
    @isTest static void test_method_three() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 recodetype');
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 recodetype');
        }
        List<RecordType> rectLost = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Lost_cancel_report__c' and DeveloperName IN ('PCL_Lost_report', 'PCL_Cancel_report') order by DeveloperName desc];
        if (rectLost.size() == 0) {
            throw new ControllerUtil.myException('not found Lost_cancel_report recodetype');
        }

        // insert
        Account company1 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name         = '病院テスト1';
        insert company1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = : company1.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        insert depart1;

        // 商談
        Opportunity opp1 = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today(),
                                           Hospital__c = company1.Id,
                                           Department_Class__c = dc1s[0].Id,
                                           AccountId = depart1.Id
                                           );
        insert opp1;

        // 失单
        Lost_cancel_report__c lcr1 = new Lost_cancel_report__c(
            Opportunity__c = opp1.Id,
            RecordTypeId = rectLost[0].Id
            );
        insert lcr1;

        // 20220720 ljh add start 
        lcr1.Report_Status__c = '申请中';
        update lcr1;
        // 20220720 ljh add edn
        //test
        lcr1.Report_Status__c = '批准';
        update lcr1;

        Opportunity opptest = [SELECT Id, Lost_Cancel_Report__c FROM Opportunity WHERE Id =: opp1.Id];
        delete lcr1;
        // System.assertEquals(lcr1.Id, opptest.Lost_Cancel_Report__c);
    }
    // 20221206 ljh start

}