2022-02-19 18:20:19 +08:00
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
from tortoise import fields
|
|
|
|
|
|
from tortoise.contrib.postgres.functions import Random
|
|
|
|
|
|
from tortoise.expressions import Q
|
2022-02-19 18:20:19 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
from services.db_context import Model
|
2022-02-19 18:20:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
class Setu(Model):
|
2022-02-19 18:20:19 +08:00
|
|
|
|
|
2023-02-18 18:46:54 +08:00
|
|
|
|
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
|
|
|
|
|
"""自增id"""
|
|
|
|
|
|
local_id = fields.IntField()
|
|
|
|
|
|
"""本地存储下标"""
|
|
|
|
|
|
title = fields.CharField(255)
|
|
|
|
|
|
"""标题"""
|
|
|
|
|
|
author = fields.CharField(255)
|
|
|
|
|
|
"""作者"""
|
|
|
|
|
|
pid = fields.BigIntField()
|
|
|
|
|
|
"""pid"""
|
|
|
|
|
|
img_hash = fields.TextField()
|
|
|
|
|
|
"""图片hash"""
|
|
|
|
|
|
img_url = fields.CharField(255)
|
|
|
|
|
|
"""pixiv url链接"""
|
|
|
|
|
|
is_r18 = fields.BooleanField()
|
|
|
|
|
|
"""是否r18"""
|
|
|
|
|
|
tags = fields.TextField()
|
|
|
|
|
|
"""tags"""
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
table = "setu"
|
|
|
|
|
|
table_description = "色图数据表"
|
|
|
|
|
|
unique_together = ("pid", "img_url")
|
2022-02-19 18:20:19 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def query_image(
|
|
|
|
|
|
cls,
|
|
|
|
|
|
local_id: Optional[int] = None,
|
|
|
|
|
|
tags: Optional[List[str]] = None,
|
2023-02-18 18:46:54 +08:00
|
|
|
|
r18: bool = False,
|
2022-02-19 18:20:19 +08:00
|
|
|
|
limit: int = 50,
|
|
|
|
|
|
):
|
|
|
|
|
|
"""
|
2022-07-23 14:09:17 +08:00
|
|
|
|
说明:
|
2022-02-19 18:20:19 +08:00
|
|
|
|
通过tag查找色图
|
2022-07-23 14:09:17 +08:00
|
|
|
|
参数:
|
2022-02-19 18:20:19 +08:00
|
|
|
|
:param local_id: 本地色图 id
|
|
|
|
|
|
:param tags: tags
|
|
|
|
|
|
:param r18: 是否 r18,0:非r18 1:r18 2:混合
|
|
|
|
|
|
:param limit: 获取数量
|
|
|
|
|
|
"""
|
|
|
|
|
|
if local_id:
|
2023-02-18 18:46:54 +08:00
|
|
|
|
return await cls.filter(is_r18=r18, local_id=local_id).first()
|
|
|
|
|
|
query = cls.filter(is_r18=r18)
|
2022-02-19 18:20:19 +08:00
|
|
|
|
if tags:
|
|
|
|
|
|
for tag in tags:
|
2023-02-18 18:46:54 +08:00
|
|
|
|
query = query.filter(
|
|
|
|
|
|
Q(tags__contains=tag)
|
|
|
|
|
|
| Q(title__contains=tag)
|
|
|
|
|
|
| Q(author__contains=tag)
|
|
|
|
|
|
)
|
|
|
|
|
|
query = query.annotate(rand=Random()).limit(limit)
|
|
|
|
|
|
return await query.all()
|
2022-02-19 18:20:19 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def delete_image(cls, pid: int) -> int:
|
|
|
|
|
|
"""
|
2022-07-23 14:09:17 +08:00
|
|
|
|
说明:
|
2022-02-19 18:20:19 +08:00
|
|
|
|
删除图片并替换
|
2022-07-23 14:09:17 +08:00
|
|
|
|
参数:
|
2022-02-19 18:20:19 +08:00
|
|
|
|
:param pid: 图片pid
|
|
|
|
|
|
"""
|
2023-02-18 18:46:54 +08:00
|
|
|
|
return_id = -1
|
|
|
|
|
|
query = await cls.get_or_none(pid=pid)
|
2022-02-19 18:20:19 +08:00
|
|
|
|
if query:
|
2023-02-18 18:46:54 +08:00
|
|
|
|
num = await cls.filter(is_r18=query.is_r18).count()
|
|
|
|
|
|
last_image = await cls.get_or_none(is_r18=query.is_r18, local_id=num - 1)
|
|
|
|
|
|
if last_image:
|
|
|
|
|
|
return_id = last_image.local_id
|
|
|
|
|
|
last_image.local_id = query.local_id
|
|
|
|
|
|
await last_image.save(update_fields=["local_id"])
|
|
|
|
|
|
await query.delete()
|
|
|
|
|
|
return return_id
|