123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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<Supplier> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var pros = CurrDb.FindListForCondition<Supplier>($" {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<Supplier>(filter, ref errorinfo);
- if (entities != null)
- {
- return entities.Count();
- }
- return 0;
- }
- public Supplier Get(int id)
- {
- var pro = CurrDb.FindEntityFor<Supplier>(id);
- return pro;
- }
- /// <summary>
- /// 添加角色并返回角色Id
- /// </summary>
- /// <param name="role"></param>
- /// <param name="userCode"></param>
- /// <returns></returns>
- public int Add(Supplier pro, string userCode, ref string errorinfo)
- {
- var entities = CurrDb.FindListForCondition<Supplier>($" 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<string>(sql).FirstOrDefault() ?? "-1");
- return id;
- }
- /// <summary>
- /// 待优化 存在删除后不能
- /// </summary>
- /// <param name="role"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int Update(Supplier role, string userCode, ref string errorinfo)
- {
- var entities = CurrDb.FindListForCondition<Supplier>($" 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<MacModel>(sql);
- if (macmodels != null && macmodels.Count() > 0)
- {
- msg = "该厂家有关联的机型未删除,请先删除机型";
- return -1;
- }
- if (CurrDb.DeleteFor<Supplier>(id) < 0)
- {
- msg = "删除失败";
- return -1;
- }
- msg = string.Empty;
- return 1;
- }
- public IEnumerable<Supplier> Get(string filter)
- {
- string error = string.Empty;
- return CurrDb.FindListForCondition<Supplier>(filter, ref error);
- }
- public Supplier AddOrGet(Supplier supplier, string userCode, ref string errorinfo)
- {
- var exist = CurrDb.FindListForCondition<Supplier>($" 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<Supplier>($" and a.FCode='{supplier.FCode}'", ref errorinfo)
- .FirstOrDefault();
- return exist;
- }
- }
- }
|