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 BasicCodeMasterController : ApiController { private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// /// 查询接口获取主表内容 /// /// /// [HttpPost, Route("basic/getBasicCodeMaster")] public RequestEntity GetBasicCodeMaster(ZpCdMasterQueryArgs queryArgs) { RequestEntity result = new RequestEntity(); //声明返回参数实体类 StringBuilder strSql = new StringBuilder(); //声明拼接Sql语句变量 DataTable dtCheck = new DataTable(); //查询语句日期时间拼接起来格式化成yyyy-mm-dd hh24:mi:ss //CD_MANA_NO , CD_MANA_NAME , BIZ_AREA , CD_LEN , CD_DESC , COLUMN_ID , strSql.AppendLine("SELECT CD_MANA_NO,CD_MANA_NAME,BIZ_AREA,CD_LEN,CD_DESC,COLUMN_ID,to_date(INS_DATE || INS_TIME,'yyyy-mm-dd hh24:mi:ss') as INS_DATE,INS_TIME,INS_EMP FROM ZP_CD_MASTER WHERE 1=1 "); if (queryArgs != null) { if (!string.IsNullOrWhiteSpace(queryArgs.QueryArgs)) { strSql.AppendLine(" AND ( CD_MANA_NO LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_MANA_NAME LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR BIZ_AREA LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_LEN LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR CD_DESC LIKE '%" + queryArgs.QueryArgs + "%'"); strSql.AppendLine(" OR COLUMN_ID LIKE '%" + queryArgs.QueryArgs + "%' )"); } } 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("GetBasicCodeMaster 报错", ex); } return result; } /// /// 添加接口 /// /// /// [HttpPost, Route("basic/addBasicCodeMaster")] public RequestEntity AddBasicCodeMaster(ZP_CD_MASTER insertModel) { RequestEntity result = new RequestEntity(); StringBuilder strSql = new StringBuilder(); DataTable dtCheck = new DataTable(); DateTime dtNow = DateTime.Now; try { #region 参数校验 //CD_MANA_NO if (string.IsNullOrEmpty(insertModel.CD_MANA_NO)) { result.msg = "代码项目不可为空!"; result.code = "0"; return result; } if (insertModel.CD_MANA_NO.Length > 5) { result.msg = "代码项目长度不可超过5位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_MANA_NAME)) if (insertModel.CD_MANA_NAME.Length > 40) { result.msg = "代码名称长度不可超过40位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.BIZ_AREA)) if (insertModel.BIZ_AREA.Length > 2) { result.msg = "所属系统长度不可超过2位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_LEN)) if (insertModel.CD_LEN.Length > 3 || !CommonUtils.IsNumber(insertModel.CD_LEN)) { result.msg = "代码长度限制不可超过3位且必须是数字!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.CD_DESC)) if (insertModel.CD_DESC.Length > 100) { result.msg = "代码描述长度不可超过100位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(insertModel.COLUMN_ID)) if (insertModel.COLUMN_ID.Length > 50) { result.msg = "数据项ID长度不可超过50位!"; result.code = "0"; return result; } #endregion // 拼接主键重复check查询语句 strSql.AppendLine("SELECT * FROM ZP_CD_MASTER WHERE 1=1 "); strSql.AppendLine(string.Format("AND CD_MANA_NO = '{0}' ", insertModel.CD_MANA_NO)); 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("AddBasicCodeMaster 报错", ex); } return result; } /// /// 更新接口 /// /// /// [HttpPost, Route("basic/updBasicCodeMaster")] public RequestEntity UpdBasicCodeMaster(IList requestData) { RequestEntity result = new RequestEntity(); DataTable dtCheck = new DataTable(); DataTable dtCheckSelf = new DataTable(); //主表sql StringBuilder strSql = new StringBuilder(); StringBuilder strSqlSelf = new StringBuilder(); //从表sql StringBuilder strSqlSub = new StringBuilder(); DateTime dtNow = DateTime.Now; using (IDbContext db = ObjectContainer.GetObject("db")) { 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 if (string.IsNullOrEmpty(requestData[1].CD_MANA_NO)) { result.msg = "代码项目不可为空!"; result.code = "0"; return result; } if (requestData[1].CD_MANA_NO.Length > 5) { result.msg = "代码项目长度不可超过5位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_MANA_NAME)) if (requestData[1].CD_MANA_NAME.Length > 40) { result.msg = "代码名称长度不可超过40位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].BIZ_AREA)) if (requestData[1].BIZ_AREA.Length > 2) { result.msg = "所属系统长度不可超过2位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_LEN)) if (requestData[1].CD_LEN.Length > 3 || !CommonUtils.IsNumber(requestData[1].CD_LEN)) { result.msg = "代码长度限制不可超过3位且必须是数字!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].CD_DESC)) if (requestData[1].CD_DESC.Length > 100) { result.msg = "代码描述长度不可超过100位!"; result.code = "0"; return result; } if (!string.IsNullOrEmpty(requestData[1].COLUMN_ID)) if (requestData[1].COLUMN_ID.Length > 50) { result.msg = "数据项ID长度不可超过50位!"; result.code = "0"; return result; } #endregion requestData[1].INS_DATE = dtNow.ToString("yyyyMMdd"); requestData[1].INS_TIME = dtNow.ToString("HHmmss"); //查询修改后主键记录是否存在 //CD_MANA_NO //CD_MANA_NAME BIZ_AREA CD_LEN CD_DESC COLUMN_ID strSql.AppendLine("SELECT * FROM ZP_CD_MASTER WHERE 1=1 "); strSql.AppendLine(string.Format("AND CD_MANA_NO = '{0}'", requestData[1].CD_MANA_NO)); strSqlSelf.AppendLine("SELECT * FROM ZP_CD_MASTER WHERE 1=1 "); strSqlSelf.AppendLine(string.Format("AND CD_MANA_NO = '{0}'", requestData[0].CD_MANA_NO)); 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 { //由于是同时操作两个表,所以开启事务,保持同步 db.BeginTransaction(); //更新哪些字段 //CD_MANA_NO , CD_MANA_NAME , BIZ_AREA , CD_LEN , CD_DESC , COLUMN_ID , if (db.Execute("UPDATE ZP_CD_MASTER SET " + string.Format(" CD_MANA_NO = '{0}' , CD_MANA_NAME = '{1}' , BIZ_AREA = '{2}' , CD_LEN = '{3}' , CD_DESC = '{4}' , COLUMN_ID = '{5}', INS_DATE = '{6}',INS_TIME='{7}',INS_EMP = '{8}' ", requestData[1].CD_MANA_NO, requestData[1].CD_MANA_NAME, requestData[1].BIZ_AREA, requestData[1].CD_LEN, requestData[1].CD_DESC, requestData[1].COLUMN_ID, requestData[1].INS_DATE, requestData[1].INS_TIME, requestData[1].INS_EMP) + //根据主键字段可以确定唯一数据即要修改的数据 string.Format("WHERE 1=1 AND CD_MANA_NO = '{0}' ", requestData[0].CD_MANA_NO)) > 0) { // 拼接主键,查询从表是否有相应记录 //CD_MANA_NO strSqlSub.AppendLine("SELECT * FROM ZP_CD WHERE 1=1 "); strSqlSub.AppendLine(string.Format("AND CD_MANA_NO = '{0}' ", requestData[0].CD_MANA_NO)); dtCheck = db.Query(strSqlSub.ToString()); if (dtCheck.Rows.Count > 0) { if (db.Execute("UPDATE ZP_CD SET " + string.Format(" CD_MANA_NO = '{0}'", requestData[1].CD_MANA_NO) + string.Format("WHERE 1=1 AND CD_MANA_NO = '{0}' ", requestData[0].CD_MANA_NO)) > 0) { result.msg = "操作成功"; result.code = "1"; //数据操作成功则保存数据 db.Commit(); } else { result.msg = "数据未发生改变,请确认后重试!"; result.code = "0"; //数据如果未发生改变则回滚 db.Rollback(); } } else { result.msg = "操作成功"; result.code = "1"; //数据操作成功则保存数据 db.Commit(); } } else { result.msg = "数据未发生改变,请确认后重试!"; result.code = "0"; //数据如果未发生改变则回滚 db.Rollback(); } } return result; } catch (Exception ex) { //数据如果未发生改变则回滚 db.Rollback(); result.msg = "数据库错误!"; result.code = "0"; logger.Error("UpdBasicCodeMaster 报错", ex); } } return result; } /// /// 删除接口 如果删除主表数据, 则相应从表数据也会删除 /// /// /// [HttpPost, Route("basic/delBasicCodeMaster")] public RequestEntity DelBasicCodeMaster(ZP_CD_MASTER delModel) { RequestEntity result = new RequestEntity(); DataTable dtCheck = new DataTable(); StringBuilder strSqlMaster = new StringBuilder(); StringBuilder strSqlMasterSub = new StringBuilder(); StringBuilder strSqlMasterSubQuery = new StringBuilder(); using (IDbContext db = ObjectContainer.GetObject("db")) { try { // 拼接主表主键 //CD_MANA_NO strSqlMaster.AppendLine("DELETE FROM ZP_CD_MASTER WHERE 1=1 "); strSqlMaster.AppendLine(string.Format("AND CD_MANA_NO = '{0}' ", delModel.CD_MANA_NO)); // 拼接从表 查询 条件即 从表cd-mana-no = 主表 cd-mana-no 的数据 若有数据则删除, 若没有数据则不用删除 //CD_MANA_NO strSqlMasterSubQuery.AppendLine("SELECT * FROM ZP_CD WHERE 1=1 "); strSqlMasterSubQuery.AppendLine(string.Format("AND CD_MANA_NO = '{0}' ", delModel.CD_MANA_NO)); // 拼接从表 删除 条件即 从表cd-mana-no = 主表 cd-mana-no 的数据 //CD_MANA_NO strSqlMasterSub.AppendLine("DELETE FROM ZP_CD WHERE 1=1 "); strSqlMasterSub.AppendLine(string.Format("AND CD_MANA_NO = '{0}' ", delModel.CD_MANA_NO)); //由于是同时操作两个表,所以开启事务,保持同步 db.BeginTransaction(); // 执行主表删除数据操作 if (db.Execute(strSqlMaster.ToString()) > 0) { //执行查询从表有没有数据 dtCheck = db.Query(strSqlMasterSubQuery.ToString()); if (dtCheck.Rows.Count > 0) { //执行从表数据删除 if (db.Execute(strSqlMasterSub.ToString()) > 0) { result.msg = "操作成功"; result.code = "1"; //数据操作成功则保存数据 db.Commit(); } else { result.msg = "数据未删除"; result.code = "0"; //数据如果未发生改变则回滚 db.Rollback(); } } else { result.msg = "操作成功"; result.code = "1"; //数据操作成功则保存数据 db.Commit(); } } else { result.msg = "数据未删除"; result.code = "0"; //数据如果未发生改变则回滚 db.Rollback(); } } catch (Exception ex) { //数据如果发生错误则回滚 db.Rollback(); result.msg = "数据库错误!"; result.code = "0"; logger.Error("DelBasicCodeMaster 报错", ex); } } return result; } } }