XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
using JiDangJia.Contracts;
using Microsoft.Data.Sqlite;
using static JiDangJia.Server.ServerStore;

namespace JiDangJia.Server;

public sealed partial class CommunityStore
{
    private static SupplyQuota Quota(SqliteConnection c,string account)
    {
        var user=Rows(c,"SELECT identityKind,identityStatus,communityPublishLimit FROM Accounts WHERE id=@p0",account).FirstOrDefault()??throw new CommunityException(404,"账户不存在");
        bool enterprise=Text(user,"identityKind")=="ENTERPRISE"&&Text(user,"identityStatus")=="APPROVED";
        var settings=Rows(c,"SELECT personalSupplyLimit,enterpriseSupplyLimit FROM CommunitySettings WHERE id=1")[0];
        int active=(int)Number(Rows(c,"SELECT COUNT(*) total FROM CommunityPosts WHERE authorId=@p0 AND kind IN ('SUPPLY','SERVICE') AND deletedAt IS NULL AND shelfStatus='ACTIVE'",account)[0],"total");
        return new(active,user.GetValueOrDefault("communityPublishLimit") is null?(int)Number(settings,enterprise?"enterpriseSupplyLimit":"personalSupplyLimit"):(int)Number(user,"communityPublishLimit"),enterprise?"ENTERPRISE":"PERSONAL");
    }
    private static void RequireSupplySlot(SqliteConnection c,string account)
    {
        var quota=Quota(c,account);if(quota.Active>=quota.Limit)throw new CommunityException(409,$"服务与供需合计已达上限({quota.Active}/{quota.Limit}),请先在‘我的发布’下架一张再发布");
    }
    public SupplyQuota SupplyQuota(Account actor)=>store.Read(c=>{Actor(c,actor);return Quota(c,actor.Id);});
    public CommunityPost Shelf(Account actor,long id,bool active)=>store.Write(c=>
    {
        var admin=Actor(c,actor);var post=FindPost(c,actor,admin,id);
        if(post.AuthorId!=actor.Id||post.Kind is not ("SUPPLY" or "SERVICE"))throw new CommunityException(403,"只能上架或下架本人的服务或供需卡");
        if(active&&post.ShelfStatus!="ACTIVE")
        {
            SocialWrite(c,actor);RequireSupplySlot(c,actor.Id);if(post.Supply is { } supply)ValidateSupply(supply);
            if(post.Kind=="SUPPLY"&&CommunityText.Length(post.Content)>CommunityText.SupplyLength)throw new ArgumentException("请先把供需简介正文修改为 200 个字符以内");
            if(post.ModerationStatus=="REJECTED")throw new CommunityException(403,"请先修改未通过审核的卡片");
        }
        Exec(c,"UPDATE CommunityPosts SET shelfStatus=@p0 WHERE id=@p1",active?"ACTIVE":"OFF",id);Log(c,actor,active?"SUPPLY_ON_SHELF":"SUPPLY_OFF_SHELF",id);return FindPost(c,actor,admin,id);
    });
    public CommunityPost Pin(Account admin,long id,bool pinned)=>store.Write(c=>
    {
        if(!Actor(c,admin))throw new CommunityException(403,"只有管理员可以置顶推荐");var post=FindPost(c,admin,true,id);
        if(post.Kind!="RECOMMEND")throw new ArgumentException("只支持置顶官方推荐");
        Exec(c,"UPDATE CommunityPosts SET pinned=@p0 WHERE id=@p1",pinned?1:0,id);Log(c,admin,pinned?"RECOMMEND_PIN":"RECOMMEND_UNPIN",id);return FindPost(c,admin,true,id);
    });
    public List<CommunityUserLabel> UserLabels(Account admin,string? q)=>store.Read(c=>
    {
        if(!Actor(c,admin))throw new CommunityException(403,"需要管理员权限");q=(q??"").Trim();if(q.Length>64)throw new ArgumentException("搜索最多 64 字");
        return Rows(c,"SELECT * FROM Accounts WHERE (@p0='' OR instr(username,@p0)>0 OR instr(name,@p0)>0) ORDER BY name,id LIMIT 100",q).Select(r=>Label(c,r)).ToList();
    });
    private static CommunityUserLabel Label(SqliteConnection c,Dictionary<string,object?> r)
    {
        var quota=Quota(c,Text(r,"id"));return new(Text(r,"id"),Text(r,"username"),Text(r,"name"),quota.IdentityKind,Text(r,"identityStatus")=="APPROVED",Text(r,"communityCredit"),Number(r,"communityLabelRevision"),quota.Active,quota.Limit,r.GetValueOrDefault("communityPublishLimit") is null?null:(int)Number(r,"communityPublishLimit"));
    }
    public CommunityUserLabel SetUserLabel(Account admin,string id,UpdateUserLabelRequest request)=>store.Write(c=>
    {
        if(!Actor(c,admin))throw new CommunityException(403,"需要管理员权限");if(request.CreditLevel is null||!CommunityCatalog.CreditLevels.ContainsKey(request.CreditLevel))throw new ArgumentException("信用等级无效");
        var row=Rows(c,"SELECT * FROM Accounts WHERE id=@p0",id).FirstOrDefault()??throw new CommunityException(404,"账户不存在");
        if(Number(row,"communityLabelRevision")!=request.ExpectedRevision)throw new CommunityException(409,"用户标签已更新,请刷新后再修改");
        if(request.PublishLimitOverride is <0 or >100000)throw new ArgumentException("特殊发布数量须为 0–100000 的整数,留空恢复身份默认额度");
        Exec(c,"UPDATE Accounts SET communityCredit=@p0,communityLabelRevision=communityLabelRevision+1,communityPublishLimit=CASE WHEN @p3=1 THEN @p2 ELSE communityPublishLimit END WHERE id=@p1",request.CreditLevel,id,request.PublishLimitOverride,request.UpdatePublishLimit?1:0);
        Exec(c,"INSERT INTO Audit VALUES(@p0,@p1,'COMMUNITY_USER_LABEL',@p2,@p3,@p4)",Guid.NewGuid().ToString("N"),admin.Id,id,request.CreditLevel,Now);return Label(c,Rows(c,"SELECT * FROM Accounts WHERE id=@p0",id)[0]);
    });
    // 通知接口不返回姓名、消息正文或账目;审核通过后按投递序列出现,避免延迟审核的消息漏提醒。
    public InboxReminderPage Inbox(Account actor,long after)=>store.Write(c=>
    {
        Actor(c,actor);if(after<0)throw new ArgumentException("通知游标无效");Deliver(c,actor);
        var delivered=Rows(c,"SELECT d.sequence,m.kind FROM ChatDeliveries d JOIN ChatMessages m ON m.id=d.messageId WHERE d.accountId=@p0 AND d.sequence>@p1 AND m.createdAt>@p2 AND m.recipientId=@p0 AND m.moderationStatus='APPROVED' AND m.id>COALESCE((SELECT throughId FROM AccountChatClears WHERE accountId=@p0),0) AND NOT EXISTS(SELECT 1 FROM ChatReadMessages r WHERE r.accountId=@p0 AND r.messageId=m.id) ORDER BY d.sequence LIMIT 101",actor.Id,after,ChatCutoff);
        var items=delivered.Take(100).Select(r=>new InboxReminder(Number(r,"sequence"),"MESSAGE")).ToList();
        long cursor=delivered.Count>100?items[^1].Sequence:Number(Rows(c,"SELECT COALESCE(MAX(sequence),0) cursor FROM ChatDeliveries WHERE accountId=@p0",actor.Id)[0],"cursor");
        return new InboxReminderPage(items,cursor,delivered.Count>100);
    });
}