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 ym_yymxDal { public List GetAllList(string id) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from ym_yymx where 1=1"; if (!string.IsNullOrEmpty(id)) { sql += " and id=@id"; } return conn.Query(sql, new { @id = id }).ToList(); } } public List GetSjd(string yyrq) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select sjdid as [key],count(0) as value from ym_yymx where yyrq=@yyrq group by sjdid"; return conn.Query(sql, new { yyrq = yyrq }).ToList(); } } public ym_yymx GeYYListByYyRq(string yyrq,string openid) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { //string sql = "SELECT * FROM (select * from ym_yymx where yyrq BETWEEN @yyrqBign AND @yyrqEnd) m where m.openid =" + openid; // DATEDIFF(day,'"+yyrq+"')" //convert(varchar(10),'" + yyrq + "', 120)" string sql = "select * from ym_yymx where openid='" + openid+ "' and yyrq ='" + yyrq + "'"; return conn.Query(sql).FirstOrDefault(); //return conn.Query(sql).FirstOrDefault(); } } public List GetList(string where) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select a.*,b.mc as sjdmc from ym_yymx a left join ym_yysjd b on a.sjdid=b.id where 1=1"; if (!string.IsNullOrEmpty(where)) { sql += " and " + where; } return conn.Query(sql, new { @where = where }).ToList(); } } public List getYYList(string xm, string yyrq,int page,int pagesize) { string sql = "select a.*,b.mc as sjdmc,row_number() over(order by a.id) as rownum from ym_yymx a join ym_yysjd b on a.sjdid=b.id where 1=1"; if (!string.IsNullOrEmpty(xm)) { sql += " and a.xm=@xm"; } DateTime dyyrq; if (DateTime.TryParse(yyrq, out dyyrq)) { sql += " and a.yyrq=@yyrq"; } 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 { xm, yyrq }).ToList(); } } public int getyyCount(string xm, string yyrq) { //throw new NotImplementedException(); string sql = "select count(1) from ym_yymx where 1=1"; if (!string.IsNullOrEmpty(xm)) { sql += " and xm=@xm"; } DateTime dyyrq; if (DateTime.TryParse(yyrq, out dyyrq)) { sql += " and yyrq=@yyrq"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { xm, yyrq }); } } public object save(ym_yymx model) { string sql = ""; if (model.id == 0) { sql = @"INSERT INTO [dbo].[ym_yymx] ([yyrq] ,[sjdid] ,[yysj] ,[lx] ,[xm] ,[zjhm] ,[lxdh] ,[openid]) VALUES (@yyrq ,@sjdid ,@yysj ,@lx ,@xm ,@zjhm ,@lxdh ,@openid)"; } else { sql = @"UPDATE [dbo].[ym_yymx] SET [yyrq] = @yyrq ,[sjdid] = @sjdid ,[yysj] = @yysj ,[lx] = @lx ,[xm] = @xm ,[zjhm] = @zjhm ,[lxdh] = @lxdh ,[openid] = @openid 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 delete(string id) { string sql = "delete from ym_yymx 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 sql = "select count(1) from dbo.ym_yymx where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and lbmc like @key"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { key = "%" + key + "%" }); } } public List getPage(int page, int pagesize, string key) { string sql = "select *,row_number() over(order by id) as rownum from ym_yymx where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and lbmc like @key"; } 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 + "%" }).ToList(); } } } }