public without sharing class eSignHomePageController {
    // 初始方法，获取当前所有任务，包括当前用户和当前用户下属的任务
    //url 为DN号的 返回需要信息
    //为了避免后期修改麻烦 目前没有把DN号 和 客户id两种后台检索方法合在一起
    //签收页面后台检索
    @AuraEnabled
    public static eSignWrapper getDNSignUpStatus(string DNName){
        list<eSignForm__c> tempEsignFormList = new list<eSignForm__c> ();
        list<eSignFormEntry__c> tempEsignFormEntryList = new list<eSignFormEntry__c> ();
        try {
            tempEsignFormList =
                [select id ,HPDNSignUpStatus__c,agencyDNSignUpStatus__c,
                HPScanDay__c,agencyScanDay__c,handleOpinionAgency__c,DNNameNo0__c,
                Sales_Root_Formula__c, HPAcceptResult__c,HPSignUpDate__c,
                agencyAcceptResult__c, salesHPManageConfirmDate__c,
                salesManageConfirmDate__c,salesHPManageFBDate__c,salesManageFBDate__c
                ,agencyAutoSignUpStatus__c,HPSignUpStatus__c,agencyConfirmDate__c,endUser__c,HPConfirmDate__c,agencySignUpDate__c,skip_Hospital_Sign__c,skip_Distribution_Sign__c,
                //根据不同情况返回对应的提示信息  精琢技术 thh 2021-10-09 start
                skip_Distribution_Sign_Message__c, skip_Hospital_Sign_Message__c
                //根据不同情况返回对应的提示信息  精琢技术 thh 2021-10-09 end
                     from eSignForm__c
                    where DNNameNo0__c =: DNName ];
            //system.debug('tempEsignFormList:'+tempEsignFormList[0]);
        } catch (Exception ex) {
            return null;
        }

        try {
            tempEsignFormEntryList =
                [select id ,entryType__c,
                Sales_Root_Formula__c, HPAcceptResult__c,HPDNSignUpStatus__c,agencyDNSignUpStatus__c,
                HPScanDay__c,agencyScanDay__c,handleOpinionAgency__c,DNNameNo0__c,
                agencyAcceptResult__c, salesHPManageConfirmDate__c,HPSignUpDate__c,
                salesManageConfirmDate__c,salesHPManageFBDate__c,salesManageFBDate__c
                ,agencyAutoSignUpStatus__c,HPSignUpStatus__c,IsSubmit__c,IsHPSubmit__c,agencyConfirmDate__c,agencySignUpDate__c
                ,agencyScanDayBack__c,agencySignUpDateBack__c,agencyConfirmDateBack__c,endUser__c,HPConfirmDate__c,IsHandled__c,skip_Hospital_Sign__c,skip_Distribution_Sign__c,   
                //根据不同情况返回对应的提示信息  精琢技术 thh 2021-10-09 start
                skip_Distribution_Sign_Message__c, skip_Hospital_Sign_Message__c
                //根据不同情况返回对应的提示信息  精琢技术 thh 2021-10-09 end
                     from eSignFormEntry__c
                    where DNNameNo0__c =: DNName 
                    order by createddate desc
                    ];
            //system.debug('tempEsignFormEntryList:'+tempEsignFormEntryList[0]);
        } catch (Exception ex) {
            return null;
        }
        Boolean isAgencySubmit = false;
        Boolean isHPSubmit = false;

        String entryType = '';
        //new 
        eSignWrapper eSignWrapper = new eSignWrapper(isAgencySubmit,isHPSubmit,entryType);
        //先判断有没有中间表 没有则赋值为空
        if(tempEsignFormEntryList.size() > 0 ){
            eSignWrapper.eSignFormEntry = tempEsignFormEntryList[0];
                if(tempEsignFormEntryList[0].IsSubmit__c){
                    isAgencySubmit = true;
                    entryType = tempEsignFormEntryList[0].entryType__c;
                }
                if(tempEsignFormEntryList[0].IsHPSubmit__c){
                    isHPSubmit = true;
                    entryType = tempEsignFormEntryList[0].entryType__c;
                }
        }else{
            eSignWrapper.eSignFormEntry = null;
        }
        //
        if(tempEsignFormList.size() > 0){
            eSignWrapper.eSignForm = tempEsignFormList[0];
        }else{
            eSignWrapper.eSignForm = null;
        }
        eSignWrapper.isAgencySubmit =isAgencySubmit;
        eSignWrapper.isHPSubmit =isHPSubmit;
        eSignWrapper.entryType =entryType;
       return eSignWrapper;
    }

    //DN清单页后台检索方法
    @AuraEnabled
    public static  eSignWrapperList getDNSignUpLists(String id ,String type){
        list<eSignForm__c> tempEsignFormList = new list<eSignForm__c> ();
        list<eSignFormEntry__c> tempEsignFormEntryList = new list<eSignFormEntry__c> ();

        //传过来是18位的 截取一下
        id = id.substring(0,15);
        Date todate = Date.today(); 
        //签收单list
        try {
            
            //检索id，DN号，经销商名称，合同编号，医院名，经销商签收状态,医院签收状态，发货日期
            String sql = 'select id ,Agency1_Name_F__c,ContractNO__c,Hospital_Name__c,DeliveryDate__c,HPDNSignUpStatus__c,agencyDNSignUpStatus__c,';
                   sql +='HPScanDay__c,agencyScanDay__c,handleOpinionAgency__c,DNNameNo0__c,Sales_Root_Formula__c, HPAcceptResult__c,HPSignUpDate__c,';
                   sql +='agencyAcceptResult__c, salesHPManageConfirmDate__c,salesManageConfirmDate__c,salesHPManageFBDate__c,salesManageFBDate__c,';
                   sql +='agencyAutoSignUpStatus__c,HPSignUpStatus__c,agencyConfirmDate__c,endUser__c,endUserName__c,HPConfirmDate__c,endConfirmDate__c,Hospital_Name_Text__c,CustomerNameContract__c,agencySignUpDate__c,skip_Hospital_Sign__c,skip_Distribution_Sign__c,skip_Distribution_Sign_Message__c, skip_Hospital_Sign_Message__c ';
                   sql +=' from  eSignForm__c ';
                   //当isProcessed__c 为true的时候，工作流更新日期 所以不会有日期为null并且还打勾的情况
                   sql +=' where (isProcessed__c = false  or (isProcessed__c = true and  endConfirmDate__c >=: todate)) ';

                   //退货则不被检索
                   //sql +=' and ReturnMark__c = false ';
                //根据传入的经销商或者医院传入不同的where 条件
                if(type == 'Agency'||type == 'AgencyContract'){
                    sql +=' and Agency1_ID__c =: id ';
                 }else if(type == 'HP'){
                    sql +=' and Hospital_ID__c =: id ';
                 }
                 sql +=' order by createddate desc ';
            tempEsignFormList =DataBase.query(sql);
            //System.debug('tempEsignFormList+++'+tempEsignFormList);
        } catch (Exception ex) {
            //System.debug('ex+++'+ex);
            return null;
        }
        //签收单录入表 list
        try {
            
            String query = 'select id ,isProcessed__c,DNSignUpStatus__c,ContractNO__c,Hospital_Name__c,DeliveryDate__c,';
                   query +='entryType__c,Sales_Root_Formula__c, HPAcceptResult__c,HPDNSignUpStatus__c,agencyDNSignUpStatus__c,';
                   query +='HPScanDay__c,agencyScanDay__c,handleOpinionAgency__c,DNNameNo0__c,agencyAcceptResult__c, salesHPManageConfirmDate__c,HPSignUpDate__c,';
                   query +='salesManageConfirmDate__c,salesHPManageFBDate__c,salesManageFBDate__c,agencyAutoSignUpStatus__c,HPSignUpStatus__c,IsSubmit__c,IsHPSubmit__c,agencyConfirmDate__c,';
                   query +='agencyScanDayBack__c,agencySignUpDateBack__c,agencyConfirmDateBack__c,endUser__c,HPConfirmDate__c,endConfirmDate__c,Hospital_Name_Text__c,CustomerNameContract__c,agencySignUpDate__c,IsHandled__c,skip_Distribution_Sign_Message__c, skip_Hospital_Sign_Message__c ';
                   query +=' from eSignFormEntry__c ';
                   query +=' where IsHandled__c = false  ';
                    //and ReturnMark__c = false 
                   //根据传入的经销商或者医院传入不同的where 条件
                    if(type == 'Agency' ||type == 'AgencyContract'){
                        query +=' and Agency1_ID__c =: id ';
                     }else if(type == 'HP'){
                        query +=' and Hospital_ID__c =: id ';
                     }
                     // query +=' order by createddate desc ';

            tempEsignFormEntryList = DataBase.query(query);
            //System.debug('tempEsignFormEntryList+++'+tempEsignFormEntryList);
        } catch (Exception ex) {
            return null;
        }
        //new 
        eSignWrapperList eWList = new eSignWrapperList();
        //先判断有没有中间表 没有则赋值为空
        if(tempEsignFormEntryList.size() > 0 ){
            for(eSignFormEntry__c esfe:tempEsignFormEntryList){
                if(esfe.endUser__c =='契約'){
                    esfe.Hospital_Name_Text__c = esfe.CustomerNameContract__c;
                }else{
                    esfe.Hospital_Name_Text__c = esfe.Hospital_Name__c;
                }
            }
            eWList.eSignFormEntryList = tempEsignFormEntryList;
        }else{
            eWList.eSignFormEntryList = null;
        }
        system.debug('ceshi4+++'+tempEsignFormList);
        if(tempEsignFormList.size() > 0){
            for(eSignForm__c esf:tempEsignFormList){
                if(esf.endUser__c =='契約'){
                    esf.Hospital_Name_Text__c = esf.CustomerNameContract__c;
                }else{
                    esf.Hospital_Name_Text__c = esf.Hospital_Name__c;
                }
            }
            eWList.eSignFormList = tempEsignFormList;
        }else{
            eWList.eSignFormList = null;
        }

        return eWList;

    }

    //区分经销商/医院  传入客户的管理编码
    @AuraEnabled
    public static eSignAccount getAccount(String managementCode){
        //CHAN-BX736B 【委托】请帮恢复河南嘉信元泰医疗器械有限公司签收单明细 精琢技术 wql 2021/01/18 start
        //根据管理编码 检索有效的客户 使用去0的管理编码 字段 and Is_Active_Formula__c = '有效'
        //CHAN-BX736B 【委托】请帮恢复河南嘉信元泰医疗器械有限公司签收单明细 精琢技术 wql 2021/01/18 end
        List<Account> accList = [select id,RecordType.developerName,name  from Account where Management_FormulaCode__c =: managementCode  ];

        String accountId = '';
        String recordTypeName = '';
        String agencyName = '';
        if(accList.size()>0){
            accountId =accList[0].id;
            recordTypeName = accList[0].RecordType.developerName;
            agencyName = accList[0].name;
            
        }
        eSignAccount acc  = new eSignAccount(accountId,recordTypeName,agencyName);
        acc.accountId = accountId;
        acc.recordTypeName = recordTypeName;
        acc.agencyName = agencyName;
    
        return acc;
    }

    @AuraEnabled
    public static List<Product2> getProduct(List<Id> ids){
        List<Product2> proLists = NewQuoteEntryWebService.getProduct2(ids);
    
        return proLists;
    }

    //公用类 返回签收单对象或者签收单录入表对象
    public class eSignWrapper{
        @AuraEnabled
        public eSignFormEntry__c eSignFormEntry;

        @AuraEnabled
        public eSignForm__c eSignForm;
        //判断是否有经销商已提交的数据
        @AuraEnabled
        public Boolean isAgencySubmit;
        //判断是否有医院已提交的数据
         @AuraEnabled
        public Boolean isHPSubmit;
        //判断传入的类型
        @AuraEnabled
        public String entryType;
        
        public eSignWrapper(Boolean isAgencySubmit,Boolean isHPSubmit,String entryType){
            eSignFormEntry = new eSignFormEntry__c();
            eSignForm = new eSignForm__c();
            isAgencySubmit = isAgencySubmit;
            isHPSubmit = isHPSubmit;
            entryType = entryType;
        }

    }

    //公用类 返回签收单list或者签收单录入表list
    public class eSignWrapperList{
        @AuraEnabled
        public List<eSignFormEntry__c> eSignFormEntryList;

        @AuraEnabled
        public List<eSignForm__c> eSignFormList;
        public eSignWrapperList(){
            eSignFormEntryList = new List<eSignFormEntry__c>();
            eSignFormList = new List<eSignForm__c>();
        }

    }

    //公用类 返回客户id 和记录类型developerName   （Agency、HP、AgencyContract）
    public class eSignAccount{
        @AuraEnabled
        public String accountId;

        @AuraEnabled
        public String recordTypeName;

        @AuraEnabled
        public String agencyName;
        
        public eSignAccount(String accountId,String recordTypeName,String agencyName){
            accountId = accountId;
            recordTypeName = recordTypeName;
            agencyName = agencyName;
        }

    }
}