合并两个工位的 ERP 页面、存储过程及测试工具,分离 8010 与 9528 工位配置
This commit is contained in:
288
server/AIEmployee/App_Code/PerformanceSettlement.cs
Normal file
288
server/AIEmployee/App_Code/PerformanceSettlement.cs
Normal file
@@ -0,0 +1,288 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Jingyao.AIEmployee
|
||||
{
|
||||
public sealed partial class PersonalInboxHandler
|
||||
{
|
||||
const string MonthlyReport = "ai_月度业绩结算";
|
||||
const string MonthlyBatches = "ai_月度业绩结算_结算批次";
|
||||
const string MonthlyDetail = "ai_月度业绩结算_结算批次_详情";
|
||||
const string SettlementOutputAccess = "ai_月度业绩结算_合同清单_输出权限";
|
||||
const string SettlementCopy = "ai_月度业绩结算_合同清单_复制";
|
||||
const string SettlementDownload = "ai_月度业绩结算_合同清单_下载";
|
||||
protected override int ResponseLimit(Dictionary<string,object> body)
|
||||
{
|
||||
object name;
|
||||
if (body.TryGetValue("name", out name) && (Object.Equals(name, SettlementCopy) || Object.Equals(name, SettlementDownload)))
|
||||
return 52 * 1024 * 1024;
|
||||
if (Object.Equals(name,QuotePictureContent)) return 8 * 1024 * 1024;
|
||||
if (Object.Equals(name,QuoteDetailPage)) return 52 * 1024 * 1024;
|
||||
return base.ResponseLimit(body);
|
||||
}
|
||||
const string LegacyPropose = "ai_月度业绩结算_历史合同_提交补录";
|
||||
const string LegacyReview = "ai_月度业绩结算_历史合同_复核补录";
|
||||
const string FormalPropose = "ai_月度业绩结算_正式合同_提交补录";
|
||||
const string FormalReview = "ai_月度业绩结算_正式合同_复核补录";
|
||||
const string BatchDraft = "ai_月度业绩结算_结算批次_创建草稿";
|
||||
const string BatchSubmit = "ai_月度业绩结算_结算批次_提交";
|
||||
const string BatchReview = "ai_月度业绩结算_结算批次_复核";
|
||||
const string BatchId = "月度结算批次流水号";
|
||||
|
||||
static bool IsSettlementWrite(string name)
|
||||
{
|
||||
return name == LegacyPropose || name == LegacyReview || name == FormalPropose ||
|
||||
name == FormalReview || name == BatchDraft || name == BatchSubmit || name == BatchReview;
|
||||
}
|
||||
|
||||
static bool IsSettlement(string name)
|
||||
{
|
||||
return name == MonthlyReport || name == MonthlyBatches || name == MonthlyDetail || IsSettlementWrite(name) ||
|
||||
name == SettlementOutputAccess || name == SettlementCopy || name == SettlementDownload;
|
||||
}
|
||||
|
||||
static void SettlementMonth(SqlCommand command, Dictionary<string,object> inputs, bool required)
|
||||
{
|
||||
string value = Text(inputs, "归集月份", 7, required ? null : "");
|
||||
DateTime date;
|
||||
if ((required || value.Length > 0) && !DateTime.TryParseExact(value + "-01", "yyyy-MM-dd", Invariant, DateTimeStyles.None, out date)) throw BadInput();
|
||||
command.Parameters.Add("@归集月份", SqlDbType.Char, 7).Value = value;
|
||||
}
|
||||
|
||||
static void SettlementId(SqlCommand command, Dictionary<string,object> inputs, string field)
|
||||
{
|
||||
bool small = field == "历史项目流水号";
|
||||
long value = Positive(inputs, field, small ? Int32.MaxValue : Int64.MaxValue, 0);
|
||||
command.Parameters.Add("@" + field, small ? SqlDbType.Int : SqlDbType.BigInt).Value = value;
|
||||
}
|
||||
|
||||
static void AddVersion(SqlCommand command, Dictionary<string,object> inputs, string field)
|
||||
{
|
||||
string version = Text(inputs, field, 16, null);
|
||||
if (!Regex.IsMatch(version, @"\A[0-9a-fA-F]{16}\z")) throw BadInput();
|
||||
byte[] bytes = new byte[8];
|
||||
for (int i = 0; i < bytes.Length; i++) bytes[i] = Byte.Parse(version.Substring(i * 2, 2), NumberStyles.HexNumber, Invariant);
|
||||
command.Parameters.Add("@" + field, SqlDbType.Binary, 8).Value = bytes;
|
||||
}
|
||||
|
||||
static void SettlementAdjustment(SqlCommand command, Dictionary<string,object> inputs)
|
||||
{
|
||||
object raw;
|
||||
decimal amount = 0m;
|
||||
if (inputs.TryGetValue("调整金额", out raw)) {
|
||||
string number = Convert.ToString(raw, Invariant);
|
||||
if (!Decimal.TryParse(number, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, Invariant, out amount) ||
|
||||
Decimal.Round(amount, 2) != amount || amount <= -10000000000000000m || amount >= 10000000000000000m) throw BadInput();
|
||||
}
|
||||
SqlParameter param = command.Parameters.Add("@调整金额", SqlDbType.Decimal);
|
||||
param.Precision = 18; param.Scale = 2; param.Value = amount;
|
||||
AddText(command, inputs, "调整原因", 500, "");
|
||||
AddText(command, inputs, "依据引用", 500, "");
|
||||
}
|
||||
|
||||
// 给已验证的、固定顺序的参数编码,长度前缀避免说明或引用中的分隔符导致碰撞。
|
||||
static string RequestFingerprint(SqlCommand command)
|
||||
{
|
||||
using (var buffer = new MemoryStream())
|
||||
using (var writer = new BinaryWriter(buffer, Encoding.UTF8))
|
||||
using (var sha = SHA256.Create()) {
|
||||
writer.Write(command.CommandText);
|
||||
foreach (SqlParameter parameter in command.Parameters) {
|
||||
writer.Write(parameter.ParameterName);
|
||||
object value = parameter.Value;
|
||||
if (value is byte[]) writer.Write(Convert.ToBase64String((byte[])value));
|
||||
else if (value is decimal) writer.Write(((decimal)value).ToString("F2", Invariant));
|
||||
else writer.Write(Convert.ToString(value, Invariant));
|
||||
}
|
||||
writer.Flush();
|
||||
return BitConverter.ToString(sha.ComputeHash(buffer.ToArray())).Replace("-", "");
|
||||
}
|
||||
}
|
||||
|
||||
static void BindSettlement(SqlCommand command, string name, Dictionary<string,object> inputs, Dictionary<string,object> body)
|
||||
{
|
||||
if (name == SettlementOutputAccess) { Only(inputs); return; }
|
||||
bool output = name == SettlementCopy || name == SettlementDownload;
|
||||
if (name == MonthlyReport || name == MonthlyBatches || output) {
|
||||
if (name == MonthlyReport || output) {
|
||||
if (output) Only(inputs, "归集月份", "负责人", "结算状态", "数据来源", "关键字", "筛选总数");
|
||||
else Only(inputs, "归集月份", "负责人", "结算状态", "数据来源", "关键字");
|
||||
AddText(command, inputs, "负责人", 100, "");
|
||||
AddText(command, inputs, "结算状态", 30, "", "", "ALL", "EXPECTED", "PENDING_REVIEW", "PENDING_MODE", "NOT_EFFECTIVE");
|
||||
AddText(command, inputs, "数据来源", 20, "", "", "ALL", "FORMAL", "LEGACY");
|
||||
AddText(command, inputs, "关键字", 200, "");
|
||||
} else {
|
||||
Only(inputs, "归集月份", "负责人姓名", "状态");
|
||||
AddText(command, inputs, "负责人姓名", 100, "");
|
||||
AddText(command, inputs, "状态", 30, "", "", "ALL", "DRAFT", "PENDING_REVIEW", "APPROVED", "REJECTED");
|
||||
}
|
||||
SettlementMonth(command, inputs, false);
|
||||
if (output) {
|
||||
command.Parameters.Add("@页码", SqlDbType.Int).Value = 1;
|
||||
// 预期条数只参与服务端完整性核对,不传给 SQL 控制查询范围。
|
||||
command.Parameters.Add("@预期条数", SqlDbType.Int).Value = (int)Positive(inputs, "筛选总数", 10000, 0);
|
||||
return;
|
||||
}
|
||||
command.Parameters.Add("@页码", SqlDbType.Int).Value = (int)Positive(body, "pageList", 100000, 1);
|
||||
command.Parameters.Add("@每页条数", SqlDbType.Int).Value = (int)Positive(body, "pageSize", 200, name == MonthlyReport ? 50 : 20);
|
||||
return;
|
||||
}
|
||||
if (name == MonthlyDetail) {
|
||||
Only(inputs, BatchId);
|
||||
SettlementId(command, inputs, BatchId);
|
||||
return;
|
||||
}
|
||||
if (body.ContainsKey("pageList") || body.ContainsKey("pageSize")) throw BadInput();
|
||||
if (name == LegacyPropose || name == FormalPropose) {
|
||||
string id = name == LegacyPropose ? "历史项目流水号" : "合同流水号";
|
||||
Only(inputs, id, "提成计算模式代码", "依据说明", "依据引用", "幂等键");
|
||||
SettlementId(command, inputs, id);
|
||||
AddText(command, inputs, "提成计算模式代码", 40, null, "REPURCHASE", "FIRST_SPECIAL_MACHINE", "FIRST_MODULE", "REFERRAL_SPECIAL_MACHINE", "REFERRAL_MODULE");
|
||||
AddText(command, inputs, "依据说明", 500, null);
|
||||
AddText(command, inputs, "依据引用", 500, "");
|
||||
} else if (name == LegacyReview || name == FormalReview || name == BatchReview) {
|
||||
string id = name == BatchReview ? BatchId : name == LegacyReview ? "历史合同提成补录流水号" : "正式合同提成补录流水号";
|
||||
Only(inputs, id, "动作", "复核意见", "预期版本", "幂等键");
|
||||
SettlementId(command, inputs, id);
|
||||
AddText(command, inputs, "动作", 20, null, "APPROVE", "REJECT");
|
||||
AddText(command, inputs, "复核意见", 500, null);
|
||||
AddVersion(command, inputs, "预期版本");
|
||||
} else if (name == BatchDraft) {
|
||||
Only(inputs, "归集月份", "负责人姓名", "币种", "调整金额", "调整原因", "依据引用", "幂等键");
|
||||
SettlementMonth(command, inputs, true);
|
||||
AddText(command, inputs, "负责人姓名", 100, null);
|
||||
string unit = Text(inputs, "币种", 3, null);
|
||||
if (!Regex.IsMatch(unit, @"\A[A-Z]{3}\z")) throw BadInput();
|
||||
command.Parameters.Add("@币种", SqlDbType.Char, 3).Value = unit;
|
||||
SettlementAdjustment(command, inputs);
|
||||
} else if (name == BatchSubmit) {
|
||||
Only(inputs, BatchId, "调整金额", "调整原因", "依据引用", "预期版本", "幂等键");
|
||||
SettlementId(command, inputs, BatchId);
|
||||
AddVersion(command, inputs, "预期版本");
|
||||
SettlementAdjustment(command, inputs);
|
||||
} else throw BadInput();
|
||||
string token = Text(inputs, "幂等键", 100, null);
|
||||
if (String.IsNullOrWhiteSpace(token)) throw BadInput();
|
||||
command.Parameters.Add("@幂等键", SqlDbType.NVarChar, 100).Value = token;
|
||||
string fingerprint = RequestFingerprint(command);
|
||||
command.Parameters.Add("@请求指纹", SqlDbType.Char, 64).Value = fingerprint;
|
||||
}
|
||||
|
||||
static List<Dictionary<string,object>> NextRows(SqlDataReader reader, int limit)
|
||||
{
|
||||
if (!reader.NextResult()) throw Unavailable();
|
||||
return ReadRows(reader, limit);
|
||||
}
|
||||
|
||||
static object ReadSettlement(SqlCommand command, string name, SqlDataReader reader)
|
||||
{
|
||||
if (name == SettlementOutputAccess) {
|
||||
var access = ReadRows(reader, 1);
|
||||
if (access.Count != 1 || reader.NextResult()) throw Unavailable();
|
||||
return access;
|
||||
}
|
||||
if (IsSettlementWrite(name)) {
|
||||
var receipt = ReadRows(reader, 1);
|
||||
if (receipt.Count != 1 || reader.NextResult()) throw Unavailable();
|
||||
return new { rows = receipt };
|
||||
}
|
||||
if (name == MonthlyDetail) {
|
||||
var record = ReadRows(reader, 1);
|
||||
var lines = NextRows(reader, Int32.MaxValue);
|
||||
var audit = NextRows(reader, Int32.MaxValue);
|
||||
if (reader.NextResult() || (record.Count == 0 && (lines.Count > 0 || audit.Count > 0))) throw Unavailable();
|
||||
return new { record = record.Count == 0 ? null : record[0], lines = lines, audit = audit };
|
||||
}
|
||||
return ReadPageWithSummary(command, reader, name == MonthlyReport);
|
||||
}
|
||||
|
||||
static object ReadPageWithSummary(SqlCommand command, SqlDataReader reader, bool monthlyAggregates)
|
||||
{
|
||||
int page = (int)command.Parameters["@页码"].Value, size = (int)command.Parameters["@每页条数"].Value;
|
||||
var rows = ReadRows(reader, size);
|
||||
var summaries = NextRows(reader, 1);
|
||||
object count;
|
||||
if (summaries.Count != 1 || !summaries[0].TryGetValue("筛选总数", out count) || count == null) throw Unavailable();
|
||||
long total = Convert.ToInt64(count, Invariant);
|
||||
if (total < 0 || total > 9007199254740991L || rows.Count != Math.Min(size, Math.Max(0L, total - (page - 1L) * size))) throw Unavailable();
|
||||
if (!monthlyAggregates) {
|
||||
if (reader.NextResult()) throw Unavailable();
|
||||
return new { rows = rows, total = total, summary = summaries[0] };
|
||||
}
|
||||
var currency = NextRows(reader, Int32.MaxValue);
|
||||
var salespeople = NextRows(reader, Int32.MaxValue);
|
||||
if (reader.NextResult()) throw Unavailable();
|
||||
return new { rows = rows, summary = summaries[0], total = total, currencySummary = currency, salespeople = salespeople };
|
||||
}
|
||||
|
||||
static readonly string[] OutputFields = {
|
||||
"数据来源", "来源业务标识", "归集月份", "签订日期", "合同编号", "合同状态", "负责人姓名", "客户编码",
|
||||
"客户名称", "产品快照", "含税金额", "提成类型", "提成比例", "预计结算金额", "结算状态", "结算说明"
|
||||
};
|
||||
|
||||
static string OutputCell(object value, bool csv)
|
||||
{
|
||||
string cell = Convert.ToString(value, Invariant) ?? "";
|
||||
if (value is string) {
|
||||
// 两种输出都送入表格软件,换行或制表符不能生成新的单元格。
|
||||
cell = Regex.Replace(cell, @"[\x00-\x1f\x7f]", " ");
|
||||
string first = cell.TrimStart();
|
||||
if (first.Length > 0 && "=+@-".IndexOf(first[0]) >= 0) cell = "'" + cell;
|
||||
}
|
||||
return csv ? "\"" + cell.Replace("\"", "\"\"") + "\"" : cell;
|
||||
}
|
||||
|
||||
static object RunSettlementOutput(SqlCommand command, string name, SqlConnection connection)
|
||||
{
|
||||
int expected = (int)command.Parameters["@预期条数"].Value;
|
||||
command.Parameters.RemoveAt("@预期条数");
|
||||
bool csv = name == SettlementDownload;
|
||||
string separator = csv ? "," : "\t";
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine(String.Join(separator, OutputFields));
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
||||
// 同一事务核验输出权限、过滤条件和各页,拒绝中途变化或只返回部分行。
|
||||
using (SqlTransaction tx = connection.BeginTransaction(IsolationLevel.Serializable)) {
|
||||
command.Transaction = tx;
|
||||
for (int page = 1; seen.Count < expected; page++) {
|
||||
command.Parameters["@页码"].Value = page;
|
||||
using (SqlDataReader reader = command.ExecuteReader()) {
|
||||
var rows = ReadRows(reader, 200);
|
||||
if (rows.Count != Math.Min(200, expected - seen.Count)) throw new ApiFailure(409, "合同筛选结果已变化,请刷新后重新输出");
|
||||
var totals = NextRows(reader, 1);
|
||||
if (totals.Count != 1 || Convert.ToInt64(totals[0]["筛选总数"], Invariant) != expected) throw new ApiFailure(409, "合同筛选总数已变化");
|
||||
NextRows(reader, Int32.MaxValue); NextRows(reader, Int32.MaxValue);
|
||||
if (reader.NextResult()) throw Unavailable();
|
||||
foreach (var row in rows) {
|
||||
string key = Convert.ToString(row["数据来源代码"], Invariant) + ":" + Convert.ToString(row["来源业务标识"], Invariant);
|
||||
if (!seen.Add(key)) throw new ApiFailure(409, "结算输出存在重复来源,请刷新核对");
|
||||
var cells = new string[OutputFields.Length];
|
||||
for (int i = 0; i < cells.Length; i++) {
|
||||
string field = OutputFields[i];
|
||||
object val = row[field];
|
||||
if (field == "预计结算金额" && !Object.Equals(row["结算状态代码"], "EXPECTED")) val = null;
|
||||
cells[i] = OutputCell(val, csv);
|
||||
}
|
||||
builder.AppendLine(String.Join(separator, cells));
|
||||
if (builder.Length > 8 * 1024 * 1024) throw new ApiFailure(400, "输出超过大小上限,请缩小筛选范围");
|
||||
}
|
||||
}
|
||||
}
|
||||
tx.Commit();
|
||||
}
|
||||
string content = builder.ToString();
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(content);
|
||||
if (bytes.Length > 20 * 1024 * 1024) throw new ApiFailure(400, "输出超过大小上限,请缩小筛选范围");
|
||||
using (var sha = SHA256.Create()) {
|
||||
return new { procedure = name, rowCount = seen.Count, text = content, contentSHA256 = BitConverter.ToString(sha.ComputeHash(bytes)).Replace("-", "") };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user