@isTest
public class PCLLostProduct2AssetHandlerTest {
	@testSetup
	public static void testSetup(){
        setUp();
    }
    public static void setUp(){
		Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = '01210000000QemG';
        company1.Name = '病院テスト1';
        company2.RecordTypeId = '01210000000QemG';
        company2.Name = '病院テスト2';
        insert new Account[] {company1, company2};

        List<Account> dc1s = 
        [Select Id, Name, Department_Class_Label__c, 
        Sys_Dept_Name_Change_Chk__c 
        from Account where Parent.Id = :company1.Id and
        RecordType.DeveloperName = 'Department_Class_BF'];

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

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

        //01210000000Qem1
        Account depart3 = new Account();
        depart3.RecordTypeId = '01210000000Qem1';
        depart3.Name         = '*';
        depart3.Hospital__c         = company1.Id;

        insert new Account[] {depart1, depart2,depart3};

        String recordtypeid = '01210000000aMAE';
        if (NFMUtil.isSandbox()) {
            recordtypeid = '01210000000aMAE';
        }
        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'HistoryPrd1';
        prd1.ProductCode            = 'HistoryPrd1';
        prd1.Repair_Product_Code__c = 'HistoryPrd1_RP';
        prd1.Name                   = 'HistoryPrd1';
        prd1.Manual_Entry__c        = true;
        prd1.Category5__c = '竞争对手';
        prd1.Brand_Name__c = 'STORZ';
        prd1.recordTypeId = recordtypeid; //20230221上线改为‘01210000000aMAE’，上完还原recordtypeid
        // prd1.Lost_By_Company__c= 'STORZ'; //add tcm 20211213

        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'HistoryPrd2';
        prd2.ProductCode            = 'HistoryPrd2';
        prd2.Repair_Product_Code__c = 'HistoryPrd2_RP';
        prd2.Name                   = 'HistoryPrd2';
        prd2.Manual_Entry__c        = true;
        prd2.Category5__c = '竞争对手';
        prd2.Brand_Name__c = 'STORZ';
        prd2.recordTypeId = recordtypeid;
        // prd2.Lost_By_Company__c= 'STORZ'; //add tcm 20211213

        insert new Product2[] {prd1, prd2};

        Opportunity opp1 = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today(),
            Hospital__c = company1.Id,
            Department_Class__c = dc1s[0].Id,
            AccountId = depart1.Id
        );
        Opportunity opp2 = new Opportunity(Name='aiueo1', StageName='contact', CloseDate=Date.today(),
            Hospital__c = company1.Id,
            Department_Class__c = dc1s[0].Id,
            AccountId = depart2.Id
        );
        insert new Opportunity[] {opp1, opp2};
        // 失单
        Lost_cancel_report__c lcr1 = new Lost_cancel_report__c(
            Opportunity__c = opp1.Id, 
            RecordTypeId = '01210000000R4hM',
            LostType__c = '失单',
            Manager_sales__c = UserInfo.getUserId()
        );
        // 部分失单
        Lost_cancel_report__c lcr2 = new Lost_cancel_report__c(
            Opportunity__c = opp2.Id, 
            RecordTypeId = '01210000000R4hM',
            LostType__c = '部分失单',
            Manager_sales__c = UserInfo.getUserId()
        );
        insert new list<Lost_cancel_report__c> {lcr1, lcr2};

        PCLLostBrand__c lcB = new PCLLostBrand__c(
        	Lost_cancel_report__c = lcr1.id,
            // ProductClass__c ='主机',
            // ProductCategory__c = '3D',
            Lost_By_Company__c = 'STORZ',
            LostPrice__c = 11,
            Agency__c = depart3.id,
            Lost_reason_main__c ='无信息，跟进不及时'
        	);
        insert lcB;
        PCLLostProduct__c lsP1 = new PCLLostProduct__c(
        	PCLLostBrand__c = lcB.id,
        	Quantity__c = 1,
        	name = 'lsp1',
            LostBrandName__c = 'STORZ'
        	);
        PCLLostProduct__c lsP2 = new PCLLostProduct__c(
        	PCLLostBrand__c = lcB.id,
        	lostProduct__c = prd1.id,
        	Quantity__c = 2,
        	name = 'lsp2',
            LostBrandName__c = 'STORZ'
        	);
        insert new list<PCLLostProduct__c>{lsP1, lsP2};
		Asset asset01 = createAsset(
			'asset01', depart1.Id, dc1s[0].Id, company1.Id, 
			'00001', prd1.Id,'主机', lsP2.id);

	}
    @isTest
    private static void testInsert() {
    	PCLLostBrand__c lcB = [select id from PCLLostBrand__c limit 1];
        product2 prd1 = [select id from product2 limit 1];
        PCLLostProduct__c lsP1 = new PCLLostProduct__c(
        	PCLLostBrand__c = lcB.id,
        	Quantity__c = 1,
        	name = 'lsp3',
        	LostProduct__c = prd1.id,
            LostBrandName__c = 'STORZ'
        	);
        insert lsP1;
  
    }
    @isTest
    private static void testdelete() {
        PCLLostProduct__c lsP1 = [select id 
        from PCLLostProduct__c 
        where name = 'lsp1'];
        delete lsP1;
  
    }

    public static Asset createAsset(String input, String accountid, 
    	String dcId, String hpId, String serialNo, String prId,String mark, id lpid) {
        Asset asset = new Asset();
        asset.Name = input;
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId;
        asset.Hospital__c = hpId;
        asset.SerialNumber = serialNo;
        asset.Product2Id = prId;
        asset.InstallDate = Date.today();
        asset.IsCompetitorProduct  = true;
        //JZHG-BSDUT4 ---20200825---update By rentongxiao---Start
        //在创建Asset的时候添加AssetMark__c 字段
        asset.AssetMark__c = mark;
        asset.LostProduct__c = lpid;
        //JZHG-BSDUT4 ---20200825---update By rentongxiao---Start
        insert asset;
        return asset;
    }

    @isTest
    private static void testupdate() {
    	list<product2> prdList = [select id from product2 limit 2];
        PCLLostProduct__c lsP1 = [select id,lostProduct__c
        from PCLLostProduct__c 
        where name = 'lsp2'];
        lsP1.lostProduct__c = null;
        update lsp1;
        lsP1.lostProduct__c = prdList[0].id;
        lsP1.Quantity__c = 3;
        update lsp1;
        lsP1.lostProduct__c = prdList[1].id;
        lsP1.Quantity__c = 1;
        update lsp1;
    }
}