using ibk.IPD.Common; using ibk.IPD.Entity; using ibk.IPD.Entity.Basic.QueryArgs; using ibk.IPD.Entity.IpdMr.Basic; 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年03月14日 * 创建者: 李跃升 * Copy Right: 北京科技大学设计研究院有限公司 ********************************************************/ namespace ibk.IPD.Controller.Basic { [RoutePrefix("ipd/ipdMr")] public class BasicCodeController : ApiController { private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static string CD_MANA_NO = null; /// /// 查询接口 /// /// /// [HttpPost, Route("basic/getBasicCode")] public RequestEntity GetBasicCode(ZpCdQueryArgs queryArgs) { RequestEntity result = new RequestEntity(); //声明返回参数实体类 StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量 DataTable dtCheck = new DataTable(); //查询语句日期时间拼接起来格式化成yyyy-mm-dd hh24:mi:ss //CD_MANA_NO , CD , CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD strSql.AppendLine("SELECT CD_MANA_NO,CD,CD_SHORT_NAME,CD_NAME,CD_SHORT_ENG,CD_FULL_ENG,APLY_STD,APPLICATION_STD,to_date(INS_DATE || INS_TIME,'yyyy-mm-dd hh24:mi:ss') as INS_DATE,INS_TIME,INS_EMP FROM ZP_CD WHERE 1=1"); if (queryArgs != null) { strSql.AppendLine(" AND CD_MANA_NO ='" + queryArgs.CD_MANA_NO + "'"); //设置全局变量的值 CD_MANA_NO = queryArgs.CD_MANA_NO; //设置查询参数 if (!string.IsNullOrWhiteSpace(queryArgs.QueryArgs)) { strSql.AppendLine(" AND ( CD LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_SHORT_NAME LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_NAME LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_SHORT_ENG LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_FULL_ENG LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR APLY_STD LIKE '%" + queryArgs.QueryArgs + "%' )"); } } try { //开启数据库连接查询数据 using (IDbContext db = ObjectContainer.GetObject("db")) { strSql.AppendLine("Order By CD ASC"); 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("GetBasicCode 报错", ex); } return result; } /// /// 查询接口下拉列表(未用到) /// /// /// [HttpPost, Route("basic/getBasicCodeList")] public IList GetBasicCodeList(string CD_MANA_NO) { StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量 IList list = new List(); //查询语句日期时间拼接起来格式化成yyyy-mm-dd hh24:mi:ss //CD_MANA_NO , CD , CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD strSql.AppendLine("SELECT * FROM ZP_CD WHERE 1=1"); strSql.AppendLine(" AND CD_MANA_NO ='" + CD_MANA_NO + "'"); try { //开启数据库连接查询数据 using (IDbContext db = ObjectContainer.GetObject("db")) { list = db.Query(strSql.ToString()); //---------------------------------------------- //获取datatable的第几列数据, 目前是第0列 //var Array = from DataRow dr in dtCheck.Rows select dr[0]; //---------------------------------------------- } } catch (Exception ex) { logger.Error("GetBasicCode 报错", ex); } return list; } /// /// 添加接口 /// /// /// [HttpPost, Route("basic/addBasicCode")] public RequestEntity AddBasicCode(ZP_CD insertModel) { RequestEntity result = new RequestEntity(); StringBuilder strSql = new StringBuilder(); DataTable dtCheck = new DataTable(); DateTime dtNow = DateTime.Now; try { //获取全局变量CD_MANA_NO //logger.Info("CD_MANA_NO: " + CD_MANA_NO); //insertModel.CD_MANA_NO = CD_MANA_NO; #region 参数校验 //CD_MANA_NO , CD , CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD if (string.IsNullOrEmpty(insertModel.CD_MANA_NO) || insertModel.CD_MANA_NO.Length > 5) { result.msg = "代码管理号不可为空且长度不可超过5!"; result.code = "0"; return result; } if (string.IsNullOrEmpty(insertModel.CD) || insertModel.CD.Length > 15) { result.msg = "代码不可为空且长度不可超过15!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_SHORT_NAME)) if (insertModel.CD_SHORT_NAME.Length > 500) { result.msg = "代码简称长度不可超过500!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_NAME)) if (insertModel.CD_NAME.Length > 500) { result.msg = "代码名称长度不可超过500!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_SHORT_ENG)) if (insertModel.CD_SHORT_ENG.Length > 90) { result.msg = "代码英文简称长度不可超过90!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_FULL_ENG)) if (insertModel.CD_FULL_ENG.Length > 80) { result.msg = "代码英文名称长度不可超过80!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.APLY_STD)) if (insertModel.APLY_STD.Length > 1) { result.msg = "适用标准长度不可超过1且不可为空!"; result.code = "0"; return result; } #endregion // 拼接主键重复check查询语句 //CD_MANA_NO , CD strSql.AppendLine("SELECT * FROM ZP_CD WHERE 1=1 "); strSql.AppendLine(string.Format("AND CD_MANA_NO = '{0}' AND CD = '{1}' ", insertModel.CD_MANA_NO, insertModel.CD)); insertModel.INS_DATE = dtNow.ToString("yyyyMMdd"); insertModel.INS_TIME = dtNow.ToString("HHmmss"); using (IDbContext db = ObjectContainer.GetObject("db")) { // 执行主键重复查询 dtCheck = db.Query(strSql.ToString()); 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("AddBasicCode 报错", ex); } return result; } /// /// 更新接口 /// /// /// [HttpPost, Route("basic/updBasicCode")] public RequestEntity UpdBasicCode(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; try { #region 参数校验 if (requestData == null) { result.msg = "传入的参数与需求参数不一致!"; result.code = "0"; return result; } if (requestData.Count != 2) { result.msg = "数组类型参数不一致,应为2!"; result.code = "0"; return result; } //CD_MANA_NO , CD , CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD if (string.IsNullOrEmpty(requestData[1].CD_MANA_NO) || requestData[1].CD_MANA_NO.Length > 5) { result.msg = "代码管理号不可为空且长度不可超过5!"; result.code = "0"; return result; } if (string.IsNullOrEmpty(requestData[1].CD) || requestData[1].CD.Length > 15) { result.msg = "代码不可为空且长度不可超过15!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_SHORT_NAME)) if (requestData[1].CD_SHORT_NAME.Length > 500) { result.msg = "代码简称长度不可超过500!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_NAME)) if (requestData[1].CD_NAME.Length > 500) { result.msg = "代码名称长度不可超过500!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_SHORT_ENG)) if (requestData[1].CD_SHORT_ENG.Length > 90) { result.msg = "代码英文简称长度不可超过90!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_FULL_ENG)) if (requestData[1].CD_FULL_ENG.Length > 80) { result.msg = "代码英文名称长度不可超过80!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].APLY_STD)) if (requestData[1].APLY_STD.Length > 1) { result.msg = "适用标准长度不可超过1!"; result.code = "0"; return result; } #endregion requestData[1].INS_DATE = dtNow.ToString("yyyyMMdd"); requestData[1].INS_TIME = dtNow.ToString("HHmmss"); using (IDbContext db = ObjectContainer.GetObject("db")) { // 查询是否有相同主键记录 ////CD_MANA_NO , CD , ///CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD strSql.AppendLine("SELECT * FROM ZP_CD WHERE 1=1 "); strSql.AppendLine(string.Format("AND CD_MANA_NO = '{0}' AND CD = '{1}' ", CD_MANA_NO, requestData[1].CD)); strSqlSelf.AppendLine("SELECT * FROM ZP_CD WHERE 1=1 "); strSqlSelf.AppendLine(string.Format("AND CD_MANA_NO = '{0}' AND CD = '{1}' ", CD_MANA_NO, requestData[0].CD)); // 执行主键重复查询 dtCheck = db.Query(strSql.ToString()); dtCheckSelf = db.Query(strSqlSelf.ToString()); if (dtCheck.Rows.Count > 0 && !CommonUtils.CompareDataTable(dtCheck, dtCheckSelf)) // 若数据库中存在则返回错误码0并msg提示用户 { result.msg = "数据已存在,请确认后重试!"; result.code = "0"; } else { string ss = string.Format("UPDATE ZP_CD SET CD = '{0}' , CD_SHORT_NAME = '{1}' , CD_NAME = '{2}' , CD_SHORT_ENG = '{3}' , CD_FULL_ENG = '{4}',APLY_STD = '{5}',APPLICATION_STD='{6}', INS_DATE = '{7}',INS_TIME='{8}',INS_EMP = '{9}' ", requestData[1].CD, requestData[1].CD_SHORT_NAME, requestData[1].CD_NAME, requestData[1].CD_SHORT_ENG, requestData[1].CD_FULL_ENG, requestData[1].APLY_STD, requestData[1].APPLICATION_STD, requestData[1].INS_DATE, requestData[1].INS_TIME, requestData[1].INS_EMP); ss += string.Format("WHERE 1=1 AND CD_MANA_NO = '{0}' AND CD = '{1}' ", requestData[0].CD_MANA_NO, requestData[0].CD) ; //更新哪些字段 // CD , CD_SHORT_NAME , CD_NAME , CD_SHORT_ENG , CD_FULL_ENG , APLY_STD //if (db.Execute("UPDATE ZP_CD SET " + string.Format("CD = '{0}' , CD_SHORT_NAME = '{1}' , CD_NAME = '{2}' , CD_SHORT_ENG = '{3}' , CD_FULL_ENG = '{4}',APLY_STD = '{5}',APPLICATION_STD='{6}', INS_DATE = '{7}',INS_TIME='{8}',INS_EMP = '{9}' ", requestData[1].CD, requestData[1].CD_SHORT_NAME, requestData[1].CD_NAME, requestData[1].CD_SHORT_ENG, requestData[1].CD_FULL_ENG,requestData[1].APLY_STD, requestData[1].APPLICATION_STD, requestData[1].INS_DATE, requestData[1].INS_TIME, requestData[1].INS_EMP) + // //根据主键字段可以确定唯一数据即要修改的数据 // string.Format("WHERE 1=1 AND CD_MANA_NO = '{0}' AND CD = '{1}' ", CD_MANA_NO, requestData[0].CD)) > 0) if(db.Execute(ss)>0) { result.msg = "操作成功"; result.code = "1"; } else { result.msg = "数据未发生改变,请确认后重试!"; result.code = "0"; } } return result; } } catch (Exception ex) { result.msg = "数据库错误!"; result.code = "0"; logger.Error("UpdBasicCode 报错", ex); return result; } } /// /// 删除接口 /// /// /// [HttpPost, Route("basic/delBasicCode")] public RequestEntity DelBasicCode(ZP_CD delModel) { RequestEntity result = new RequestEntity(); StringBuilder strSql = new StringBuilder(); try { //CD_MANA_NO , CD strSql.AppendLine("DELETE FROM ZP_CD WHERE 1=1 "); strSql.AppendLine(string.Format("AND CD_MANA_NO = '{0}' AND CD = '{1}' ", delModel.CD_MANA_NO, delModel.CD)); 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("DelBasicCode 报错", ex); } return result; } } }