using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
public class PlateBindingController : ApiController
{
private json objJsonResult = new json();
public DataSet ds = new DataSet();
SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
///
/// 保存平板绑定
///
///
///
[Route("Save/Xt_PlateBinding")]
[HttpPost]
public object Save([FromBody] JObject msg)
{
try
{
DataSet ds;
var _value = msg["msg"].ToString();
string msg3 = _value.ToString();
string[] sArray = msg3.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string msg1 = sArray[0].ToString();
string user = sArray[1].ToString();
string HPath = sArray[2].ToString();
string filePath = "MESConfiguration";
bool isAppend = true;
filePath = $@"{filePath}\PlateBinding.txt";
//filePath = "C:\\" + filePath;
filePath = HPath;
if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
{
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
//判断是否已经存在平板绑定文件,若已存在则删除,重新创建
bool fileExists = System.IO.File.Exists(filePath);
//存在 则删除该文件
if (fileExists)
{
File.Delete(filePath);
}
//创建文件
using (StreamWriter writer = new StreamWriter(filePath, isAppend))
{
//存在的时候才写一行
if (fileExists && isAppend)
{
writer.WriteLine();
}
var content = msg1 is string ? msg1 : JsonConvert.SerializeObject(msg1);
writer.WriteLine($"{DateTime.Now}");
writer.WriteLine("");
msg1 = msg1.Replace("{","").Replace("}","").Replace("\"","");
string[] arr = msg1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr.Length; i++)
{
writer.WriteLine(arr[i]);
}
}
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "平板绑定信息写入成功!";
objJsonResult.data = "";
return objJsonResult;
}
catch (Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "写入平板绑定信息过程中发生异常!";
objJsonResult.data = "";
return objJsonResult;
}
}
#region 获取平板绑定文件数据
[Route("Get/Xt_PlateBinding")]
[HttpGet]
public object Get(string user,string HPath)
{
try
{
//判断是否存在文件夹,若不存在则创建
if (!System.IO.Directory.Exists(Path.GetDirectoryName(HPath)))
{
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(HPath));
}
//判断是否已经存在平板绑定文件,若不存在则创建一份文件
bool fileExists = System.IO.File.Exists(HPath);
//不存在则创建该文件
if (!fileExists)
{
bool isAppend = true;
using (StreamWriter writer = new StreamWriter(HPath, isAppend))
{
//存在的时候才写一行
if (fileExists && isAppend)
{
writer.WriteLine();
}
string msg1 = "";
var content = msg1 is string ? msg1 : JsonConvert.SerializeObject(msg1);
writer.WriteLine($"{DateTime.Now}");
writer.WriteLine("");
msg1 = msg1.Replace("{", "").Replace("}", "").Replace("\"", "");
string[] arr = msg1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr.Length; i++)
{
writer.WriteLine(arr[i]);
}
}
}
//根据指定路径,读取文件内容,返回数据为数组格式
string[] ConfigFileInfo = File.ReadAllLines(HPath);
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "Sucess!";
objJsonResult.data = ConfigFileInfo;
return objJsonResult;
}
catch (Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "Exception!" + e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
}
}