public without sharing class LexArriveGoodsController {
    public static String barcode { get; set; }
    public static Consumable_order__c coc { get; set; }
    /*****************画面表示Bean******************/
    public static List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
    public static List<ConsumableorderdetailsInfo> consumableorderdetailsRecordsUse = new List<ConsumableorderdetailsInfo>();
    public static List<ConsumableorderdetailsInfo> consumableInventory { get; set; }
    public static List<ConsumableorderdetailsInfo> detailsSummary { get; set; }
    public static List<ConsumableorderdetailsInfo> consumableInventoryUse = new List<ConsumableorderdetailsInfo>();
    //更新明细取消时间
    public static List<Consumable_order_details2__c> consumableorderdetails2Cancle = new List<Consumable_order_details2__c>();
    // 更新借调信息 add by gzw 2020-0-415 start
    public static Map<String, Consumable_order_details2__c> orderdetails2trMap = new Map<String, Consumable_order_details2__c>();
    // 更新借调信息 add by gzw 2020-0-415 end
    public static List<Consumable_order_details2__c> consumableorderdetails2Insert = new List<Consumable_order_details2__c>();
    public static List<Consumable_order_details2__c> consumableorderdetails2Nobox = new List<Consumable_order_details2__c>();
    public static List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordserror { get; set; }
    public static transient List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordsdummy { get; set; }
    public static List<ConsumableorderdetailsInfo> ShowGoodsofReturnList { get; set; }
    public static List<ConsumableorderdetailsInfo> ShowGoodsofRemoveBoxReturnList { get; set; }
    //add by rentx 20210602 CHAN-C3K4ZQ
    public static List<List<ConsumableorderdetailsInfo>> consumableorderdetailsRecordsview { get; set; }
    //add by rentx 20210602 CHAN-C3K4ZQ
    public static List<String> GrList = new List<String>();
    //超出定货数量的产品
    public static List<String> cancellationProList = new List<String>();
    //错误信息
    public static String alertMessage { get; set; }
    public static integer getinventorysize() {
        return consumableInventory.size();
    }
    public static integer geterrorsize() {
        return ConsumableorderdetailsRecordserror.size();
    }
    public static Set<String> noboxBarcodeList = new Set<String>();
    public static transient Map<String, String> ExistIdMap = new Map<String, String>();
    public static transient Map<String, String> ErrorIdMap = new Map<String, String>();
    public static transient Map<String, String> AllMap = new Map<String, String>();
    public static Map<String, String> otherAgencyMap = new Map<String, String>();
    public static transient Map<String, String> barMcodeMap = new Map<String, String>();
    public static Map<String, Integer> BarcodeCntMap = new Map<String, Integer>();
    public static transient List<String> BarCodeListP = new List<String>();
    public static Map<String, Decimal> orderProductCunMap = new Map<String, Decimal>();
    public static Map<String, Decimal> orderProductArrivedCunMap = new Map<String, Decimal>();
    public static Map<String, Decimal> orderWantArriveCunMap = new Map<String, Decimal>();
    //超出型号和数量
    public static Map<String, Decimal> overOrderCunMap = new Map<String, Decimal>();
    //超出BARCODE和型号
    public static Map<String, List<String>> overOrderBARcodeCunMap = new Map<String, List<String>>();
    public static List<String> overOrderBARcodeTTTTTT = new List<String>();
    //不符合的barcode和型号
    public static Map<String, List<String>> notEqualBARcodeCunMap = new Map<String, List<String>>();
    public static List<String> overOrderBARcodeSSSSSS = new List<String>();
    public static List<String> productList = new List<String>();
    public static List<String> notInlist = new List<String>();
    //不选择订单时保存订单ID、产品型号用
    //库存登录使用
    public static List<String> notInStorelist = new List<String>();
    public static List<String> exitBarCodeList = new List<String>();

    public static Map<String, String> HosErrorMap = new Map<String, String>();
    public static List<String> HosErrorList = new List<String>();
    public static String baseUrl { get; private set; }
    public static String ESetId { get; set; }
    public static String arriveId = '';
    private static String accountid = null;
    private static String accountName = null;
    public static Decimal orderCountAll = 0;
    public static Decimal orderCountNotarrive = 0;
    private static id return_Order_id = null;
    private static id inventory_Order_id = null;
    public static boolean ReturnFLGbln { get; set; }
    public static boolean saveFLGbln { get; set; }
    public static String ArrType { get; set; }
    // 登录者工作地
    private static String userWorkLocation;

    //用户产品区分
    public static String userPro_Typestr = null;
    public static String userPro_Type = null;
    public static String product_Type = null;
    public static Boolean EngFlag = false;
    public static Boolean ETFlag = false;

    @AuraEnabled
    public static Results init(String arrTypeStr, String eSetIdStr) {
        Results results = new Results();
        ESetId = eSetIdStr;
        ArrType = arrTypeStr;
        ConsumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
        ConsumableorderdetailsRecordserror = new List<ConsumableorderdetailsInfo>();
        ConsumableorderdetailsRecordsdummy = new List<ConsumableorderdetailsInfo>();
        consumableInventory = new List<ConsumableorderdetailsInfo>();
        consumableorderdetailsRecordsUse = new List<ConsumableorderdetailsInfo>();
        consumableInventoryUse = new List<ConsumableorderdetailsInfo>();
        consumableorderdetails2Cancle = new List<Consumable_order_details2__c>();
        orderdetails2trMap = new Map<String, Consumable_order_details2__c>();
        detailsSummary = new List<ConsumableorderdetailsInfo>();
        saveFLGbln = false;
        consumableorderdetailsRecordsview = new List<List<ConsumableorderdetailsInfo>>();
        try {
            String userId = UserInfo.getUserId();
            List<user> Useracc = new List<user>();
            Useracc = [SELECT accountid, Work_Location__c, UserPro_Type__c FROM user WHERE id = :userId];
            accountid = Useracc[0].accountid;
            userPro_Type = Useracc[0].UserPro_Type__c;
            if (String.isBlank(userPro_Type)) {
                userPro_Type = 'ET';
            }

            if (userPro_Type == 'ENG') {
                product_Type = 'ET';
                EngFlag = true;
            } else {
                product_Type = 'ENG';
                ETFlag = true;
            }
            userPro_Typestr = '%' + userPro_Type + '%';
            userWorkLocation = Useracc[0].Work_Location__c;
            system.debug('userWorkLocation' + userWorkLocation);
            List<account> accountInfo = [SELECT Name FROM account WHERE id = :accountid];
            accountName = accountInfo[0].Name;
            system.debug('accountName' + accountName);
            coc = new Consumable_order__c();
            if (ArrType == 'ReG') {
                ReturnFLGbln = true;
            } else {
                ReturnFLGbln = false;
            }
            if (ESetId != '' && ESetId != null) {
                List<Consumable_order__c> qs = new List<Consumable_order__c>();
                List<Consumable_order_details2__c> reSet1 = new List<Consumable_order_details2__c>();
                List<Consumable_Orderdetails__c> reSet = new List<Consumable_Orderdetails__c>();
                qs = [
                    SELECT
                        Id,
                        Name,
                        Order_status__c,
                        Dealer_Info__c,
                        Deliver_date__c,
                        ReturnGs_Upload_Date__c,
                        Order_Reason__c,
                        Order_date__c,
                        Contract_application_decision__c,
                        Order_type__c,
                        Total_num__c,
                        OrderNumber_arrived__c,
                        Delivery_detail_count__c,
                        OrderNumber_notarrive__c,
                        orderPattern__c
                    FROM Consumable_order__c
                    WHERE Id = :ESetId AND Order_Owner_WorkLocal__c = :userWorkLocation AND Dealer_Info__c = :accountid
                ];
                if (qs.size() > 0) {
                    coc = qs[0];
                    orderCountAll = coc.Total_num__c;
                    orderCountNotarrive = coc.OrderNumber_notarrive__c;
                }
                //产品定货数量
                reSet = [
                    SELECT Asset_Model_No__c, Consumable_order__c, Consumable_count__c
                    FROM Consumable_Orderdetails__c
                    WHERE Consumable_order__c = :ESetId AND Order_Owner_WorkLocal__c = :userWorkLocation
                ];
                for (Integer i = 0; i < reSet.size(); i++) {
                    productList.add(reSet[i].Asset_Model_No__c);
                    orderProductCunMap.put(reSet[i].Asset_Model_No__c, reSet[i].Consumable_count__c);
                    orderProductArrivedCunMap.put(reSet[i].Asset_Model_No__c, 0);
                    orderWantArriveCunMap.put(reSet[i].Asset_Model_No__c, 0);
                    overOrderCunMap.put(reSet[i].Asset_Model_No__c, 0);
                    overOrderBARcodeCunMap.put(reSet[i].Asset_Model_No__c, overOrderBARcodeTTTTTT);
                }
                reSet1 = [
                    SELECT
                        Id,
                        Name,
                        Intra_Trade_List_RMB__c,
                        Asset_Model_No__c,
                        Consumable_Product__c,
                        Consumable_Product__r.Name,
                        Consumable_Product__r.Name__c,
                        Sterilization_limit__c,
                        Deliver_date__c,
                        Bar_Code__c,
                        Arrive_date__c,
                        Send_Date__c,
                        Consumable_order_minor__r.Name,
                        Consumable_order_minor__c,
                        Dealer_Arrive__c,
                        Guarantee_period_for_products__c,
                        CFDA_Status__c,
                        Box_Piece__c,
                        Rrturn_count__c,
                        ProductPacking_list_manual__c,
                        Report_Product_Approbation__c,
                        Report_Product_Expiration__c,
                        ContractNo_text__c,
                        Consumable_order_minor__r.ContractNo__c
                    FROM Consumable_order_details2__c
                    WHERE
                        recordtypeid = :System.Label.RT_ConOrderDetail2_Delivery
                        AND Arrive_Owner_Work_Location__c = :userWorkLocation
                        AND Consumable_order_minor__c = :ESetId
                        AND Dealer_Arrive__c = TRUE
                        AND Box_Piece__c != '个'
                ];
                for (Integer i = 0; i < reSet1.size(); i++) {
                    if (orderProductArrivedCunMap.containsKey(reSet1[i].Asset_Model_No__c)) {
                        orderProductArrivedCunMap.put(
                            reSet1[i].Asset_Model_No__c,
                            orderProductArrivedCunMap.get(reSet1[i].Asset_Model_No__c) + 1
                        );
                    }
                }
            }
            results.accountId = accountid;
            results.accountName = accountName;
            results.userProType = userPro_Type;
            results.userProTypestr = userPro_Typestr;
            results.productType = product_Type;
            results.saveFLGbln = saveFLGbln;
            results.returnFLGbln = ReturnFLGbln;
            results.engFlag = EngFlag;
            results.etFlag = ETFlag;
            results.userWorkLocation = userWorkLocation;
            results.productList = productList;
            results.orderProductCunMap = orderProductCunMap;
            results.orderProductArrivedCunMap = orderProductArrivedCunMap;
            results.orderWantArriveCunMap = orderWantArriveCunMap;
            results.overOrderCunMap = overOrderCunMap;
            results.overOrderBARcodeCunMap = overOrderBARcodeCunMap;
            results.result = 'Success';
        } catch (Exception e) {
            results.result = 'Fail';
            results.errorMsg = e.getLineNumber() + '---' + e.getMessage();
        }
        return results;
    }

    @AuraEnabled
    public static Results methodName() {
        Results results = new Results();
        try {
            results.result = 'Success';
        } catch (Exception e) {
            results.result = 'Fail';
            results.errorMsg = e.getLineNumber() + '---' + e.getMessage();
        }
        return results;
    }

    public class Results {
        @AuraEnabled
        public String result;
        @AuraEnabled
        public String errorMsg;
        @AuraEnabled
        public String accountId;
        @AuraEnabled
        public String accountName;
        @AuraEnabled
        public String userProType;
        @AuraEnabled
        public String userProTypestr;
        @AuraEnabled
        public String productType;
        @AuraEnabled
        public String userWorkLocation;
        @AuraEnabled
        public String inventoryOrderId;
        @AuraEnabled
        public String returnOrderId;
        @AuraEnabled
        public String arriveId;
        @AuraEnabled
        public Boolean returnFLGbln;
        @AuraEnabled
        public Boolean saveFLGbln;
        @AuraEnabled
        public Boolean engFlag;
        @AuraEnabled
        public Boolean etFlag;
        @AuraEnabled
        public Consumable_order__c coc;
        @AuraEnabled
        public List<String> productList;
        @AuraEnabled
        public Map<String, Decimal> orderProductCunMap;
        @AuraEnabled
        public Map<String, Decimal> orderProductArrivedCunMap;
        @AuraEnabled
        public Map<String, Decimal> orderWantArriveCunMap;
        @AuraEnabled
        public Map<String, Decimal> overOrderCunMap;
        @AuraEnabled
        public Map<String, List<String>> overOrderBARcodeCunMap;
    }

    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; }

        public String ReportProductExpirationDate { get; set; }
        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;
        }
    }
}
