public with sharing class LexPrintInspectupReportController {
    public lexPrintInspectupReportController() {
        
    }
    @AuraEnabled
    public static InitData init(String recordId) {
        InitData initData = new InitData();
        initData.maintenanceContractId = recordId;
        getAssetType(initData);

        return initData;
    }

    public class InitData {
        //维修合同id
        public String maintenanceContractId;
        //是否空白单据
        public String isResultShow;
        //根据保有设备查询合同
        public String selectAssetType;
        //战略科室的下拉内容
        public List<CustomData> strategyDepartment;
        //科室的下拉内容
        public List<CustomData> department;
        //设备的装机地点
        public List<CustomData> installationSite;
        //是否分页
        public Boolean isPaging;

        public String produceText;
        //洗消信息
        public Boolean activeOn ;
        //点检数据
        public List<Inspectup_Plan__c>  inspectupPlanList;
        //条件参数
        public String parameter;

    }


    //获取修理合同上的保有设备都是那个战略科室和科室
    public static void getAssetType(InitData initData){
        String assetTypeSQl = makeTextDepSQl(initData.maintenanceContractId);
        try{
            List<Maintenance_Contract_Asset__c> assetTypeList = Database.query(assetTypeSQl);
            if(assetTypeList != null && assetTypeList.size() > 0){
                //临时字符串
                String strategyDepartmentStr = '';
                String departmentStr = '';
                String installationSiteStr = '';
                //战略科室存放处
                initData.strategyDepartment = new List<CustomData>();
                initData.department = new List<CustomData>();
                initData.installationSite = new List<CustomData>();
                for(Maintenance_Contract_Asset__c mca : assetTypeList){
                    //所有的战略科室
                    if(strategyDepartmentStr.indexOf(mca.Asset__r.Strategic_department_Class_Name__c) == -1){
                        strategyDepartmentStr += mca.Asset__r.Strategic_department_Class_Name__c;
                        initData.strategyDepartment.add(new CustomData(mca.Asset__r.Department_Class__c,mca.Asset__r.Acc_Department_Class_Name__c));
                    }
                    //科室
                    if(departmentStr.indexOf(mca.Asset__r.Account.Name) == -1){
                        departmentStr += mca.Asset__r.Account.Name;
                        initData.department.add(new CustomData(mca.Asset__r.Account.Id,mca.Asset__r.Department_Name__c));
                    }
                    //装机地点
                    if(mca.Asset__r.Installation_Site__c != null && mca.Asset__r.Installation_Site__c != ''){
                        if(installationSiteStr.indexOf(mca.Asset__r.Installation_Site__c) == -1){
                            installationSiteStr += mca.Asset__r.Installation_Site__c;
                            initData.installationSite.add(new CustomData(mca.Asset__r.Installation_Site__c,mca.Asset__r.Installation_Site__c));
                        }
                    }
                }
            }
        }catch(exception e){
            //失败提示
            ApexPages.addMessages(e);
        }
    }
    //获取所有的点检数据
    public static void getInspectupPlan(InitData initData){
        String InspectupPlanSQL = makeTextInspectupPlanSQl(initData.maintenanceContractId);
        try{
            initData.inspectupPlanList = Database.query(InspectupPlanSQL);
        }catch(exception e){
            //失败提示
            ApexPages.addMessages(e);
        }
    }
    //查询点检报告书的pdf
    public static String makeTextInspectupPlanSQl(String maintenanceContractId){
        String InspectupPlanSQL = 'SELECT ID,NAME,Execution_End_Date__c,Chack_Plan_NO__c FROM Inspectup_Plan__c WHERE Maintenance_Contract__c = \''+maintenanceContractId+'\' '
                                + ' order by Maintenance_Contract__c desc';
        return InspectupPlanSQL;
    }

    //查询修理合同里面保有设备都是那些科室和战略科室
    public static String makeTextDepSQl(String maintenanceContractId){
        String depSQl = 'select id,name,Asset__c,Asset__r.name,Asset__r.SerialNumber,Asset__r.Strategic_department_Class_Name__c,Asset__r.Department_Class__c,Asset__r.Acc_Department_Class_Name__c,'
                    +' Asset__r.Department_Name__c,Asset__r.Account.Id,Asset__r.Account.Name,Asset__r.Installation_Site__c '
                    +' from Maintenance_Contract_Asset__c '
                    //+' where Check_object__c = true and Maintenance_Contract__c = \''+maintenanceContractId+'\' '
                    +' where Maintenance_Contract__c = \''+maintenanceContractId+'\' and isdianjian__c != 0 '
                    +' order by Asset__r.Acc_Department_Class_Name__c,Asset__r.Account.Name';
        return depSQl;
    }



    /**
     * 为了方便前端table获取值
     */
    class CustomData {
        //id
        public String parameterId ;
        //name
        public String parameterName ; 

        public CustomData(String accountId,String accountName){
            parameterId = accountId;
            parameterName = accountName;
        }    
    }
}