public without sharing class ArriveGsDetailsController {
	/*****************検索用******************/
    public Consumable_order__c coc { get; set; }
    private String accountid = null;
    private Decimal orderallcount = 0;
    //public String alertMessage {set;get;}
    public String baseUrl {get;private set;}
    public Boolean arrivetoorder { get; set; }
    /*****************画面表示Bean******************/
    public List<ConsumableorderdetailsInfo> consumableorderdetailsRecords { get; set; }
    /****************************************/
    //add by rentx 20210622 start  CHAN-C3K4ZQ
    public List<List<ConsumableorderdetailsInfo>> consumableorderdetailsRecordsview { get; set; }
    
     public void makeRecordsView(List<ConsumableorderdetailsInfo> recordsdummy){
        consumableorderdetailsRecordsview = new List<List<ConsumableorderdetailsInfo>>();

        // if(recordsdummy.size() > 1000){
            List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordsbreak = new List<ConsumableorderdetailsInfo>();
            List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordsbreakover = new List<ConsumableorderdetailsInfo>();
            ConsumableorderdetailsInfo c = null;
            ConsumableorderdetailsRecordsbreak.clear();
            ConsumableorderdetailsRecordsbreakover.clear();
            consumableorderdetailsRecordsview.clear();
            //选择产品view
            ConsumableorderdetailsRecordsbreak = new List<ConsumableorderdetailsInfo>();
            ConsumableorderdetailsRecordsbreakover = new List<ConsumableorderdetailsInfo>();
            if(recordsdummy.size() > 1000){
                for(Integer i = 0; i < recordsdummy.size(); i++){
                    if(i < 1000){
                        c = recordsdummy.get(i);
                        ConsumableorderdetailsRecordsbreak.add(c);
                    }else{
                        c = recordsdummy.get(i);
                        ConsumableorderdetailsRecordsbreakover.add(c);
                    }
                }
                consumableorderdetailsRecordsview.add(ConsumableorderdetailsRecordsbreak);
                consumableorderdetailsRecordsview.add(ConsumableorderdetailsRecordsbreakover);
            }else{
                consumableorderdetailsRecordsview.add(recordsdummy);
            }
        // }else{
        //     consumableorderdetailsRecordsview.add(recordsdummy);
        // }
    }
    // add by rentx 20210622 end CHAN-C3K4ZQ

    //到货明细2
    //private List<Consumable_order_details2__c> consumablearriveproductdetailsSelected = new List<Consumable_order_details2__c>();
	// 产品 ID
	private String ESetId = '';
    private Integer consumableorderdetailsCount = null;
    public ArriveGsDetailsController(){
		baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
		ESetId = ApexPages.currentPage().getParameters().get('esetId');
        consumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
        consumableorderdetailsRecordsview = new List<List<ConsumableorderdetailsInfo>>();
    }
    public void init(){
        //取经销商信息
        String userId = UserInfo.getUserId();
        List<user> Useracc = New List<user>();
        Useracc = [select accountid from user where id =:userId];
        accountid = Useracc[0].accountid;

    	List<Consumable_order__c> Rs = New List<Consumable_order__c>();
    	if(ESetId== NULL || ESetId==''){
    	}else{
    	Rs = [SELECT id,name,Order_reason__c,Arrive_Order__c,Arrive_Order__r.Total_num__c,Arrive_Order__r.IsShipment__c,
                    recordtypeid,Order_status__c,Deliver_date__c,Arrive_total_amount__c
                FROM Consumable_order__c
                WHERE id =:ESetID];
    	coc =  new Consumable_order__c();
    	if(Rs.size()>0){
    		coc = Rs[0];
            orderallcount = coc.Arrive_Order__r.Total_num__c;
    	}
        if(coc.Arrive_Order__c == null){
            arrivetoorder = true;
            //alertMessage = '到货订单没有与发货单联动。';
        }
    	List<Consumable_order_details2__c> consumablearriveproductdetailsSelected = [SELECT Id,Dealer_Arrive__c,Bar_Code__c, RecordTypeid, Name,Consumable_Product__r.Name__c,
                                                    Consumable_Product__c,Consumable_Product__r.Name,Dealer_Saled__c,
                                                	Sterilization_limit__c,Deliver_date__c,
                                                    Intra_Trade_List_RMB__c,Asset_Model_No__c,Arrive_Date__c,Send_Date__c,
                                                    Consumable_Product__r.SFDA_Status__c,
                                                    Consumable_Product__r.Product2__r.Packing_list_manual__c,
                                                    Consumable_Product__r.Product2__r.SFDA_Approbation_No__c,
                                                    Consumable_Product__r.Product2__r.SFDA_Expiration_Date__c
                                                FROM Consumable_order_details2__c
                                            	WHERE Consumable_Arrived_order__c = :ESetId
                                                //AND Consumable_order_minor__r.Dealer_Info__c = :accountid
                                                ORDER BY Name ];
    	for (Integer i = 0; i < consumablearriveproductdetailsSelected.size(); i++) {
            	consumableorderdetailsRecords.add(new ConsumableorderdetailsInfo(consumablearriveproductdetailsSelected[i]));
        	}
    	}
        for(ConsumableorderdetailsInfo bss : consumableorderdetailsRecords){
            bss.packing_list = bss.Prod.Product2__r.Packing_list_manual__c;
            bss.approbation_No = bss.Prod.Product2__r.SFDA_Approbation_No__c;
            //update by rentx 20210622 start CHAN-C3K4ZQ
            // bss.expiration_Date = bss.Prod.Product2__r.SFDA_Expiration_Date__c;
            bss.expiration_DateStr = bss.Prod.Product2__r.SFDA_Expiration_Date__c == null ? '' : bss.Prod.Product2__r.SFDA_Expiration_Date__c.format();
            bss.Sterilization_limitStr = bss.esd.Sterilization_limit__c == null ? '' : bss.esd.Sterilization_limit__c.format();
            //update by rentx 20210622 end CHAN-C3K4ZQ
        }
        consumableorderdetailsCount = consumableorderdetailsRecords.size();
        //add by rentx 20210622 CHAN-C3K4ZQ
        makeRecordsView(consumableorderdetailsRecords);
        //add by rentx 20210622 CHAN-C3K4ZQ
        
    }

    public PageReference ProSale() {

        List<Consumable_order_details2__c> orderdetails = new List<Consumable_order_details2__c>();
        //发货明细2
        orderdetails = [SELECT Id 
                        FROM Consumable_order_details2__c
                        WHERE Consumable_order_minor__c =:coc.Arrive_Order__c
                        AND recordtypeid = :System.Label.RT_ConOrderDetail2_Delivery
                        AND Dealer_Arrive__c = true ];
        if(orderallcount!= consumableorderdetailsCount){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '订单不完全到货，无法出库。'));
            return null;
        }
        return ProS();
    }

    // 销售画面
    public PageReference ProS() {
        // 出库指示画面
        if(coc.Arrive_Order__r.IsShipment__c == true){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '订单已经出库'));
            return null;
        }
        PageReference ref = new Pagereference('/summonsCreat?arriveorder=' + ESetId +'&KeyWords=Redirect');
        ref.setRedirect(true);
        return ref;
    }
   		// Data Bean
    	class ConsumableorderdetailsInfo implements Comparable {
    	public Consumable_order_details2__c esd { get; set; }
    	public Product2__c Prod { get; set; }
        public Decimal packing_list { get; set; }
        public Date expiration_Date { get; set; }
        public String approbation_No { get; set; }
        //add by rentx 20210622 start 
        public String expiration_DateStr {get;set;}
        public String Sterilization_limitStr {get;set;}
        // 已存在消耗品明细用
        public ConsumableorderdetailsInfo(Consumable_order_details2__c e) {
            esd = e;
            Prod = e.Consumable_Product__r;
        }
        // 排序Consumable_order__c
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }

    public void testMock(){
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}