using ibk.IPD.Common;
using ibk.IPD.Entity;
using ibk.IPD.Entity.IPD_MR.HotRoll;
using ibk.IPD.Entity.IPD_MR.HotRoll.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;
/********************************************************
* 简 介:南钢热轧制造规范管理
* 余材替代标准维护后端接口
* 版本号:V1.0
* 日 期:2022年07月14日
* 创建者: xuqilin
* Copy Right: 北京科技大学设计研究院有限公司
********************************************************/
namespace ibk.IPD.Controller.IPD_MR.HotRoll
{
[RoutePrefix("ipd/ipdMr")]
public class MaterialStandardController : ApiController
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
///
/// 参数校验
///
///
/// ///
/// 是否通过参数校验
public bool ParameterCalibration(YCTD_WH cncs, RequestEntity result)
{
#region 参数判断
if (string.IsNullOrEmpty(cncs.PLT) || cncs.PLT.Length > 10)
{
result.msg = "工厂不可为空且长度不可超过10!";
result.code = "0";
return false;
}
if (string.IsNullOrEmpty(cncs.PLT_NAME) || cncs.PLT.Length > 100)
{
result.msg = "工厂名称不可为空且长度不可超过100!";
result.code = "0";
return false;
}
if (string.IsNullOrEmpty(cncs.STANDARD_NO) || cncs.STANDARD_NO.Length > 200)
{
result.msg = "标准号不可为空且长度不可超过200!";
result.code = "0";
return false;
}
if (string.IsNullOrEmpty(cncs.TD_STANDARD_NO) || cncs.TD_STANDARD_NO.Length > 200)
{
result.msg = "替代标准不可为空且长度不可超过200!";
result.code = "0";
return false;
}
if (string.IsNullOrEmpty(cncs.SEQ_NO) || cncs.SEQ_NO.Length > 200)
{
result.msg = "优先顺序不可为空且长度不可超过200!";
result.code = "0";
return false;
}
return true;
#endregion
}
///
/// 获取主键的条件
///
///
/// AND 开头的 主键条件字符串
public string GetPrimaryKeyString(YCTD_WH primaryKeyEntity)
{
StringBuilder strSql = new StringBuilder();
//if (!string.IsNullOrWhiteSpace(primaryKeyEntity.APLY_ITEM))
strSql.AppendLine(" AND PLT = '" + primaryKeyEntity.PLT + "'");
strSql.AppendLine(" AND PLT_NAME = '" + primaryKeyEntity.PLT_NAME + "'");
strSql.AppendLine(" AND STANDARD_NO = '" + primaryKeyEntity.STANDARD_NO + "'");
strSql.AppendLine(" AND TD_STANDARD_NO = '" + primaryKeyEntity.TD_STANDARD_NO + "'");
strSql.AppendLine(" AND SEQ_NO = '" + primaryKeyEntity.SEQ_NO + "'");
return strSql.ToString();
}
///
/// 获取主键查询的sql
///
///
/// 主键查询sql
public string GetCheckString(YCTD_WH checkEntity)
{
string strSql = "SELECT * FROM YCTD_WH WHERE 1=1 " + GetPrimaryKeyString(checkEntity);
return strSql;
}
///
/// 查询接口
///
///
///
[HttpPost, Route("hotRoll/getMaterialStand")]
public RequestEntity GetMaterialStand(YctdStQueryArgs queryArgs)
{
RequestEntity result = new RequestEntity(); //声明返回参数实体类
StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量
DataTable dtCheck = new DataTable();
//普通查询
strSql.AppendLine("SELECT PLT, PLT_NAME, STANDARD_NO, TD_STANDARD_NO, SEQ_NO ,to_date(INS_DATE ,'yyyy-mm-dd') as INS_DATE,INS_TIME,INS_EMP , to_date(UPD_DATE || UPD_TIEM ,'yyyy-mm-dd hh24:mi:ss') as UPD_DATE, UPD_TIEM,UPD_EMP FROM YCTD_WH WHERE 1 = 1 ");
if (queryArgs != null)
{
if (!string.IsNullOrWhiteSpace(queryArgs.PLT)) strSql.AppendLine(" AND PLT LIKE '" + queryArgs.PLT + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.PLT_NAME)) strSql.AppendLine(" AND PLT_NAME LIKE '" + queryArgs.PLT_NAME + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.STANDARD_NO)) strSql.AppendLine(" AND STANDARD_NO LIKE '" + queryArgs.STANDARD_NO + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.TD_STANDARD_NO)) strSql.AppendLine(" AND TD_STANDARD_NO LIKE '" + queryArgs.TD_STANDARD_NO + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.SEQ_NO)) strSql.AppendLine(" AND SEQ_NO LIKE '" + queryArgs.SEQ_NO + "%'");
if (!string.IsNullOrWhiteSpace(CommonUtils.ObjectToStr(queryArgs.START_TIME)) && CommonUtils.ObjectToStr(queryArgs.START_TIME) != "0001/1/1 0:00:00") strSql.AppendLine(" AND to_date(INS_DATE ,'yyyy-mm-dd hh24:mi:ss') >= to_date('" + CommonUtils.ObjectToStr(queryArgs.START_TIME) + "','yyyy-mm-dd hh24:mi:ss')");
if (!string.IsNullOrWhiteSpace(CommonUtils.ObjectToStr(queryArgs.END_TIME)) && CommonUtils.ObjectToStr(queryArgs.END_TIME) != "0001/1/1 0:00:00") strSql.AppendLine(" AND to_date(INS_DATE ,'yyyy-mm-dd hh24:mi:ss') <= to_date('" + CommonUtils.ObjectToStr(queryArgs.END_TIME) + "','yyyy-mm-dd hh24:mi:ss')");
}
try
{
//开启数据库连接查询数据
using (IDbContext db = ObjectContainer.GetObject("db"))
{
strSql.AppendLine("Order By SEQ_NO DESC ");
dtCheck = db.Query(strSql.ToString());
result.data = db.Query(strSql.ToString());
if (dtCheck.Rows.Count > 0)
{
result.msg = "操作成功!";
result.code = "1";
}
else
{
result.msg = "未找到查询所需数据!";
result.code = "1";
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("getMaterialStand 报错", ex);
}
return result;
}
///
/// 添加接口
///
///
///
[HttpPost, Route("hotRoll/addMaterialStand")]
public RequestEntity AddMaterialStand(YCTD_WH insertModel)
{
RequestEntity result = new RequestEntity();
DateTime dtNow = DateTime.Now;
try
{
#region 参数判断
if (!ParameterCalibration(insertModel, result))
return result;
#endregion
insertModel.INS_DATE = dtNow.ToString("yyyyMMdd");
insertModel.INS_TIME = dtNow.ToString("HHmmss");
using (IDbContext db = ObjectContainer.GetObject("db"))
{
// 执行主键重复查询
DataTable dtCheck = db.Query(GetCheckString(insertModel));
if (dtCheck.Rows.Count > 0) // 若数据库中存在则返回错误码0并msg提示用户
{
result.msg = "数据已存在,请确认后重试!";
result.code = "0";
return result;
}
if (db.Insert(insertModel) > 0)
{
result.msg = "操作成功";
result.code = "1";
}
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("AddMaterialStand 报错 : ", ex);
}
return result;
}
///
/// 更新接口
///
///
///
[HttpPost, Route("hotRoll/updMaterialStand")]
public RequestEntity UpdMaterialStand(IList requestData)
{
RequestEntity result = new RequestEntity();
DataTable dtCheck = new DataTable();
DataTable dtCheckSelf = new DataTable();
StringBuilder strSql = new StringBuilder();
StringBuilder strSqlSelf = new StringBuilder();
DateTime dtNow = DateTime.Now;
requestData[1].UPD_DATE = dtNow.ToString("yyyyMMdd");
requestData[1].UPD_TIEM = dtNow.ToString("HHmmss");
#region 参数判断
if (!ParameterCalibration(requestData[1], result))
return result;
#endregion
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
// 执行主键重复查询
dtCheck = db.Query(GetCheckString(requestData[1]));
dtCheckSelf = db.Query(GetCheckString(requestData[0]));
if (dtCheck.Rows.Count > 0 && !CommonUtils.CompareDataTable(dtCheck, dtCheckSelf)) // 若数据库中存在则返回错误码0并msg提示用户
{
result.msg = "主键数据已存在,请确认后重试!";
result.code = "0";
}
else
{
//更新哪些字段
//
if (db.Execute("UPDATE YCTD_WH SET " + string.Format(" PLT = '{0}' , PLT_NAME = '{1}' , STANDARD_NO = '{2}' , TD_STANDARD_NO = '{3}' , SEQ_NO = '{4}' , UPD_DATE = '{5}' , UPD_TIEM='{6}',UPD_EMP = '{7}'",
requestData[1].PLT, requestData[1].PLT_NAME, requestData[1].STANDARD_NO, requestData[1].TD_STANDARD_NO, requestData[1].SEQ_NO, requestData[1].UPD_DATE, requestData[1].UPD_TIEM, requestData[1].UPD_EMP) +
//根据主键字段可以确定唯一数据即要修改的数据
string.Format("WHERE 1=1 AND PLT = '{0}' AND PLT_NAME = '{1}' AND STANDARD_NO = '{2}' AND TD_STANDARD_NO = '{3}' AND SEQ_NO = '{4}'", requestData[0].PLT, requestData[0].PLT_NAME, requestData[0].STANDARD_NO, requestData[0].TD_STANDARD_NO, requestData[0].SEQ_NO)) > 0)
{
result.msg = "操作成功";
result.code = "1";
}
else
{
result.msg = "数据未发生改变,请确认后重试!";
result.code = "0";
}
}
return result;
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("UpdMaterialStand 报错", ex);
return result;
}
}
///
/// 删除接口
///
///
///
[HttpPost, Route("hotRoll/delMaterialStand")]
public RequestEntity DelMaterialStand(YCTD_WH delModel)
{
RequestEntity result = new RequestEntity();
StringBuilder strSql = new StringBuilder();
try
{
strSql.AppendLine("DELETE FROM YCTD_WH WHERE 1=1 ");
strSql.AppendLine(GetPrimaryKeyString(delModel));
using (IDbContext db = ObjectContainer.GetObject("db"))
{
db.Execute(strSql.ToString()); // 执行删除数据操作
result.msg = "操作成功";
result.code = "1";
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("DelMaterialStand 报错", ex);
}
return result;
}
}
}