using System; using System.Collections.Generic; using System.Linq; using System.Text; 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; } public bool Add(Vouch vouch) { string accid = NX.ERP.U8.U8LoginContext.Instance.AccID; var conn = new SqlConnection(this._connStr); conn.Open(); var trans = conn.BeginTransaction(); try { //获取ID var idEnity = new IdEnity { cAcc_Id = accid,//TODO cVouchType = "mv", iAmount = vouch.Vouchs.Count() }; DynamicParameters sp = new DynamicParameters(idEnity); sp.Add("@iFatherId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output,0); sp.Add("@iChildId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output,0); conn.Execute("sp_GetID", sp, trans, commandType: System.Data.CommandType.StoredProcedure); int iFatherId = sp.Get("@iFatherId"); int iChildId = sp.Get("@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(sqlMax, new { cSeed }, trans,200,System.Data.CommandType.Text); 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(sqlPrefix, null, trans,200,System.Data.CommandType.Text); //组装Code var cCode = prefix + cSeed.Substring(2, cSeed.Length - 2) + maxCode.ToString().PadLeft(5, '0'); //主表 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)"; vouch.ID = iFatherId; vouch.cDefine12 = vouch.CCode; 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)"; //明细 var n = vouch.Vouchs.Count() - 1; foreach (var item in vouch.Vouchs) { item.autoid = iChildId - n; item.id = iFatherId; n--; item.IRowNO = n; } conn.Execute(sql, vouch.Vouchs, trans); trans.Commit(); } catch (Exception err) { trans.Rollback(); throw err; } finally { conn.Close(); } return true; } } }