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

302 lines
10 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 tkdDal
{
public List<tkdModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from tkd where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
return conn.Query<tkdModel>(sql, new { @id = id }).ToList();
}
}
public List<tkdModel> GetAllList(string id, string zt)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from tkd where 1=1";
if (!string.IsNullOrEmpty(id))
{
sql += " and id=@id";
}
if (!string.IsNullOrEmpty(zt))
{
sql += " and zt=@zt";
}
return conn.Query<tkdModel>(sql, new { @id = id, @zt = zt }).ToList();
}
}
public object save(tkdModel model)
{
string sql = "";
if (model.id == 0)
{
sql = @"INSERT INTO [dbo].[tkd]
([tkdh]
,[tksj]
,[tkrid]
,[tkr]
,[ksid]
,[ksmc]
,[tkje]
,[tkry])
VALUES
(@tkdh
,@tksj
,@tkrid
,@tkr
,@ksid
,@ksmc
,@tkje
,@tkry)";
}
else
{
sql = @"UPDATE [dbo].[tkd]
SET [tkdh] = @tkdh
,[tksj] = @tksj
,[tkrid] = @tkrid
,[tkr] = @tkr
,[ksid] = @ksid
,[ksmc] = @ksmc
,[tkje] = @tkje
,[tkry] = @tkry
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 opSave(string id, string ksid, string ksmc, string tkry, string sign, string items, int tkrid, string tkr,string tkje)
{
string errmsg = "";
string tkdh = gettkkdh(out errmsg);
if (errmsg != "")
return new { State = 0, Message = "false" };
tkdModel model = new tkdModel();
model.tkdh = tkdh;
model.tkrid = tkrid;
model.tkr = tkr;
model.tksj = DateTime.Now;
model.ksid = Convert.ToInt32(ksid);
model.ksmc = ksmc;
model.tkje = Convert.ToDecimal(tkje);
model.tkry = tkry;
model.sign = Convert.ToInt32(sign);
string sql = @"INSERT INTO [dbo].[tkd]
([tkdh] ,[tksj],[tkrid],[tkr] ,[ksid],[ksmc],[tkje],[tkry],[sign])
VALUES
(@tkdh,@tksj ,@tkrid ,@tkr ,@ksid,@ksmc,@tkje ,@tkry,@sign)select SCOPE_IDENTITY()";
int tkdid = 0;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
tkdid = conn.Query<int>(sql, model).FirstOrDefault();
}
catch (Exception ex)
{
return new { State = 0, Message = "false" };
}
}
List<tkdmxModel> models = new List<tkdmxModel>();
tkdmxModel m;
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
m = new tkdmxModel();
m.tkdid = tkdid;
m.wzid = Convert.ToInt32(itemss[0]);
m.wzmc = itemss[1];
m.dj = Convert.ToDecimal(itemss[2]);
m.sl = Convert.ToInt32(itemss[3]);
m.czsj = DateTime.Now;
m.sign = Convert.ToInt32(sign);
models.Add(m);
}
}
string sql2 = @"INSERT INTO [dbo].[tkdmx]
([tkdid],[wzid],[wzmc],[sl],[dj],[czsj],[sign])
VALUES
(@tkdid,@wzid,@wzmc,@sl,@dj,@czsj,@sign)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int result = conn.Execute(sql2, models);
if (result > 0)
{
//退货
tk(models, 1);
return new { State = 1, Message = tkdh };
}
else
return new { State = 0, Message = "false" };
}
catch (Exception ex)
{
return new { State = 0, Message = "false" };
}
}
}
private void tk(List<tkdmxModel> models, int multiplier)
{
List<kcModel> kcModels = new List<kcModel>();
kcModel kcModel;
foreach (tkdmxModel 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 gettkkdh(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 tkdh from tkd where tkdh >= '" + start + "' and tkdh<= '" + 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 List<tkdModel> getTkdByThdh(string tkdh)
{
string sql = "select * from tkd where tkdh in (@tkdh)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<tkdModel>(sql, new { tkdh = tkdh }).ToList();
}
}
public object getTkdMxByTkdId(string tkdid)
{
string sql = "select a.*,b.ggxh,b.jldw dw,(a.sl*a.dj) hj from tkdmx a join wz b on a.wzid=b.id where a.tkdid in (@tkdid)";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query(sql, new { tkdid = tkdid }).ToList();
}
}
public object delete(string id)
{
string sql = "delete from tkd where id=@id";
string sql2 = "delete from tkdmx where tkdid=@id";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
//删除之前获取明细
var kclist = new tkdmxDal().GetListByParent(id);
int result = conn.Execute(sql, new { id = id });
result = conn.Execute(sql2, new { id = id });
if (result > 0)
{
//退货
tk(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.tkd 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<tkdModel> getPage(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id desc) as rownum from tkd 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.tkdModel>(sql, new { key = key }).ToList();
}
}
}
}