123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DllEapDal
- {
- public class SecDal
- {
- private IDatabase CurrDb = null;
- public SecDal(IDatabase db)
- {
- CurrDb = db;
- }
- public Sec IUSec(Sec mst, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- int id = mst.ID;
- if (mst.EntityStatusID == 1)
- {
- string code = GetSecCode(mst.FType, ref errorinfo);
- if (code == "")
- {
- return null;
- }
- mst.FCode = code;
- mst.RecCode = usercode;
- mst.RecTime = DateTime.Now;
- mst.ModCode = usercode;
- mst.ModTime = DateTime.Now;
- result = CurrDb.InsertFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- id = int.Parse(objid.ToString());
- }
- else
- {
- mst.ModCode = usercode;
- mst.ModTime = DateTime.Now;
- result = CurrDb.UpdateFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- }
- mst = CurrDb.FindEntityFor<Sec>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private string GetSecCode(int ftype, ref string errorinfo)
- {
- try
- {
- StringBuilder sqlstr = new StringBuilder(100);
- string code = "";
- switch (ftype)
- {
- case 1:
- code = "S";
- break;
- case 2:
- code = "E";
- break;
- case 3:
- code = "C";
- break;
- default:
- code = "F";
- break;
- }
- sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code);
- object obj = CurrDb.FindObject(sqlstr.ToString());
- if (obj.ToString() == "")
- return code + "00001";
- int fnum = int.Parse(obj.ToString().Substring(1, 5));
- fnum++;
- return code + fnum.ToString().PadLeft(5, '0');
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- #region WEB
- public IEnumerable<Sec> GetSecs(string filter, ref string errorinfo)
- {
- return CurrDb.FindListForCondition<Sec>(filter, ref errorinfo);
- }
- public IEnumerable<Sec> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var pros = CurrDb.FindListForCondition<Sec>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
- return pros;
- }
- public int GetCount(string filter)
- {
- string errorinfo = string.Empty;
- var entities = CurrDb.FindListForCondition<Sec>(filter, ref errorinfo);
- if (entities != null)
- {
- return entities.Count();
- }
- return 0;
- }
- public Sec Get(int id)
- {
- string errorinfo = string.Empty;
- var pro = CurrDb.FindEntityFor<Sec>(id);
- return pro;
- }
- /// <summary>
- /// 添加角色并返回角色Id
- /// </summary>
- /// <param name="role"></param>
- /// <param name="userCode"></param>
- /// <returns></returns>
- public int Add(Sec pro, string userCode, ref string errorinfo)
- {
- pro.RecCode = pro.ModCode = userCode;
- pro.RecTime = pro.ModTime = DateTime.Now;
- var entity = CurrDb.FindListForCondition<Sec>($" and a.FName ='{pro.FName}' ", ref errorinfo).FirstOrDefault();
- if (entity != null)
- {
- errorinfo = "参数名已存在,请修改后重新添加";
- return -1;
- }
- var fCode = string.Empty;
- fCode = this.GetSecCode(pro.FType, ref errorinfo);
- if (string.IsNullOrEmpty(fCode))
- {
- return -1;
- }
- string sql = $"insert into Sec(FCode,FName,DCode,FType,FVal,Remark,RecCode,RecTime,ModCode,ModTime) values('{fCode}','{pro.FName}'," +
- $"'{pro.DCode}','{pro.FType}','{pro.FVal}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}');";
- sql += "select @@identity;";
- var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
- errorinfo = "";
- return id;
- }
- public int Update(Sec role, string userCode, ref string error)
- {
- var entity = CurrDb.FindListForCondition<Sec>($" and a.FName ='{role.FName}' and a.id <> '{role.ID}'", ref error).FirstOrDefault();
- if (entity != null)
- {
- error = "参数名已存在,请确认";
- return -1;
- }
- if (CurrDb.UpdateFor(role, userCode) > 0)
- {
- return role.ID;
- }
- return -1;
- }
- public int Delete(int id, ref string msg)
- {
- string sql = $"select * from orderevent where preid='{id}'";
- var events = CurrDb.FindList<OrderEvent>(sql);
- if (events != null && events.Count() > 0)
- {
- msg = "该参数已有机型在使用,不能删除";
- return -1;
- }
- var secDetails = CurrDb.FindListForCondition<MMSecDetail>($" and a.SecID='{id}'",ref msg);
- if (secDetails != null && secDetails.Count() > 0)
- {
- msg = "该参数已有机型在使用,不能删除";
- return -1;
- }
- var reportParams = CurrDb.FindListForCondition<ReportDetail>($" and a.Secid={id}", ref msg);
- if (reportParams != null && reportParams.Count() > 0)
- {
- msg = "该参数已有机型在使用,不能删除";
- return -1;
- }
- if (CurrDb.DeleteFor<Sec>(id) < 0)
- {
- msg = "删除程序主表时出错,请联系管理员";
- return -1;
- }
- msg = string.Empty;
- return 1;
- }
- #endregion
- }
- }
|