public without sharing class TydelikeACController {
	public TydelikeAccount__c tya {get;set;}
	public TydelikeContact__c tyc {get;set;}
	public Account acc {get;set;}
    public String settyAcId {get;set;}
    public String settyCoId {get;set;}
    public Boolean settyof {get;set;}
    public Integer lineNum {get;set;}
    //报错及显示信息
    public String errorMessage{get;set;}
    public String accId{get;set;}
    public String accountId{get;set;}
    public String accname {get;set;}
    //url
	public String baseUrl { get; set; }
    public String rtUrl { get; set; }
    //检索
    public String tycontactname { get; set; } //联系人姓名
    public String tycountname { get; set; }  //客户姓名
    public String tycountcity { get; set; }  //客户的城市
    // Bean
    public List<DataLineBean> dataLines { get; set; }
    // 行数
    public Integer getDatalineSize() {
        return dataLines == null ? 0 : dataLines.size();
    }

    public TydelikeACController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
    //页面初始化
    public PageReference init() {
        dataLines = new List<DataLineBean>();
        Map<Id,TydelikeAccount__c> tyacMap = new Map<Id,TydelikeAccount__c>();
        Map<Id,List<TydelikeContact__c>> tycoMap = new Map<Id,List<TydelikeContact__c>>();
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        List<TydelikeContact__c> tycoList = new List<TydelikeContact__c>();
        // 明细Bean
        tyacList = [select id,name,OfSkrap__c,TyCity__c,TyAddress__c,TyPostCode__c,Account1__c from TydelikeAccount__c];
        tycoList = [select id,name,OfSkrap__c,TydelikeAccount__c,TyFaxD__c,TyAddress__c,TyPhoneD__c,TyPostcode__c from TydelikeContact__c];
        if (tyacList.size() == 0) {
            return null;
        }
        if (tycoList.size() == 0) {
            return null;
        }
        for (TydelikeAccount__c tyac: tyacList) {
            tyacMap.put(tyac.Id, tyac);
        }
        for (TydelikeContact__c tyco: tycoList) {
            if (tycoMap.containsKey(tyco.TydelikeAccount__c)) {
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }else {
                tycoMap.put(tyco.TydelikeAccount__c, new List<TydelikeContact__c>());
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }  
        }
        Integer cnt = 0;
        for (TydelikeAccount__c tyac1: tyacList) {
            if (tycoMap.containsKey(tyac1.Id)) {
                for(Integer i = 0; i< tycoMap.get(tyac1.Id).size(); i++){
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt ;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = tycoMap.get(tyac1.Id)[i].Name;
                dataLine.tycoId = tycoMap.get(tyac1.Id)[i].Id;
                dataLine.tycoof = tycoMap.get(tyac1.Id)[i].OfSkrap__c;
                dataLine.tyaccount = tycoMap.get(tyac1.Id)[i].TydelikeAccount__c;
                dataLine.tycofax = tycoMap.get(tyac1.Id)[i].TyFaxD__c;
                dataLine.tycoadd = tycoMap.get(tyac1.Id)[i].TyAddress__c;
                dataLine.tycophone = tycoMap.get(tyac1.Id)[i].TyPhoneD__c;
                dataLine.tycopost = tycoMap.get(tyac1.Id)[i].TyPostcode__c;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }else {
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = null;
                dataLine.tycoId = null;
                dataLine.tyaccount = null;
                dataLine.tycofax = null;
                dataLine.tycoadd = null;
                dataLine.tycophone = null;
                dataLine.tycopost = null;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
            }
        }
        System.debug('dataLines='+dataLines);
    	return null;
    }
    //检索
    public PageReference searchTy() {
        dataLines = new List<DataLineBean>();
        Map<Id,TydelikeAccount__c> tyacMap = new Map<Id,TydelikeAccount__c>();
        Map<Id,List<TydelikeContact__c>> tycoMap = new Map<Id,List<TydelikeContact__c>>();
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        List<TydelikeContact__c> tycoList = new List<TydelikeContact__c>();
        // 明细Bean
        String acsoql = 'select id,name,OfSkrap__c,TyCity__c,TyAddress__c,TyPostCode__c,Account1__c from TydelikeAccount__c WHERE Id != null';

        if (String.isNotBlank(tycountname)) {
            acsoql += ' and  Name like \'%' + String.escapeSingleQuotes(tycountname.trim().replaceAll('%', '\\%')) + '%\'';
        }
        if (String.isNotBlank(tycountcity)) {
            acsoql += ' and  TyCity__c like \'%' + String.escapeSingleQuotes(tycountcity.trim().replaceAll('%', '\\%')) + '%\'';
        }

        String cosoql = 'select id,name,OfSkrap__c,TydelikeAccount__c,TyFaxD__c,TyAddress__c,TyPhoneD__c,TyPostcode__c from TydelikeContact__c WHERE Id != null';
       
        if (String.isNotBlank(tycontactname)) {
            cosoql += ' and  Name like \'%' + String.escapeSingleQuotes(tycontactname.trim().replaceAll('%', '\\%')) + '%\'';
        }

        tyacList = Database.query(acsoql);
        tycoList = Database.query(cosoql);
        if (tyacList.size() == 0) {
            return null;
        }
        if (tycoList.size() == 0) {
            return null;
        }
        for (TydelikeAccount__c tyac: tyacList) {
            tyacMap.put(tyac.Id, tyac);
        }
        for (TydelikeContact__c tyco: tycoList) {
            if (tycoMap.containsKey(tyco.TydelikeAccount__c)) {
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }else {
                tycoMap.put(tyco.TydelikeAccount__c, new List<TydelikeContact__c>());
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }
        }
        Integer cnt = 0;
        for (TydelikeAccount__c tyac1: tyacList) {
            if (tycoMap.containsKey(tyac1.Id)) {
                for(Integer i = 0; i< tycoMap.get(tyac1.Id).size(); i++){
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt ;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = tycoMap.get(tyac1.Id)[i].Name;
                dataLine.tycoId = tycoMap.get(tyac1.Id)[i].Id;
                dataLine.tycoof = tycoMap.get(tyac1.Id)[i].OfSkrap__c;
                dataLine.tyaccount = tycoMap.get(tyac1.Id)[i].TydelikeAccount__c;
                dataLine.tycofax = tycoMap.get(tyac1.Id)[i].TyFaxD__c;
                dataLine.tycoadd = tycoMap.get(tyac1.Id)[i].TyAddress__c;
                dataLine.tycophone = tycoMap.get(tyac1.Id)[i].TyPhoneD__c;
                dataLine.tycopost = tycoMap.get(tyac1.Id)[i].TyPostcode__c;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }else {
                if (String.isBlank(tycontactname)) {
                    DataLineBean dataLine = new DataLineBean();
                    dataLine.lineNum = cnt;
                    cnt ++;
                    dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                    dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                    dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                    dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                    dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                    dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                    dataLine.tycoName = null;
                    dataLine.tycoId = null;
                    dataLine.tyaccount = null;
                    dataLine.tycofax = null;
                    dataLine.tycoadd = null;
                    dataLine.tycophone = null;
                    dataLine.tycopost = null;
                    dataLines.add(dataLine);
                    System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }
        }
        System.debug('dataLines='+dataLines);
        return null;
    }
    //删除和批量删除
    public PageReference setOfSkrapBtn() {
        List<Id> tycoofIdList = new List<Id>();
        List<Id> tyacofIdList = new List<Id>();
        for (DataLineBean d : dataLines) {
            if(d.bondel == true){
                if (d.tyaccount != null) {
                    tycoofIdList.add(d.tycoId);
                }else {
                    tyacofIdList.add(d.tyacId);
                }
            }
        }
        System.debug('tyacofIdList='+tyacofIdList);

        
        Map<Id,TydelikeAccount__c> uptyacMap = new Map<Id,TydelikeAccount__c>();
        List<TydelikeAccount__c> tyacofList = new List<TydelikeAccount__c>();
        tyacofList = [select id,OfSkrap__c from TydelikeAccount__c];
        if (tyacofIdList.size() > 0) {
            for (TydelikeAccount__c uptyac : tyacofList) {
                TydelikeAccount__c uptyacof = new TydelikeAccount__c();
                if (tyacofIdList.contains(uptyac.Id)) {
                    uptyacof.Id = uptyac.Id;
                    uptyacof.OfSkrap__c = true;
                    //uptyacList.add(uptyacof);
                    uptyacMap.put(uptyacof.Id,uptyacof);
                }
            }
        }
        if (uptyacMap.size() > 0) {
            update uptyacMap.values();
        }
        
        Map<Id,TydelikeContact__c> uptycoMap = new Map<Id,TydelikeContact__c>();
        List<TydelikeContact__c> tycoofList = new List<TydelikeContact__c>();
        tycoofList = [select id,OfSkrap__c from TydelikeContact__c];
        if (tycoofIdList.size() > 0) {
            for (TydelikeContact__c uptyco : tycoofList) {
                TydelikeContact__c uptycoof = new TydelikeContact__c();
                if (tycoofIdList.contains(uptyco.Id)) {
                    uptycoof.Id = uptyco.Id;
                    uptycoof.OfSkrap__c = true;
                    //uptyacList.add(uptyacof);
                    uptycoMap.put(uptycoof.Id,uptycoof);
                }
            }
        }
        if (uptycoMap.size() > 0) {
            update uptycoMap.values();
        }
        
        
        return null;
    }

    public class DataLineBean {
        //批量
        public Boolean bondel {get;set;}
        //行号
        public Integer lineNum {get;set;}
        //临时客户Id
        public String tyacId {get;set;}
        //临时客户名
        public String tyacName {get;set;}
        //临时客户是否删除
        public Boolean tyacof {get;set;}
        //临时客户城市
        public String tyaccity {get;set;}
        //临时客户地址
        public String tyacadd {get;set;}
        //临时客户邮编
        public String tyacpost {get;set;}
        //临时联系人Id
        public String tycoId {get;set;}
        //临时联系人名
        public String tycoName {get;set;}
        //临时联系人是否删除
        public Boolean tycoof {get;set;}
        //临时联系人的查找临时客户 同时也是临时客户
        public String tyaccount {get;set;} 
        //临时联系人传真
        public String tycofax {get;set;}
        //临时联系人地址
        public String tycoadd {get;set;}
        //临时联系人电话 
        public String tycophone {get;set;}
        //临时联系人邮政编码
        public String tycopost {get;set;}
    }
}