/**********************************************************************
 * 
 *
 * @url: /services/apexrest/rest
 * @data:
 *  {
        
    }
    *************************************************************************/
    @RestResource(urlMapping='/NFM208/*')
    global with sharing class NFM208Rest {
      //CHAN-BGYE7C 新增NFM208接口 精琢技术 2019/11/22 Start
      global class ProductsDeliveryRest{
        public ProductsDelivery ProductsDelivery;
      }

      global class ProductsDelivery {
        public NFMUtil.Monitoring Monitoring;
        public ReturnProductCode[] ReturnProductCode;

      }
      //报文是两层[]的
      global class ReturnProductCode {
      public String ProductCode; //产品代码
      public ReturnProductModel[] ReturnProductModel;
    }
    global class ReturnProductModel {

        public String SerialNumber; // 机身编码
        public String ManagementNumber; // 管理代码
        public String CreateDate; //生产日期
        public String EndDate; //有效期
      }
      @HttpGet
      global static  void getProduct() {

      RestRequest request = RestContext.request;
      //接收传过来的 合同编号
      String caseId = request.params.get('ContractNumber'); //订单编号

      system.debug('caseId:'+caseId);

      String rowDataStr = null;

      //添加日志 精琢技术 2019/12/12 Start
      String MessageGroupNumber = '';
      String logstr = '';
      BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'NFM208';
        iflog.MessageGroupNumber__c = MessageGroupNumber;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
      insert iflog;
      //添加日志 精琢技术 2019/12/12 End

      try{
        List<Asset> assList2 = [ Select Id,SerialNumber,OT_CODE__c ,Product_Serial_No__c,TracingCode__c,ProductionDate__c,  Guaranteen_end__c from Asset where Order_No__c =:caseId];
        ProductsDelivery  pd = new ProductsDelivery();
        ReturnProductCode pc = new ReturnProductCode();
          pd.ReturnProductCode   = new List<ReturnProductCode>();
          pc.ReturnProductModel = new List<ReturnProductModel>();
        List<ReturnProductModel> rpmList = new List<ReturnProductModel>();

        AggregateResult[] assList1 = [ Select Count(Id),product2.productCode from Asset where Order_No__c =:caseId group by product2.productCode ];
        for(AggregateResult ass1 : assList1){
          ReturnProductCode rpc  = new ReturnProductCode();
            rpc.ProductCode = String.valueOf(ass1.get('productCode'));
            rpc.ReturnProductModel = new List<ReturnProductModel>();
          for(Asset ass2 : assList2){
            if(String.valueOf(ass1.get('productCode'))== ass2.OT_CODE__c){

              ReturnProductModel rpm = new ReturnProductModel();
                rpm.SerialNumber = ass2.SerialNumber;
                rpm.ManagementNumber =ass2.TracingCode__c;
                //添加日志 精琢技术 2019/12/12 Start  2021/07/16 wql 修改日志信息显示 start
                logstr +=ass2.SerialNumber+',';
                //添加日志 精琢技术 2019/12/12 End 2021/07/16 wql 修改日志信息显示 end
                rpm.CreateDate = NFMUtil.formatDate2Str(ass2.ProductionDate__c);
                rpm.EndDate = NFMUtil.formatDate2Str(ass2.Guaranteen_end__c);
              rpc.ReturnProductModel.add(rpm);
            }
          }
          pd.ReturnProductCode.add(rpc);
        }



        NFMUtil.Monitoring me = new NFMUtil.Monitoring();
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        String timeStr = nowStr.substring(8);

        me.Tag                  = 'MSGH';
        me.Sender               = 'SFDC';
        me.Receiver             = 'SPO';
        me.MessageType          = 'NFM208';
        me.MessageGroupNumber   = timeStr+assList2.size();
        me.NumberOfRecord       = '' + assList2.size();
        me.TransmissionDateTime = nowStr;
        me.Text = '';
        pd.Monitoring = me;

        BatchIF_Log__c rowData  = NFMUtil.makeRowData(me, 'NFM208', pd);
        rowData.retry_cnt__c=0;
        upsert  rowData;

        //添加日志 精琢技术 2019/12/12 Start
        MessageGroupNumber = rowData.MessageGroupNumber__c;
        logstr += '\nend';
        //添加日志 精琢技术 2019/12/12 End

        rowDataStr = NFMUtil.getRowDataStr(rowData);


      }catch(Exception ex){
        //添加日志 精琢技术 2019/12/12 Start
        logstr += '\n' + ex.getMessage();
        iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
      }

      iflog.MessageGroupNumber__c = MessageGroupNumber;
      iflog.Log__c =MessageGroupNumber + ' start\n' +'合同编号 ： '+caseId +'\n'+'机身编码：'+ logstr;
      if (iflog.Log__c.length() > 131072) {
        iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
      }
      if (iflog.ErrorLog__c.length() > 32768) {
        iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
      }
      update iflog;
      //添加日志 精琢技术 2019/12/12 End

      // JSONを戻す  返回的数据
      RestResponse res = RestContext.response;
      res.addHeader('Content-Type', 'application/json');
      res.statusCode = 200;
      String jsonResponse = rowDataStr;
      res.responseBody = blob.valueOf(jsonResponse);
      return; 
      
    }
    //CHAN-BGYE7C 新增NFM208接口 精琢技术 2019/11/22 End
  }