Files
U8-Import/U8AppMain/Voucher/VoucherDAL.cs

119 lines
4.0 KiB
C#
Raw Normal View History

2020-12-19 15:30:38 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
namespace U8AppMain.Voucher
{
public class IdEnity
{
public string cAcc_Id { get; set; }
public string cVouchType { get; set; }
public int iAmount { get; set; }
//public int iFatherId { get; set; }
//public int iChildId { get; set; }
public bool bEnableNewRule { get; set; } = true;
}
public class VoucherDAL
{
private string _connStr;
public VoucherDAL(string connStr)
{
_connStr = connStr;
}
2020-12-19 16:33:33 +08:00
public bool Add(Vouch vouch)
2020-12-19 15:30:38 +08:00
{
var conn = new SqlConnection(this._connStr);
conn.Open();
var trans = conn.BeginTransaction();
try
{
//获取ID
2020-12-19 16:33:33 +08:00
var idEnity = new IdEnity
{
cAcc_Id = "003",
cVouchType = "mv",
iAmount = vouch.Vouchs.Count()
};
2020-12-19 15:30:38 +08:00
DynamicParameters sp = new DynamicParameters(idEnity);
sp.Add("@iFatherId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output);
sp.Add("@iChildId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output);
conn.Execute("sp_GetID", sp, trans, commandType: System.Data.CommandType.StoredProcedure);
int iFatherId = sp.Get<int>("@iFatherId");
int iChildId = sp.Get<int>("@iChildId");
//获取最大值
var cSeed = DateTime.Today.ToString("yyyyMM");
var sqlMax = $@"select cNumber as Maxnumber From VoucherHistory with (ROWLOCK) Where CardNumber='0413' and cContent='日期' and cSeed=@cSeed";
var maxCode = conn.ExecuteScalar<int>(sqlMax, new { cSeed }, trans);
maxCode++;
//更新最大之
var sqlUpdate = "update VoucherHistory set cNumber=@cNumber Where CardNumber='0413' and cContent='日期' and cSeed=cSeed ";
conn.Execute(sqlUpdate, new { cNumber = maxCode, cSeed }, trans);
//获取前缀
var sqlPrefix = " Select Prefix1Rule From VoucherNumber Where CardNumber = '0413'";
var prefix = conn.ExecuteScalar<string>(sqlPrefix, null, trans);
//组装Code
2020-12-25 11:07:27 +08:00
var cCode = prefix + cSeed.Substring(2, cSeed.Length - 2) + maxCode.ToString().PadLeft(5, '0');
2020-12-19 15:30:38 +08:00
//主表
2020-12-19 16:33:33 +08:00
var sql = @"insert into MaterialAppVouch(id,ddate,ccode,crdcode,cdepcode,cpersoncode,cmaker,vt_id,iswfcontrolled,dnmaketime,cDefine12)
values(@id,@ddate,@ccode,@crdcode,@cdepcode,@cpersoncode,@cmaker,@vt_id,@iswfcontrolled, getdate(),@cDefine12)";
2020-12-19 15:30:38 +08:00
vouch.ID = iFatherId;
2020-12-19 16:33:33 +08:00
vouch.cDefine12 = vouch.CCode;
2020-12-19 15:30:38 +08:00
vouch.CCode = cCode;
conn.Execute(sql, vouch, trans);
sql = @"INSERT INTO MaterialAppVouchs (autoid, id, cinvcode, inum, iquantity , cwhcode, iexpiratdatecalcu, irowno, iordertype, isotype , ipesotype)
VALUES (@autoid, @id, @cinvcode, @inum, @iquantity , @cwhcode, @iexpiratdatecalcu, @irowno, @iordertype, @isotype , @ipesotype)";
//明细
2020-12-25 11:07:27 +08:00
var n = vouch.Vouchs.Count() - 1;
2020-12-19 15:30:38 +08:00
foreach (var item in vouch.Vouchs)
{
item.autoid = iChildId - n;
2020-12-25 11:07:27 +08:00
item.id = iFatherId;
2020-12-19 15:30:38 +08:00
n--;
item.IRowNO = n;
}
conn.Execute(sql, vouch.Vouchs, trans);
trans.Commit();
}
2020-12-25 11:07:27 +08:00
catch (Exception err)
2020-12-19 15:30:38 +08:00
{
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
trans.Rollback();
throw err;
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
}
finally
{
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
conn.Close();
}
2020-12-19 16:33:33 +08:00
return true;
2020-12-19 15:30:38 +08:00
}
}
}