using ibk.IPD.Common;
using ibk.IPD.Entity;
using ibk.IPD.Entity.IPD_MR.CapacityAndTime;
using ibk.IPD.Entity.IPD_MR.CapacityAndTime.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年05月20日
* 创建者: 李跃升
* Copy Right: 北京科技大学设计研究院有限公司
********************************************************/
namespace ibk.IPD.Controller.IPD_MR.CapacityAndTime
{
[RoutePrefix("ipd/ipdMr")]
public class ConfiTestEquController : ApiController
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
///
/// 参数校验
///
///
/// ///
/// 是否通过参数校验
public bool ParameterCalibration(CONFI_TEST_EQU cncs, RequestEntity result)
{
#region 参数判断
if (!string.IsNullOrEmpty(cncs.EQU_MODEL))
if (CommonUtils.GetLength(cncs.EQU_MODEL) > 6)
{
result.msg = "设备型号长度不可超过8!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.DEAL_TYPE))
if (CommonUtils.GetLength(cncs.DEAL_TYPE) > 20)
{
result.msg = "处理大类长度不可超过20!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.EQU_NUM))
if (!CommonUtils.IsNumber(cncs.EQU_NUM) || CommonUtils.GetLength(cncs.EQU_NUM) > 2)
{
result.msg = "设备数量是长度不可超过2的数字!";
result.code = "0";
return false;
}
if (!string.IsNullOrEmpty(cncs.TEST_DETAIL_CATE))
if (CommonUtils.GetLength(cncs.TEST_DETAIL_CATE) > 20)
{
result.msg = "试验详细类别长度不可超过20!";
result.code = "0";
return false;
}
return true;
#endregion
}
///
/// 获取主键的条件
///
///
/// AND 开头的 主键条件字符串
public string GetPrimaryKeyString(CONFI_TEST_EQU primaryKeyEntity)
{
StringBuilder strSql = new StringBuilder();
//if (!string.IsNullOrWhiteSpace(primaryKeyEntity.APLY_ITEM))
//因为主键是自增主键, 所以将判断主键重复换成重复数据判断
strSql.AppendLine(" AND EQU_MODEL = '" + primaryKeyEntity.EQU_MODEL + "'");
strSql.AppendLine(" AND DEAL_TYPE = '" + primaryKeyEntity.DEAL_TYPE + "'");
strSql.AppendLine(" AND EQU_NUM = '" + primaryKeyEntity.EQU_NUM + "'");
strSql.AppendLine(" AND TEST_DETAIL_CATE = '" + primaryKeyEntity.TEST_DETAIL_CATE + "'");
return strSql.ToString();
}
///
/// 获取主键查询的sql
///
///
/// 主键查询sql
public string GetCheckString(CONFI_TEST_EQU checkEntity)
{
string strSql = "SELECT * FROM CONFI_TEST_EQU WHERE 1=1 " + GetPrimaryKeyString(checkEntity);
return strSql;
}
///
/// 查询接口
///
///
///
[HttpPost, Route("inspectionCycle/getConfiTestEqu")]
public RequestEntity GetConfiTestEqu(ConfiTestEquQueryArgs queryArgs)
{
RequestEntity result = new RequestEntity(); //声明返回参数实体类
StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量
DataTable dtCheck = new DataTable();
//查询
//PK , EQU_MODEL , DEAL_TYPE , EQU_NUM , TEST_DETAIL_CATE
strSql.AppendLine("SELECT PK , EQU_MODEL , DEAL_TYPE , EQU_NUM , TEST_DETAIL_CATE ,to_date(INS_DATE || INS_TIME ,'yyyy-mm-dd hh24:mi:ss') 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 CONFI_TEST_EQU WHERE 1 = 1 ");
if (queryArgs != null)
{
if (!string.IsNullOrWhiteSpace(queryArgs.EQU_MODEL)) strSql.AppendLine(" AND EQU_MODEL LIKE '%" + queryArgs.EQU_MODEL + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.DEAL_TYPE)) strSql.AppendLine(" AND DEAL_TYPE LIKE '%" + queryArgs.DEAL_TYPE + "%'");
if (!string.IsNullOrWhiteSpace(queryArgs.START_EQU_NUM) && CommonUtils.IsNumber(queryArgs.START_EQU_NUM)) strSql.AppendLine(" AND EQU_NUM >=" + queryArgs.START_EQU_NUM);
if (!string.IsNullOrWhiteSpace(queryArgs.END_EQU_NUM) && CommonUtils.IsNumber(queryArgs.END_EQU_NUM)) strSql.AppendLine(" AND EQU_NUM <=" + queryArgs.END_EQU_NUM);
if (!string.IsNullOrWhiteSpace(queryArgs.TEST_DETAIL_CATE)) strSql.AppendLine(" AND TEST_DETAIL_CATE LIKE '%" + queryArgs.TEST_DETAIL_CATE + "%'");
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 INS_DATE 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("GetConfiTestEqu 报错", ex);
}
return result;
}
///
/// 添加接口
///
///
///
[HttpPost, Route("inspectionCycle/addConfiTestEqu")]
public RequestEntity AddConfiTestEqu(CONFI_TEST_EQU 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("AddConfiTestEqu 报错 : ", ex);
}
return result;
}
///
/// 更新接口
///
///
///
[HttpPost, Route("inspectionCycle/updConfiTestEqu")]
public RequestEntity UpdConfiTestEqu(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
{
//更新哪些字段
//PK ,EQU_MODEL , DEAL_TYPE , EQU_NUM , TEST_DETAIL_CATE
if (db.Execute("UPDATE CONFI_TEST_EQU SET " + string.Format(" EQU_MODEL = '{0}' , DEAL_TYPE = '{1}' , EQU_NUM = '{2}' , TEST_DETAIL_CATE = '{3}' , UPD_DATE = '{4}' , UPD_TIME='{5}',UPD_EMP = '{6}' WHERE 1 = 1 ",
requestData[1].EQU_MODEL, requestData[1].DEAL_TYPE, requestData[1].EQU_NUM, requestData[1].TEST_DETAIL_CATE, requestData[1].UPD_DATE, requestData[1].UPD_TIME, requestData[1].UPD_EMP) +
//根据主键字段可以确定唯一数据即要修改的数据
string.Format(" AND PK = {0}", requestData[0].PK)) > 0)
{
result.msg = "操作成功";
result.code = "1";
}
else
{
result.msg = "数据未发生改变,请确认后重试!";
result.code = "0";
}
}
return result;
}
}
catch (Exception ex)
{
result.msg = "数据库错误!";
result.code = "0";
logger.Error("UpdConfiTestEqu 报错", ex);
return result;
}
}
///
/// 删除接口
///
///
///
[HttpPost, Route("inspectionCycle/delConfiTestEqu")]
public RequestEntity DelConfiTestEqu(CONFI_TEST_EQU delModel)
{
RequestEntity result = new RequestEntity();
StringBuilder strSql = new StringBuilder();
try
{
strSql.AppendLine("DELETE FROM CONFI_TEST_EQU WHERE 1=1 AND");
strSql.AppendLine(string.Format("PK = {0}", delModel.PK));
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("DelConfiTestEqu 报错", ex);
}
return result;
}
}
}