1
0
forked from erp-dev/erp

feat: added wecom notify when printing_job production was completed(new status) and shipment was created

This commit is contained in:
2026-04-09 17:35:14 +08:00
parent 76ea445880
commit 646dbe7f18
27 changed files with 1122 additions and 13 deletions

View File

@@ -294,3 +294,118 @@ def render_printing_order_created_markdown(
fabric=str(fabric or "-"),
outgoing_date=str(outgoing_date or "-"),
)
def render_printing_job_production_completed_markdown(
*,
printing_order_identifier: str,
printing_job_id: str,
product_name: str,
unshipped_sales_items_count: str,
unshipped_sales_items_quantity: str,
sender_label: str,
) -> str:
template = getattr(
settings,
"PRINTING_JOB_PRODUCTION_COMPLETED_WECOM_MARKDOWN_TEMPLATE",
(
"### 印染任务已完成生产\n"
"\n"
"- **订单号**`{printing_order_identifier}`\n"
"- **印染任务ID**`{printing_job_id}`\n"
"- **产品名称**{product_name}\n"
"- **未出货销售品条数**`{unshipped_sales_items_count}`\n"
"- **未出货销售品数量**`{unshipped_sales_items_quantity}`\n"
"- **发送者**{sender}\n"
),
)
return template.format(
printing_order_identifier=str(printing_order_identifier or "-"),
printing_job_id=str(printing_job_id or "-"),
product_name=str(product_name or "-"),
unshipped_sales_items_count=str(unshipped_sales_items_count or "0"),
unshipped_sales_items_quantity=str(unshipped_sales_items_quantity or "0"),
sender=str(sender_label or "系统自动发送"),
)
def send_printing_job_production_completed_wecom(
*,
printing_job_id: int,
triggered_by_id: int | None = None,
key: str | None = None,
timeout_seconds: float = 10.0,
dry_run: bool = False,
) -> dict:
from django.contrib.auth import get_user_model
from django.db.models import Sum
from printing.models import PrintingJob
from shipment.services import get_active_sales_items_queryset
job = (
PrintingJob.objects.select_related("product", "printing_order")
.filter(id=int(printing_job_id))
.first()
)
if not job:
raise ValueError(f"PrintingJob 不存在id={printing_job_id}")
triggered_by = None
if triggered_by_id:
triggered_by = get_user_model().objects.filter(id=int(triggered_by_id)).first()
order = getattr(job, "printing_order", None)
external_order_id = str(getattr(order, "external_order_id", "") or "").strip()
printing_order_identifier = external_order_id or str(getattr(order, "id", "-") or "-")
product_name = getattr(getattr(job, "product", None), "name", None) or "-"
unshipped_sales_items_qs = get_active_sales_items_queryset().filter(
printing_job_id=job.id,
shipment__isnull=True,
)
unshipped_sales_items_count = unshipped_sales_items_qs.count()
unshipped_sales_items_quantity = (
unshipped_sales_items_qs.aggregate(total=Sum("quantity")).get("total") or 0
)
sender_label = _user_label(triggered_by)
msg = render_printing_job_production_completed_markdown(
printing_order_identifier=str(printing_order_identifier),
printing_job_id=str(job.id),
product_name=str(product_name),
unshipped_sales_items_count=str(unshipped_sales_items_count),
unshipped_sales_items_quantity=str(unshipped_sales_items_quantity),
sender_label=str(sender_label),
)
if dry_run:
return {
"dry_run": True,
"message": msg,
"printing_job_id": job.id,
"printing_order_identifier": str(printing_order_identifier),
"product_name": str(product_name),
"unshipped_sales_items_count": unshipped_sales_items_count,
"unshipped_sales_items_quantity": str(unshipped_sales_items_quantity),
"sender_label": str(sender_label),
}
resp = send_wecom_webhook_message(
content=msg,
msgtype="markdown",
key=key,
timeout_seconds=timeout_seconds,
)
return {
"dry_run": False,
"message": msg,
"printing_job_id": job.id,
"printing_order_identifier": str(printing_order_identifier),
"product_name": str(product_name),
"unshipped_sales_items_count": unshipped_sales_items_count,
"unshipped_sales_items_quantity": str(unshipped_sales_items_quantity),
"sender_label": str(sender_label),
"wecom": resp.raw,
"ok": resp.ok,
"errcode": resp.errcode,
"errmsg": resp.errmsg,
}