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年04月15日
* 创建者: 李跃升
* Copy Right: 北京科技大学设计研究院有限公司
********************************************************/
namespace ibk.IPD.Controller.IPD_MR.HotRoll
{
[RoutePrefix("ipd/ipdMr")]
public class ApplyItemController : ApiController
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
///
/// 参数校验
///
///
/// ///
/// 是否通过参数校验
public bool ParameterCalibration(ZP_APLY_ITEM cncs, RequestEntity result)
{
#region 参数判断
if (string.IsNullOrEmpty(cncs.APLY_ITEM) || cncs.APLY_ITEM.Length > 15)
{
result.msg = "适用项目不可为空且长度不可超过15!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.BIZ_AREA))
if (cncs.BIZ_AREA.Length > 1)
{
result.msg = "业务范围长度不可超过1!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.TABLE_ID))
if (cncs.TABLE_ID.Length > 20)
{
result.msg = "数据表ID长度不可超过20!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.SEQ))
if (!CommonUtils.IsNumber(cncs.SEQ) || cncs.SEQ.Length > 3)
{
result.msg = "顺序是长度不可超过6的数字!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.APLY_ITEM_NAME))
if (cncs.APLY_ITEM_NAME.Length > 50)
{
result.msg = "适用项目名称长度不可超过50!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.APLY_ITEM_DESC))
if (cncs.APLY_ITEM_DESC.Length > 100)
{
result.msg = "适用项目说明长度不可超过100!";
result.code = "0";
return false;
}
return true;
#endregion
}
///
/// 获取主键的条件
///
///
/// AND 开头的 主键条件字符串
public string GetPrimaryKeyString(ZP_APLY_ITEM primaryKeyEntity)
{
StringBuilder strSql = new StringBuilder();
//if (!string.IsNullOrWhiteSpace(primaryKeyEntity.APLY_ITEM))
strSql.AppendLine(" AND APLY_ITEM = '" + primaryKeyEntity.APLY_ITEM + "'");
return strSql.ToString();
}
///
/// 获取主键查询的sql
///
///
/// 主键查询sql
public string GetCheckString(ZP_APLY_ITEM checkEntity)
{
string strSql = "SELECT * FROM ZP_APLY_ITEM WHERE 1=1 " + GetPrimaryKeyString(checkEntity);
return strSql;
}
///
/// 查询接口
///
///
///
[HttpPost, Route("hotRoll/getApplyItem")]
public RequestEntity GetApplyItem(ZpAplyItemQueryArgs queryArgs)
{
RequestEntity result = new RequestEntity(); //声明返回参数实体类
StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量
DataTable dtCheck = new DataTable();
//普通查询
strSql.AppendLine("SELECT APLY_ITEM , BIZ_AREA ,TABLE_ID , SEQ , APLY_ITEM_NAME , APLY_ITEM_DESC ,to_date(INS_DATE ,'yyyy-mm-dd') 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 FROM ZP_APLY_ITEM WHERE 1 = 1 ");
if (queryArgs != null)
{
if (!string.IsNullOrWhiteSpace(queryArgs.APLY_ITEM)) strSql.AppendLine(" AND APLY_ITEM LIKE '%" + queryArgs.APLY_ITEM + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.BIZ_AREA)) strSql.AppendLine(" AND BIZ_AREA LIKE '%" + queryArgs.BIZ_AREA + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.TABLE_ID)) strSql.AppendLine(" AND TABLE_ID LIKE '%" + queryArgs.TABLE_ID + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.START_SEQ) && CommonUtils.IsNumber(queryArgs.START_SEQ)) strSql.AppendLine(" AND SEQ >=" + queryArgs.START_SEQ);
if (!string.IsNullOrWhiteSpace(queryArgs.END_SEQ) && CommonUtils.IsNumber(queryArgs.END_SEQ)) strSql.AppendLine(" AND SEQ <=" + queryArgs.END_SEQ);
if (!string.IsNullOrWhiteSpace(queryArgs.APLY_ITEM_NAME)) strSql.AppendLine(" AND APLY_ITEM_NAME LIKE '%" + queryArgs.APLY_ITEM_NAME + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.APLY_ITEM_DESC)) strSql.AppendLine(" AND APLY_ITEM_DESC LIKE '%" + queryArgs.APLY_ITEM_DESC + "%'");
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 TABLE_ID DESC , SEQ ");
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("GetApplyItem 报错", ex);
}
return result;
}
///
/// 添加接口
///
///
///
[HttpPost, Route("hotRoll/addApplyItem")]
public RequestEntity AddApplyItem(ZP_APLY_ITEM 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("AddApplyItem 报错 : ", ex);
}
return result;
}
///
/// 更新接口
///
///
///
[HttpPost, Route("hotRoll/updApplyItem")]
public RequestEntity UpdApplyItem(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_TIME = 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 ZP_APLY_ITEM SET " + string.Format(" APLY_ITEM = '{0}' , BIZ_AREA = '{1}' , TABLE_ID = '{2}' , SEQ = '{3}' , APLY_ITEM_NAME = '{4}',APLY_ITEM_DESC = '{5}' , UPD_DATE = '{6}' , UPD_TIME='{7}',UPD_EMP = '{8}' WHERE 1 = 1 ",
requestData[1].APLY_ITEM, requestData[1].BIZ_AREA, requestData[1].TABLE_ID, requestData[1].SEQ, requestData[1].APLY_ITEM_NAME, requestData[1].APLY_ITEM_DESC, requestData[1].UPD_DATE, requestData[1].UPD_TIME, requestData[1].UPD_EMP) +
//根据主键字段可以确定唯一数据即要修改的数据
GetPrimaryKeyString(requestData[0])) > 0)
{
result.msg = "操作成功";
result.code = "1";
}
else
{
result.msg = "数据未发生改变,请确认后重试!";
result.code = "0";
}
}
return result;
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("UpdApplyItem 报错", ex);
return result;
}
}
///
/// 删除接口
///
///
///
[HttpPost, Route("hotRoll/delApplyItem")]
public RequestEntity DelApplyItem(ZP_APLY_ITEM delModel)
{
RequestEntity result = new RequestEntity();
StringBuilder strSql = new StringBuilder();
try
{
strSql.AppendLine("DELETE FROM ZP_APLY_ITEM 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("DelApplyItem 报错", ex);
}
return result;
}
}
}