tijian_tieying/web/dccdc.DAL/xmjingfeiDal.cs
2025-02-20 12:14:39 +08:00

166 lines
5.5 KiB
C#

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 xmjingfeiDal
{
public List<xmjingfeiModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from xmjingfei where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
return conn.Query<xmjingfeiModel>(sql, new { @id = id }).ToList();
}
}
public List<xmjingfeiModel> GetAllList(string id, string zt)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from xmjingfei where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
if (!string.IsNullOrEmpty(zt))
{
sql += " and zt=@zt";
}
return conn.Query<xmjingfeiModel>(sql, new { @id = id, @zt = zt }).ToList();
}
}
public List<xmjingfeiModel> GetListByKey(string key, string key1)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from xmjingfei where 1=1 and zt=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and xmmc like @key";
}
if (!string.IsNullOrEmpty(key1))
{
sql += " and lbid = @key1";
}
return conn.Query<xmjingfeiModel>(sql, new { key = "%" + key + "%", key1 = key1 }).ToList();
}
}
public object save(xmjingfeiModel model)
{
string sql = "";
if (model.id == 0)
{
sql = @"INSERT INTO [dbo].[xmjingfei]
([lbid]
,[xmid]
,[xmmc]
,[bkdw]
,[srje]
,[hfje]
,[tjr]
,[tjsj]
,[zt])
VALUES
(@lbid
,@xmid
,@xmmc
,@bkdw
,@srje
,@hfje
,@tjr
,@tjsj
,@zt)";
}
else
{
sql = @"UPDATE [dbo].[xmjingfei]
SET [lbid] = @lbid
,[xmid] = @xmid
,[xmmc] = @xmmc
,[bkdw] = @bkdw
,[tjr] = @tjr
,[tjsj] = @tjsj
,[zt] = @zt
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 xmjingfei 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.xmjingfei where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and xmmc like @key";
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql, new { key = "%" + key + "%" });
}
}
public List<xmjingfeiModel> getPage(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id) as rownum from xmjingfei where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and xmmc 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<Models.xmjingfeiModel>(sql, new { key = "%" + key + "%" }).ToList();
}
}
}
}