public with sharing class EnquiryNoBiddingController {
    public EnquiryNoBiddingController() {

    }

    //import GetInspectById from '@salesforce/apex/EnquiryNoBiddingController.GetInspectById';
// import SaveDataYB from '@salesforce/apex/EnquiryNoBiddingController.SaveDataYB';
// import ChangeDataYB from '@salesforce/apex/EnquiryNoBiddingController.ChangeDataYB';
// import GetIrresponsibleReason from '@salesforce/apex/EnquiryNoBiddingController.GetIrresponsibleReason';

// import CheckOwnerRequest from '@salesforce/apex/EnquiryNoBiddingController.CheckOwner';

    class EnquiryNoBiddingModel 
    {
        public Opportunity opp;
        public Tender_information__c tender;
        public Enquiry_No_Bidding_Apply__c enquiry;
    }

    @AuraEnabled
    public  static string GetDataById(string Id)
    {
        EnquiryNoBiddingModel model = new EnquiryNoBiddingModel();
        //Opp_Bid__c | Bidding_Project_Name_BidId__c
        string OpportunityProp = CommonUtils.GetSqlToPorps(Opportunity.SObjectType);
        string sql = 'select '+OpportunityProp+' from Opportunity where Id = :Id';
        List<Opportunity> arrays = Database.query(sql);

        model.opp = arrays[0];
        string bId = arrays[0].Bidding_Project_Name_Bid__c;
        string oId = arrays[0].Opp_Bid__c;
        if(bId!= null)
        {
            string TenderProp = CommonUtils.GetSqlToPorps(Tender_information__c.SObjectType);
            string sql2 = 'select '+TenderProp+' from Tender_information__c where Id = :bId';
            List<Tender_information__c> arraysTender = Database.query(sql2);
            model.tender = arraysTender[0];
        }
        if(oId != null)
        {
            string EnquiryNoProp = CommonUtils.GetSqlToPorps(Enquiry_No_Bidding_Apply__c.SObjectType);
            string sql3 = 'select '+EnquiryNoProp+' from Enquiry_No_Bidding_Apply__c where Id = :oId';
            List<Enquiry_No_Bidding_Apply__c> arraysEnquiry = Database.query(sql3);     
            model.enquiry = arraysEnquiry[0];
        }
        return JSON.serialize(model); 
    }

     //根据ID查询  Enquiry_No_Bidding_Apply__c
     @AuraEnabled
     public  static string GetInspectById(String Id){
         QueryWrapper query = new QueryWrapper(Enquiry_No_Bidding_Apply__c.SObjectType);
         query.eq('Id', Id);
         List<Enquiry_No_Bidding_Apply__c> arrays = DataBasePlus.listPlus(query);
         return JSON.serialize(arrays); 
     }
 

    //根据ID查询  招投标项目
    @AuraEnabled
    public  static string GetTenderInformationById(String Id){
        QueryWrapper query = new QueryWrapper(Tender_information__c.SObjectType);
        query.eq('Id', Id);
        List<Tender_information__c> arrays = DataBasePlus.listPlus(query);
        return JSON.serialize(arrays); 
    }

      //招投标项目列表字段 irresponsibleReason__c 所有的值
     @AuraEnabled
     public  static string GetIrresponsibleReason(){
       String JsonData = CommonUtils.GetSelectedValues( Enquiry_No_Bidding_Apply__c.irresponsibleReason__c.getDescribe());
       return JsonData;
     }
 
  

 
     //保存 应标数据 JSONData 是 招投标项目 对象的 json格式的值 
      @AuraEnabled
     public  static String SaveDataYB(String JsonData,String Id,String IsInsert){
        QueryWrapper query = new QueryWrapper(Opportunity.SObjectType);
        query.eq('Id', Id);
        List<Opportunity> arrays = DataBasePlus.listPlus(query);
        Opportunity OppTemp = arrays[0];
        
        try{
            Enquiry_No_Bidding_Apply__c temp = (Enquiry_No_Bidding_Apply__c)JSON.deserialize(JsonData,Enquiry_No_Bidding_Apply__c.class);
            temp.Opportunity__c = Id;
            temp.InfoOwner__c = OppTemp.ownerid;
            temp.NotBidApprovalStatus__c = '草案中';
            if(IsInsert == '是')
            {
                insert temp;
                OppTemp.Opp_Bid__c = temp.Id;
                update OppTemp;
                
            }
            else{
                update temp;
            }
            return '成功';
            
         }catch(Exception ex)
         {
             return  ex.getMessage();
         }
        //  return  '成功';
     }
 
     //提交审核 应标数据 JSONData 是 招投标项目 对象的 json格式的值 
     @AuraEnabled
     public  static String ChangeDataYB(String JsonData,String Id,String IsInsert){
        QueryWrapper query = new QueryWrapper(Opportunity.SObjectType); 
        query.eq('Id', Id);
        List<Opportunity> arrays = DataBasePlus.listPlus(query);
        Opportunity OppTemp = arrays[0]; 

        try{
            Enquiry_No_Bidding_Apply__c temp = (Enquiry_No_Bidding_Apply__c)JSON.deserialize(JsonData,Enquiry_No_Bidding_Apply__c.class);
            temp.Opportunity__c = Id;
        
             temp.InfoOwner__c = OppTemp.ownerid;
             temp.leading_Cadre_Assistant__c = OppTemp.Sales_assistant_name_text__c;
             boolean flag = false;
             if(temp.NotBidApprovalStatus__c == null)
             {
                temp.NotBidApprovalStatus__c = '草案中';
                flag = true;
             }
             if(IsInsert == '是')
             {
                 insert temp;
                 OppTemp.Opp_Bid__c = temp.Id;
                 update OppTemp;
                 
             }
             else{
                 update temp;
             }
            temp.NotBidApprovalStatus__c = '填写完毕';

            update temp;
             
             return '成功';
         }catch(Exception ex)
         {
             return  ex.getMessage();
         }
     }



     
     //判断当前登录用户 是否是 所有人
     @AuraEnabled
     public  static String CheckOwner(String Id){
         String OwnerId = UserInfo.getUserId();
         String sql = 'select Id, owner.Id ,Name from Tender_information__c where  Id = :Id ';
         List<Tender_information__c> arrays = Database.query(sql);
         
         if(UserInfo.getProfileId() == System.Label.ProfileId_SystemAdmin)
         {
             return '1'+ '==' + '1';
         }
         if(arrays.size()<1)
         {
             return  '2'+ '==' + '1';
         }
         return OwnerId + '==' + arrays[0].owner.Id;
     } 
 
}