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

619 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 sqwzcgbxDal
{
public List<sqwzcgbxModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
return conn.Query<sqwzcgbxModel>(sql, new { @id = id }).ToList();
}
}
public List<sqwzcgbxModel> GetAllList(string id, string zt)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
if (!string.IsNullOrEmpty(zt))
{
sql += " and zt=@zt";
}
return conn.Query<sqwzcgbxModel>(sql, new { @id = id, @zt = zt }).ToList();
}
}
public string GetHavedydhs()
{
string sql = " select stuff((select ','+dydh from sqwzcgbx where dydh is not null and dydh != '' for xml path ('')),1,1,'') as haveids";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<string>(sql);
}
}
public string GetHavedydh2s()
{
string sql = " select stuff((select ','+dydh2 from sqwzcgbx where dydh2 is not null and dydh2 != '' for xml path ('')),1,1,'') as haveids";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<string>(sql);
}
}
public object save(sqwzcgbxModel model)
{
string sql = "";
if (model.id == 0)
{
sql = @"INSERT INTO [dbo].[sqwzcgbx]
([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].[sqwzcgbx]
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<sqwzcgbxModel> getListBydh(string dh)
{
string sql = "select * from sqwzcgbx where sqdh in (@dh)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<sqwzcgbxModel>(sql, new { dh = dh }).ToList();
}
}
public object opSave(string id, int ksid, string ksmc, string items, int sqrid, string sqr, string je, string mx, string bz, string fyly, string fydh, string fydhs, string fydh2, string dydh, string dydh2, string sqje, int zt)
{
string errmsg = "";
string sqdh = getckkdh(out errmsg);
if (errmsg != "")
return new { State = 0, Message = errmsg };
sqwzcgbxModel model = new sqwzcgbxModel();
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.mx = mx;
model.bz = bz;
model.fyly = fyly;
model.fydh = fydh;
model.fydhs = fydhs;
model.fydh2 = fydh2;
model.dydh = dydh;
model.dydh2 = dydh2;
model.sqje = Convert.ToDecimal(sqje);
model.zt = zt;
if (zt == 7)
{
model.zt = 6; //需要财务审核 状态是7变成6
}
string sql = @"INSERT INTO [dbo].[sqwzcgbx]
([sqdh],[sqsj],[tjsj],[sqrid],[sqr],[ksid],[ksmc],[je],[mx],[bz],[fyly],[fydh],[fydhs],[fydh2],[dydh],[dydh2],[sqje],[zt])
VALUES
(@sqdh,@sqsj,@tjsj,@sqrid,@sqr,@ksid,@ksmc,@je,@mx,@bz,@fyly,@fydh,@fydhs,@fydh2,@dydh,@dydh2,@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<sqwzcgbxmxModel> models = new List<sqwzcgbxmxModel>();
sqwzcgbxmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
m = new sqwzcgbxmxModel();
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].[sqwzcgbxmx]
([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 mx, string bz, string fyly, string fydh, string fydhs, string fydh2, string dydh, string dydh2, string sqje, int zt)
{
sqwzcgbxModel model = new sqwzcgbxModel();
model.id = Convert.ToInt32(id);
model.je = Convert.ToDecimal(je);
model.mx = mx;
model.bz = bz;
model.fyly = fyly;
model.fydh = fydh;
model.fydhs = fydhs;
model.fydh2 = fydh2;
model.dydh = dydh;
model.dydh2 = dydh2;
model.sqje = Convert.ToDecimal(sqje);
model.zt = zt;
if (zt == 7)
{
model.zt = 6; //需要财务审核 状态是7变成6
}
string sql = @"UPDATE [dbo].[sqwzcgbx]
SET [je] = @je,[mx] = @mx,[bz] = @bz,[fyly] = @fyly,[fydh] = @fydh,[fydhs] = @fydhs,[fydh2] = @fydh2,[dydh] = @dydh,[dydh2] = @dydh2,[sqje] = @sqje,[zt] = @zt
WHERE id=@id";
string sql3 = "delete from sqwzcgbxmx 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<sqwzcgbxmxModel> models = new List<sqwzcgbxmxModel>();
sqwzcgbxmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
m = new sqwzcgbxmxModel();
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].[sqwzcgbxmx]
([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 sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqwzcgbxModel model = conn.Query<sqwzcgbxModel>(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 sqwzcgbx 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 sqwzcgbx 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 sqwzcgbx 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 sqwzcgbx 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 sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqwzcgbxModel model = conn.Query<sqwzcgbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "";
switch (zw)
{
case "科室领导":
sql = "update sqwzcgbx set sprid1=@sprid, spr1=@spr,sptime1=@sptime,spnr1=@spnr where id=@id";
break;
case "分管领导":
sql = "update sqwzcgbx 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 sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqwzcgbxModel model = conn.Query<sqwzcgbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "update sqwzcgbx 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 sqwzcgbx where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql2 += " and id=@id";
}
sqwzcgbxModel model = conn.Query<sqwzcgbxModel>(sql2, new { @id = id }).FirstOrDefault();
if (model == null)
return new { State = 0, Message = "不存在记录" };
else
{
string sql = "update sqwzcgbx 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 sqwzcgbx 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 sqwzcgbx where id=@id";
string sql2 = "delete from sqwzcgbxmx 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.sqwzcgbx 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<sqwzcgbxModel> getPage(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id desc) as rownum from sqwzcgbx 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.sqwzcgbxModel>(sql, new { key = key }).ToList();
}
}
public int getCountLevel(string where)
{
string sql = "select count(1) from dbo.sqwzcgbx";
if (!string.IsNullOrEmpty(where))
{
sql += " where " + where;
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql);
}
}
public List<sqwzcgbxModel> getPageLevel(int page, int pagesize, string where)
{
string sql = "select *,row_number() over(order by id desc) as rownum from sqwzcgbx";
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.sqwzcgbxModel>(sql).ToList();
}
}
}
}