@isTest
private class DataLoadAccountControllerTest {
	public static List<RecordType> rectIE;
	public static List<RecordType> rectRVI;
	public static List<RecordType> rectNDT;
	public static List<RecordType> rectANI;
	public static Campaign campain;
	public static DataLoadAccountController controller;
    public static Account accountIE;
    public static Account accountNDT;
    public static Account dealer;
    public static Contact core;
    public static Contact core2;
    public static Contact core3;
	public static void Basicdata(){
		rectIE  = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
    	rectRVI = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer RVI'];
    	rectNDT = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer NDT'];
    	rectANI = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];

        Profile prof = [select Id from Profile where Name ='SSBG DEALER IE'];

        //代理商
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.FacilityNameD__c = '测试测试1111';
        dealer.FacilityName__c = '测试测试1111';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c='000000';
        insert dealer;
        // 代理商下联系人
        core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id);
        insert core;

        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        // 市场部用户(MarketingStrategy__c =true)
        User hpOwner = new User( LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = System.Label.SystemAdmin,MarketingStrategy__c =true);
        insert hpOwner;
        // 创建代理商用户(ContactId = core.id)
        User dealerUser = new User( LastName = 'ceshi20200803', FirstName = 'owner20200803', Alias = 'hp', CommunityNickname = 'testUser01', Email = 'olympus_hpowner@sunbridge.com', Username = 'testUser01@prectech.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja',  ProfileId = prof.Id,MarketingStrategy__c =true,ContactId = core.id);
        insert dealerUser;
        // 模拟市场部创建用户
        System.runAs(hpOwner){
            accountIE = new Account();
            accountIE.Name = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭00';
            accountIE.FacilityNameD__c = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭00动力试制院';
            accountIE.FacilityName__c = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭00动力试制院';
            accountIE.DivisionName_D__c = '动力试制院';
            accountIE.RecordTypeId = rectIE[0].Id;
            accountIE.ProductSegment__c = 'IE';
            accountIE.stautesD__c = 'Pass';

            insert accountIE;

            // accountNDT

            accountNDT = new Account();
            accountNDT.Name = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01';
            accountNDT.FacilityNameD__c = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01动力试制院';
            accountNDT.FacilityName__c = '安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01动力试制院';
            accountNDT.DivisionName_D__c = '动力试制院';
            accountNDT.RecordTypeId = rectNDT[0].Id;
            accountNDT.ProductSegment__c = 'NDT';
            accountNDT.stautesD__c = 'Pass';

            // insert accountNDT;
        }
        // 创建客户的联系人
        core2 = new Contact(email='jplumber@salesforce.com', firstname='',lastname='曹迎春1',accountid=accountIE.id);
        insert core2;
        core3 = new Contact(email='jplumber@salesforce.com', firstname='',lastname='曹迎春2',accountid=accountIE.id);
        insert core3;
       
        // 创建客户的客户团队
        List<AccountTeamMember> insertList = new List<AccountTeamMember>();
        // 客户团队-代理商用户
        AccountTeamMember atm1 = new AccountTeamMember();
        atm1.accountId = accountIE.Id;
        atm1.userId = dealerUser.Id;
        atm1.teamMemberRole = 'Sales Manager';
        insertList.add(atm1);
        // 客户团队-正常
        AccountTeamMember atm2 = new AccountTeamMember();
        atm2.accountId = accountIE.Id;
        atm2.userId = hpOwner.Id;
        atm2.teamMemberRole = 'Sales Manager';
        insertList.add(atm2);
        insert insertList;
        // 创建市场活动
        campain = new Campaign();
        campain.Name = '测试市场活动';

        insert campain;

        List<Account> accountList =    [SELECT id,Name,DivisionName_D__c,ProductSegment__c,createddate,OwnerId ,ManagementCode_F__c from Account where IsMarketingStrategyCreated__c = true];

        System.debug('accountList--->'+accountList);

        PageReference page = new PageReference('/apex/DataLoadAccount?campaignId='+campain.Id);
        System.Test.setCurrentPage(page);
        controller = new DataLoadAccountController();
	}
	//全部导入数据都有值
    static testMethod void fullData_ANI() {
    	
    	Basicdata();

        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
    	       strblob+= '"ANI","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","其他","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
    	controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();

        controller.save();
    }

     static testMethod void fullData_NDT_0() {
        
        Basicdata();

        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
               strblob+= '"NDT","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","其他","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();

        controller.save();
    }

    static testMethod void fullData_NDT() {
        
        Basicdata();

        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
               strblob+= '"NDT","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","其他","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
               strblob+= '"NDT","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","其他","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();

        controller.save();
    }

    static testMethod void fullData_IE() {
        
        Basicdata();

        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
               strblob+= '"IE","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院1","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春IE01","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
               strblob+= '"IE","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭02","动力试制院2","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春IE02","合肥市蜀山区包河经济开发区紫云路2","13956020242","230000","联系人1"\n';
               strblob+= '"IE","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭03","动力试制院3","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","anhuijianghuaiqichegufenyouxiangongsi1","销售活动","公共机构","目标","客户1","曹迎春IE03","合肥市蜀山区包河经济开发区紫云路3","13956020243","230000","联系人1"\n';
            
        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();

        controller.save();
    }

    // 验证客户必填字段
    static testMethod void inspectData_Account() {
    	Basicdata();
    	controller.init();
    	String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
              strblob +=  '"IE","测试客户字段验证","测试客户字段验证","","","","","","","","","","","","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
              strblob +=  '"IE","测试客户字段验证2","测试客户字段验证","xxxx","xxx","","","xxx","xxx","","xxx","xxx","xxx","xxx","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
    	controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
    }
    // 验证联系人
    static testMethod void inspectData_Contact() {
    	Basicdata();
    	controller.init();
    	String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
    		   strblob += '"RVI","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭00","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","Security/Defense","","","","","客户1","","","","",""\n';
    		   strblob += '"","","","","","","","","","","","","","","曹迎春1","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
    		   strblob += '"","","","","","","","","","","","","","","曹迎春2","","","","联系人1"\n';
    		   strblob += '"","","","","","","","","","","","","","","曹迎春3","合肥市蜀山区包河经济开发区紫云路1","13956020241","23000","联系人1"\n';
    		   strblob += '"","","","","","","","","","","","","","","","合肥市蜀山区包河经济开发区紫云路1","13956020241","230000","联系人1"\n';
    		   strblob += '"RVI","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭01","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","23001","Security/Defense","","","","","客户1","","","","",""\n';
    	controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
    }
    // 同一客户下的前面的联系人都为空或验证不通过。将剩余符合规则的放入联系人集合中。
    static testMethod void inspectData_Contact_1() {
    	Basicdata();
    	controller.init();
    	String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
    		   strblob += '"ANI","安徽江淮汽车股份有限公司技术中心-测试导入页面关闭ANI","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","三元催化剂","","","","","客户1","","","","",""\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
    	controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
    }
    // 客户名为空
    static testMethod void inspectData_Contact_AccountNameIsNull() {
        Basicdata();
        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
               strblob += '"ANI","","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","","","","","客户1","","","","",""\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
    }

    // csv格式错误
    static testMethod void csvRead_csvFormalError() {
        Basicdata();
        controller.init();
        String strblob = '"客户","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码","联系人备注"\n';
               strblob += '"ANI","","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","","","","","客户1","","","","",""\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
        controller.contentFile = Blob.valueOf(strblob);
        controller.csvRead();
    }

    // csv 列数不足
    static testMethod void csvRead_csvWrongNumberOfColumns() {
        Basicdata();
        controller.init();
        String strblob = '"客户分野","客户/代理商名称.","客户部门名称","客户审批人","客户省","客户城市","客户地址","客户邮编","客户Sub Use","客户英文名称(IE必填)","客户来源(IE必填)","客户用户属性(IE必填)","客户类型(IE必填)","客户备注","联系人姓名","联系人地址","联系人手机","联系人邮政编码"\n';
               strblob += '"ANI","","动力试制院","王 磊","安徽","合肥","安徽省合肥市蜀山区包河经济开发区紫云路1","230000","电子部品","","","","","客户1","","","","",""\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
               strblob += '"","","","","","","","","","","","","","","首行为空","首行为空","首行为空","230000","首行为空"\n';
        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
    }
}