public with sharing class TransferApplyDetailController {
    //OCSM_BP5-35 邮件模板 公用调拨明细
    public Id taId{get;set;}
    public String varType{get;set;}
    public List<TransferApplyDetail__c> tadList {get;set;}
    public String gatherName{get;set;}
    public List<String>  title{get;set;}
    public List<String>  column {get;set;}
    private static final Map<String, Schema.FieldSet> FSMAP = Schema.SObjectType.TransferApplyDetail__c.fieldSets.getMap();
    public String  getInit(){
        Schema.FieldSet fs = FSMAP.get(gatherName);//TransferEmail
        List<FieldSetMember> fsmList = fs.getFields();
        // 获得字段集中的字段名
        title = new List<String>();
        column = new List<String>();
        for (FieldSetMember fsm : fsmList) {
            if(fsm.getLabel().contains('(Sys)')){
                title.add(fsm.getLabel().replace('(Sys)',''));
            }else{
                title.add(fsm.getLabel()); 
            }
            column.add(fsm.getFieldPath());
        }
        List<TransferApplyDetail__c> tadTempList = new List<TransferApplyDetail__c>();
        String soql = 'SELECT  Asset__c,TransferApplySummary__c,TransferType__c,Add_Request_demo_time__c ';
        for (Integer i=0;i<column.size();i++) {
            soql += ',' + column[i];
        }
        soql += ' FROM TransferApplyDetail__c '; 
        soql += ' WHERE  TransferApply__c = \''+taId+'\''; 
        if(String.isNotBlank(varType) && varType == 'cancel'){
            soql += ' AND Cancel_Time__c != null ';
            soql += ' AND Cancel_Select__c = true'; 
            soql += ' AND Inspection_result__c != \'NG\'';           
        }else{
            soql += ' AND Cancel_Select__c = False';
        }
        if(String.isNotBlank(varType) && varType == 'wp'){
            soql += ' AND After_Inspection_time_F__c != null AND Inspection_result_after_F__c != null AND Inspection_result_after_NG_F__c != \'维修\'';
            soql += ' AND IsPerfect__c = False';               
        }
        if(String.isNotBlank(varType) && varType == 'IsPerfect'){
            soql += ' AND IsPerfect__c = true';
            soql += ' AND Arrival_in_wh__c = False';                
        }
        if(String.isNotBlank(varType) && varType == 'add'){
            soql += ' AND Create_State__c = 1';
        }
        if(String.isNotBlank(varType) && varType == 'add'){
            soql += ' ORDER BY  Add_Request_demo_time__c DESC nulls first,TransferApplySummary__c,Name';
        }else{
            soql += ' ORDER BY  TransferApplySummary__c,Name';
        }
        tadTempList = Database.query(soql);
        if(String.isNotBlank(varType) && varType == 'add'){
            TransferApplyDetail__c t;
            List<TransferApplyDetail__c> addList = new List<TransferApplyDetail__c>();
            for (TransferApplyDetail__c tt : tadTempList) {
                if (t == null) {
                    t = tt;
                }
                if (t.Add_Request_demo_time__c != tt.Add_Request_demo_time__c) {
                    break;
                }
                addList.add(tt);
            }
            //if(addList != null && addList.size()>0){
            tadTempList = null;
            tadTempList = addList;
            //}
        }
        Map<String,TransferApplyDetail__c> tempMap = new Map<String,TransferApplyDetail__c>();
        for(TransferApplyDetail__c temp:tadTempList){
            String tempKey = temp.TransferApplySummary__c;
            if(String.isNotBlank(temp.Fixture_Model_No_F__c)){
                tempKey += temp.Fixture_Model_No_F__c;
            }
            if(String.isNotBlank(temp.SerialNumber__c)){
                tempKey += temp.SerialNumber__c;
            }
            if(tempMap.containsKey(tempKey)){
                TransferApplyDetail__c  tad = tempMap.get(tempKey);
                //调拨数量  待废弃调拨数  待修理调拨数  丢失调拨数
                if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '待废弃'){
                    tad.VF_TransferCount_Abandoned__c += 1;
                }else if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '待修理'){
                    tad.VF_TransferCount_Repair__c += 1;
                }else if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '丢失找回'){
                    tad.VF_TransferCount_Lost__c += 1;
                }else{
                    tad.VF_TransferCount__c += 1;
                }
                tempMap.put(tempKey,tad);
            }else{
                temp.VF_TransferCount_Abandoned__c = 0;
                temp.VF_TransferCount_Repair__c = 0;
                temp.VF_TransferCount_Lost__c = 0;
                temp.VF_TransferCount__c = 0;
                if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '待废弃'){
                    temp.VF_TransferCount_Abandoned__c = 1;
                }else if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '待修理'){
                    temp.VF_TransferCount_Repair__c = 1;
                }else if(String.isNotBlank(temp.TransferType__c) && temp.TransferType__c == '丢失找回'){
                    temp.VF_TransferCount_Lost__c = 1;
                }else{
                    temp.VF_TransferCount__c = 1;
                }
                tempMap.put(tempKey,temp);
            }
        }
        tadList = (List<TransferApplyDetail__c>)tempMap.values();
        return '';
    }
}