using Cksoft.Data; using DllEapEntity.Dtos; using DllEapEntity.Mes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Toolkits; namespace DllEapDal { public class IdealOutputConfigDal { public IDatabase CurrDb; public IdealOutputConfigDal(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); var sql = $"select * from (select row_number() over (order by {sort} {order}) as rowNum,t.* from ({baseSql}) t) tl " + $"where tl.RowNum>={start} and tl.RowNum<{end}"; return CurrDb.FindList(sql); } 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 IdealOutputConfig 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(IdealOutputConfig pro, string userCode, ref string errorinfo) { pro.reccode = userCode; pro.modcode = userCode; pro.rectime = DateTime.Now; pro.modtime = DateTime.Now; var existsSql = $"select * from IdealOutputConfig where FCode='{pro.FCode}'"; var entity = CurrDb.FindList(existsSql).FirstOrDefault(); if (entity != null) { errorinfo = WebErrorMsg.CodeDuplicate; return -1; } var sql = $"insert into IdealOutputConfig(FCode,FName,FLength,Remark,RecCode,recTime,modcode,modTime) values('{pro.FCode}','{pro.FName}'," + $"'{pro.FLength}','{pro.Remark}','{pro.reccode}','{pro.rectime.Value.ToString("yyyy-MM-dd")}','{pro.modcode}','{pro.modtime.Value.ToString("yyyy-MM-dd")}');" + $"select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); return id; } public int Update(IdealOutputConfig role, string userCode, ref string error) { var existsSql = $"select * from IdealOutputConfig where FCode='{role.FCode}' and id<>{role.ID}"; var entity = CurrDb.FindList(existsSql).FirstOrDefault(); if (entity != null) { error = WebErrorMsg.CodeDuplicate; return -1; } role.modcode = userCode; role.modtime = DateTime.Now; if (CurrDb.Update(role) < 0) { error = WebErrorMsg.UpdateFailed; return -1; } return 1; } 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 recName,c.Fname as modname from IdealOutputConfig a left join Staff b on a.reccode=b.FCode left join Staff c on a.modcode=c.FCode " + $"where 1=1 {filter}"; return sql; } public IEnumerable> GetConfigs() { string sql = "select id as value,fname+' | '+convert(varchar(50),flength)+'件/小时' as label from IdealOutputConfig"; return CurrDb.FindList>(sql); } } }