using Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace OtherInterface
{
  public static class ZnlcInterface
  {
    private static LimisResponse sendDataToZn(WeightInfoBeta model)
    {
      DataContractJsonSerializer objseria = new DataContractJsonSerializer(typeof(WeightInfoBeta));
      MemoryStream mem = new MemoryStream();
      objseria.WriteObject(mem, model);
      string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
      WebClient webClient = new WebClient();
      webClient.Headers["Content-type"] = "application/json";
      webClient.Encoding = Encoding.UTF8;
      string retStr = webClient.UploadString("http://10.1.213.84:8080/LimsService.svc/SendWeightResultBeta", "POST", data);
      LimisResponse limisResponse = JsonConvert.DeserializeObject<LimisResponse>(retStr);
      return limisResponse;
    }
    public static string sendFirst(MeterWorkCarActualFirst meterWorkCarActualFirst)
    {
      string strResult = "";
      try
      {
        WeightInfoBeta w = new WeightInfoBeta();
      w.DeviceNo = meterWorkCarActualFirst.baseSpotNo;//计量站点
      w.ConveyanceNo = meterWorkCarActualFirst.carNo;//车号
      w.WeightVoucherNo = meterWorkCarActualFirst.predictionNo;//委托编号
      //double t =double.Parse( meterWorkCarActualFirst.meterWeight.Value.ToString());
      //w.GrossWeight =double.Parse( MyStringUtils.div1000(t.ToString()));
      w.GrossWeight = meterWorkCarActualFirst.meterWeight / 1000;
      w.TareWeight = null;
      w.NetWeight = null;
      w.MeteringPerson = meterWorkCarActualFirst.createManNo;
        //DateTime dt =DateTime.Parse( meterWorkCarActualFirst.createTime.ToString());
        //w.MeteringTime = dt.ToString("yyyy-MM-dd hh:mm:ss");
        w.MeteringTime = meterWorkCarActualFirst.createTime.ToString();
           LimisResponse ls = sendDataToZn(w);
        if (ls.Success)
        {
          strResult = ls.Msg;
        }
        else
        {
          if (ls.Code.Equals("401") || ls.Code.Equals("402") || ls.Code.Equals("404"))
          {
          }
        }
      }
      catch (Exception eee)
      {
        strResult = "error:"+eee.Message;
      }
      return strResult;
    }
    public static string sendNet(MeterWorkCarActual meterWorkCarActual)
    {
      string strResult = "";
      try
      {
        WeightInfoBeta w = new WeightInfoBeta();
      w.DeviceNo = meterWorkCarActual.baseSpot2No;//计量站点
      w.ConveyanceNo = meterWorkCarActual.carNo;//车号
      w.WeightVoucherNo = meterWorkCarActual.predictionNo;//委托编号
      w.GrossWeight = meterWorkCarActual.grossWeight / 1000;
      w.TareWeight = meterWorkCarActual.tareWeight / 1000;
      w.NetWeight = meterWorkCarActual.netWeight / 1000;
      w.MeteringPerson = meterWorkCarActual.createManNo;
      w.MeteringTime = meterWorkCarActual.createTime.ToString();
     
        LimisResponse ls = sendDataToZn(w);
        if (ls.Success)
        {
          strResult = ls.Msg;
        }
        else
        {
          if (ls.Code.Equals("401") || ls.Code.Equals("402") || ls.Code.Equals("404"))
          {
          }
        }
      }
      catch (Exception eee)
      {
        strResult = "error:" + eee.Message;
      }
      return strResult;
    }
  }
  public class LimisResponse
  {
    public bool Success { get; set; }
    public string Msg { get; set; }
    public string Code { get; set; }
  }
  public class WeightInfoBeta
  {
    public string ConveyanceNo { get; set; }
    public string DeviceNo { get; set; }
    public double? GrossWeight { get; set; }
    public string MeteringPerson { get; set; }
    public string MeteringTime { get; set; }
    public double? NetWeight { get; set; }
    public double? TareWeight { get; set; }
    public string WeightVoucherNo { get; set; }
  }
}
