/*
 * @Description: 备品检测分析报告
 * @Author: [lijinhuan]
 * @Date: 2023-06-25 20:04:41
 * @LastEditors: [lijinhuan]
 * @LastEditTime: 2023-07-06 15:50:03
 */
public without sharing class lexRentalApplyFaultReportController {
    @AuraEnabled
    public static List<RetObject> init(String recordId){
        try {
            List<RetObject> retObjectList = new List<RetObject>();
            //备品借出申请一览明细
            List<Rental_Apply_Equipment_Set_Detail__c> eList = [
                SELECT Id, Name,  
                Rental_Apply__r.OCM_dept_category__c,
                Rental_Apply__r.Name,
                Rental_Apply__r.Person_In_Charge__c,
                Rental_Apply__r.Person_In_Charge__r.Name,  
                Rental_Apply__r.demo_purpose2__c, 
                Rental_Apply__r.WorkPlace__c, 
                Rental_Apply__r.Hospital__r.Name, 
                Loaner_asset_no__c,
                AssetName__c,
                Fixture_Model_No__c, 
                SerialNumber_F__c, 
                Inspection_result_after__c, 
                Inspection_result_after_NG__c
                FROM Rental_Apply_Equipment_Set_Detail__c
                WHERE Rental_Apply__c = :recordId
                AND Inspection_result_after__c = 'NG'
                AND Is_Body__c = true
                ORDER BY Id];

            List<Id> eIds = new List<Id>();
            for(Rental_Apply_Equipment_Set_Detail__c e :eList){
                eIds.add(e.Id);
            }
            //查当前明细下有多少故障报告
            // 0 没有报告
            // Id 有 最新一条为草案中
            // 2 有很多条
            Map<String,String> sIdMap = New Map<String,String>();
            for(Rental_Apply_Fault__c raf : [SELECT Id ,status__c ,Rental_Apply_Equipment_Set_Detail__c from Rental_Apply_Fault__c where Rental_Apply_Equipment_Set_Detail__c in :eIds Order By CreatedDate DESC]){
                if(!sIdMap.containsKey(raf.Rental_Apply_Equipment_Set_Detail__c)){
                    sIdMap.put(raf.Rental_Apply_Equipment_Set_Detail__c,raf.Id);
                }
            }
            for(Rental_Apply_Equipment_Set_Detail__c rae : eList){
                RetObject temp = new RetObject();
                temp.rae = rae;
                if(sIdMap.containsKey(rae.Id)){        
                    temp.faultId = sIdMap.get(rae.Id) ;    
                }else{
                    temp.faultId = '';
                }
                retObjectList.add(temp);
            }
            return retObjectList;
        }
        catch (Exception e) {
            System.debug('*******lexRentalApplyFaultReportController******'+e.getMessage());
        } 
        return null;
    } 

    public class RetObject {
        @AuraEnabled
        public Rental_Apply_Equipment_Set_Detail__c rae;
        @AuraEnabled
        public String faultId;
    }
}