using Dapper; using dccdc.Models; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace dccdc.DAL { public class wzDal { public List GetAllList(string id) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from wz where 1=1"; if (!string.IsNullOrEmpty(id)) { sql += " and id=@id"; } return conn.Query(sql, new { @id = id }).ToList(); } } public List GetAllList(string id, string zt) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from wz where 1=1"; if (!string.IsNullOrEmpty(id)) { sql += " and id=@id"; } if (!string.IsNullOrEmpty(zt)) { sql += " and zt=@zt"; } return conn.Query(sql, new { @id = id, @zt = zt }).ToList(); } } public List GetListByKey(string key) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from wz where 1=1 and zt=1"; if (!string.IsNullOrEmpty(key)) { sql += " and mc like @key"; } return conn.Query(sql, new { key = "%" + key + "%" }).ToList(); } } public object getcbhs(string b, string e) { //throw new NotImplementedException(); string sql = @"select a.ksmc ks,d.lbmc xm ,sum(b.sl*b.dj) je from ckd a join ckdmx b on a.id=b.ckdid join wz c on c.id=b.wzid join wzlb d on d.id=c.wzlb where 1=1 "; DateTime db = DateTime.Now; DateTime de = DateTime.Now; if (!string.IsNullOrEmpty(b)) { if (DateTime.TryParse(b, out db)) { sql += " and a.cksj>=@b"; } } if (!string.IsNullOrEmpty(e)) { if (DateTime.TryParse(e, out de)) { sql += " and a.cksj<@e"; } } sql += " group by a.ksmc,d.lbmc"; using (var conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { b = db, e = de.Date.AddDays(1) }); } } public object savegylyr(string id, string xm) { //throw new NotImplementedException(); if(string.IsNullOrEmpty(id)) { id = "0"; } string sql = ""; if(id=="0") { sql = "insert gylyr (xm) values(@xm)"; } else { sql = "update gylyr set xm=@xm where id=@id"; } try { using (var conn = CommHelper.GetSqlConnection()) { conn.Execute(sql, new { id = id, xm = xm }); } return new { State = 1, Message = "保存成功!" }; } catch(Exception ex) { return new { State = 0, Message = ex.Message }; } } public IEnumerable getgylyrlist() { using (var conn = CommHelper.GetSqlConnection()) { return conn.Query("select * from gylyr").ToList(); } } public object getkcpdd(string kcid, string key, string wzlb) { //throw new NotImplementedException(); string sql = ""; if (string.IsNullOrEmpty(kcid)) sql = @"select a.mc,a.ggxh gg,a.jldw dw,a.dj,b.sl,(a.dj*b.sl) je from wz a join kc b on a.id=b.wzid where 1=1 and b.sl>0"; else sql = @"select a.mc,a.ggxh gg,a.jldw dw,a.dj,b.sl,(a.dj*b.sl) je from wz a join kc b on a.id=b.wzid where 1=1 and sign=" + kcid + " and b.sl>0"; if (!string.IsNullOrEmpty(key)) { sql += " and a.mc like @key"; } if (!string.IsNullOrEmpty(wzlb)) { sql += " and a.wzlb=" + wzlb; } using (var conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { key = "%" + key + "%" }); } } public List GetListByKey(string key, string key1) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from wz where 1=1 and zt=1"; if (!string.IsNullOrEmpty(key)) { sql += " and mc like @key"; } if (!string.IsNullOrEmpty(key1)) { sql += " and wzlb = @key1"; } return conn.Query(sql, new { key = "%" + key + "%", key1 = key1 }).ToList(); } } public bool GetBm(string bm) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select count(1) from wz where 1=1 and bm=@bm and zt=1"; int result = conn.ExecuteScalar(sql, new { @bm = bm }); if (result > 0) return true; else return false; } } public int GetMaxBm() { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select max(cast(bm as integer)) from wz where ISNUMERIC(bm)=1 "; int result = conn.ExecuteScalar(sql); return result; } } //根据 名称 规格 生产厂家 计量单位 价格 五个确定唯一 只要有一个不一样就是新的 public int Getone(string mc, string ggxh, string sccj, string jldw, decimal price) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select top 1 id from wz where mc='" + mc + "' and ggxh='" + ggxh + "' and sccj='" + sccj + "' and jldw='" + jldw + "' and dj=" + price; int result = conn.ExecuteScalar(sql); return result; } } public object save(wzModel model) { string sql = ""; if (model.id == 0) { sql = @"INSERT INTO [dbo].[wz] ([bm] ,[mc] ,[ggxh] ,[sccj] ,[jldw] ,[dj] ,[kcsx] ,[kcxx] ,[tjr] ,[tjsj] ,[zt] ,[wzlb]) VALUES (@bm ,@mc ,@ggxh ,@sccj ,@jldw ,@dj ,@kcsx ,@kcxx ,@tjr ,@tjsj ,@zt ,@wzlb)"; } else { sql = @"UPDATE [dbo].[wz] SET [bm] = @bm ,[mc] = @mc ,[ggxh] = @ggxh ,[sccj] = @sccj ,[jldw] = @jldw ,[dj] = @dj ,[kcsx] = @kcsx ,[kcxx] = @kcxx ,[tjr] = @tjr ,[tjsj] = @tjsj ,[zt] = @zt ,[wzlb] = @wzlb WHERE id=@id"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql, model); if (result > 0) return new { State = 1, Message = "保存成功!" }; else return new { State = 0, Message = "保存失败!" }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } public object save2(wzModel model) { string sql = @"INSERT INTO [dbo].[wz] ([bm] ,[mc] ,[ggxh] ,[sccj] ,[jldw] ,[dj] ,[kcsx] ,[kcxx] ,[tjr] ,[tjsj] ,[zt] ,[wzlb]) VALUES (@bm ,@mc ,@ggxh ,@sccj ,@jldw ,@dj ,@kcsx ,@kcxx ,@tjr ,@tjsj ,@zt ,@wzlb) select SCOPE_IDENTITY()"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int id = conn.Query(sql, model).FirstOrDefault(); return new { State = 1, Message = "保存成功!", id = id }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } public object delete(string id) { string sql = "delete from wz where id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql, new { id = id }); if (result > 0) return new { State = 1, Message = "操作成功!" }; else return new { State = 0, Message = "操作失败!" }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } public int getCount(string key, string wzlb) { string sql = "select count(1) from dbo.wz where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and mc like @key"; } if (!string.IsNullOrEmpty(wzlb)) { sql += " and wzlb=@wzlb"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { key = "%" + key + "%", wzlb = wzlb }); } } public List getPage(int page, int pagesize, string key, string wzlb) { string sql = "select *,row_number() over(order by id desc) as rownum from wz where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and mc like @key"; } if (!string.IsNullOrEmpty(wzlb)) { sql += " and wzlb=@wzlb"; } sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { key = "%" + key + "%", wzlb = wzlb }).ToList(); } } public object getwz(string id) { //throw new NotImplementedException(); string sql = "select * from wz where id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { id }).FirstOrDefault(); } } } }