using Cksoft.Data; using DllEapEntity.Mes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Toolkits; namespace DllEapDal { public class ProductOutputConfigDal { public IDatabase CurrDb; public ProductOutputConfigDal(IDatabase db) { CurrDb = db; } public IEnumerable Get(int start, int length, string order, string sort, string filter, string errorinfo) { int end = start + length; var baseSql = this.GetSelectSql(filter); return CurrDb.FindList(baseSql).OrderBy(c => c.ProcessCode).OrderBy(c => c.MacCode); } public int GetCount(string filter) { string errorinfo = string.Empty; var entities = CurrDb.FindList(this.GetSelectSql(filter)); if (entities != null) { return entities.Count(); } return 0; } public ProductIdealOutputConfig Get(int id) { string errorinfo = string.Empty; var sql = this.GetSelectSql(string.Empty) + $" and a.id={id}"; var pro = CurrDb.FindList(sql).FirstOrDefault(); return pro; } /// /// 添加角色并返回角色Id /// /// /// /// public int Add(ProductIdealOutputConfig pro, string userCode, ref string errorinfo) { pro.RecCode = userCode; pro.ModCode = userCode; pro.RecTime = DateTime.Now; pro.ModTime = DateTime.Now; var existsSql = $"select * from ProductIdealOutputConfig where ProductCode='{pro.ProductCode}' and MacCode='{pro.MacCode}'"; var entity = CurrDb.FindList(existsSql).FirstOrDefault(); if (entity != null) { errorinfo = "该产品的当前机台已设置理论产量,不能重复设置"; return -1; } var sql = $"insert into ProductIdealOutputConfig(MacModelCode,ProcessCode,ProductCode,ConfigId,Remark,RecCode,recTime,modcode,modTime,MacCode) " + $"values('{pro.MacModelCode}','{pro.ProcessCode}','{pro.ProductCode}'," + $"'{pro.ConfigId}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd")}','{pro.MacCode}');" + $"select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); return id; } public int Update(ProductIdealOutputConfig role, string userCode, ref string error) { var old = CurrDb.FindEntity(role.ID); role.RecCode = old.RecCode; role.RecTime = old.RecTime; role.ModCode = userCode; role.ModTime = DateTime.Now; if (CurrDb.Update(role) < 0) { error = WebErrorMsg.UpdateFailed; return -1; } return 1; } public IdealOutputConfig getIdealOutputConfig(int id) { return CurrDb.FindEntity(id); } public int Delete(int id, ref string msg) { // 判断是否有产品正在使用该程序 string sql = $"select * from ProductIdealOutputConfig where ConfigID='{id}'"; var rule = CurrDb.FindList(sql).FirstOrDefault(); if (rule != null) { msg = "该理想产量信息正在被使用,无法删除"; return -1; } sql = $"delete from IdealOutputConfig where id={id}"; if (CurrDb.ExecuteBySql(sql) < 0) { msg = WebErrorMsg.DeleteFailed; return -1; } msg = string.Empty; return 1; } private string GetSelectSql(string filter) { var sql = @"select a.*,b.FName as macmodelName,c.FName as ProcessName,d.FName as ProductName,e.FName as ConfigName, e.FLength as FLen,f.FName as RecName,g.FName as ModName from ProductIdealOutputConfig a left join MacModel b on a.MacModelCode = b.FCode left join TProcess c on a.ProcessCode=c.FCode left join Part d on a.ProductCode=d.FCode left join IdealOutputConfig e on a.ConfigId=e.id left join Staff f on a.RecCode=f.FCode left join Staff g on a.ModCode=g.FCode " + $"where 1=1 {filter}"; return sql; } } }