You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

668 lines
24 KiB
C#

7 months ago
using ibk.IPD.Common;
using ibk.IPD.Entity;
using ibk.IPD.Entity.IPD_PS.QueryArgs;
using ibk.IPD.Entity.IPD_MR.EquipmentMgt.QueryArgs;
using log4net;
using SOA.Objects;
using SOA.Persistent;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using ibk.IPD.Entity.IPD_PS;
using System.Diagnostics;
/********************************************************
*
*
* V1.0
* 2022078
* : LiZhuo
* Copy Right:
********************************************************/
namespace ibk.IPD.Controller.IPD_MR.EquipmentMgt
{
[RoutePrefix("ipd/ipdMr")]
public class TbPlateSpeController : ApiController
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
//设置一个加锁对象
private static readonly object LOCK = new object();
/// <summary>
/// 参数校验
/// </summary>
/// <param name="cncs"></param>
/// /// <param name="result"></param>
/// <returns>是否通过参数校验</returns>
public bool ParameterCalibration(TB_PLATE_SPE cncs, RequestEntity result)
{
#region 参数判断
if (CommonUtils.GetLength(cncs.PLATE_NO) > 100)
{
result.msg = "钢板号长度不可超过100";
result.code = "0";
return false;
}
return true;
#endregion
}
/// <summary>
/// 查询接口
/// </summary>
/// <param name="queryArgs"></param>
/// <returns></returns>
[HttpPost, Route("GetTbPlateSpe")]
public RequestEntity GetTbPlateSpe(TB_PLATE_SPE query)
{
RequestEntity result = new RequestEntity(); //声明返回参数实体类
StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量
string PROC_CD = string.Empty;
DataTable dtCheck = new DataTable();
DataTable dtPlate = new DataTable();
//查询
strSql.AppendLine("SELECT DISTINCT PLATE_NO, PLATE_FLG, PLATE_CON,TO_CHAR(to_date(INS_DATE || INS_TIME ,'yyyy-mm-dd hh24:mi:ss'),'mm-dd HH24:mi') as INS_DATE ,INS_TIME,INS_EMP , to_date(UPD_DATE || UPD_TIME ,'yyyy-mm-dd hh24:mi:ss') as UPD_DATE, UPD_TIME,UPD_EMP,PROC_CD,IS_DELETE,PLATE_CON,PK FROM TB_PLATE_SPE WHERE 1 = 1 ");
if (query != null)
{
if (!string.IsNullOrWhiteSpace(query.PLATE_NO))
strSql.AppendLine(" AND PLATE_NO LIKE '%" + query.PLATE_NO.Trim() + "%'");
if (!string.IsNullOrWhiteSpace(query.PROC_CD))
PROC_CD = query.PROC_CD;
//if (!string.IsNullOrWhiteSpace(query.IS_DELETE))
// strSql.AppendLine(" AND IS_DELETE = '是'");
//if (string.IsNullOrWhiteSpace(query.IS_DELETE))
// strSql.AppendLine(" AND IS_DELETE <> '是'");
}
try
{
//开启数据库连接查询数据
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
strSql.AppendLine("Order By PK DESC ");
dtCheck = db.Query(strSql.ToString());
dtCheck.Columns.Add("PROC_CD_NOW");
string plate_no = " select PROC_CD || DECODE(PRICE_GRD, '2', '现货', '') PROC_CD, PLATE_NO from NISCO.GP_PLATE where PLATE_NO in ('";
//string ord_no = " select ORD_NO,ORD_ITEM,ORD_KND from NISCO.BP_ORDER_ITEM where 1=1 ";
if (dtCheck.Rows.Count > 0)
{
int n = 0;
foreach (DataRow row in dtCheck.Rows)
{
if (n == 0)
{
plate_no += row["PLATE_NO"].ToString().Trim();
}
else
{
plate_no += "','" + row["PLATE_NO"].ToString().Trim();
}
//多条1000会报错 截取城 多列sql 查询
if (n > 900)
{
n = n - 900;
plate_no += "') or PLATE_NO in ('" + row["PLATE_NO"].ToString();
}
//ord_no += " or ORD_NO='" + row["BOI_ORD_NO"].ToString() + "' and ORD_ITEM='" + row["BOI_ORD_ITEM"].ToString() + "'";
n++;
}
plate_no += "')";
using (IDbContext db_sur = ObjectContainer.GetObject<IDbContext>("db_sur"))
{
dtPlate = db_sur.Query(plate_no.ToString());
//dtOrd = db_sur.Query(ord_no.ToString());
}
if (dtPlate.Rows.Count > 0)
{
foreach (DataRow row in dtCheck.Rows)
{
DataRow[] rw = dtPlate.Select(" PLATE_NO='" + row["PLATE_NO"].ToString() + "'");
if (rw.Length > 0)
{
string PROC_CD_NOW = rw[0]["PROC_CD"].ToString();
row["PROC_CD_NOW"] = PROC_CD_NOW;
}
}
}
}
DataTable dtcopy = dtCheck.Clone();
if(!string.IsNullOrEmpty(PROC_CD))
{
dtcopy = dtCheck.Select(" PROC_CD_NOW like '%" + PROC_CD + "%'").CopyToDataTable();
}
else
{
dtcopy = dtCheck;
}
result.data = dtcopy;
if (dtcopy.Rows.Count > 0)
{
result.msg = "操作成功!";
result.code = "1";
}
else
{
result.data = new DataTable();
result.msg = "未找到查询所需数据!";
result.code = "1";
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("GetTbUstFl 报错", ex);
}
return result;
}
/// <summary>
/// 添加接口
/// </summary>
/// <param name="insertModel"></param>
/// <returns></returns>
[HttpPost, Route("AddTbPlateSpe")]
public RequestEntity AddTbPlateSpe(IList<TB_PLATE_SPE> insertModel)
{
RequestEntity result = new RequestEntity();
DateTime dtNow = DateTime.Now;
string strSql = string.Empty; //声明拼接Sql语句变量
DataTable dtCheck = new DataTable();
try
{
bool b = true;
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
strSql = "SELECT PLATE_NO, PROC_CD,PRICE_GRD FROM NISCO.GP_PLATE where PLATE_NO in ('";
db.BeginTransaction();
List<TB_PLATE_SPE> ilist = new List<TB_PLATE_SPE>();
int n = 0;
foreach (TB_PLATE_SPE model in insertModel)
{
#region 参数判断
if (string.IsNullOrEmpty(model.PLATE_NO))
{
continue;
}
if (!ParameterCalibration(model, result))
{
db.Rollback();
return result;
}
#endregion
if (n == 0)
{
strSql += model.PLATE_NO;
}
else
{
strSql += "','" + model.PLATE_NO;
}
n++;
model.PK = "";
model.PLATE_NO = model.PLATE_NO.Trim();
if (string.IsNullOrEmpty(model.IS_DELETE))
{
model.IS_DELETE = "";
}
else
{
model.IS_DELETE = model.IS_DELETE.Trim();
}
model.INS_DATE = dtNow.ToString("yyyyMMdd");
model.INS_TIME = dtNow.ToString("HHmmss");
model.UPD_DATE = dtNow.ToString("yyyyMMdd");
model.UPD_TIME = dtNow.ToString("HHmmss");
//if (string.IsNullOrEmpty(model.IS_DELETE))
//{
// model.IS_DELETE = model.IS_DELETE.Trim();
//}
ilist.Add(model);
}
strSql += "')";
using (IDbContext db_sur = ObjectContainer.GetObject<IDbContext>("db_sur"))
{
dtCheck = db_sur.Query(strSql);
//dtOrd = db_sur.Query(ord_no.ToString());
}
if (ilist.Count > 0)
{
foreach (var model in ilist)
{
DataRow[] rw = dtCheck.Select(" PLATE_NO='" + model.PLATE_NO + "'");
if (rw.Length > 0)
model.PROC_CD = rw[0]["PROC_CD"].ToString();
if (db.Insert<TB_PLATE_SPE>(model) == 0)
{
b = false;
break;
}
}
}
else
{
b = false;
}
//if (db.Insert<TB_PLATE_SPE>(model) == 0)
//{
// b = false;
// break;
//}
if (b)
{
result.msg = "操作成功!";
result.code = "1";
//数据操作成功则保存数据
db.Commit();
}
else
{
result.msg = "操作失败!";
result.code = "0";
//操作失败回滚数据到初始状态
db.Rollback();
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("AddTbPlateSpe 报错 : ", ex);
if (ex.ToString().Contains("00001"))
{
result.msg = "重复插入!";
}
}
return result;
}
/// <summary>
/// 添加接口
/// </summary>
/// <param name="insertModel"></param>
/// <returns></returns>
[HttpPost, Route("AddTbBatchTest")]
public RequestEntity AddTbBatchTest(IList<TB_BATCH_TEST> insertModel)
{
RequestEntity result = new RequestEntity();
DateTime dtNow = DateTime.Now;
DataTable dtCheck = new DataTable();
try
{
bool b = true;
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
db.BeginTransaction();
List<TB_BATCH_TEST> ilist = new List<TB_BATCH_TEST>();
int n = 0;
string TIMESTAMP = GetTimeStamp();
string insdate = dtNow.ToString("yyyyMMdd");
string instime = dtNow.ToString("HHmmss");
foreach (TB_BATCH_TEST model in insertModel)
{
#region 参数判断
#endregion
model.TIMESTAMP = TIMESTAMP;
model.PLATE_NO = model.PLATE_NO.Trim();
model.ORD_NO = model.ORD_NO.Trim();
model.ORD_ITEM = model.ORD_ITEM.Trim();
model.INS_DATE = insdate;
model.INS_TIME = instime;
model.UPD_DATE = insdate;
model.UPD_TIME = instime;
model.STATUS = "N";
//if (string.IsNullOrEmpty(model.IS_DELETE))
//{
// model.IS_DELETE = model.IS_DELETE.Trim();
//}
ilist.Add(model);
}
if (ilist.Count > 0)
{
foreach (var model in ilist)
{
if (db.Insert<TB_BATCH_TEST>(model) == 0)
{
b = false;
break;
}
}
}
else
{
b = false;
}
if (b)
{
result.msg = "操作成功!";
result.code = "1";
//数据操作成功则保存数据
db.Commit();
}
else
{
result.msg = "操作失败!";
result.code = "0";
//操作失败回滚数据到初始状态
db.Rollback();
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
}
return result;
}
/// <summary>
/// 添加接口
/// </summary>
/// <param name="insertModel"></param>
/// <returns></returns>
[HttpPost, Route("TbBatchTestVerification")]
public RequestEntity TbBatchTestVerification(string[] str)
{
lock (LOCK)
{
RequestEntity result = new RequestEntity(); //声明返回参数实体类
DataTable dtCheck = new DataTable();
//开启数据库连接查询数据
try
{
string args = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (i == 0)
{
args += str[i];
}
else
{
args += "," + str[i];
}
}
//string args = string.Empty;
//if(ArgsSupersede.Count>)
//foreach(var a in ArgsSupersede)
//string args = ArgsSupersede;
Process KHMsg = new Process();
KHMsg.StartInfo.FileName = @"D:\Program\BatchTest\Debug\BatchTextHandle.exe";
//KHMsg.StartInfo.FileName = @"D:\南钢项目\新改代码\批量测试\ipd-file\SinglePlateHandle\SinglePlateHandle\bin\Debug\BatchTextHandle.exe";
KHMsg.StartInfo.Arguments = args;
//KHMsg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//隐藏
KHMsg.Start();
while (!KHMsg.HasExited) { } //如果exe还没关闭则等待
if (KHMsg.ExitCode == 1)
{
result.msg = "操作成功!";
result.code = "1";
}
else
{
result.msg = "操作失败!";
result.code = "0";
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("AddSingleSup 报错", ex);
}
return result;
}
}
/// <summary>
/// 添加接口
/// </summary>
/// <param name="insertModel"></param>
/// <returns></returns>
[HttpPost, Route("AddTbPlateSpeOne")]
public RequestEntity AddTbPlateSpeOne(TB_PLATE_SPE model)
{
RequestEntity result = new RequestEntity();
DateTime dtNow = DateTime.Now;
DataTable dtCheck = new DataTable();
string strSql = string.Empty; //声明拼接Sql语句变量
try
{
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
db.BeginTransaction();
strSql = "SELECT PLATE_NO, PROC_CD FROM NISCO.GP_PLATE where PLATE_NO = '" + model.PLATE_NO + "'";
#region 参数判断
if (string.IsNullOrEmpty(model.PLATE_NO))
{
db.Rollback();
return result;
}
if (!ParameterCalibration(model, result))
{
db.Rollback();
return result;
}
#endregion
model.PK = "";
model.INS_DATE = dtNow.ToString("yyyyMMdd");
model.INS_TIME = dtNow.ToString("HHmmss");
model.UPD_DATE = dtNow.ToString("yyyyMMdd");
model.UPD_TIME = dtNow.ToString("HHmmss");
if (string.IsNullOrEmpty(model.IS_DELETE))
{
model.IS_DELETE = "";
}
else
{
model.IS_DELETE = model.IS_DELETE.Trim();
}
using (IDbContext db_sur = ObjectContainer.GetObject<IDbContext>("db_sur"))
{
dtCheck = db_sur.Query(strSql);
//dtOrd = db_sur.Query(ord_no.ToString());
}
if (dtCheck.Rows.Count > 0)
{
model.PROC_CD = dtCheck.Rows[0]["PROC_CD"].ToString();
}
if (db.Insert<TB_PLATE_SPE>(model) > 0)
{
result.msg = "操作成功!";
result.code = "1";
//数据操作成功则保存数据
db.Commit();
}
else
{
result.msg = "操作失败!";
result.code = "0";
//操作失败回滚数据到初始状态
db.Rollback();
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("AddTbPlateSpe 报错 : ", ex);
if (ex.ToString().Contains("00001"))
{
result.msg = "重复插入!";
}
}
return result;
}
/// <summary>
/// 删除接口
/// </summary>
/// <param name="delModel"></param>
/// <returns></returns>
[HttpPost, Route("DelTbPlateSpe")]
public RequestEntity DelTbPlateSpe(IList<TB_PLATE_SPE> delModel)
{
RequestEntity result = new RequestEntity();
StringBuilder strSql = new StringBuilder();
try
{
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
foreach (var model in delModel)
{
try
{
//PLATE_NO
strSql.AppendLine("DELETE FROM TB_PLATE_SPE WHERE 1=1 ");
strSql.AppendLine(string.Format("AND PLATE_NO = '{0}' ", model.PLATE_NO));
db.Execute(strSql.ToString()); // 执行删除数据操作
//执行完操作之后清空strSql 准备下次 appendLine
strSql.Length = 0;
}
catch
{
logger.Error("该条记录删除失败: " + delModel.ToString());
}
}
result.msg = "操作成功";
result.code = "1";
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("DelTbPlateSpe 报错", ex);
}
return result;
}
/// <summary>
/// 修改接口
/// </summary>
/// <param name="updateModel"></param>
/// <returns></returns>
[HttpPost, Route("UpdateTbPlateSpe")]
public RequestEntity UpdateTbPlateSpe(IList<TB_PLATE_SPE> model)
{
RequestEntity result = new RequestEntity();
StringBuilder strSql = new StringBuilder();
try
{
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
try
{
//ORD_NO, ORD_ITEM
strSql.AppendLine("Update TB_PLATE_SPE ");
strSql.AppendLine(string.Format("set IS_DELETE = '{0}' where PLATE_NO = '{1}' ", model[1].IS_DELETE, model[1].PLATE_NO));
db.Execute(strSql.ToString()); // 执行删除数据操作
//执行完操作之后清空strSql 准备下次 appendLine
strSql.Length = 0;
}
catch
{
logger.Error("该条记录修改失败: " + model.ToString());
}
result.msg = "操作成功";
result.code = "1";
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("UpdateTbPlateSpe 报错", ex);
}
return result;
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 8, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
}
}