Blame view

netcore/src/Modularity/Extend/NCC.Extend/AttendancePunchApplyConfigHelper.cs 5.06 KB
83c6d5d1   “wangming”   工作台更新 2026-11-10 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using NCC.Extend.Entitys.Dto.LqAttendanceSetting;
  using NCC.Extend.Entitys.lq_attendance_punch_apply_fee_rule;
  using NCC.Extend.Entitys.lq_attendance_punch_apply_setting;
  using SqlSugar;
  
  namespace NCC.Extend
  {
      /// <summary>
      /// 补卡申请额度:超出免费次数后的分档扣款计算(与缺卡规则相同区间语义:min 含、max 不含)。
      /// </summary>
      public static class AttendancePunchApplyConfigHelper
      {
          /// <summary>
          /// 租户当前补卡额度配置(无表数据时返回默认:免费 3、不限上限、无分档扣款)
          /// </summary>
          public sealed class PunchApplyRuntimeConfig
          {
              /// <summary>每月免费次数</summary>
              public int FreeCount { get; set; } = 3;
  
              /// <summary>是否启用月度补卡次数上限(0-否)</summary>
              public int LimitEnabled { get; set; }
  
              /// <summary>月度上限(LimitEnabled=1 时有效)</summary>
              public int? LimitCount { get; set; }
  
              /// <summary>分档规则(已排序)</summary>
              public List<AttendancePunchApplyFeeRuleModel> FeeTiers { get; set; } = new List<AttendancePunchApplyFeeRuleModel>();
          }
  
          /// <summary>
          /// 从数据库加载单行设置与分档规则(DeleteMark=0)
          /// </summary>
          public static async Task<PunchApplyRuntimeConfig> LoadRuntimeConfigAsync(ISqlSugarClient db)
          {
              var cfg = new PunchApplyRuntimeConfig();
              if (db == null)
              {
                  return cfg;
              }
  
              var setting = (await db.Queryable<LqAttendancePunchApplySettingEntity>()
                  .Where(x => SqlFunc.IsNull(x.DeleteMark, 0) == 0)
                  .OrderBy(x => x.CreateTime, OrderByType.Desc)
                  .Take(1)
                  .ToListAsync()).FirstOrDefault();
              if (setting != null)
              {
                  if (setting.FreeCount >= 0)
                  {
                      cfg.FreeCount = setting.FreeCount;
                  }
  
                  cfg.LimitEnabled = setting.LimitEnabled;
                  cfg.LimitCount = setting.LimitCount;
              }
  
              var rules = await db.Queryable<LqAttendancePunchApplyFeeRuleEntity>()
                  .Where(x => SqlFunc.IsNull(x.DeleteMark, 0) == 0)
                  .OrderBy(x => x.MinCount)
                  .OrderBy(x => x.SortCode)
                  .ToListAsync();
              cfg.FeeTiers = rules
                  .Select(r => new AttendancePunchApplyFeeRuleModel
                  {
                      id = r.Id,
                      minCount = r.MinCount,
                      maxCount = r.MaxCount,
                      deductPerTime = r.DeductPerTime,
                      expressionText = r.ExpressionText,
                      remark = r.Remark
                  })
                  .ToList();
  
              return cfg;
          }
  
          /// <summary>
          /// 按「超出第 1、2、… 次付费补卡」逐次匹配阶梯并累加扣款。
          /// </summary>
          /// <param name="totalQuotaCount">计入额度的当月补卡总次数(上班/下班各计 1)</param>
          /// <param name="freeCount">每月免费次数</param>
          /// <param name="tiers">按 MinCount 排序的阶梯;区间为 [min, max)</param>
          /// <returns>超出免费次数后的扣款合计</returns>
          public static decimal CalcPunchApplyDeduction(
              int totalQuotaCount,
              int freeCount,
              IList<AttendancePunchApplyFeeRuleModel> tiers)
          {
              if (totalQuotaCount <= 0 || freeCount < 0)
              {
                  return 0m;
              }
  
              var paidCount = global::System.Math.Max(0, totalQuotaCount - freeCount);
              if (paidCount <= 0)
              {
                  return 0m;
              }
  
              if (tiers == null || tiers.Count == 0)
              {
                  return 0m;
              }
  
              var ordered = tiers
                  .Where(t => t != null)
                  .OrderBy(t => t.minCount)
                  .ThenBy(t => t.maxCount ?? int.MaxValue)
                  .ToList();
              if (ordered.Count == 0)
              {
                  return 0m;
              }
  
              decimal sum = 0m;
              for (var i = 1; i <= paidCount; i++)
              {
                  var deduct = PickTierDeductPerTime(i, ordered);
                  sum += deduct;
              }
  
              return sum;
          }
  
          private static decimal PickTierDeductPerTime(int paidOrdinal, List<AttendancePunchApplyFeeRuleModel> ordered)
          {
              foreach (var tier in ordered)
              {
                  if (paidOrdinal < tier.minCount)
                  {
                      continue;
                  }
  
                  if (tier.maxCount.HasValue && paidOrdinal >= tier.maxCount.Value)
                  {
                      continue;
                  }
  
                  return tier.deductPerTime >= 0m ? tier.deductPerTime : 0m;
              }
  
              return 0m;
          }
      }
  }