public with sharing class TenderDeleteLwcController {
    @AuraEnabled
    public static  String GetTenderinformationcData(String ParamIdStr) {

         List<Tender_information__c> arrays = Database.query('Select Id,Name,InfoId__c,Logical_delete__c,ProjectId__c,Retain_Tender__c From Tender_information__c Where id = : ParamIdStr');
         
         return JSON.serialize(arrays);
    }

	// 查询招标项目
    @AuraEnabled
    public  static String searchTender(String content){
		String soql = 'Select Id, Name, TenderManageCode__c, Logical_delete__c, Retain_Tender__c From Tender_information__c where Logical_delete__c = false ';
		if (String.isNotBlank(content) && String.isNotBlank(content.replaceAll(' ', ''))) {
			content = '%'+content+'%';
            soql += ' and TenderManageCode__c like :content ';
		}
		system.debug('System.Label.BiddingProjectRecordTypeID+++'+System.Label.BiddingProjectRecordTypeID);
		String RecordTypeIdvalue=System.Label.BiddingProjectRecordTypeID;
		soql += 'and RecordTypeId =\''+RecordTypeIdvalue+'\' Order by CreatedDate desc limit 5';
        String jsonData = JSON.serialize(Database.query(soql));
        return jsonData;
    }
	
    @AuraEnabled
    public static  String saveData(String Tenderinformationc) {
		system.debug('Tenderinformationc++'+Tenderinformationc);
        Tender_information__c TenInfo=(Tender_information__c)JSON.deserialize(Tenderinformationc,Tender_information__c.class);
        List<Tender_information__c> updateTenInfoList = new List<Tender_information__c>();
		// 更新保留招投标
		// List<Tender_information__c> updateBTenList = new List<Tender_information__c>();
		// 如果点击保存后,未选则保留招投标则报错
		// SaveErrorflag = false;
		System.debug('--------' + TenInfo.Retain_Tender__c);
		if (String.isNotBlank(TenInfo.Retain_Tender__c)) {
			System.debug('---------2--------' + TenInfo.Retain_Tender__c);
			//要保留的招投标
			Tender_information__c BTen =  [select Id, InfoId__c From Tender_information__c
			                               Where Id = : TenInfo.Retain_Tender__c];

			// 保留招投标关联的询价
			System.debug('---------BTen--------' + BTen);
			List<Tender_Opportunity_Link__c> BlinksList = [select Opportunity__c
			        from Tender_Opportunity_Link__c
			        where Tender_information__c = :BTen.Id];
			Set<Id> BlinkOppId = new Set<Id>();
			// if (BlinksList.size() > 0) {
			for (Tender_Opportunity_Link__c Blink : BlinksList) {
				BlinkOppId.add(Blink.Opportunity__c);
			}
			// 删除项目关联但不与保留项目关联的询价关联信息
			List<Tender_Opportunity_Link__c> linksList = [select id, Opportunity__c, Tender_information__c
			        from Tender_Opportunity_Link__c
			        where Tender_information__c = :TenInfo.Id ];
			                                    //  and Opportunity__c not in : BlinkOppId];
			// 把删除招投标 关联的询价 赋给 保留招投标上
			List<Tender_Opportunity_Link__c> addlinksList = new List<Tender_Opportunity_Link__c>();
			// 删除招投标关联的询价
			// 增加一个判断 看看是否还会写空进去
			if (linksList != null && linksList.size() > 0) {
				for (Tender_Opportunity_Link__c link : linksList) {
					if(!BlinkOppId.contains(link.Opportunity__c)){
						Tender_Opportunity_Link__c linkinfo =  new Tender_Opportunity_Link__c();
						linkinfo.Tender_information__c = BTen.Id;
						linkinfo.Opportunity__c = link.Opportunity__c;
						linkinfo.Tender_Opportunity_Uniq__c = BTen.Id + '' + link.Opportunity__c;
						linkinfo.IsRelated__c = true;
						// if (BlinksList.contains(linkinfo)) {
						addlinksList.add(linkinfo);
						// }
					}
				}
				// 删除项目删掉关联询价
				delete linksList;
				// 保留项目新增关联询价
				if (addlinksList.size() > 0) {
					insert addlinksList;
				}
			}
			// 【委托】招标项目-逻辑删除的项目需要自动取消与询价的关系 fy 2022.3.7 start
			List<Tender_Opportunity_Link__c> linksListdelect = [select id, Opportunity__c, Tender_information__c
			        from Tender_Opportunity_Link__c
			        where Tender_information__c = :TenInfo.Id];
			if(linksListdelect.size()>0){
				delete linksListdelect;
			}
			// 【委托】招标项目-逻辑删除的项目需要自动取消与询价的关系 fy 2022.3.7 end
			// }
			// 互换保留招投标与删除招投标的信息Id
			TenInfo.Retain_Tender__c = BTen.Id;
			// 【委托】招标项目-逻辑删除的项目需要自动取消与询价的关系 fy 2022.3.7 start
			TenInfo.Name = '逻辑删除:'+TenInfo.Name;
			// 【委托】招标项目-逻辑删除的项目需要自动取消与询价的关系 fy 2022.3.7 end
			String BTenInfo = BTen.InfoId__c;
			BTen.InfoId__c = TenInfo.InfoId__c;//保留招投标的信息Id赋给删除招投标的信息Id
			TenInfo.InfoId__c = BTenInfo;//删除招投标的信息Id赋给保留招投标的信息Id
			// 点击保存后 删除招投标上的逻辑删除字段变为true
			TenInfo.Logical_delete__c = true;
			// update TenInfo;
			// 反正要更新 放在一起也是一样的
			updateTenInfoList.add(TenInfo);
			updateTenInfoList.add(BTen);
			if (!TenInfo.Id.equals(BTen.Id)) {
				update updateTenInfoList;
			}
			// updateBTenList.add(BTen);
			// update updateBTenList;
		}
		if (String.isNotBlank(TenInfo.Retain_Tender__c)&&String.isNotBlank(TenInfo.ProjectId__c)&&String.isNotBlank(TenInfo.InfoId__c)) {
			// 调用接口
			// NFM504Controller.sendRequest(TenInfo.Id);
		}
		// System.debug('1111111122222' + SaveErrorflag);

		return '成功';
        //  return Tenderinformationc;
    }
	// public PageReference returnFresh() {
	// 	// System.debug('1234567891234567890');
	// 	String url = '/' + TenIds;
	// 	PageReference ref =  new Pagereference(url);
	// 	ref.setRedirect(true);
	// 	return ref;
	// }
}