using Cksoft.Data; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapDal { public class SupplierDal { private IDatabase CurrDb; public SupplierDal(IDatabase db) { CurrDb = db; } public IEnumerable Get(int start, int length, string order, string sort, string filter, string errorinfo) { var pros = CurrDb.FindListForCondition($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo); return pros; } public int GetCount(string filter) { string errorinfo = string.Empty; var entities = CurrDb.FindListForCondition(filter, ref errorinfo); if (entities != null) { return entities.Count(); } return 0; } public Supplier Get(int id) { var pro = CurrDb.FindEntityFor(id); return pro; } /// /// 添加角色并返回角色Id /// /// /// /// public int Add(Supplier pro, string userCode, ref string errorinfo) { var entities = CurrDb.FindListForCondition($" and a.FCode='{pro.FCode}' ", ref errorinfo); if (entities != null && entities.Count() > 0) { errorinfo = "厂家已存在,请确认"; return -1; } pro.RecCode = userCode; pro.ModCode = userCode; pro.RecTime = DateTime.Now; pro.ModTime = pro.RecTime; string sql = $"insert into Supplier(FCode,FName,Remark,RecCode,RecTime," + $"ModCode,ModTime) values('{pro.FCode}','{pro.FName}','{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(sql).FirstOrDefault() ?? "-1"); return id; } /// /// 待优化 存在删除后不能 /// /// /// /// /// public int Update(Supplier role, string userCode, ref string errorinfo) { var entities = CurrDb.FindListForCondition($" and a.FCode='{role.FCode}' " + $"and a.ID<>{role.ID}", ref errorinfo); if (entities != null && entities.Count() > 0) { errorinfo = "已存在相同的厂家,请确认"; return -1; } if (CurrDb.UpdateFor(role, userCode) < 0) { return -1; } return role.ID; } public int Delete(int id, ref string msg) { string sql = $"select * from MacModel where SupplierID={id}"; var macmodels = CurrDb.FindList(sql); if (macmodels != null && macmodels.Count() > 0) { msg = "该厂家有关联的机型未删除,请先删除机型"; return -1; } if (CurrDb.DeleteFor(id) < 0) { msg = "删除失败"; return -1; } msg = string.Empty; return 1; } public IEnumerable Get(string filter) { string error = string.Empty; return CurrDb.FindListForCondition(filter, ref error); } public Supplier AddOrGet(Supplier supplier, string userCode, ref string errorinfo) { var exist = CurrDb.FindListForCondition($" and a.FCode='{supplier.FCode}'", ref errorinfo) .FirstOrDefault(); if (exist != null) return exist; var entity = new Supplier { FCode = supplier.FCode, FName = supplier.FName }; if (CurrDb.InsertFor(entity, userCode) < 0) { return null; } exist = CurrDb.FindListForCondition($" and a.FCode='{supplier.FCode}'", ref errorinfo) .FirstOrDefault(); return exist; } } }