@isTest(SeeAllData = false)
public class DNUpsertBatchTest {
    static Account company;
    static Account section1;        // 呼吸科
    static Account section2;        // 消化科
    static Account olympus;
    static Account olympus1;        // 呼吸科
    static Account olympus2;        // 消化科
    static Account depart1;
    static Account depart2;
    @testSetup
    private static void initHpData() {
        ControllerUtil.EscapeNFM001Trigger = true;
        ID rectCoID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        ID rectDpt1ID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        ID rectDpt2ID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();

        // テストデータ
        company = new Account(RecordTypeId = rectCoID, Name = 'NFM110TestCompany', FSE_SP_Main_Leader__c = UserInfo.getUserId(), FSE_GI_Main_Leader__c = UserInfo.getUserId());
        olympus = new Account(RecordTypeId = rectCoID, AgentCode_Ext__c = '9999999', Name = 'olympus');
        insert new Account[] {company, olympus};
            company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        section1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 呼吸科'];
        section2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 消化科'];
        olympus1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 呼吸科'];
        olympus2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 消化科'];
        System.debug('section1.Department_Class_Label__c=' + section1.Department_Class_Label__c);
        System.debug('section2.Department_Class_Label__c=' + section2.Department_Class_Label__c);
        depart1 = new Account(RecordTypeId = rectDpt1ID, Name = '*', ParentId  = section1.Id, Department_Class__c = section1.Id, Hospital__c = company.Id,
                              Department_Name__c = 'NFM110TestDepart1');
        depart2 = new Account(RecordTypeId = rectDpt2ID, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                              Department_Name__c = 'NFM110TestDepart2');
        Account ocm = new Account(RecordTypeId = rectDpt2ID, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                                  AgentCode_Ext__c = '9999900', Department_Name__c = 'NFM110TestDepart3');
        Account olympus_return = new Account(RecordTypeId = rectDpt2ID, Name = '*', ParentId  = olympus2.Id, Department_Class__c = olympus2.Id, Hospital__c = olympus.Id,
                                             AgentCode_Ext__c = '9999901', Department_Name__c = 'olympus_return');
        insert new Account[] {depart1, depart2, ocm, olympus_return};
        depart1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart1.Id];
        depart2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart2.Id];
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        NFM110Rest.GeneralData generalData1 = new NFM110Rest.GeneralData();
        NFM110Rest.ProductsDelivery ProductsDelivery = new NFM110Rest.ProductsDelivery();

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110Rest.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110Rest.DnInformation dnInfo1 = new NFM110Rest.DnInformation();
        generalData1.DnInformation = new NFM110Rest.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData1.InquiryNo = 'test0001' + ',';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';
        generalData1.DeliveryNote = '11111';


        NFM110Rest.GeneralData generalData2 = new NFM110Rest.GeneralData();
        NFM110Rest.ProductsDelivery ProductsDelivery1 = new NFM110Rest.ProductsDelivery();

        Datetime nowDT1 = Datetime.now();
        String nowStr1 = nowDT1.format('yyyyMMddHHmm');
        ProductsDelivery1.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery1.Monitoring.MessageGroupNumber = nowStr1 + '01';

        ProductsDelivery1.GeneralData = new NFM110Rest.GeneralData[] { generalData1 };
        generalData2.DeliveryNote = '';
        NFM110Rest.DnInformation dnInfo2 = new NFM110Rest.DnInformation();
        generalData2.DnInformation = new NFM110Rest.DnInformation[] { dnInfo2 };
        generalData2.EndUserNo     = company.Management_Code_Auto__c;
        generalData2.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData2.InquiryNo = 'test0001' + ',';
        dnInfo2.SorLMark        = 'S';
        dnInfo2.OTCode          = prd.ProductCode_Ext__c;
        dnInfo2.SerialNoorLotNo = '2577010001675';
        generalData2.DeliveryNote = '';

       
        BatchIF_Log__c rowData = NFMUtil.saveRowData(ProductsDelivery1.Monitoring, 'NFM110', ProductsDelivery1.GeneralData);
         
        
    }
    @isTest
  private static void test_void() {
        list<BatchIF_Log__c> rowDataList  = [select id from BatchIF_Log__c where RowDataFlg__c = true ];
        rowDataList[0].IsUpsertDN__c = true;
        update rowDataList;
    System.Test.startTest();
        Database.executeBatch(new DNUpsertBatch(rowDataList[0].Id),1);
        System.Test.stopTest();
    }
    @isTest
  private static void test_checkSerialNoorLotNo() {
        string a = 'strsA0trstr';
        string b = 'strA50trstr';
        string c = 'str250trstr';
         
        DNUpsertBatch.checkSerialNoorLotNo(a);
        DNUpsertBatch.checkSerialNoorLotNo(b);
        DNUpsertBatch.checkSerialNoorLotNo(c);
        DNUpsertBatch.checkSerialNoorLotNo(null);
    }
    //跑创建电子签收单的方法
    @isTest
    private static void test_upsertESign() {


        // 检索出签收单，并生成签收单map key 为DN号
        map <string, eSignForm__c> eSFMap = new  map <string, eSignForm__c>();

       

        //新建电子签收单
        eSignForm__c eSignForm = new eSignForm__c();
        eSignForm.Name = '::测试电子签收单';
        eSignForm.DNName__c = '112233';


        eSFMap.put(eSignForm.DNName__c, eSignForm);


        Statu_Achievements_DN__c  sad = new Statu_Achievements_DN__c();
        sad.Name = '112233';
        sad.DeliveryDate_Raw__c = '1111';
        sad.Sales_assistant_name_text__c = UserInfo.getUserId();
        sad.RC_Manager__c = UserInfo.getUserId();
        sad.EsignTestAccount__c =true;
        sad.ReturnMark__c = false;



        list<Statu_Achievements_DN__c> DNList = new list<Statu_Achievements_DN__c>();
        DNList.add(sad);

        
        DNUpsertBatch.SetupeSignForm(eSFMap, DNList);

        
    }
    //跑创建签收单明细的方法
    @isTest
    private static void test_upsertESignLineItems() {


        list<Statu_Achievements_DN_details__c> insertSADNdetailList = new list<Statu_Achievements_DN_details__c>();

        Statu_Achievements_DN_details__c saddc = new Statu_Achievements_DN_details__c();
        saddc.DNName__c = '112233';
        saddc.CaseNumber__c = '112233';
        saddc.OTCode_Raw__c= '112233';
        insertSADNdetailList.add(saddc);

        
        map <string, eSignFormLineItem__c> eSFLMap = new map <string, eSignFormLineItem__c>();
        
        //新建电子签收单明细
        eSignFormLineItem__c eSignFormLineItem = new eSignFormLineItem__c();
        eSignFormLineItem.Name = '::电子签收单明细';
        eSignFormLineItem.agencyGoodStatus__c = '完好';
        eSignFormLineItem.DNProductNo__c = '112233-112233-112233';
        eSignFormLineItem.Count__c = 0;

        eSFLMap.put(eSignFormLineItem.DNProductNo__c,eSignFormLineItem);

        // 检索出签收单，并生成签收单map key 为DN号
        map <string, eSignForm__c> eSFMap = new  map <string, eSignForm__c>();

        //新建电子签收单
        eSignForm__c eSignForm = new eSignForm__c();
        eSignForm.Name = '::测试电子签收单';
        eSignForm.DNName__c = '112233';


        eSFMap.put(eSignForm.DNName__c, eSignForm);

        Map<String, Product2> prdsMap = new Map<String, Product2>();
        Product2 prd  = new Product2();
        prd.ProductCode_Ext__c = '112233';

        prdsMap.put(prd.ProductCode_Ext__c,prd);

        
        DNUpsertBatch.SetupeSignFormLineItems(insertSADNdetailList,eSFLMap,eSFMap, prdsMap);

        
    }

}