tijian_tieying/web/dccdc.DAL/charge_refundDal.cs

322 lines
13 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 charge_refundDal
{
public int getCount(string key)
{
string sql = "select count(1) from charge_refund where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and ()";
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.ExecuteScalar<int>(sql, new { area_name = "%" + key + "%" });
}
}
public List<charge_refundModel> getList(int page, int pagesize, string key)
{
string sql = "select *,row_number() over(order by id) as rownum from charge_refund where 1=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and ()";
}
sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<charge_refundModel>(sql, new { area_name = "%" + key + "%" }).ToList();
}
}
public List<charge_refundModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string param = "";
if (id != "")
{
param += " and id=@id";
}
string sql = "select * from charge_refund where 1=1 ";
return conn.Query<charge_refundModel>(sql + param, new { id = id }).ToList();
}
}
public List<charge_refundModel> GetAllList2(string start, string end, string where)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string param = "";
if (where != "")
{
}
string sql = "select * from charge_refund where 1=1 and tfshsj>=@start and tfshsj<=@end and tfzt='审核'";
return conn.Query<charge_refundModel>(sql + param, new { start = start + " 00:00:00", end = end + " 23:59:59", where = where }).ToList();
}
}
public List<charge_refund_detailModel> GetChild(string pid)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string param = "";
if (pid != "")
{
param += " and tfsqid=@pid";
}
string sql = "select * from charge_refund_detail where 1=1 ";
return conn.Query<charge_refund_detailModel>(sql + param, new { pid = pid }).ToList();
}
}
public object save(charge_refundModel ct)
{
string sql = "";
if (ct.id == 0)
{
sql = @"INSERT INTO [dbo].[charge_refund]
([id]
,[sqdid]
,[sfbid]
,[sqrid]
,[sqrxm]
,[sqsj]
,[tfshrid]
,[tfshsj]
,[tfrid]
,[tfsj]
,[tfje]
,[jfrxm]
,[tfzt])
VALUES
(@id
,@sqdid
,@sfbid
,@sqrid
,@sqrxm
,@sqsj
,@tfshrid
,@tfshsj
,@tfrid
,@tfsj
,@tfje
,@jfrxm
,@tfzt)";
}
else
{
sql = @"UPDATE [dbo].[charge_refund]
SET [id] = @id, int,>
,[sqdid] = @sqdid
,[sfbid] = @sfbid
,[sqrid] = @sqrid
,[sqrxm] = @sqrxm
,[sqsj] = @sqsj
,[tfshrid] = @tfshrid
,[tfshsj] = @tfshsj
,[tfrid] = @tfrid
,[tfsj] = @tfsj
,[tfje] = @tfje
,[jfrxm] = @jfrxm
,[tfzt] = @tfzt
WHERE id=@id";
}
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
int c = conn.Execute(sql, ct);
if (c > 0)
{
return new { State = 1, Message = "保存成功!" };
}
else
{
return new { State = 0, Message = "操作失败,请联系管理员!" };
}
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
}
public int saveReturnId(charge_refundModel ct)
{
string sql = @"INSERT INTO [dbo].[charge_refund]
([sqdid]
,[sfbid]
,[sqrid]
,[sqrxm]
,[sqsj]
,[tfshrid]
,[tfshsj]
,[tfrid]
,[tfsj]
,[tfje]
,[jfrxm]
,[tfzt])
VALUES
(@sqdid
,@sfbid
,@sqrid
,@sqrxm
,@sqsj
,@tfshrid
,@tfshsj
,@tfrid
,@tfsj
,@tfje
,@jfrxm
,@tfzt) select scope_identity()";
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
int id = conn.ExecuteScalar<int>(sql,ct);
return id;
}
}
public object refund(string id, ERPUser user)
{
//退费申请主表 明细表
charge_refundModel result1;
List<charge_refund_detailModel> result11;
//收费项目主表明细表
chargeModel result2;
List<charge_detailModel> result22;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
try
{
result1 = conn.Query<charge_refundModel>("select * from charge_refund where 1=1 and id=@id", new { id = id }).FirstOrDefault();
result11 = conn.Query<charge_refund_detailModel>("select a.*,b.chargeid from charge_refund_detail a left join vaccine b on a.ymid=b.id where 1=1 and a.tfsqid=@id", new { id = id }).ToList();
result2 = conn.Query<chargeModel>("select * from charge where 1=1 and id=@id", new { id = result1.sfbid }).FirstOrDefault();
result22 = conn.Query<charge_detailModel>("select * from charge_detail where 1=1 and chargeid=@id", new { id = result1.sfbid }).ToList();
}
catch (Exception ex)
{
return new { State = 0, Message = ex.Message };
}
}
//计算
List<charge_detailModel> result31 = new List<charge_detailModel>();
List<charge_detailModel> result32 = new List<charge_detailModel>();
bool have;
charge_detailModel model2;
foreach (charge_detailModel model in result22)
{
if (model.pid > 0)
continue;
have = false;
foreach (charge_refund_detailModel m in result11)
{
if (model.projectid == m.chargeid)
{
model2 = new charge_detailModel() { id = model.id, chargeid = 0, projectid = model.projectid, projectname = model.projectname, money = model.money, count = -m.ymsl, pid = model.pid };
result31.Add(model2);
if (model.count - m.ymsl > 0)
{
model2 = new charge_detailModel() { id = model.id, chargeid = 0, projectid = model.projectid, projectname = model.projectname, money = model.money, count = model.count - m.ymsl, pid = model.pid };
result32.Add(model2);
}
have = true;
break;
}
}
if (have == false)
{
model2 = new charge_detailModel() { id = model.id, chargeid = 0, projectid = model.projectid, projectname = model.projectname, money = model.money, count = model.count, pid = model.pid };
result32.Add(model2);
}
}
foreach (charge_detailModel model in result31)
{
foreach (charge_detailModel m in result22)
{
if (m.pid == model.id)
{
model2 = new charge_detailModel() { id = m.id, chargeid = 0, projectid = m.projectid, projectname = m.projectname, money = m.money, count = model.count, pid = m.pid };
result31.Add(model2);
}
}
}
foreach (charge_detailModel model in result32)
{
foreach (charge_detailModel m in result22)
{
if (m.pid == model.id)
{
model2 = new charge_detailModel() { id = m.id, chargeid = 0, projectid = m.projectid, projectname = m.projectname, money = m.money, count = model.count, pid = m.pid };
result32.Add(model2);
}
}
}
string sql1 = @"INSERT INTO [dbo].[charge]
([type] ,[type_id],[register_type],[register_num],[person_type],[person] ,[personcount] ,[company],[department],[method],[status] ,[isprint] ,[money] ,[moneydate] ,[moneyperson],[createperson] ,[createdate] ,[zfid])
VALUES
(@type,@type_id ,@register_type,@register_num,@person_type ,@person,@personcount ,@company
,@department ,@method ,@status,@isprint ,@money ,@moneydate,@moneyperson,@createperson ,@createdate,@zfid)select SCOPE_IDENTITY()";
string sql2 = @"INSERT INTO [dbo].[charge_detail]
([chargeid],[projectid] ,[projectname] ,[money] ,[count] ,[pid])
VALUES
(@chargeid ,@projectid,@projectname,@money,@count ,@pid)";
decimal oldprice = result2.money;
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
IDbTransaction transaction = conn.BeginTransaction();
try
{
conn.Execute("update charge_refund set tfrid=@tfrid,tfsj=@tfsj,tfzt='退费' where id=@id", new {id=id, tfrid = user.ID, tfsj = DateTime.Now }, transaction);
//增加退费记录
result2.money = -result1.tfje;
result2.moneydate = DateTime.Now;
result2.moneyperson = user.TrueName;
int newid = conn.Query<int>(sql1, result2, transaction).FirstOrDefault();
foreach (charge_detailModel mm in result31)
{
mm.chargeid = newid;
}
conn.Execute(sql2, result31, transaction);
//添加剩余费用
if (oldprice - result1.tfje > 0)
{
result2.money = oldprice - result1.tfje;
newid = conn.Query<int>(sql1, result2, transaction).FirstOrDefault();
foreach (charge_detailModel mm in result32)
{
mm.chargeid = newid;
}
conn.Execute(sql2, result32, transaction);
}
transaction.Commit();
//transaction.Rollback();
return new { State = 1, Message = "退费成功" };
}
catch (Exception ex)
{
transaction.Rollback();
return new { State = 0, Message = ex.Message };
}
}
}
}
}