global class  OpdPlanWebService {
    //按钮调用 SWAG-BUF6J5  20201110 youchang

    WebService  static String changeTrade(String opdplanId) { //取消opd 和取消备品得窗口调用
        List<Rental_Apply__c> raList = [select Yi_loaner_arranged__c, Shipment_request_Cnt__c from Rental_Apply__c where OPDPlan__c = :opdplanId];
        String rtn = 'OK';
        if (null != raList && raList.size() > 0) {
            Integer yla = 0;
            Integer srct = 0;
            for (Rental_Apply__c ra : raList) {
                yla += Integer.valueOf(ra.Yi_loaner_arranged__c);
                srct += Integer.valueOf(ra.Shipment_request_Cnt__c);
            }
            if (yla > 0) {
                rtn = '备品已经出库，不能取消或延期OPD计划.';
            } else if (srct > 0) {
                rtn = '备品已做出库指示，请联络备品中心取消或延期计划.';
            }
        }

        return rtn;
    }
    WebService  static String changeCampaign(String campaignId) { //学会取消得窗口调用
        List<Rental_Apply__c> raList = [select Yi_loaner_arranged__c, Shipment_request_Cnt__c from Rental_Apply__c where Campaign__c = :campaignId];
        String rtn = 'OK';

        //因为报50001得错误。所以取消聚合函数得引用
        // List<AggregateResult> opdList = [select sum(Yi_loaner_arranged__c) yla,sum(Shipment_request_Cnt__c) src from Rental_Apply__c where Campaign__c = :campaignId];

        if (null != raList && raList.size() > 0) {
            Integer yla = 0;
            Integer srct = 0;
            for (Rental_Apply__c ra : raList) {
                yla += Integer.valueOf(ra.Yi_loaner_arranged__c);
                srct += Integer.valueOf(ra.Shipment_request_Cnt__c);
            }
            if (yla > 0) {
                rtn = '备品已经出库，不能取消或延期学会.';
            } else if (srct > 0) {
                rtn = '备品已做出库指示，不能取消或延期学会.';
            }
        }

        return rtn;

    }
    // 20230118 ljh DB202301265636 start
    /**
     * @fuction   [当会议中有备品出库的时候允许进行会议取消申请,
     * 会议取消的时候对应的OPD计划也需要取消
     * 预计回收日需要修改]
     * @Author    lijinhuan
     * @DateTime  2023-01-18T15:10:48+0800
     * @version   [version]
     * @param     campaignId               [description]
     * @return                             [description]
     */
    WebService  static String changeCampaignCancel(String campaignId) { //学会取消得窗口调用
        List<Rental_Apply_Equipment_Set__c> raesList = [select Id,Rental_End_Date__c from Rental_Apply_Equipment_Set__c where Rental_End_Date__c != null 
                                AND Shippment_loaner_time2__c != null
                                AND Rental_Apply__r.RA_Status__c != '完了'
                                AND Rental_Apply__r.RA_Status__c != '取消'
                                AND Rental_Apply__r.Campaign__c = :campaignId ];
        String rtn = 'OK';
        Savepoint sp = Database.setSavepoint();
        try{
            if (null != raesList && raesList.size() > 0) {
                for (Rental_Apply_Equipment_Set__c raes : raesList) {
                    raes.Rental_End_Date__c = Date.today().addDays(5);            
                }
                update raesList;  
            }
        }catch (Exception ex) {
            System.debug(ex.getStackTraceString());
            System.debug('ex.getMessage()***'+ ex.getMessage()+ex.getLineNumber());
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            rtn = ex.getMessage();
        }
        return rtn;
    }
    // 20230118 ljh DB202301265636 end

    WebService  static String dataCheck(String rentalApplyId) {
        string result = 'OK';
        if (String.isBlank(rentalApplyId)) {
            result = 'opd计划无备品借出申请，无法进行OPD补充申请';
            return result;
        }
        List<String> RAStatusList = new List<String>();
        RAStatusList.add('申请中');
        RAStatusList.add('已批准');
        RAStatusList.add('已出库指示');
        RAStatusList.add('已下架');
        RAStatusList.add('出库前已检测');
        List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
        raList = [SELECT Id, Name, RA_Status__c, Status__c  FROM Rental_Apply__c
                  WHERE Id = :rentalApplyId AND demo_purpose2__c != '试用（有询价）'
                             AND demo_purpose2__c != '试用（无询价）'
                             AND demo_purpose2__c != '新产品评价'
                             AND Status__c != '申请中'
                             AND RA_Status__c not In:RAStatusList limit 1];
        //不满足要求 不能创建 opd计划
        if (raList.size() > 0) {
            Rental_Apply__c ra =  raList[0];
            String status = ra.RA_Status__c;
            if (ra.Status__c != '申请中') {
                result = 'opd计划下备品借出申请 状态 不是 申请中，无法进行OPD补充申请';
            } else if (!RAStatusList.contains(status)) {
                result = 'opd计划下备品借出申请 备品借出状态 不是 申请中、已批准、已出库指示、已下架、出库前已检测，无法进行OPD补充申请';
            }  else {
                result = 'opd计划下备品借出申请 使用目的2 不是 试用（有询价）、试用（无询价）、新产品评价，无法进行OPD补充申请';
            }
        }

        return result;
    }

    WebService  static String raesCountCheck(String rentalApplyId) {
        String result = 'OK';
        List<Rental_Apply__c> raLsit = [
                                           SELECT Id,
                                           OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c
                                           from Rental_Apply__c
                                           where SupplementCreated__c = true
                                                   AND OPDPlan__c != null
                                                   AND Id = :rentalApplyId];
        if (raLsit.size() > 0 ) {
            // 获取 补充申请OPD计划创建的 备品借出申请Id 以及 被克隆的备品借出申请Id
            Set<Id> raIdSet = new Set<Id>();
            for (Rental_Apply__c ra : raLsit) {
                Id raId = ra.Id;
                Id cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
                raIdSet.add(raId);
                raIdSet.add(cloneRaId);
            }

            //查找 备品借出申请下 已有多少个借出备品配套一览
            AggregateResult[] aggArr = [
                                           SELECT Rental_Apply__c raId, count(Id) cnt
                                           FROM Rental_Apply_Equipment_Set__c
                                           WHERE Rental_Apply__c
                                           In :raIdSet
                                           //AND Cancel_Reason__c != null
                                           group by Rental_Apply__c
                                       ];
            Map<String, Integer> raIdCntMap = new Map<String, Integer>();

            for (AggregateResult ar : aggArr ) {
                String raId = String.valueOf(ar.get('raId'));
                Integer cnt = Integer.valueOf(ar.get('cnt'));
                raIdCntMap.put(raId, cnt);

            }
            Map<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
            for (Rental_Apply__c ra : raLsit) {
                String raId = ra.Id;
                Integer  raCnt = 0;
                if (raIdCntMap.containskey(raId)) {
                    raCnt = raIdCntMap.get(raId);
                }
                String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;

                Integer cloneRacnt = 0;
                if (raIdCntMap.containskey(cloneRaId)) {
                    cloneRacnt = raIdCntMap.get(cloneRaId);
                }
                //system.debug('cloneRacnt------>' + cloneRacnt);
                if ( raCnt > cloneRacnt) {
                    raCheckMap.put(raId, true);
                }

            }
            if (raCheckMap != null && raCheckMap.size() > 0) {
                result = '借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量';
            }

        }
        return result;
    }

    @TestVisible public static void test() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }

}