using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RB_MES_API.Context;
using RB_MES_API.Controllers.Cloud;
using RB_MES_API.Models;
using RB_MES_API.Models.Cloud;
using RB_MES_API.Models.Pangu;
using RB_MES_APICore.Models.Pangu;
using RB_MES_APICore.Models.Pangu;
//using RBMESAPI.Models.Cloud;
using RB_MES_APICore.Models.Pangu;
//using RBMESAPI.Models.Cloud;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace RB_MES_API.Controllers
{
///
/// 专属Enpower的API接口
///
[ApiController]
[Route("[controller]")]
public class ApiController : ControllerBase
{
private readonly RBContext _context;
private readonly IShareController _shareController;
private readonly IKDCloudHttpClient _KdhttpClient;
private readonly ILogger _logger;
private readonly IChiledSelect _chiledselect;
private readonly IKDSqlHelper _kdsqlhelper;
/////
///// 需要查询的请求列表
/////
//private IList queryJsons = new List();
///
/// 构造
///
///
///
///
///
///
///
public ApiController(RBContext context, IShareController shareController, IKDCloudHttpClient kdhttpClient,
ILogger logger, IChiledSelect chiledSelect,IKDSqlHelper kdsqlhelper)
{
_context = context;
_shareController = shareController;
_KdhttpClient = kdhttpClient;
_logger = logger;
_chiledselect = chiledSelect;
_kdsqlhelper = kdsqlhelper;
//_pushmesinterface = pushmesinterface;
}
private void WritLog(string errstr)
{
_logger.LogInformation(DateTime.UtcNow.ToLongTimeString() + errstr);
}
///
/// MES从ERP里查询的JSON结构,逻辑判断符都是‘=’
///
public class GetErpParameters
{
///
/// 查询类型
///
public string DocType { get; set; } = string.Empty;
///
/// 查询参数
///
public Dictionary KeyValues { get; set; }
}
public class RequestParm
{
///
/// 接口类型
///
public string DocType { get; set; } = string.Empty;
///
/// 发送的内容
///
public object data { get; set; } = new object();
}
///
/// 根据SelectClumnConvert创建一个新的空表,准备装返回数据。暂时不用............
///
/// SelectClumnConvert
///
private DataTable CreateDatatable(List clumnConvert)
{
DataTable dataTable = new DataTable();
var selectconvert = clumnConvert
.Select(s => new { s.FIndexID, s.FDesTableName })
.DistinctBy(s => new { s.FIndexID, s.FDesTableName })
.OrderBy(s => s.FIndexID)
.ToList();
foreach (var selectClumnConvert in selectconvert)
{
dataTable.Columns.Add(selectClumnConvert.FDesTableName, typeof(string));
}
return dataTable;
}
private string UpdateDataRow_Unit(BD_UNIT unit, DataRow row)
{
string result = "UPDATE kdCloud_BD_UNIT SET ";
string usql = "";
Type type = typeof(BD_UNIT);
PropertyInfo[] propertyInfos = type.GetProperties();
for (int i = 0; i < propertyInfos.Length; i++)
{
PropertyInfo propertyInfo = propertyInfos[i];
string colname = propertyInfo.Name;
if (row.Table.Columns.Contains(colname))
{
object v= propertyInfo.GetValue(unit, null);
if (row[colname].Equals(v))
{
string sform =string.Format(colname + "='{0}'",v);
usql += usql == "" ? sform : "," + sform;
};
}
}
if (usql != "")
{
return result + usql + string.Format(" where FUNITID={0}", unit.FUNITID);
}
return "";
}
private void AddDataRow_Unit(BD_UNIT unit, DataTable table)
{
DataRow row = table.NewRow();
Type type= typeof(BD_UNIT);
PropertyInfo[] propertyInfos = type.GetProperties();
for (int i = 0; i < propertyInfos.Length; i++)
{
PropertyInfo propertyInfo = propertyInfos[i];
string colname=propertyInfo.Name;
if (table.Columns.Contains(colname))
{
row[colname]=propertyInfo.GetValue(unit, null);
}
}
table.Rows.Add(row);
table.AcceptChanges();
}
///
/// 云星空计量单位变动反向同步到沃比系统_初始化
///
///
[HttpGet]
[Route("UnitChangedinfo")]
public async Task UnitChangedinfo()
{
string messstr = string.Empty;
string fileds = "FUNITID,FNumber,FName,FIsBaseUnit,FPrecision,FDocumentStatus,FForbidStatus,FConvertDenominator,FConvertNumerator,FRoundType";
//从云星空中把所有计量单位获取过来
CloudBillQueryJson queryJson = new CloudBillQueryJson()
{
FieldKeys = fileds,
FormId = "BD_UNIT"
};
if (!_KdhttpClient.Islogin)
{
await _KdhttpClient.LoginErp();
}
//从云星空查询
string jsonstr = JsonConvert.SerializeObject(queryJson);
List