@isTest
private class LexOverdueStockControllerTest {
  static testMethod void testMethod1() {
    user MyUser_Test;
    Account myAccount1;
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    System.runAs(thisUser) {
      Profile prof1 = [
        SELECT Id
        FROM Profile
        WHERE Name = '901_经销商社区普通权限_2重验证(ET)'
      ];
      List<RecordType> rectCo = [
        SELECT Id
        FROM RecordType
        WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '販売店'
      ];
      if (rectCo.size() == 0) {
        return;
      }
      StaticParameter.EscapeConsumableOrderDetail2Trigger = true;

      RecordType rectCoO = [
        SELECT Id
        FROM RecordType
        WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
      ];
      Account olympus = new Account(
        RecordTypeId = rectCoO.Id,
        AgentCode_Ext__c = '9999900',
        Name = 'olympus'
      );
      insert olympus;
      myAccount1 = new Account(
        name = 'Testaccount001',
        Dealer_discount__c = 10,
        RecordTypeId = rectCo[0].Id,
        AgentCode_Ext__c = '8888888'
      );
      insert myAccount1;
      Contact core = new Contact(
        email = 'jplumber@salesforce.com',
        firstname = 'Joe',
        lastname = 'Plumber',
        accountid = myAccount1.id
      );
      insert core;
      MyUser_Test = new User(
        ContactId = core.id,
        Alias = 'newUser',
        Email = 'newuser@testorg.com',
        EmailEncodingKey = 'UTF-8',
        LastName = 'TestUser',
        LanguageLocaleKey = 'zh_CN',
        LocaleSidKey = 'zh_CN',
        ProfileId = prof1.Id,
        TimeZoneSidKey = 'Asia/Shanghai',
        UserName = 'testUser@testorg.com',
        Work_Location__c = '重庆'
      );
      insert MyUser_Test;
    }

    System.runAs(MyUser_Test) {
      //产品数据
      Product2 prod07 = new Product2(
        Name = 'Test07',
        ProductCode = 'Test07',
        Asset_Model_No__c = 'Test07',
        SFDA_Status__c = '有効',
        Dealer_special_Object__c = true,
        Packing_list_manual__c = 1,
        Manual_Entry__c = false
      );
      Product2 prod08 = new Product2(
        Name = 'Test08',
        ProductCode = 'Test08',
        Asset_Model_No__c = 'Test08',
        SFDA_Status__c = '有効',
        Dealer_special_Object__c = true,
        Packing_list_manual__c = 1,
        Manual_Entry__c = false
      );
      Product2 prod09 = new Product2(
        Name = 'Test09',
        ProductCode = 'Test09',
        Asset_Model_No__c = 'Test09',
        SFDA_Status__c = '有効',
        Dealer_special_Object__c = true,
        Packing_list_manual__c = 1,
        Manual_Entry__c = false
      );
      insert new List<Product2>{ prod07, prod08, prod09 };

      //制作产品
      Product2__c proG = new Product2__c(
        Name = 'MH-155:白平衡帽',
        OT_CODE_Text__c = 'Test001',
        Product2__c = prod07.Id
      );
      insert proG;
      Product2__c proH = new Product2__c(
        Name = 'MB-677:BNC电缆',
        OT_CODE_Text__c = 'Test002',
        Product2__c = prod08.Id
      );
      insert proH;
      Product2__c proF = new Product2__c(
        Name = 'TooMAJ-643R:水囊鞘管003',
        OT_CODE_Text__c = 'Test003',
        Product2__c = prod09.Id
      );
      insert proF;

      //订单
      Consumable_order__c order = new Consumable_order__c();
      order.Name = 'testMing';
      order.Order_status__c = '批准';
      order.RecordTypeid = System.Label.RT_ConOrder_Order;
      order.Dealer_Info__c = myAccount1.id;
      order.Inventory_date__c = Date.today();
      order.Order_type__c = '订单';
      order.Order_ProType__c = 'ET';
      insert order;
      //到货
      Consumable_order__c order2 = new Consumable_order__c();
      order2.Name = 'testMing1';
      order2.Order_status__c = '批准';
      order2.RecordTypeid = '01210000000c9dqAAA';
      order2.Dealer_Info__c = myAccount1.id;
      order2.Order_type__c = '到货';
      order2.Order_ProType__c = 'ET';
      insert order2;

      List<Consumable_order_details2__c> conList = new List<Consumable_order_details2__c>();
      //制作Consumable_order_details__c-------ProductCount
      // 销售
      Consumable_order_details2__c dataForProductCount7 = new Consumable_order_details2__c();
      dataForProductCount7.Bar_Code__c = '123456789298k250AAAAA';
      dataForProductCount7.Sterilization_limit__c = Date.today().addDays(2);
      dataForProductCount7.Arrive_date__c = Date.today();
      dataForProductCount7.RecordTypeid = System.Label.RT_ConOrderDetail2_Delivery;
      dataForProductCount7.Consumable_Product__c = proH.id;
      dataForProductCount7.Asset_Model_No__c = proH.Asset_Model_No__c;
      dataForProductCount7.Box_Piece__c = '盒';
      dataForProductCount7.Used_date__c = Date.today();
      dataForProductCount7.Consumable_order_minor__c = order.id;

      conList.add(dataForProductCount7);
      // 在库 过期 盒
      Consumable_order_details2__c dataForProduct = new Consumable_order_details2__c();
      dataForProduct.Bar_Code__c = '123456789498k250BBBBB';
      dataForProduct.Sterilization_limit__c = Date.today().addDays(-2);
      dataForProduct.Arrive_date__c = Date.today();
      dataForProduct.RecordTypeid = System.Label.RT_ConOrderDetail2_Delivery;
      dataForProduct.Consumable_Product__c = proF.id;
      dataForProduct.Asset_Model_No__c = proF.Asset_Model_No__c;
      dataForProduct.Box_Piece__c = '盒';
      dataForProduct.Consumable_order_minor__c = order.id;

      conList.add(dataForProduct);
      // 在库 过期 个
      Consumable_order_details2__c dataForProductCount3 = new Consumable_order_details2__c();
      dataForProductCount3.Bar_Code__c = '123456788398k250CCCCC';
      dataForProductCount3.Sterilization_limit__c = Date.today().addDays(-2);
      dataForProductCount3.Arrive_date__c = Date.today();
      dataForProductCount3.RecordTypeid = System.Label.RT_ConOrderDetail2_Delivery;
      dataForProductCount3.Consumable_Product__c = proG.id;
      dataForProductCount3.Asset_Model_No__c = proG.Asset_Model_No__c;
      dataForProductCount3.Box_Piece__c = '个';
      dataForProductCount3.RemoveBox_No__c = 1;
      dataForProductCount3.Consumable_order_minor__c = order.id;
      conList.add(dataForProductCount3);
      insert conList;

      //过期库存汇总信息 add by Wang Xueqin
      List<Consumable_order_details2__c> conList1 = new List<Consumable_order_details2__c>();
      Consumable_order_details2__c dataForProductCount4 = new Consumable_order_details2__c();
      dataForProductCount4.Bar_Code__c = '1001';
      dataForProductCount4.Arrive_date__c = Date.today();
      dataForProductCount4.Send_Date__c = null;
      dataForProductCount4.Used_date__c = null;
      dataForProductCount4.Return_date__c = null;
      dataForProductCount4.Lose_Flag__c = false;
      dataForProductCount4.Cancellation_Flag__c = false;
      dataForProductCount4.Consumable_Product__c = proG.Id;
      dataForProductCount4.Consumable_Arrived_order__c = order2.id;
      dataForProductCount4.Used_account__c = myAccount1.Id;
      dataForProductCount4.Sterilization_limit__c = Date.today().addDays(-2);
      dataForProductCount4.Box_Piece__c = '盒';
      conList1.add(dataForProductCount4);
      System.debug('conList1==>' + conList1);
      //查询所有过期库存
      Consumable_order_details2__c dataForProductCount5 = new Consumable_order_details2__c();
      dataForProductCount5.Bar_Code__c = '1002';
      dataForProductCount5.Arrive_date__c = Date.today();
      dataForProductCount5.Send_Date__c = Date.today();
      dataForProductCount5.Used_date__c = Date.today();
      dataForProductCount5.Return_date__c = null;
      dataForProductCount5.Lose_Flag__c = false;
      dataForProductCount5.Cancellation_Flag__c = false;
      dataForProductCount5.Consumable_Product__c = proG.Id;
      dataForProductCount5.Consumable_Arrived_order__c = order2.id;
      dataForProductCount5.Used_account__c = myAccount1.Id;
      dataForProductCount5.Sterilization_limit__c = Date.today().addDays(-2);
      dataForProductCount5.Box_Piece__c = '盒';
      conList1.add(dataForProductCount5);
      insert conList1;

      System.Test.startTest();
      List<Consumable_order_details2__c> overdueList = new List<Consumable_order_details2__c>();
      LexOverdueStockController overCon = new LexOverdueStockController();
      LexOverdueStockController.init();
      List<LexOverdueStockController.orderBean> cro = new List<LexOverdueStockController.orderBean>();
      List<LexOverdueStockController.orderBean> croList = LexOverdueStockController.codPageRecords;
      for (LexOverdueStockController.orderBean i : croList) {
        i.check = true;
      }

      String codPageRecordsLWC = JSON.serialize(
        LexOverdueStockController.codPageRecords
      );
      System.debug(
        'codPageRecordsLWC===>test' + LexOverdueStockController.codPageRecords
      );
      LexOverdueStockController.searchorderBean(
        'ET',
        '重庆',
        'Testaccount001',
        '123456789298k250AAAAA\n123456789498k250BBBBB\n123456788398k250CCCCC\n1001',
        codPageRecordsLWC
      );
      LexOverdueStockController.searchorderBean(
        'ET',
        '重庆',
        'Testaccount001',
        '',
        codPageRecordsLWC
      ); //barcode为空
      LexOverdueStockController.save(true, codPageRecordsLWC, overdueList);
      // LexOverdueStockController.save(false,codPageRecordsLWC,overdueList);
      System.Test.stopTest();
    }
  }
}
