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

611 lines
24 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 sqqtbxDal
{
public List<sqqtbxModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
return conn.Query<sqqtbxModel>(sql, new { @id = id }).ToList();
}
}
public List<sqqtbxModel> GetAllList(string id, string zt)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
if (!string.IsNullOrEmpty(zt))
{
sql += " and zt=@zt";
}
return conn.Query<sqqtbxModel>(sql, new { @id = id, @zt = zt }).ToList();
}
}
public List<sqqtbxModel> GetHavedydhs()
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from sqqtbx where 1=1 and dydh != '' and hblx != ''";
return conn.Query<sqqtbxModel>(sql).ToList();
}
}
public object save(sqqtbxModel model)
{
string sql = "";
if (model.id == 0)
{
sql = @"INSERT INTO [dbo].[sqqtbx]
([sqdh]
,[sqsj]
,[sqrid]
,[sqr]
,[ksid]
,[ksmc]
,[je]
,[bz]
,[mx]
,[fyly]
,[fydh]
,[zt]
,[spr1]
,[sptime1]
,[spzt1]
,[spr2]
,[sptime2]
,[spzt2]
,[spr3]
,[sptime3]
,[spzt3]
,[spr4]
,[sptime4]
,[spzt4])
VALUES
(@sqdh
,@sqsj
,@sqrid
,@sqr
,@ksid
,@ksmc
,@je
,@bz
,@mx
,@fyly
,@fydh
,@zt
,@spr1
,@sptime1
,@spzt1
,@spr2
,@sptime2
,@spzt2
,@spr3
,@sptime3
,@spzt3
,@spr4
,@sptime4
,@spzt4)";
}
else
{
sql = @"UPDATE [dbo].[sqqtbx]
SET [sqdh] = @sqdh
,[sqsj] = @sqsj
,[sqrid] = @sqrid
,[sqr] = @sqr
,[ksid] = @ksid
,[ksmc] = @ksmc
,[je] = @je
,[bz] = @bz
,[mx] = @mx
,[fyly] = @fyly
,[fydh] = @fydh
,[zt] = @zt
,[spr1] = @spr1
,[sptime1] = @sptime1
,[spzt1] = @spzt1
,[spr2] = @spr2
,[sptime2] = @sptime2
,[spzt2] = @spzt2
,[spr3] = @spr3
,[sptime3] = @sptime3
,[spzt3] = @spzt3
,[spr4] = @spr4
,[sptime4] = @sptime4
,[spzt4] = @spzt4
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 List<sqqtbxModel> getListBydh(string dh)
{
string sql = "select * from sqqtbx where sqdh in (@dh)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<sqqtbxModel>(sql, new { dh = dh }).ToList();
}
}
public object opSave(string id, int ksid, string ksmc, string items, int sqrid, string sqr, string je, string bz, string fyly1, string fyly2, string fyly, string fydh, string fydhs, string fydh2, string dydh, string hblx, string sqje, int zt)
{
string errmsg = "";
string sqdh = getckkdh(out errmsg);
if (errmsg != "")
return new { State = 0, Message = errmsg };
sqqtbxModel model = new sqqtbxModel();
model.sqdh = sqdh;
model.sqrid = sqrid;
model.sqr = sqr;
model.sqsj = DateTime.Now;
model.tjsj = DateTime.Now;
model.ksid = ksid;
model.ksmc = ksmc;
model.je = Convert.ToDecimal(je);
model.bz = bz;
model.fyly1 = Convert.ToBoolean(fyly1);
model.fyly2 = Convert.ToBoolean(fyly2);
model.fyly = fyly;
model.fydh = fydh;
model.fydhs = fydhs;
model.fydh2 = fydh2;
model.dydh = dydh;
model.hblx = hblx;
model.sqje = Convert.ToDecimal(sqje);
model.zt = zt;
if (zt == 7)
{
model.zt = 6; //需要财务审核 状态是7变成6
}
string sql = @"INSERT INTO [dbo].[sqqtbx]
([sqdh],[sqsj],[tjsj],[sqrid],[sqr],[ksid],[ksmc],[je],[bz],[fyly1],[fyly2],[fyly],[fydh],[fydhs],[fydh2],[dydh],[hblx],[sqje],[zt])
VALUES
(@sqdh,@sqsj,@tjsj,@sqrid,@sqr,@ksid,@ksmc,@je,@bz,@fyly1,@fyly2,@fyly,@fydh,@fydhs,@fydh2,@dydh,@hblx,@sqje,@zt)select SCOPE_IDENTITY()";
int sqdid = 0;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
sqdid = conn.Query<int>(sql, model).FirstOrDefault();
//附件修改
string sqlfj = @"update sqfile set sqid=" + sqdid + " where type='其他报销' and sqid=0 and sqrid=" + sqrid;
conn.Execute(sqlfj);
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
List<sqqtbxmxModel> models = new List<sqqtbxmxModel>();
sqqtbxmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
m = new sqqtbxmxModel();
m.sqdid = sqdid;
m.title = itemss[0];
m.content = itemss[1];
m.sl = Convert.ToDecimal(itemss[2]);
m.je = Convert.ToDecimal(itemss[3]);
m.czsj = DateTime.Now;
models.Add(m);
}
}
string sql2 = @"INSERT INTO [dbo].[sqqtbxmx]
([sqdid],[title],[content],[sl],[je],[czsj])
VALUES
(@sqdid,@title,@content,@sl,@je,@czsj)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int result = conn.Execute(sql2, models);
if (result > 0)
{
return new { State = 1, Message = "操作成功", id = sqdid };
}
else
return new { State = 0, Message = "操作失败" };
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
}
public object opSave2(string id, int ksid, string ksmc, string items, int sqrid, string sqr, string je, string bz, string fyly1, string fyly2, string fyly, string fydh, string fydhs, string fydh2, string dydh, string hblx, string sqje, int zt)
{
sqqtbxModel model = new sqqtbxModel();
model.id = Convert.ToInt32(id);
model.je = Convert.ToDecimal(je);
model.bz = bz;
model.fyly1 = Convert.ToBoolean(fyly1);
model.fyly2 = Convert.ToBoolean(fyly2);
model.fyly = fyly;
model.fydh = fydh;
model.fydhs = fydhs;
model.fydh2 = fydh2;
model.dydh = dydh;
model.hblx = hblx;
model.sqje = Convert.ToDecimal(sqje);
model.zt = zt;
if (zt == 7)
{
model.zt = 6; //需要财务审核 状态是7变成6
}
string sql = @"UPDATE [dbo].[sqqtbx]
SET [je] = @je,[bz] = @bz,[fyly1] = @fyly1,[fyly2] = @fyly2,[fyly] = @fyly,[fydh] = @fydh,[fydhs] = @fydhs,[fydh2] = @fydh2,[dydh] = @dydh,[hblx] = @hblx,[sqje] = @sqje,[zt] = @zt
WHERE id=@id";
string sql3 = "delete from sqqtbxmx where sqdid=@id";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
conn.Execute(sql, model);
conn.Execute(sql3, new { @id = id });
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
List<sqqtbxmxModel> models = new List<sqqtbxmxModel>();
sqqtbxmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
m = new sqqtbxmxModel();
m.sqdid = Convert.ToInt32(id);
m.title = itemss[0];
m.content = itemss[1];
m.sl = Convert.ToDecimal(itemss[2]);
m.je = Convert.ToDecimal(itemss[3]);
m.czsj = DateTime.Now;
models.Add(m);
}
}
string sql2 = @"INSERT INTO [dbo].[sqqtbxmx]
([sqdid],[title],[content],[sl],[je],[czsj])
VALUES
(@sqdid,@title,@content,@sl,@je,@czsj)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int result = conn.Execute(sql2, models);
if (result > 0)
{
return new { State = 1, Message = "操作成功", id = id };
}
else
return new { State = 0, Message = "操作失败" };
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
}
public object opSp(string id, int sprid, string spr, string yj, bool ty)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql2 = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqqtbxModel model = conn.Query<sqqtbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "";
int zt = 0;
switch (model.zt)
{
case 1:
sql = "update sqqtbx set sprid1=@sprid, spr1=@spr,sptime1=@sptime,spnr1=@spnr,zt=@zt where id=@id";
if (ty) { zt = 2; } else { zt = -1; }
break;
case 2:
sql = "update sqqtbx set sprid2=@sprid, spr2=@spr,sptime2=@sptime,spnr2=@spnr,zt=@zt where id=@id";
if (ty) { zt = 6; } else { zt = -2; }
break;
case 6:
sql = "update sqqtbx set sprid3=@sprid, spr3=@spr,sptime3=@sptime,spnr3=@spnr,zt=@zt where id=@id";
if (ty) { zt = 7; } else { zt = -6; }
break;
case 7:
sql = "update sqqtbx set sprid4=@sprid, spr4=@spr,sptime4=@sptime,spnr4=@spnr,zt=@zt where id=@id";
if (ty) { zt = 8; } else { zt = -7; }
break;
}
try
{
int result = conn.Execute(sql, new { @id = id, @sprid = sprid, @spr = spr, @sptime = DateTime.Now, @spnr = yj, @zt = zt });
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 opSpOneself(string id, int sprid, string spr, string yj, bool ty, string zw)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql2 = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqqtbxModel model = conn.Query<sqqtbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "";
switch (zw)
{
case "科室领导":
sql = "update sqqtbx set sprid1=@sprid, spr1=@spr,sptime1=@sptime,spnr1=@spnr where id=@id";
break;
case "分管领导":
sql = "update sqqtbx set sprid2=@sprid, spr2=@spr,sptime2=@sptime,spnr2=@spnr where id=@id";
break;
}
try
{
int result = conn.Execute(sql, new { @id = id, @sprid = sprid, @spr = spr, @sptime = DateTime.Now, @spnr = yj });
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 opZf(string id, int zfrid, string zfr)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql2 = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqqtbxModel model = conn.Query<sqqtbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "update sqqtbx set zfrid=@zfrid, zfr=@zfr,zftime=@zftime where id=@id";
try
{
int result = conn.Execute(sql, new { @id = id, @zfrid = zfrid, @zfr = zfr, @zftime = DateTime.Now });
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 opBx(string id, int bxrid, string bxr)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql2 = "select * from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqqtbxModel model = conn.Query<sqqtbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "update sqqtbx set bxrid=@bxrid, bxr=@bxr,bxtime=@bxtime where id=@id";
try
{
int result = conn.Execute(sql, new { @id = id, @bxrid = bxrid, @bxr = bxr, @bxtime = DateTime.Now });
if (result > 0)
{
return new { State = 1, Message = "修改成功" };
}
else
return new { State = 0, Message = "修改失败" };
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
}
}
//获取申请单号
private string getckkdh(out string errmsg)
{
errmsg = "";
string result = "";
string start = DateTime.Now.ToString("yyyyMM") + "0001";
string end = DateTime.Now.ToString("yyyyMM") + "9999";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select top 1 sqdh from sqqtbx where sqdh >= '" + start + "' and sqdh<= '" + end + "' order by id desc";
try
{
var result2 = conn.ExecuteScalar(sql);
if (result2 == null)
result = start;
else
{
result = (Convert.ToInt32(result2) + 1).ToString();
if (result == end)
errmsg = "单号不足!";
}
}
catch (Exception ex)
{
errmsg = ex.Message;
}
}
return result;
}
public object delete(string id)
{
string sql = "delete from sqqtbx where id=@id";
string sql2 = "delete from sqqtbxmx where sqdid=@id";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int result = conn.Execute(sql, new { id = id });
result = conn.Execute(sql2, 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.sqqtbx where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and ksid = @key";
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql, new { key = key });
}
}
public List<sqqtbxModel> getPage(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id desc) as rownum from sqqtbx where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and ksid = @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.sqqtbxModel>(sql, new { key = key }).ToList();
}
}
public int getCountLevel(string where)
{
string sql = "select count(1) from dbo.sqqtbx";
if (!string.IsNullOrEmpty(where))
{
sql += " where " + where;
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql);
}
}
public List<sqqtbxModel> getPageLevel(int page, int pagesize, string where)
{
string sql = "select *,row_number() over(order by id desc) as rownum from sqqtbx";
if (!string.IsNullOrEmpty(where))
{
sql += " where " + where;
}
sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<Models.sqqtbxModel>(sql).ToList();
}
}
}
}