public without sharing class LexArriveGoodsController {
  //初始化
  @AuraEnabled
  public static Results init(String arrType, String eSetId) {
    Results results = new Results();
    results.isNoteStay = LexUtility.getIsNoteStay();
    try {
      ArriveGoodsController arrController = new ArriveGoodsController(
        eSetId,
        arrType
      );
      arrController.isLwc = true;
      arrController.init();
      results.arrGoodCon = JSON.serialize(arrController);
      results.coc = arrController.coc;
      results.saveFLGbln = arrController.saveFLGbln;
      results.returnFLGbln = arrController.ReturnFLGbln;
      results.result = 'Success';
    } catch (Exception e) {
      results.result = 'Fail';
      results.errorMsg = e.getLineNumber() + '---' + e.getMessage();
    }
    return results;
  }

  //搜索产品
  @AuraEnabled
  public static Results searchProduct(String barcode, String arrControllerStr) {
    Results results = new Results();
    try {
      System.debug('arrControllerStr:' + arrControllerStr);
      ArriveGoodsController arrController = (ArriveGoodsController) JSON.deserialize(
        arrControllerStr,
        ArriveGoodsController.class
      );
      arrController.barcode = barcode;
      arrController.returnError = null;
      arrController.warningList = new List<String>();
      arrController.errorList = new List<String>();
      arrController.SearchPro();
      results.arrGoodCon = JSON.serialize(arrController);
      if (
        arrController.returnError != null &&
        arrController.returnError != ''
      ) {
        results.result = 'Fail';
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.errorMsg = arrController.returnError;
      } else {
        results.coc = arrController.coc;
        results.saveFLGbln = arrController.saveFLGbln;
        results.returnFLGbln = arrController.ReturnFLGbln;
        results.detailsSummary = changeType1(arrController.detailsSummary);
        results.consumableorderdetailsRecords = changeType1(
          arrController.ConsumableorderdetailsRecords
        );
        results.showGoodsofReturnList = changeType1(
          arrController.ShowGoodsofReturnList
        );
        results.consumableInventory = changeType1(
          arrController.consumableInventory
        );
        results.consumableorderdetailsRecordserror = changeType1(
          arrController.ConsumableorderdetailsRecordserror
        );
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.result = 'Success';
      }
    } catch (Exception e) {
      results.result = 'Fail';
      results.errorMsg =
        e.getLineNumber() +
        '---' +
        e.getMessage() +
        '---' +
        e.getStackTraceString();
    }
    return results;
  }

  //到货确认
  @AuraEnabled
  public static Results arriveGoodsConfim(String arrControllerStr) {
    Results results = new Results();
    try {
      ArriveGoodsController arrController = (ArriveGoodsController) JSON.deserialize(
        arrControllerStr,
        ArriveGoodsController.class
      );
      arrController.returnError = null;
      arrController.warningList = new List<String>();
      arrController.errorList = new List<String>();
      Pagereference page = arrController.ArriveGoodsConfim();
      results.arrGoodCon = JSON.serialize(arrController);
      if (
        arrController.returnError != null &&
        arrController.returnError != ''
      ) {
        results.result = 'Fail';
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.errorMsg = arrController.returnError;
      } else {
        if (arrController.urlType == 'UnabletoEdit') {
          results.url = '/lexarrivegsdetails?EsetId=' + arrController.arriveId;
        }
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.result = 'Success';
      }
    } catch (Exception e) {
      results.result = 'Fail';
      results.errorMsg = e.getLineNumber() + '---' + e.getMessage();
    }
    return results;
  }

  //登录返品
  @AuraEnabled
  public static Results updateGoodsOfReturn(
    String arrControllerStr,
    String showGoodsofReturnListStr
  ) {
    Results results = new Results();
    try {
      List<ConsumableorderdetailsInfo> showGoodsofReturnList = (List<ConsumableorderdetailsInfo>) JSON.deserialize(
        showGoodsofReturnListStr,
        List<ConsumableorderdetailsInfo>.class
      );
      ArriveGoodsController arrController = (ArriveGoodsController) JSON.deserialize(
        arrControllerStr,
        ArriveGoodsController.class
      );
      arrController.ShowGoodsofReturnList = changeType2(showGoodsofReturnList);
      arrController.returnError = null;
      arrController.warningList = new List<String>();
      arrController.errorList = new List<String>();
      Pagereference page = arrController.UpdateGoodsOfReturn();
      results.arrGoodCon = JSON.serialize(arrController);
      if (
        arrController.returnError != null &&
        arrController.returnError != ''
      ) {
        results.result = 'Fail';
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.errorMsg = arrController.returnError;
      } else {
        if (arrController.urlType == 'ToReturnGoodsPage') {
          results.url = '/detail/' + arrController.return_Order_id;
        }
        if (arrController.urlType == 'ToInventoryGoodsPage') {
          results.url = '/detail/' + arrController.inventory_Order_id;
        }
        results.errorMsgList = arrController.errorList;
        results.warningMsgList = arrController.warningList;
        results.result = 'Success';
      }
    } catch (Exception e) {
      results.result = 'Fail';
      results.errorMsg = e.getLineNumber() + '---' + e.getMessage();
    }
    return results;
  }

  public static List<ConsumableorderdetailsInfo> changeType1(
    List<ArriveGoodsController.ConsumableorderdetailsInfo> conList
  ) {
    List<ConsumableorderdetailsInfo> conList1 = new List<ConsumableorderdetailsInfo>();
    if (conList != null) {
      for (ArriveGoodsController.ConsumableorderdetailsInfo con : conList) {
        ConsumableorderdetailsInfo con1 = new ConsumableorderdetailsInfo();
        con1.esd = con.esd;
        con1.Prod = con.Prod;
        con1.ProductName = con.ProductName;
        con1.barCodeNo = con.barCodeNo;
        con1.sterilizationlimitDate = con.sterilizationlimitDate;
        con1.serialNoorLotNo = con.serialNoorLotNo;
        con1.tracingCodeNo = con.tracingCodeNo;
        con1.ReturnReason = con.ReturnReason;
        con1.oldConsumableCount = con.oldConsumableCount;
        con1.ErrorReason = con.ErrorReason;
        con1.canEdit = con.canEdit;
        con1.intMark = con.intMark;
        con1.arriveAmount = con.arriveAmount;
        con1.arrivedCount = con.arrivedCount;
        con1.ReportProductExpirationDate = con.ReportProductExpirationDate;
        conList1.add(con1);
      }
    }
    return conList1;
  }

  public static List<ArriveGoodsController.ConsumableorderdetailsInfo> changeType2(
    List<ConsumableorderdetailsInfo> conList
  ) {
    List<ArriveGoodsController.ConsumableorderdetailsInfo> conList1 = new List<ArriveGoodsController.ConsumableorderdetailsInfo>();
    if (conList != null) {
      for (ConsumableorderdetailsInfo con : conList) {
        ArriveGoodsController.ConsumableorderdetailsInfo con1 = new ArriveGoodsController.ConsumableorderdetailsInfo();
        con1.esd = con.esd;
        con1.Prod = con.Prod;
        con1.ProductName = con.ProductName;
        con1.barCodeNo = con.barCodeNo;
        con1.sterilizationlimitDate = con.sterilizationlimitDate;
        con1.serialNoorLotNo = con.serialNoorLotNo;
        con1.tracingCodeNo = con.tracingCodeNo;
        con1.ReturnReason = con.ReturnReason;
        con1.oldConsumableCount = con.oldConsumableCount;
        con1.ErrorReason = con.ErrorReason;
        con1.canEdit = con.canEdit;
        con1.intMark = con.intMark;
        con1.arriveAmount = con.arriveAmount;
        con1.arrivedCount = con.arrivedCount;
        con1.ReportProductExpirationDate = con.ReportProductExpirationDate;
        conList1.add(con1);
      }
    }
    return conList1;
  }

  public class Results {
    @AuraEnabled
    public String result;
    @AuraEnabled
    public String errorMsg;
    @AuraEnabled
    public String url;
    @AuraEnabled
    public String arrGoodCon;
    @AuraEnabled
    public Consumable_order__c coc;
    @AuraEnabled
    public Boolean returnFLGbln;
    @AuraEnabled
    public Boolean saveFLGbln;
    @AuraEnabled
    public List<ConsumableorderdetailsInfo> detailsSummary;
    @AuraEnabled
    public List<ConsumableorderdetailsInfo> consumableorderdetailsRecords;
    @AuraEnabled
    public List<ConsumableorderdetailsInfo> showGoodsofReturnList;
    @AuraEnabled
    public List<ConsumableorderdetailsInfo> consumableInventory;
    @AuraEnabled
    public List<ConsumableorderdetailsInfo> consumableorderdetailsRecordserror;
    @AuraEnabled
    public List<String> errorMsgList;
    @AuraEnabled
    public List<String> warningMsgList;
    @AuraEnabled
    public Boolean isNoteStay;
  }

  public class ConsumableorderdetailsInfo implements Comparable {
    @AuraEnabled
    public Consumable_order_details2__c esd { get; set; }
    @AuraEnabled
    public Product2__c Prod { get; set; }
    @AuraEnabled
    public String ProductName { get; set; }
    @AuraEnabled
    public String barCodeNo { get; set; }
    @AuraEnabled
    public Date sterilizationlimitDate { get; set; }
    @AuraEnabled
    public String serialNoorLotNo { get; set; }
    @AuraEnabled
    public String tracingCodeNo { get; set; }
    @AuraEnabled
    public String ReturnReason { get; set; }
    @AuraEnabled
    public String oldConsumableCount { get; set; }
    @AuraEnabled
    public String ErrorReason { get; set; }
    @AuraEnabled
    public boolean canEdit { get; set; }
    @AuraEnabled
    public Integer intMark { get; set; }
    @AuraEnabled
    public Decimal arriveAmount { get; set; }
    @AuraEnabled
    public Decimal arrivedCount { get; set; }
    @AuraEnabled
    public String ReportProductExpirationDate { get; set; }

    public ConsumableorderdetailsInfo() {
    }

    public ConsumableorderdetailsInfo(
      Consumable_order_details2__c e,
      string str
    ) {
      esd = e;
      Prod = e.Consumable_Product__r;
      oldConsumableCount = e.name;
      ErrorReason = str;
      intMark = 1;
      if (e.Report_Product_Expiration__c != null) {
        ReportProductExpirationDate = e.Report_Product_Expiration__c.format();
      }
    }
    public ConsumableorderdetailsInfo(Consumable_order_details2__c e) {
      canEdit = false;
      if (e.Box_Piece__c == '盒') {
        e.Rrturn_count__c = 1;
        canEdit = true;
      }
      esd = e;
      Prod = e.Consumable_Product__r;
      oldConsumableCount = e.name;
      if (e.Report_Product_Expiration__c != null) {
        ReportProductExpirationDate = e.Report_Product_Expiration__c.format();
      }
    }

    public ConsumableorderdetailsInfo(Product2__c e, List<String> barinfoL) {
      esd = new Consumable_order_details2__c();
      ProductName = e.Name__c;
      Prod = e;
      barCodeNo = barinfoL[0];
      if (String.isNotBlank(barinfoL[1])) {
        sterilizationlimitDate = Date.valueOf(barinfoL[1]);
      }
      serialNoorLotNo = barinfoL[2];
      tracingCodeNo = barinfoL[3];
      ReturnReason = '';
    }

    public ConsumableorderdetailsInfo(
      Consumable_order_details2__c e,
      List<String> barinfoL
    ) {
      esd = e;
      Prod = e.Consumable_Product__r;
      barCodeNo = barinfoL[0];
      if (String.isNotBlank(barinfoL[1])) {
        sterilizationlimitDate = Date.valueOf(barinfoL[1]);
      }
      serialNoorLotNo = barinfoL[2];
      tracingCodeNo = barinfoL[3];
      if (e.Report_Product_Expiration__c != null) {
        ReportProductExpirationDate = e.Report_Product_Expiration__c.format();
      }
    }
    public ConsumableorderdetailsInfo(Product2__c e) {
      Prod = e;
    }
    // 排序
    public Integer compareTo(Object compareTo) {
      return null;
    }
  }
}
