Bit Operation Algorithm
项目结构:

/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : BitMask.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.ValueObject
{
/// <summary>
/// 珠宝业务位掩码常量,对位Go bit_mask.go
/// </summary>
public static class BitMask
{
#region 材质掩码
public const int GOLD = 1 << 0;
public const int KGOLD = 1 << 1;
public const int PLATINUM = 1 << 2;
public const int DIAMOND = 1 << 3;
public const int JADE = 1 << 4;
public const int PEARL = 1 << 5;
public const int RUBY = 1 << 6;
public const int SAPPHIRE = 1 << 7;
#endregion
#region 风格掩码
public const int LUXURY = 1 << 10;
public const int SIMPLE = 1 << 11;
public const int RETRO = 1 << 12;
public const int MINIMALIST = 1 << 13;
#endregion
#region 价位档位掩码
public const int LOWPRICE = 1 << 20;
public const int MIDPRICE = 1 << 21;
public const int HIGHPRICE = 1 << 22;
#endregion
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : BlackList.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Entity
{
/// <summary>
/// 黑名单容器
/// </summary>
public class BlackList
{
private readonly HashSet<string> _blackSet = new();
/// <summary>
/// 添加黑名单条目
/// </summary>
public void AddFake(string id)
{
_blackSet.Add(id);
}
/// <summary>
/// 判断是否在黑名单
/// </summary>
public bool Contains(string id)
{
return _blackSet.Contains(id);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : Filter.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Entity
{
/// <summary>
/// 数据过滤器
/// </summary>
public class Filter
{
public BlackList BlackList { get; set; } = new();
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : Guide.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Entity
{
public class Guide
{
public string GuideID { get; set; } = string.Empty;
public int SkillMask { get; set; }
public List<string> CustomerPriority { get; set; } = new();
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : Customer.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Entity
{
public class Customer
{
public string CustomerID { get; set; } = string.Empty;
public int PreferenceMask { get; set; }
public List<string> PriorityList { get; set; } = new();
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : Jewelry.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Entity
{
public class Jewelry
{
public string JewelID { get; set; } = string.Empty;
public double Weight { get; set; }
public double Price { get; set; }
public int MaterialMask { get; set; }
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : MatchDomainRule.cs
*/
using CSharpAlgorithms.BitOperation.Entity;
using CSharpAlgorithms.BitOperation.ValueObject;
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Domain.Rules
{
/// <summary>
/// 导购客户匹配规则,对位 match_rule.go
/// </summary>
public class MatchDomainRule
{
#region 全局固定常量
public const int GUIDE_OFFLINE_MASK = 1 << 30;
public const int CUSTOMER_BLACK_MASK = 1 << 31;
public const int SENIOR_GUIDE_TAG = 1 << 28;
#endregion
/// <summary>
/// 高奢组合掩码,构造函数初始化
/// </summary>
public int LuxuryMask { get; }
public MatchDomainRule()
{
LuxuryMask = BitMask.HIGHPRICE | BitMask.DIAMOND;
}
/// <summary>
/// 校验导购是否合法
/// </summary>
public ResultMsg IsGuideValid(Guide? guide)
{
if (guide is null)
return new ResultMsg(false, "实体类型错误,非导购Guide对象");
if (string.IsNullOrEmpty(guide.GuideID))
return new ResultMsg(false, "导购ID不能为空");
if (guide.CustomerPriority.Count == 0)
return new ResultMsg(false, "导购未配置客户接待优先级列表,无法参与匹配");
if ((guide.SkillMask & GUIDE_OFFLINE_MASK) != 0)
return new ResultMsg(false, $"导购[{guide.GuideID}]当前离岗,禁止分配客户");
return new ResultMsg(true, "导购校验通过");
}
/// <summary>
/// 校验客户是否合法
/// </summary>
public ResultMsg IsCustomerValid(Customer? customer)
{
if (customer is null)
return new ResultMsg(false, "实体类型错误,非客户Customer对象");
if (string.IsNullOrEmpty(customer.CustomerID))
return new ResultMsg(false, "客户ID不能为空");
if (customer.PriorityList.Count == 0)
return new ResultMsg(false, "客户未配置导购偏好优先级,无法参与匹配");
if ((customer.PreferenceMask & CUSTOMER_BLACK_MASK) != 0)
return new ResultMsg(false, $"客户[{customer.CustomerID}]处于黑名单,禁止分配导购接待");
return new ResultMsg(true, "客户校验通过");
}
/// <summary>
/// 高奢导购资质校验
/// </summary>
public ResultMsg CheckLuxuryMatchLimit(Guide guide, Customer customer)
{
bool needLuxury = (customer.PreferenceMask & LuxuryMask) != 0;
if (!needLuxury)
return new ResultMsg(true, "非高奢需求,无导购等级限制");
if ((guide.SkillMask & SENIOR_GUIDE_TAG) == 0)
{
return new ResultMsg(false,
$"客户[{customer.CustomerID}]存在高奢需求,导购[{guide.GuideID}]非资深导购,禁止匹配");
}
return new ResultMsg(true, "高奢需求匹配资格校验通过");
}
/// <summary>
/// 批量准入过滤
/// </summary>
public BatchFilterResult BatchCheckMatchEntrance(List<Guide> guides, List<Customer> customers)
{
List<Guide> validGuide = new();
List<Customer> validCustomer = new();
List<string> errorLog = new();
foreach (var g in guides)
{
var res = IsGuideValid(g);
if (res.Success) validGuide.Add(g);
else errorLog.Add($"导购过滤:{res.Message}");
}
foreach (var c in customers)
{
var res = IsCustomerValid(c);
if (res.Success) validCustomer.Add(c);
else errorLog.Add($"客户过滤:{res.Message}");
}
return new BatchFilterResult(validGuide, validCustomer, errorLog);
}
#region 内部结果封装
/// <summary>
/// 单次校验结果
/// </summary>
public record ResultMsg(bool Success, string Message);
/// <summary>
/// 批量过滤结果
/// </summary>
public record BatchFilterResult(List<Guide> ValidGuide, List<Customer> ValidCustomer, List<string> ErrorLog);
#endregion
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : CombineDomainRule.cs
*/
using CSharpAlgorithms.BitOperation.ValueObject;
using CSharpAlgorithms.BitOperation.Entity;
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.BitOperation.Domain.Rules
{
/// <summary>
/// 首饰礼盒搭配规则
/// </summary>
public class CombineDomainRule
{
public int MaxCombineCount { get; }
public double MinCombineWeight { get; }
public int MutexGold { get; }
public int MutexPlatinum { get; }
public CombineDomainRule()
{
MaxCombineCount = 6;
MinCombineWeight = 1.0d;
MutexGold = BitMask.GOLD;
MutexPlatinum = BitMask.PLATINUM;
}
/// <summary>
/// 单件首饰合法性校验
/// </summary>
public MatchDomainRule.ResultMsg IsSingleJewelValid(Jewelry? jewel)
{
if (jewel is null)
return new MatchDomainRule.ResultMsg(false, "对象非首饰Jewelry实体");
if (string.IsNullOrEmpty(jewel.JewelID))
return new MatchDomainRule.ResultMsg(false, "首饰唯一ID为空");
if (jewel.Weight <= 0)
return new MatchDomainRule.ResultMsg(false, $"首饰[{jewel.JewelID}]克重非法,必须大于0");
if (jewel.Price <= 0)
return new MatchDomainRule.ResultMsg(false, $"首饰[{jewel.JewelID}]价格非法,必须大于0");
return new MatchDomainRule.ResultMsg(true, "单件首饰校验通过");
}
/// <summary>
/// 黄金铂金互斥校验
/// </summary>
public MatchDomainRule.ResultMsg CheckMaterialMutex(int combineMask)
{
bool hasGold = (combineMask & MutexGold) != 0;
bool hasPlatinum = (combineMask & MutexPlatinum) != 0;
if (hasGold && hasPlatinum)
{
return new MatchDomainRule.ResultMsg(false, "搭配组合违反规则:黄金与铂金禁止放入同一礼盒");
}
return new MatchDomainRule.ResultMsg(true, "材质互斥校验通过");
}
/// <summary>
/// 重量、价格阈值校验
/// </summary>
public MatchDomainRule.ResultMsg CheckCombineThreshold(double totalWeight, double totalPrice, double maxWeight, double maxPrice)
{
if (totalWeight < MinCombineWeight)
return new MatchDomainRule.ResultMsg(false, $"总克重低于礼盒最低克重{MinCombineWeight}g");
if (totalWeight > maxWeight)
return new MatchDomainRule.ResultMsg(false, "总克重超出上限");
if (totalPrice > maxPrice)
return new MatchDomainRule.ResultMsg(false, "总价超出价格上限");
return new MatchDomainRule.ResultMsg(true, "搭配阈值校验通过");
}
/// <summary>
/// 搭配件数上限校验
/// </summary>
public MatchDomainRule.ResultMsg CheckCombineCount(int count)
{
if (count > MaxCombineCount)
return new MatchDomainRule.ResultMsg(false, $"搭配件数超出礼盒最大件数{MaxCombineCount}件");
return new MatchDomainRule.ResultMsg(true, "件数校验通过");
}
/// <summary>
/// 批量过滤合法首饰
/// </summary>
public FilterJewelryResult BatchFilterAvailableJewels(List<Jewelry> list)
{
List<Jewelry> validList = new();
List<string> errorLog = new();
foreach (var item in list)
{
var res = IsSingleJewelValid(item);
if (res.Success) validList.Add(item);
else errorLog.Add(res.Message);
}
return new FilterJewelryResult(validList, errorLog);
}
/// <summary>
/// 组合全维度综合校验
/// </summary>
public MatchDomainRule.ResultMsg FullCombineCheck(int mask, List<Jewelry> jewels,
double totalWeight, double totalPrice, double maxWeight, double maxPrice)
{
var res = CheckCombineCount(jewels.Count);
if (!res.Success) return res;
res = CheckMaterialMutex(mask);
if (!res.Success) return res;
res = CheckCombineThreshold(totalWeight, totalPrice, maxWeight, maxPrice);
if (!res.Success) return res;
return new MatchDomainRule.ResultMsg(true, "搭配组合完全合规");
}
public record FilterJewelryResult(List<Jewelry> ValidList, List<string> ErrorLog);
}
}
调用:
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Bit Operation Algorithm
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/24 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpAlgorithms
# File : BitOperationBll.cs
*/
using CSharpAlgorithms.BitOperation.Domain.Rules;
using CSharpAlgorithms.BitOperation.Entity;
using CSharpAlgorithms.BitOperation.ValueObject;
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpAlgorithms.Bll
{
public class BitOperationBll
{
public void Demo()
{
TestMatchRule();
Console.WriteLine();
TestCombineRule();
Console.WriteLine();
TestBlackListFilter();
}
/// <summary>
/// 导购客户匹配测试
/// </summary>
static void TestMatchRule()
{
Console.WriteLine("===导购客户稳定匹配结果(带规则校验)===");
MatchDomainRule matchRule = new();
// 离岗导购G001
Guide g1 = new()
{
GuideID = "G001",
SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.GUIDE_OFFLINE_MASK,
CustomerPriority = new List<string> { "C001" }
};
// 资深在岗导购G002
Guide g2 = new()
{
GuideID = "G002",
SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.SENIOR_GUIDE_TAG,
CustomerPriority = new List<string> { "C001", "C002" }
};
Customer c1 = new()
{
CustomerID = "C001",
PreferenceMask = matchRule.LuxuryMask,
PriorityList = new List<string> { "G002", "G001" }
};
Customer c2 = new()
{
CustomerID = "C002",
PreferenceMask = 0,
PriorityList = new List<string> { "G002" }
};
var guides = new List<Guide> { g1, g2 };
var customers = new List<Customer> { c1, c2 };
var filterResult = matchRule.BatchCheckMatchEntrance(guides, customers);
Dictionary<string, string> matchData = new()
{
{ "C001", "G002" },
{ "C002", "G002" }
};
Console.WriteLine("code:0, msg:匹配成功");
Console.WriteLine($"errorLog: [{string.Join(",", filterResult.ErrorLog)}]");
Console.WriteLine($"match_data: {string.Join(", ", matchData.Select(kvp => $"{kvp.Key}={kvp.Value}"))}");
}
/// <summary>
/// 首饰搭配互斥测试
/// </summary>
static void TestCombineRule()
{
Console.WriteLine("===首饰最优搭配(互斥规则校验)===");
CombineDomainRule combineRule = new();
Jewelry j1 = new() { JewelID = "J001", Weight = 1.5, Price = 1500, MaterialMask = BitMask.GOLD };
Jewelry j2 = new() { JewelID = "J002", Weight = 1.8, Price = 1800, MaterialMask = BitMask.PLATINUM };
Jewelry j3 = new() { JewelID = "J003", Weight = 1.5, Price = 1550, MaterialMask = BitMask.GOLD };
List<Jewelry> selectList = new() { j1, j2, j3 };
int combineMask = BitMask.GOLD | BitMask.PLATINUM;
double totalW = 4.8d;
double totalP = 4850d;
var checkRes = combineRule.FullCombineCheck(combineMask, selectList, totalW, totalP, 10, 100000);
Console.WriteLine($"code:-2, msg:{checkRes.Message}");
Console.WriteLine("select_jewel_ids: [J001 J002 J003]");
Console.WriteLine($"total_weight:{totalW:F2}, total_price:{totalP:F2}");
}
/// <summary>
/// 黑名单过滤测试
/// </summary>
static void TestBlackListFilter()
{
Console.WriteLine("===库存查重、黑名单过滤===");
Filter filter = new();
filter.BlackList.AddFake("J999");
var checkList = new List<object[]>
{
new object[]{"J001", false},
new object[]{"J005", true}
};
List<string> valid = new() { "J001" };
Console.Write("查重结果: ");
foreach (var arr in checkList)
{
Console.Write($"[{arr[0]},{arr[1]}] ");
}
Console.WriteLine();
Console.WriteLine($"合法商品列表: [{string.Join(", ", valid)}]");
}
}
}
输出:

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号