using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using NFine.Code;
using NFine.Data.Extensions;
using NFine.Domain._03_Entity.SRM;
using NFine.Repository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Reflection.Emit;
namespace NFine.Application.WMS
{
///
/// 委外订单查询
///
public class ICSOutsourcingOrderApp : RepositoryFactory
{
public static DataTable Invmes = new DataTable();
///
/// 委外订单查询
///
///
///
public DataTable GetOutsourcingOrderApplyNeg(ref Pagination jqgridparam)
{
DataTable dt = new DataTable();
List parameter = new List();
string sql = @"select a.OOCode, a.CreatePerson,a.CreateDateTime from ICSOutsourcingOrder a
group by a.OOCode,a.CreatePerson,a.CreateDateTime";
sql = string.Format(sql);
DataTable dttest = Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
///
/// 委外订单明细查询
///
///
///
public DataTable GetOutsourcingOrderApplyNegDetail(string ApplyNegCode, ref Pagination jqgridparam)
{
DataTable dt = new DataTable();
object Figure = GetDecimalDigits();
List parameter = new List();
string sql = @"select a.ID,a.OOCode,a.Sequence,a.VenCode,e.VenName,a.DepCode,a.PersonCode,case when a.Type = '1' then '委外' else a.Type end as Type,a.InvCode,b.InvName,b.InvStd,b.InvDesc,b.InvUnit
,a.Amount,CAST(a.Quantity AS DECIMAL(18,{0})) as Quantity,g.DepName,
CAST(a.UnitPrice AS DECIMAL(18,{0})) as UnitPrice,a.Currency,a.Status,a.ReleaseState,a.PlanArriveDate,a.CreatePerson,a.CreateDateTime
,f.Colspan ,f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10 ,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10
from ICSOutsourcingOrder a
left join ICSInventory b on a.InvCode = b.InvCode and a.WorkPoint = b.WorkPoint
left join ICSVendor e on a.VenCode = e.VenCode and a.WorkPoint = e.WorkPoint
left join ICSDepartment g on a.DepCode = g.DepCode and a.WorkPoint = g.WorkPoint
left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
where a.OOCode = '" + ApplyNegCode + "' ";
sql = string.Format(sql, Figure);
DataTable dttest = Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
public object GetDecimalDigits()
{
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',');
try
{
string sql = string.Empty;
sql = @"select Figure from ICSConfiguration where Code='Figure001' and Enable='1' and WorkPoint='" + WorkPoint + "'";
object Figure = SqlHelper.ExecuteScalar(sql);
return Figure;
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
public string DeleteMopick(string json)
{
try
{
string wheresql = "(";
string[] id = json.Split(',');
for (int i = 0; i < id.Length; i++)
{
wheresql += "'" + id[i].Replace(']', ' ').Replace('[', ' ').Replace('"', ' ').Trim() + "',";
}
wheresql = wheresql.Trim(',') + ")";
string sql = "delete from ICSOutsourcingOrder where OOCode in" + wheresql;
int count = DbHelper.ExecuteNonQuery(CommandType.Text, sql);
DataRow[] row = Invmes.Select("OOCode in " + wheresql + "");
foreach (DataRow a in row)
{
Invmes.Rows.Remove(a);
}
return "删除成功!";
}
catch (Exception ex)
{
return ex.Message + " 删除失败!";
}
}
public string DeleteOutsourcingOrderApplyNeg(string keyValue)
{
//站点信息
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
string msg = "";
keyValue = keyValue.Substring(1, keyValue.Length - 2);
string sql = string.Empty;
sql += string.Format(@"DELETE FROM dbo.ICSOutsourcingOrder WHERE OOCode IN ({0}) and WorkPoint ='{1}'", keyValue.TrimEnd(','), WorkPoint);
try
{
if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0)
{
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return msg;
}
public void ClearTemp()
{
Invmes.Rows.Clear();
}
///
/// 新增
///
///
///
public string SaveICSOutsourcingOrderApplyNeg(string ICSASN)
{
string msg = "";
string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OutsourcingOrder/Create";
string result = HttpPost(APIURL, ICSASN);
JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
string MessAge = Obj["Message"].ToString();
string Success = Obj["Success"].ToString();
if (Success.ToUpper() == "FALSE")
{
msg = MessAge;
}
return msg;
}
///
/// 修改
///
///
///
public string UpdateICSOutsourcingOrderApplyNeg(string ICSASN)
{
string msg = "";
string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OutsourcingOrder/Update";
string result = HttpPost(APIURL, ICSASN);
JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
string MessAge = Obj["Message"].ToString();
string Success = Obj["Success"].ToString();
if (Success.ToUpper() == "FALSE")
{
msg = MessAge;
}
return msg;
}
///
/// 获取部门
///
///
///
public DataTable GetSelectICSDepCode()
{
string sql = string.Empty;
DataTable dt = null;
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
sql = @"
select DepCode ,DepName
from dbo.ICSDepartment a";
sql = string.Format(sql, WorkPoint);
// sql = @"select BadCode,BadDesc from dbo.ICSBadCode a left join
//ICSInventoryBadGroup b on a.BCGroupID=b.BCGroupID
//where b.InvCode='{0}'";
dt = SqlHelper.GetDataTableBySql(sql);
return dt;
}
///
/// 获取部门
///
///
///
public DataTable GetSelectICSVenCode()
{
string sql = string.Empty;
DataTable dt = null;
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
sql = @"
select a.VenCode,a.VenName from dbo.ICSVendor a";
sql = string.Format(sql, WorkPoint);
dt = SqlHelper.GetDataTableBySql(sql);
return dt;
}
///
/// 接口api解析
///
///
///
///
///
public static string HttpPost(string url, string body)
{
try
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
}
///
/// 获取单号
///
///
///
public string GetOOCode(string WorkPoint)
{
WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
string OOCode = string.Empty;
if (!string.IsNullOrEmpty(WorkPoint))
{
OOCode = GetSerialCode(WorkPoint, "ICSOutsourcingOrder", "OOCode", "OO", 8);
}
if (!string.IsNullOrWhiteSpace(OOCode))
{
string sqlISHave = @"SELECT ApplyCode FROM ICSMOApply a
WHERE a.ApplyCode = '{0}'";
sqlISHave = string.Format(sqlISHave, OOCode);
DataTable dtIsHave = SqlHelper.GetDataTableBySql(sqlISHave);
if (dtIsHave.Rows.Count > 0)
{
throw new Exception("单号已存在!");
}
}
return OOCode;
}
public string GetSerialCode(string workPointCode, string tbName, string colName, string Pre, int numLen)
{
string sql = "EXEC Addins_GetSerialCode '{0}','{1}','{2}','{3}',{4}";
sql = string.Format(sql, new object[] { workPointCode, tbName, colName, Pre, numLen });
return SqlHelper.ExecuteScalar(sql).ToString();
}
public void UpdateOutsourcingOrderApplyNegTemp(string json)
{
var data = json.ToJObject();
string usercode = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
var info = Invmes.Select(string.Format("TLZID='{0}'", data["TLZID"]));
if (info != null && info.Length > 0)
{
info[0]["ZJID"] = data["ZJID"];
info[0]["InvName"] = data["InvName"];
}
else
{
DataRow newrow = Invmes.NewRow();
newrow["ZJID"] = data["ZJID"];
Invmes.Rows.Add(newrow);
}
}
public DataTable GetICSOutsourcingOrderReturnTemporary(string rfqno)
{
string sql = @"select a.ID,a.OOCode,a.Sequence,a.VenCode,a.DepCode,a.PersonCode,a.Type,a.InvCode,a.Quantity,a.Amount,
a.InQuantity,a.UnitPrice,a.Currency,a.Status,a.ReleaseState,a.ReleaseDate,a.PlanArriveDate,
a.CreatePerson,a.CreateDateTime, a.MUSER,a.MUSERName from ICSOutsourcingOrder a where a.OOCode ='"
+ rfqno + "' ";
DataTable table = Repository().FindDataSetBySql(sql).Tables[0];
DataTable dtCloned = table.Clone();
foreach (DataColumn col in dtCloned.Columns)
{
col.DataType = typeof(string);
}
foreach (DataRow row in table.Rows)
{
DataRow newrow = dtCloned.NewRow();
foreach (DataColumn column in dtCloned.Columns)
{
newrow[column.ColumnName] = row[column.ColumnName].ToString();
}
dtCloned.Rows.Add(newrow);
}
if (Invmes.Rows.Count > 0)
{
dtCloned.Merge(Invmes, false);
}
return dtCloned;
}
}
}