@isTest
public class LogAutoSendBatchTest {

    static Account company;
    static Account section1;        // 呼吸科
    static Account section2;        // 消化科
    static Account olympus;
    static Account olympus1;        // 呼吸科
    static Account olympus2;        // 消化科
    static Account depart1;
    static Account depart2;

    static void initHpData() {
        StaticParameter.EscapeNFM007Trigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        RecordType rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        // テストデータ
        company = new Account(RecordTypeId = rectCo.Id, Name = 'NFM110TestCompany');
        olympus = new Account(RecordTypeId = rectCo.Id, 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 = rectDpt1.Id, Name = '*', ParentId  = section1.Id, Department_Class__c = section1.Id, Hospital__c = company.Id,
                Department_Name__c = 'NFM110TestDepart1');
        depart2 = new Account(RecordTypeId = rectDpt2.Id, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                Department_Name__c = 'NFM110TestDepart2');
        Account ocm = new Account(RecordTypeId = rectDpt2.Id, 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 = rectDpt2.Id, 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];
    }
    
    
    
    @isTest
    static void test_insert() {
        initHpData();
        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;

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

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

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.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';

        System.Test.startTest();
        //NFM110WebService.NFM110(ProductsDelivery);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(ProductsDelivery.Monitoring, 'NFM110', ProductsDelivery.GeneralData);
        if (String.isBlank(rowData.Log__c) == false) 
        NFM110Rest.executefuture(rowData.Id);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Return_Flag__c, AccountId, Department_Class__c, Hospital__c, Product2Id, SerialNumber, SLMark__c, IF_Information_From__c, InstallDate from Asset];
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
        System.assertEquals(1, rslts.size());
        if(rslts.size() > 0){
            System.assertEquals(1, rslts.size());
            System.assertEquals(false, rslts[0].Return_Flag__c);
            System.assertEquals(depart1.Id, rslts[0].AccountId);
            System.assertEquals(section1.Id, rslts[0].Department_Class__c);
            System.assertEquals(company.Id, rslts[0].Hospital__c);
            System.assertEquals(prd.Id, rslts[0].Product2Id);
            System.assertEquals(dnInfo1.SerialNoorLotNo, rslts[0]. SerialNumber);
            System.assertEquals('Serial Number', rslts[0]. SLMark__c);
            System.assertEquals(true, rslts[0]. IF_Information_From__c);
            System.assertEquals(null, rslts[0]. InstallDate);
        }

        BatchIF_Log__c[] logs =[Select Id,
                                    Name,
                                    Log__c,
                                    Type__c,
                                    ErrorLog__c,
                                    Log2__c,
                                    Log3__c,
                                    Log4__c,
                                    Log5__c,
                                    Log6__c,
                                    Log7__c,
                                    Log8__c,
                                    Log9__c,
                                    Log10__c,
                                    Log11__c,
                                    Log12__c,
                                    MessageGroupNumber__c,
                                    CreatedDate,
                                    retry_cnt__c
                                from BatchIF_Log__c
                                where RowDataFlg__c = true
                                and Type__c = 'NFM110'
                                order by CreatedDate desc ];

        logs[0].retry_cnt__c=1;
        update logs;
        System.assertEquals(1, logs[0].retry_cnt__c);

        System.assertEquals('NFM110', logs[0].Type__c);

        Id execBTId = Database.executeBatch(new LogAutoSendBatch(), 5);
        BatchIF_Log__c[] logss =[Select Id, Name, Log__c, ErrorLog__c, Log2__c,retry_cnt__c from BatchIF_Log__c where  id = :logs[0].id];
        System.assertEquals(1, logss[0].retry_cnt__c);

        List<BatchIF_Log__c> ll = [SELECT Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
                        Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c,Type__c,
                        retry_cnt__c
                   FROM BatchIF_Log__c
                  WHERE RowDataFlg__c = true
                    AND Type__c = :logs[0].Type__c AND CreatedDate >= :logs[0].CreatedDate  AND MessageGroupNumber__c >= :logs[0].MessageGroupNumber__c ];
        System.assertEquals(1, ll.size());   

        Database.executeBatch(new LogAutoSendBatch(logs[0].Type__c,logs[0].MessageGroupNumber__c,logs[0].CreatedDate), 1);
        BatchIF_Log__c[] logsss =[Select Id, Name, Log__c, ErrorLog__c, Log2__c,retry_cnt__c from BatchIF_Log__c where  id = :logs[0].id];
        System.assertEquals(1, logsss[0].retry_cnt__c);
    }
}