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月19日 * 创建者: 李跃升 * Copy Right: 北京科技大学设计研究院有限公司 ********************************************************/ namespace ibk.IPD.Controller.IPD_MR.CapacityAndTime { [RoutePrefix("ipd/ipdMr")] public class RollingWorkTimeController : ApiController { private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// /// 参数校验 /// /// /// /// /// 是否通过参数校验 public bool ParameterCalibration(ROLLING_WORK_TIME cncs, RequestEntity result) { #region 参数判断 if (!string.IsNullOrEmpty(cncs.PLT)) if (CommonUtils.GetLength(cncs.PLT) > 2) { result.msg = "工厂长度不可超过2!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.PRC_LINE)) if (CommonUtils.GetLength(cncs.PRC_LINE) > 1) { result.msg = "机号长度不可超过1!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.SLAB_THK_MAX)) if (!CommonUtils.IsNumber(cncs.SLAB_THK_MAX) || CommonUtils.GetLength(cncs.SLAB_THK_MAX) > 6) { result.msg = "板坯厚度上限是长度不可超过6的数字!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.SLAB_THK_MIN)) if (!CommonUtils.IsNumber(cncs.SLAB_THK_MIN) || CommonUtils.GetLength(cncs.SLAB_THK_MIN) > 6) { result.msg = "板坯厚度下限是长度不可超过6的数字!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.MILL_TGT_THK_MAX)) if (!CommonUtils.IsNumber(cncs.MILL_TGT_THK_MAX) || CommonUtils.GetLength(cncs.MILL_TGT_THK_MAX) > 6) { result.msg = "轧制厚度上限是长度不可超过6的数字!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.MILL_TGT_THK_MIN)) if (!CommonUtils.IsNumber(cncs.MILL_TGT_THK_MIN) || CommonUtils.GetLength(cncs.MILL_TGT_THK_MIN) > 6) { result.msg = "轧制厚度下限是长度不可超过6的数字!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.STEEL_GRADE)) if (CommonUtils.GetLength(cncs.STEEL_GRADE) > 200) { result.msg = "钢种长度不可超过200!"; result.code = "0"; return false; } if (!string.IsNullOrEmpty(cncs.ROLLING_TIME)) if (!CommonUtils.IsNumber(cncs.ROLLING_TIME) || CommonUtils.GetLength(cncs.ROLLING_TIME) > 6) { result.msg = "轧制时间是长度不可超过6的数字!"; result.code = "0"; return false; } return true; #endregion } /// /// 获取主键的条件 /// /// /// AND 开头的 主键条件字符串 public string GetPrimaryKeyString(ROLLING_WORK_TIME primaryKeyEntity) { StringBuilder strSql = new StringBuilder(); //if (!string.IsNullOrWhiteSpace(primaryKeyEntity.APLY_ITEM)) //因为主键是自增主键, 所以将判断主键重复换成重复数据判断 strSql.AppendLine(" AND PLT = '" + primaryKeyEntity.PLT + "'"); strSql.AppendLine(" AND PRC_LINE = '" + primaryKeyEntity.PRC_LINE + "'"); strSql.AppendLine(" AND SLAB_THK_MAX = '" + primaryKeyEntity.SLAB_THK_MAX + "'"); strSql.AppendLine(" AND SLAB_THK_MIN = '" + primaryKeyEntity.SLAB_THK_MIN + "'"); strSql.AppendLine(" AND MILL_TGT_THK_MAX = '" + primaryKeyEntity.MILL_TGT_THK_MAX + "'"); strSql.AppendLine(" AND MILL_TGT_THK_MIN = '" + primaryKeyEntity.MILL_TGT_THK_MIN + "'"); strSql.AppendLine(" AND STEEL_GRADE = '" + primaryKeyEntity.STEEL_GRADE + "'"); strSql.AppendLine(" AND ROLLING_TIME = '" + primaryKeyEntity.ROLLING_TIME + "'"); return strSql.ToString(); } /// /// 获取主键查询的sql /// /// /// 主键查询sql public string GetCheckString(ROLLING_WORK_TIME checkEntity) { string strSql = "SELECT * FROM ROLLING_WORK_TIME WHERE 1=1 " + GetPrimaryKeyString(checkEntity); return strSql; } /// /// 查询接口 /// /// /// [HttpPost, Route("capacityAndTime/getRollingWorkTime")] public RequestEntity GetRollingWorkTime(RollingWorkTimeQueryArgs queryArgs) { RequestEntity result = new RequestEntity(); //声明返回参数实体类 StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量 DataTable dtCheck = new DataTable(); //查询 //PK , PLT , PRC_LINE , SLAB_THK_MAX , SLAB_THK_MIN ,MILL_TGT_THK_MAX , MILL_TGT_THK_MIN , STEEL_GRADE , ROLLING_TIME strSql.AppendLine("SELECT PK , PLT , PRC_LINE , SLAB_THK_MAX , SLAB_THK_MIN ,MILL_TGT_THK_MAX , MILL_TGT_THK_MIN , STEEL_GRADE , ROLLING_TIME ,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 ROLLING_WORK_TIME WHERE 1 = 1 "); if (queryArgs != null) { if (!string.IsNullOrWhiteSpace(queryArgs.PLT)) strSql.AppendLine(" AND PLT LIKE '%" + queryArgs.PLT + "%'"); if (!string.IsNullOrWhiteSpace(queryArgs.PRC_LINE)) strSql.AppendLine(" AND PRC_LINE LIKE '%" + queryArgs.PRC_LINE + "%'"); if (!string.IsNullOrWhiteSpace(queryArgs.SLAB_THK_MAX) && CommonUtils.IsNumber(queryArgs.SLAB_THK_MAX)) strSql.AppendLine(" AND SLAB_THK_MAX >=" + queryArgs.SLAB_THK_MAX); if (!string.IsNullOrWhiteSpace(queryArgs.SLAB_THK_MIN) && CommonUtils.IsNumber(queryArgs.SLAB_THK_MIN)) strSql.AppendLine(" AND SLAB_THK_MIN <=" + queryArgs.SLAB_THK_MIN); if (!string.IsNullOrWhiteSpace(queryArgs.MILL_TGT_THK_MAX) && CommonUtils.IsNumber(queryArgs.MILL_TGT_THK_MAX)) strSql.AppendLine(" AND MILL_TGT_THK_MAX >=" + queryArgs.MILL_TGT_THK_MAX); if (!string.IsNullOrWhiteSpace(queryArgs.MILL_TGT_THK_MIN) && CommonUtils.IsNumber(queryArgs.MILL_TGT_THK_MIN)) strSql.AppendLine(" AND MILL_TGT_THK_MIN <=" + queryArgs.MILL_TGT_THK_MIN); if (!string.IsNullOrWhiteSpace(queryArgs.STEEL_GRADE)) strSql.AppendLine(" AND STEEL_GRADE LIKE '%" + queryArgs.STEEL_GRADE + "%'"); if (!string.IsNullOrWhiteSpace(queryArgs.START_ROLLING_TIME) && CommonUtils.IsNumber(queryArgs.START_ROLLING_TIME)) strSql.AppendLine(" AND ROLLING_TIME >=" + queryArgs.START_ROLLING_TIME); if (!string.IsNullOrWhiteSpace(queryArgs.END_ROLLING_TIME) && CommonUtils.IsNumber(queryArgs.END_ROLLING_TIME)) strSql.AppendLine(" AND ROLLING_TIME <=" + queryArgs.END_ROLLING_TIME); 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("GetRollingWorkTime 报错", ex); } return result; } /// /// 添加接口 /// /// /// [HttpPost, Route("capacityAndTime/addRollingWorkTime")] public RequestEntity AddRollingWorkTime(ROLLING_WORK_TIME 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("AddRollingWorkTime 报错 : ", ex); } return result; } /// /// 更新接口 /// /// /// [HttpPost, Route("capacityAndTime/updRollingWorkTime")] public RequestEntity UpdRollingWorkTime(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 , PLT , PRC_LINE , SLAB_THK_MAX , SLAB_THK_MIN ,MILL_TGT_THK_MAX , MILL_TGT_THK_MIN , STEEL_GRADE , ROLLING_TIME if (db.Execute("UPDATE ROLLING_WORK_TIME SET " + string.Format(" PLT = '{0}' , PRC_LINE = '{1}' , SLAB_THK_MAX = '{2}' , SLAB_THK_MIN = '{3}' , MILL_TGT_THK_MAX = '{4}' , MILL_TGT_THK_MIN = '{5}' , STEEL_GRADE = '{6}' , ROLLING_TIME = '{7}' , UPD_DATE = '{8}' , UPD_TIME='{9}',UPD_EMP = '{10}' WHERE 1 = 1 ", requestData[1].PLT, requestData[1].PRC_LINE, requestData[1].SLAB_THK_MAX, requestData[1].SLAB_THK_MIN, requestData[1].MILL_TGT_THK_MAX, requestData[1].MILL_TGT_THK_MIN, requestData[1].STEEL_GRADE, requestData[1].ROLLING_TIME, 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("UpdRollingWorkTime 报错", ex); return result; } } /// /// 删除接口 /// /// /// [HttpPost, Route("capacityAndTime/delRollingWorkTime")] public RequestEntity DelRollingWorkTime(ROLLING_WORK_TIME delModel) { RequestEntity result = new RequestEntity(); StringBuilder strSql = new StringBuilder(); try { strSql.AppendLine("DELETE FROM ROLLING_WORK_TIME 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("DelRollingWorkTime 报错", ex); } return result; } } }