public without sharing class SearchManagementController {

    public String baseUrl { get; private set; }
    public String baId { get; private set; }
    public String tsRepairId { get; private set; }
    public String ufId { get; private set; }
    public Boolean ErrorFlag { get; private set; }

    public String searchManagementCode {get;set;}

    public User_FaultInfo__c  UserFaultInfo  {get;set;}  
    public List<User_FaultInfo__c> UserFaultInfoList {get;set;}
    public Integer ErrorNum {get;set;} 

    public String fromUserFaultInfoId {get;set;}

    public String lineno {get;set;}
    public SearchManagementController() {
        ErrorFlag = false;
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        // 待更新的业务活动Id
        baId = System.currentPageReference().getParameters().get('baId');
        // TS日报Id
        tsRepairId = System.currentPageReference().getParameters().get('repairId');
        // 待更新的用户故障信息的Id
        ufId = System.currentPageReference().getParameters().get('ufId');

        lineno = System.currentPageReference().getParameters().get('lineno');
    }
    public void init () {
        ErrorFlag = false;
        ErrorNum = 0;
        UserFaultInfoList = new List<User_FaultInfo__c>();
        searchManagementCode = '';
        UserFaultInfo = new User_FaultInfo__c();  
        if (searchManagementCode != null && searchManagementCode != '' || UserFaultInfo.UserFaultInfoReportDate__c != null ) {
            serUserFaultInfo();
        }
    }

    public PageReference serUserFaultInfo(){
        //当前用户ID
        String userId = UserInfo.getUserId();
        String soql = TSRepairUtil.UserFaultInfoBaseSQL();
        String recordTypeId = System.label.Case_RecordTypeTSTSReport;
        //管理编号不为NULL
        if(String.isNotBlank(searchManagementCode)){
            soql += ' and UFManagementCode__c like \'%' + String.escapeSingleQuotes(searchManagementCode.trim().replaceAll('%', '\\%')) + '%\'';
        }
        //报告日期不为NULL
        if(UserFaultInfo.UserFaultInfoReportDate__c != null ){
           
            soql += ' and UserFaultInfoReportDate__c = '+ String.valueOf(UserFaultInfo.UserFaultInfoReportDate__c);
        }
        //查找客户不为null
        if(UserFaultInfo.UFAccount__c !=null){
            String accountId = UserFaultInfo.UFAccount__c;
            soql += ' and UFAccount__c  =\''+ accountId +'\'';
            // soql += ' AND ' + TSRepairUtil.AccountBaseRecordTypeIdSQL2();
        }

        soql +=  ' and OwnerId = \'' + userId + '\'' ;
        soql +=  ' AND UserFaultInfoCompletionDate__c = null ';  
        soql +=  ' AND RecordTypeId = \'' + recordTypeId + '\''; 
        soql +=  ' order by Id' ;
        System.debug('soql------>222->'+soql);
        UserFaultInfoList = Database.query(soql);

        return null;
    }

    public void save(){

        // 业务活动
        ErrorFlag = true;
        String errorMessages = '';
        Map<String, User_FaultInfo__c> userFaultInfoMap = new Map<String, User_FaultInfo__c>();
        if (UserFaultInfoList != null && UserFaultInfoList.size() > 0) {

            for (User_FaultInfo__c uf :UserFaultInfoList) {
                if (uf.UserFaultInfoBeSelected__c) {
                    uf.UserFaultInfoBeSelected__c = false;
                    userFaultInfoMap.put(uf.UFManagementCode__c, uf);
                }
            }
            if (userFaultInfoMap != null && userFaultInfoMap.size() == 1) {
                ErrorNum = userFaultInfoMap.size();
                ErrorFlag = false;
            } else if (userFaultInfoMap.size() > 1) {
                
                errorMessages = '只能选择一个业务活动，请确认';
            } else if (userFaultInfoMap.size() < 1) {
                errorMessages = '请选择一个业务活动，请确认';
            }

            if (ErrorFlag) {
                ErrorNum = userFaultInfoMap.size();
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, errorMessages));
            } else {

                Savepoint sp = Database.setSavepoint();
                try {
                    for (String uFManagementCode : userFaultInfoMap.keySet()) {
                        User_FaultInfo__c updateUserFaultInfo = userFaultInfoMap.get(uFManagementCode);
                        fromUserFaultInfoId = updateUserFaultInfo.Id;
                    }
                
                }catch (Exception e) {
                    ApexPages.addMessages(e);
                    Database.rollback(sp);
                }
            }
        }


        
    }
}