using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; namespace Jingyao.AIEmployee { public sealed partial class PersonalInboxHandler { const string PaymentHome = "ai_供应商付款与核销"; static readonly Dictionary PaymentModes = new Dictionary(StringComparer.Ordinal) { {PaymentHome, "LIST"}, {PaymentHome+"_付款详情", "DETAIL"}, {PaymentHome+"_办理历史", "HISTORY"}, {PaymentHome+"_办理历史_原始回执", "RECEIPT"}, {PaymentHome+"_付款登记_供应商查询", "SUPPLIERS"}, {PaymentHome+"_分次核销_应付单查询", "PAYABLES"}, {PaymentHome+"_本人原请求", "LOOKUP"}, {PaymentHome+"_付款登记_凭证查询","VOUCHERS"}, {PaymentHome+"_付款登记_提交", "REGISTER"}, {PaymentHome+"_付款审批_复核", "REVIEW"}, {PaymentHome+"_分次核销_提交", "ALLOCATE"}, {PaymentHome+"_本人原请求_核对", "INSPECT"}, {PaymentHome+"_本人原请求_关闭", "CLOSE_UNCOMMITTED"}, {PaymentHome+"_本人原请求_收讫", "ACK"} }; static bool IsPaymentJournal(string name) { return PaymentModes.ContainsKey(name); } static bool IsPaymentJournalWrite(string name) { string mode; return PaymentModes.TryGetValue(name,out mode) && (mode=="REGISTER" || mode=="REVIEW" || mode=="ALLOCATE" || mode=="CLOSE_UNCOMMITTED" || mode=="ACK"); } static bool PaymentPaged(string mode) { return mode=="LIST" || mode=="HISTORY" || mode=="SUPPLIERS" || mode=="PAYABLES" || mode=="VOUCHERS"; } static bool PaymentLedger(string mode) { return PaymentPaged(mode) || mode=="DETAIL" || mode=="RECEIPT"; } static void BindPaymentJournal(SqlCommand command,string name,Dictionary inputs,Dictionary body) { string mode=PaymentModes[name]; if (!PaymentPaged(mode) && (body.ContainsKey("pageList") || body.ContainsKey("pageSize"))) throw BadInput(); if (mode=="LOOKUP") { Only(inputs); return; } if (PaymentLedger(mode)) { if (mode=="LIST" || mode=="VOUCHERS") { if (mode=="LIST") { Only(inputs,"状态","供应商流水号","关键字"); AddText(command,inputs,"状态",9,"ALL","ALL","PENDING","CONFIRMED","REJECTED"); } else Only(inputs,"供应商流水号","关键字"); object supplier; if(inputs.TryGetValue("供应商流水号",out supplier) && supplier!=null && !Object.Equals(supplier,"")) command.Parameters.Add("@供应商流水号",SqlDbType.Int).Value=(int)Positive(inputs,"供应商流水号",Int32.MaxValue,0); } else if (mode=="SUPPLIERS") Only(inputs,"关键字"); else { if(mode=="PAYABLES") Only(inputs,"付款流水号","关键字"); else if(mode=="RECEIPT") Only(inputs,"付款流水号","审计流水号"); else Only(inputs,"付款流水号"); command.Parameters.Add("@付款流水号",SqlDbType.BigInt).Value=Positive(inputs,"付款流水号",Int64.MaxValue,0); if(mode=="RECEIPT") command.Parameters.Add("@审计流水号",SqlDbType.BigInt).Value=Positive(inputs,"审计流水号",Int64.MaxValue,0); } if (mode=="LIST" || mode=="SUPPLIERS" || mode=="PAYABLES" || mode=="VOUCHERS") AddText(command,inputs,"关键字",100,""); if (PaymentPaged(mode)) { command.Parameters.Add("@页码",SqlDbType.Int).Value=(int)Positive(body,"pageList",10000,1); command.Parameters.Add("@每页条数",SqlDbType.Int).Value=(int)Positive(body,"pageSize",100,20); } return; } Only(inputs,"原请求"); object raw; if(!inputs.TryGetValue("原请求",out raw) || !(raw is Dictionary)) throw BadInput(); var original=(Dictionary)raw; string actor=Convert.ToString(command.Parameters["@人员编号"].Value,Invariant); try { using (var bound=new SqlCommand()) { if(mode=="ACK") FinancePaymentInboxRequest.BindRecovery(bound,original,actor,true); else if(mode=="INSPECT" || mode=="CLOSE_UNCOMMITTED") { if(Text(original,"mode",17,null)!=mode) throw BadInput(); FinancePaymentInboxRequest.BindResolution(bound,original,actor); } else FinancePaymentInboxRequest.BindWrite(bound,mode,original,actor); foreach(SqlParameter p in bound.Parameters) { if(p.ParameterName=="@操作人" || p.ParameterName=="@模式") continue; command.Parameters.Add((SqlParameter)((ICloneable)p).Clone()); } } } catch(ArgumentException) { throw BadInput(); } } static DataSet PaymentSets(SqlDataReader reader) { var result=new DataSet(); do { if(result.Tables.Count>=3) throw Unavailable(); var table=new DataTable(); for(int i=0;i=200) throw Unavailable(); var values=new object[reader.FieldCount]; reader.GetValues(values); table.Rows.Add(values); } result.Tables.Add(table); } while(reader.NextResult()); return result; } static Dictionary PaymentRow(DataRow row) { var result=new Dictionary(); foreach(DataColumn c in row.Table.Columns) { object v=row[c]; if(v==DBNull.Value) v=null; else if(v is DateTime) v=((DateTime)v).ToString("yyyy-MM-dd HH:mm:ss",Invariant); result.Add(c.ColumnName,v); } return result; } static List> PaymentRows(DataTable table) { var rows=new List>(); foreach(DataRow r in table.Rows) rows.Add(PaymentRow(r)); return rows; } static Dictionary PaymentProof(SqlCommand command,DataTable header,DataTable allocations,bool replay) { if(header.Rows.Count!=1) throw Unavailable(); DataRow r=header.Rows[0]; string actor=Convert.ToString(command.Parameters["@人员编号"].Value,Invariant); string action=(string)command.Parameters["@动作"].Value; if((string)r["动作"]!=action || (string)r["操作人"]!=actor || (string)r["幂等键"]!=(string)command.Parameters["@幂等键"].Value || (string)r["策略版本"]!=FinancePaymentJournalPolicy.Version || (action=="ALLOCATE")!=(allocations.Rows.Count>0) || (replay && !(bool)r["是否幂等返回"])) throw Unavailable(); return new Dictionary {{"record",PaymentRow(r)},{"idempotent",(bool)r["是否幂等返回"]},{"allocations",PaymentRows(allocations)}}; } static object ReadPaymentJournal(SqlCommand command,string name,SqlDataReader reader) { string mode=PaymentModes[name], actor=Convert.ToString(command.Parameters["@人员编号"].Value,Invariant); using(var data=PaymentSets(reader)) { if(data.Tables.Count<2 || data.Tables[0].Rows.Count!=1) throw Unavailable(); DataRow h=data.Tables[0].Rows[0]; if(PaymentLedger(mode)) { if(data.Tables.Count!=3 || (string)h["查询模式"]!=mode || (string)h["策略版本"]!=FinancePaymentJournalPolicy.Version) throw Unavailable(); foreach(string key in new[]{"付款流水号","审计流水号"}) { string expected=command.Parameters.Contains("@"+key)?Convert.ToString(command.Parameters["@"+key].Value,Invariant):""; if(Convert.ToString(h[key],Invariant)!=expected) throw Unavailable(); } long total=Convert.ToInt64(h["总数"],Invariant), page=PaymentPaged(mode)?(int)command.Parameters["@页码"].Value:1, size=PaymentPaged(mode)?(int)command.Parameters["@每页条数"].Value:20; if(total<0 || total>9007199254740991L || data.Tables[1].Rows.Count!=Math.Min(size,Math.Max(0,total-(page-1)*size)) || (!PaymentPaged(mode) && total!=1) || (mode!="RECEIPT" && data.Tables[2].Rows.Count!=0)) throw Unavailable(); return new {rows=PaymentRows(data.Tables[1]),summary=PaymentRow(h),total=total,entries=PaymentRows(data.Tables[2])}; } if(mode=="LOOKUP" || mode=="ACK" || (command.Parameters.Contains("@请求处理") && (string)command.Parameters["@请求处理"].Value=="PREPARE")) { var permissions=new List(); if(mode=="LOOKUP") { if(data.Tables.Count!=3 || data.Tables[2].Rows.Count>6) throw Unavailable(); foreach(DataRow r in data.Tables[2].Rows) permissions.Add((string)r["权限代码"]); data.Tables.RemoveAt(2); } if(mode=="LOOKUP" || mode=="ACK") command.Parameters.Add("@模式",SqlDbType.VarChar,6).Value=mode; var inbox=FinancePaymentInboxRequest.Read(data,actor,command); if(mode=="LOOKUP") inbox.Add("permissions",permissions); return inbox; } if(mode=="INSPECT" || mode=="CLOSE_UNCOMMITTED") { if(data.Tables.Count!=3 || (string)h["请求处理"]!=mode || (string)h["操作人"]!=actor || (string)h["动作"]!=(string)command.Parameters["@动作"].Value || (string)h["幂等键"]!=(string)command.Parameters["@幂等键"].Value || (string)h["策略版本"]!=FinancePaymentJournalPolicy.Version) throw Unavailable(); string state=(string)h["请求状态"]; if(state!="CLOSED" && state!="COMMITTED" && !(state=="ABSENT" && mode=="INSPECT")) throw Unavailable(); var receipt=state=="COMMITTED"?PaymentProof(command,data.Tables[1],data.Tables[2],true):null; if(receipt==null && (data.Tables[1].Rows.Count!=0 || data.Tables[2].Rows.Count!=0)) throw Unavailable(); if(receipt!=null && !Object.Equals(data.Tables[1].Rows[0]["请求摘要"],h["请求摘要"])) throw Unavailable(); if(state=="CLOSED" && (h["关闭流水号"]==DBNull.Value || h["关闭版本"]==DBNull.Value || h["关闭说明"]==DBNull.Value || (mode=="CLOSE_UNCOMMITTED" && !Object.Equals(h["关闭说明"],command.Parameters["@关闭说明"].Value)))) throw Unavailable(); return new {resolution=PaymentRow(h),receipt=receipt}; } if(data.Tables.Count!=2) throw Unavailable(); return PaymentProof(command,data.Tables[0],data.Tables[1],false); } } } }