public with sharing class ConsumApplyApprovalProcessController {

    @AuraEnabled
    public static InitDate initSubmitApprovalProcessController(String recordId) {
        InitDate res = new InitDate();

        
        Consum_Apply__c apply = [SELECT Id,
                            Request_Shipping_7days_Before__c,
                            Request_shipping_day__c,
                            Campaign__c,
                            QIS_number__c,
                            QIS_ID_Line__c,
                            Status__c,
                            Min_Consum_Can_Request_approval_Date__c,
                            applyUser__c,
                            JingliApprovalManager__c,
                            SalesManager__c,
                            BuchangApprovalManager__c,
                            BuchangApprovalManagerSales__c,
                            ZongjianApprovalManager__c, 
                            AccDealerBlacklist__c,
                            direct_send__c,
                            Medical_Institution_Address__c, // 20240103 fy  DB202312530994 add
                            Medical_Institution_Address_sup__c, // 20240103 fy  DB202312530994 add
                            CreatedDate,// 20240103 fy  DB202312530994 add
                            Hospital__r.TradeComplianceStatus__c, //20240121 kk 贸易合规2 add
                            Hospital__r.Name,//20240121 kk 贸易合规2 add
                            OwnerId FROM Consum_Apply__c WHERE Id = :recordId LIMIT 1];
        res.id = apply.Id;
        res.shippingDays = apply.Request_Shipping_7days_Before__c;
        res.shippingDay = apply.Request_shipping_day__c;
        res.campaign = apply.Campaign__c;
        res.qISNumber = apply.QIS_number__c;
        res.qISIDLine = apply.QIS_ID_Line__c;
        res.status = apply.Status__c;
        res.minApprovalDate = apply.Min_Consum_Can_Request_approval_Date__c;
        res.applyUser = apply.applyUser__c;
        res.jingliManager = apply.JingliApprovalManager__c;
        res.salesManager = apply.SalesManager__c;
        res.buchangManager = apply.BuchangApprovalManager__c;
        res.buchangManagerSale = apply.BuchangApprovalManagerSales__c;
        res.zongjianManager = apply.ZongjianApprovalManager__c;
        res.accDealerBlacklistc = apply.AccDealerBlacklist__c;
        res.tradeComplianceStatusFlagBP = System.Label.TradeComplianceStatusFlagBP;
        res.iFTradeComplianceAlertBP = System.Label.IFTradeComplianceAlertBP;
        res.owner = apply.OwnerId;
        res.directSend = apply.direct_send__c;
        // 20240103 fy  DB202312530994 start
        res.medicalInstitutionAddress = apply.Medical_Institution_Address__c;
        res.medicalInstitutionAddresssup = apply.Medical_Institution_Address_sup__c;
        res.createdDate = apply.CreatedDate;
        // 20240103 fy  DB202312530994 end
        //kk 贸易合规2期  20240121 start 
        res.accountTradeStatus = apply.Hospital__r.TradeComplianceStatus__c;
        res.accountName = apply.Hospital__r.Name;
        List<String> caesdStringList = new List<String>();
        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [select id,name,Asset__c,Asset__r.Product2.ProTradeComplianceStatus__c,
                                                                    Asset__r.Account.TradeComplianceStatus__c  
                                                                    from Consum_Apply_Equipment_Set_Detail__c where Consum_Apply__c = :recordId and Asset__c  != null];
        if (caesdList.size()>0) {
            for (Consum_Apply_Equipment_Set_Detail__c caesd : caesdList) {
                if (apply.Hospital__r.TradeComplianceStatus__c == '警示名单' && caesd.Asset__r.Product2.ProTradeComplianceStatus__c == '0') {
                    res.hgerqi = 'true';
                    caesdStringList.add(caesd.Id);
                }
            }

            //20240204 sx 发送邮件 start
            if(caesdStringList.size()>0){
                SendEmailUtil.tradeConsumTempleSend(recordId, '提交待审批', null, caesdStringList);
            }
            //20240204 sx 发送邮件 end
        }
        //kk 贸易合规2期  20240121 end
        
        return res;

    }

    public class InitDate{

        @AuraEnabled
        public String id;
    
        @AuraEnabled
        public Date shippingDays;//希望到货日前7个工作日
    
        @AuraEnabled
        public Date shippingDay;//希望到货日
    
        @AuraEnabled
        public String campaign ;//学会
    
        @AuraEnabled
        public String qISNumber;//QIS
    
        @AuraEnabled
        public String qISIDLine;//QIS的ID值
    
        @AuraEnabled
        public String status;//	状态
    
        @AuraEnabled
        public Date minApprovalDate;//最小可以提交申请日期
    
        @AuraEnabled
        public String applyUser;//操作者
    
        @AuraEnabled
        public String jingliManager;//CL6 经理级(服务)
    
        @AuraEnabled
        public String salesManager;//CL6 经理级
    
        @AuraEnabled
        public String buchangManager;//CL5 总监级(服务)
    
        @AuraEnabled
        public String buchangManagerSale;//CL5 总监级
    
        @AuraEnabled
        public String zongjianManager;//CL4 高级总监

        @AuraEnabled
        public String accDealerBlacklistc;//客户是否为黑名单

        @AuraEnabled
        public String tradeComplianceStatusFlagBP;//贸易合规开关

        @AuraEnabled
        public String iFTradeComplianceAlertBP;//贸易合规提醒

        //kk 贸易合规2期  20240121 start 
        @AuraEnabled
        public String accountTradeStatus;//医院贸易合规状态

        @AuraEnabled
        public String hgerqi;//是否有不合规的保有设备

        @AuraEnabled
        public String accountName; //医院名字
        //kk 贸易合规2期  20240121 end

    
        @AuraEnabled
        public String owner;//所有人

        @AuraEnabled
        public String directSend;//发送方

        // 20240103 fy  DB202312530994 start
        @AuraEnabled
        public String medicalInstitutionAddress;//医疗机构地址

        @AuraEnabled
        public String medicalInstitutionAddresssup;//医疗机构地址补充

        @AuraEnabled
        public Datetime createdDate;//创建日期
        // 20240103 fy  DB202312530994 end
      
    }

    @AuraEnabled
    public static List<Campaign> selectCampaignById(String recordId){
        
        List<Campaign> campaign=[SELECT Status, Rental_Apply_Flag__c FROM Campaign WHERE id  =:recordId]; 
        
        return campaign;
    }
    
    @AuraEnabled
    public static List<QIS_report__c> selectReportById(String recordId){
        
        List<QIS_report__c> report=[SELECT id,next_action__c FROM QIS_report__c WHERE id  =:recordId]; 
        return report;
    }
    
    @AuraEnabled
    public static List<Consum_Apply_Equipment_Set__c> selectEquipmentById(String recordId){
        
        List<Consum_Apply_Equipment_Set__c> equipment=[SELECT Id FROM Consum_Apply_Equipment_Set__c 
                                                            WHERE RetalFSetDetail_Cnt__c = 0 AND Consum_Apply__c  =:recordId]; 
    
        return equipment;
    }

    @AuraEnabled
    public static String approvalCheck(String recordId){
        
        String res = ConsumApplyWebService.approvalCheck(recordId);
        return res;
    }

    @AuraEnabled
    public static String setSObjectShare(String applyUser,String parentId,List<String> userAccess, String ownerId){
        
        String res = ControllerUtil.setSObjectShare('Consum_Apply__Share','ApplyUserShare__c',parentId,userAccess,ownerId);
        System.debug('controller---res'+res);
        return res;
    }

    @AuraEnabled
    public static String updateConsumApply(String recordId){
        try{
            Consum_Apply__c apply = [SELECT Id,
            Request_Shipping_7days_Before__c,
            Request_shipping_day__c,
            Campaign__c,
            QIS_number__c,
            QIS_ID_Line__c,
            Status__c,
            Min_Consum_Can_Request_approval_Date__c,
            applyUser__c,
            JingliApprovalManager__c,
            SalesManager__c,
            BuchangApprovalManager__c,
            BuchangApprovalManagerSales__c,
            ZongjianApprovalManager__c, 
            OwnerId FROM Consum_Apply__c WHERE Id = :recordId LIMIT 1];
            apply.Status__c = '填写完毕';
            update apply;
            return '1';
        }catch(Exception e){
            if (e.getMessage().contains(':')) {
                String eMessage = e.getMessage();
                Integer left  = e.getMessage().indexOf(',')+1;
                Integer right = e.getMessage().lastIndexOf('。')+1;
                return eMessage.substring(left,right);
            }else{
                return e.getMessage();
            }
        }
        
        
        
    }

    
}