tijian_tieying/web/dccdc.DAL/thdDal.cs

301 lines
10 KiB
C#
Raw Normal View History

2025-02-20 12:14:39 +08:00
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 thdDal
{
public List<thdModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from thd where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
return conn.Query<thdModel>(sql, new { @id = id }).ToList();
}
}
public List<thdModel> GetAllList(string id, string zt)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from thd where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
if (!string.IsNullOrEmpty(zt))
{
sql += " and zt=@zt";
}
return conn.Query<thdModel>(sql, new { @id = id, @zt = zt }).ToList();
}
}
public object save(thdModel model)
{
string sql = "";
if (model.id == 0)
{
sql = @"INSERT INTO [dccdc].[dbo].[thd]
([thdh]
,[thsj]
,[thrid]
,[thr]
,[gysid]
,[gys]
,[thje]
,[thyy])
VALUES
(@thdh
,@thsj
,@thrid
,@thr
,@gysid
,@gys
,@thje
,@thyy)";
}
else
{
sql = @"UPDATE [dccdc].[dbo].[thd]
SET [thdh] = @thdh
,[thsj] = @thsj
,[thrid] = @thrid
,[thr] = @thr
,[gysid] = @gysid
,[gys] = @gys
,[thje] = @thje
,[thyy] = @thyy
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<thdModel> getThdByThdh(string thdh)
{
string sql = "select * from thd where thdh in (@thdh)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<thdModel>(sql, new { thdh = thdh }).ToList();
}
}
public object getThdMxByThdId(string thdid)
{
string sql = "select a.*,b.ggxh,b.jldw dw,(a.sl*a.dj) hj from thdmx a join wz b on a.wzid=b.id where a.thdid in (@thdid)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query(sql, new { thdid = thdid }).ToList();
}
}
public object opSave(string id, string gysid, string gys, string thje, string thyy, string items, int thrid, string thr)
{
string errmsg = "";
string thdh = getthkdh(out errmsg);
if (errmsg != "")
return new { State = 0, Message = "false" };
thdModel model = new thdModel();
model.thdh = thdh;
model.thrid = thrid;
model.thr = thr;
model.thsj = DateTime.Now;
model.gysid = Convert.ToInt32(gysid);
model.gys = gys;
model.thje = Convert.ToDecimal(thje);
model.thyy = thyy;
string sql = @"INSERT INTO [dccdc].[dbo].[thd]
([thdh],[thsj],[thrid],[thr],[gysid],[gys],[thje],[thyy])
VALUES
(@thdh,@thsj,@thrid,@thr,@gysid,@gys,@thje,@thyy)select SCOPE_IDENTITY()";
int thdid = 0;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
thdid = conn.Query<int>(sql, model).FirstOrDefault();
}
catch (Exception ex)
{
return new { State = 0, Message = "false" };
}
}
List<thdmxModel> models = new List<thdmxModel>();
thdmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 5)
{
m = new thdmxModel();
m.thdid = thdid;
m.wzid = Convert.ToInt32(itemss[0]);
m.wzmc = itemss[1];
m.dj = Convert.ToDecimal(itemss[2]);
m.sl = Convert.ToInt32(itemss[3]);
m.sign = Convert.ToInt32(itemss[4]);
m.czsj = DateTime.Now;
models.Add(m);
}
}
string sql2 = @"INSERT INTO [dbo].[thdmx]
([thdid],[wzid],[wzmc],[sl],[dj],[czsj],[sign])
VALUES
(@thdid,@wzid,@wzmc,@sl,@dj,@czsj,@sign)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int result = conn.Execute(sql2, models);
if (result > 0)
{
//退货
th(models, -1);
return new { State = 1, Message = thdh };
}
else
return new { State = 0, Message = "false" };
}
catch (Exception ex)
{
return new { State = 0, Message = "false" };
}
}
}
private void th(List<thdmxModel> models, int multiplier)
{
List<kcModel> kcModels = new List<kcModel>();
kcModel kcModel;
foreach (thdmxModel model in models)
{
kcModel = new kcModel();
kcModel.wzid = model.wzid;
kcModel.wzmc = model.wzmc;
kcModel.sl = model.sl * multiplier; //负数
kcModel.sign = model.sign;
kcModels.Add(kcModel);
}
new kcDal().saveList(kcModels);
}
//获取申请单号
private string getthkdh(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 thdh from thd where thdh >= '" + start + "' and thdh<= '" + 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 thd where id=@id";
string sql2 = "delete from thdmx where thdid=@id";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
//删除之前获取明细
var kclist = new thdmxDal().GetListByParent(id);
int result = conn.Execute(sql, new { id = id });
result = conn.Execute(sql2, new { id = id });
if (result > 0)
{
//退货
th(kclist, 1);
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.thd where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and gysid = @key";
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql, new { key = key });
}
}
public List<thdModel> getPage(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id desc) as rownum from thd where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and gysid = @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.thdModel>(sql, new { key = key }).ToList();
}
}
}
}