You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

344 lines
10 KiB

{
"properties" : { },
"id" : "b2c3d4e5f6a7488990abcdef123456789",
"script" : null,
"groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1",
"name" : "牦牛供应详情",
"createTime" : 1780882100000,
"updateTime" : null,
"lock" : null,
"createBy" : "admin",
"updateBy" : "admin",
"path" : "/yak-supply-detail",
"method" : "GET",
"parameters" : [ {
"name" : "id",
"value" : null,
"description" : "进场登记 ID(yak_trade_entry_record.id)",
"required" : true,
"dataType" : "String",
"type" : null,
"defaultValue" : null,
"validateType" : null,
"error" : null,
"expression" : null,
"children" : null
} ],
"options" : [ ],
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : null,
"description" : "牦牛供应单条详情:yak_trade_entry_record + 耳标库存 + 订单/称重 + 证件图(只读)。",
"requestBodyDefinition" : null,
"responseBodyDefinition" : null
}
================================
// 只读查询,不修改任何数据
if (!id) {
exit 400, '缺少参数 id'
}
var pickUrl = (value) => {
if (!value) {
return ''
}
var text = (value + '').trim()
if (!text) {
return ''
}
if (text.indexOf('data:') === 0) {
return text
}
if (text.indexOf('http://localhost:8080') === 0) {
return text.substring('http://localhost:8080'.length)
}
if (text.indexOf('http://127.0.0.1:8080') === 0) {
return text.substring('http://127.0.0.1:8080'.length)
}
if (text.indexOf('http') === 0 || text.indexOf('/') === 0) {
return text
}
if (text.indexOf('profile/') === 0) {
return '/' + text
}
if (text.match(/^\d{4}\/\d{2}\/\d{2}\//)) {
return '/profile/' + text
}
return ''
}
var baseSql = """
SELECT
e.id,
e.name,
e.vehicle_no,
e.phone,
e.party_id,
COALESCE(c.region_name, c.address, '') AS origin,
COALESCE(e.quarantine_certificate_no, '') AS quarantine_no,
to_char(e.entered_at, 'YYYY-MM-DD HH24:MI') AS entry_time,
COALESCE(NULLIF(inv.yak_count, 0), et.yak_count, 0) AS yak_count,
COALESCE(inv.sold_count, 0) AS sold_count,
e.entry_photo_id,
cert.certificate_image_file_id,
cr.entry_image AS car_entry_image,
entry_oss.url AS entry_oss_url,
cert_oss.url AS cert_oss_url,
COALESCE(ord.order_count, 0) AS order_count
FROM yak_trade_entry_record e
LEFT JOIN (
SELECT
seller_entry_record_id,
COUNT(*) AS yak_count,
COUNT(*) FILTER (WHERE status = 'SOLD') AS sold_count
FROM yak_trade_ear_tag_inventory
GROUP BY seller_entry_record_id
) inv ON inv.seller_entry_record_id = e.id
LEFT JOIN (
SELECT entry_record_id, COUNT(*) AS yak_count
FROM yak_trade_entry_record_ear_tag
GROUP BY entry_record_id
) et ON et.entry_record_id = e.id
LEFT JOIN yak_sn_customer c
ON c.id = e.party_id
AND c.del_flag = '0'
LEFT JOIN LATERAL (
SELECT certificate_image_file_id
FROM yak_trade_entry_record_ear_tag t
WHERE t.entry_record_id = e.id
AND t.certificate_image_file_id IS NOT NULL
AND TRIM(t.certificate_image_file_id) <> ''
ORDER BY t.create_time
LIMIT 1
) cert ON TRUE
LEFT JOIN LATERAL (
SELECT entry_image
FROM yak_car_record cr
WHERE cr.del_flag = '0'
AND cr.plate_no = e.vehicle_no
ORDER BY cr.entry_time DESC NULLS LAST
LIMIT 1
) cr ON TRUE
LEFT JOIN yak_trade_quarantine_certificate qc
ON qc.certificate_no = e.quarantine_certificate_no
LEFT JOIN LATERAL (
SELECT o.url
FROM sys_oss o
WHERE e.entry_photo_id IS NOT NULL
AND TRIM(e.entry_photo_id) <> ''
AND (
o.oss_id::text = e.entry_photo_id
OR o.ext1 = e.entry_photo_id
OR o.file_name ILIKE '%' || e.entry_photo_id || '%'
OR o.original_name ILIKE '%' || e.entry_photo_id || '%'
OR o.url ILIKE '%' || e.entry_photo_id || '%'
)
ORDER BY
CASE
WHEN o.oss_id::text = e.entry_photo_id THEN 0
WHEN o.ext1 = e.entry_photo_id THEN 1
ELSE 2
END
LIMIT 1
) entry_oss ON TRUE
LEFT JOIN LATERAL (
SELECT o.url
FROM sys_oss o
WHERE (
cert.certificate_image_file_id IS NOT NULL
AND TRIM(cert.certificate_image_file_id) <> ''
AND (
o.oss_id::text = cert.certificate_image_file_id
OR o.ext1 = cert.certificate_image_file_id
OR o.file_name ILIKE '%' || cert.certificate_image_file_id || '%'
OR o.original_name ILIKE '%' || cert.certificate_image_file_id || '%'
OR o.url ILIKE '%' || cert.certificate_image_file_id || '%'
)
)
OR (
qc.image_file_id IS NOT NULL
AND TRIM(qc.image_file_id) <> ''
AND (
o.oss_id::text = qc.image_file_id
OR o.ext1 = qc.image_file_id
OR o.file_name ILIKE '%' || qc.image_file_id || '%'
OR o.original_name ILIKE '%' || qc.image_file_id || '%'
OR o.url ILIKE '%' || qc.image_file_id || '%'
)
)
ORDER BY
CASE
WHEN cert.certificate_image_file_id IS NOT NULL
AND o.oss_id::text = cert.certificate_image_file_id THEN 0
WHEN qc.image_file_id IS NOT NULL
AND o.oss_id::text = qc.image_file_id THEN 1
ELSE 2
END
LIMIT 1
) cert_oss ON TRUE
LEFT JOIN (
SELECT
inv.seller_entry_record_id,
COUNT(DISTINCT o.id) AS order_count
FROM yak_trade_ear_tag_inventory inv
JOIN yak_trade_order_item i ON i.ear_tag_no = inv.ear_tag_no
JOIN yak_trade_order o ON o.id = i.order_id
WHERE o.status <> 'CANCELLED'
GROUP BY inv.seller_entry_record_id
) ord ON ord.seller_entry_record_id = e.id
WHERE e.id = #{id}
AND e.entry_type = 'SELLER'
"""
var orderSql = """
SELECT
o.id,
COALESCE(bc.customer_name, be.name, o.buyer_id, '') AS buyer,
COALESCE(sc.customer_name, e.name, o.seller_id, '') AS seller,
to_char(o.created_at, 'YYYY-MM-DD HH24:MI') AS trade_time,
COALESCE(SUM(COALESCE(i.quantity, 1)), 0) AS quantity,
COALESCE(SUM(i.weight), 0) AS weight,
MAX(w.photo_id) AS weight_photo_id,
MAX(wo.url) AS weight_photo_url
FROM yak_trade_entry_record e
JOIN yak_trade_ear_tag_inventory inv
ON inv.seller_entry_record_id = e.id
JOIN yak_trade_order_item i
ON i.ear_tag_no = inv.ear_tag_no
JOIN yak_trade_order o
ON o.id = i.order_id
LEFT JOIN yak_sn_customer bc
ON bc.id = o.buyer_id
AND bc.del_flag = '0'
LEFT JOIN yak_sn_customer sc
ON sc.id = o.seller_id
AND sc.del_flag = '0'
LEFT JOIN yak_trade_entry_record be
ON be.party_id = o.buyer_id
AND be.entry_type = 'BUYER'
LEFT JOIN yak_trade_weighing_record w
ON w.id = i.weighing_record_id
LEFT JOIN LATERAL (
SELECT o.url
FROM sys_oss o
WHERE w.photo_id IS NOT NULL
AND TRIM(w.photo_id) <> ''
AND (
o.oss_id::text = w.photo_id
OR o.ext1 = w.photo_id
OR o.file_name ILIKE '%' || w.photo_id || '%'
OR o.original_name ILIKE '%' || w.photo_id || '%'
OR o.url ILIKE '%' || w.photo_id || '%'
)
ORDER BY
CASE
WHEN o.oss_id::text = w.photo_id THEN 0
WHEN o.ext1 = w.photo_id THEN 1
ELSE 2
END
LIMIT 1
) wo ON TRUE
WHERE e.id = #{id}
AND o.status <> 'CANCELLED'
GROUP BY o.id, bc.customer_name, be.name, sc.customer_name, e.name, o.buyer_id, o.seller_id, o.created_at
ORDER BY o.created_at DESC
"""
var resolveOssUrl = (ossUrl, rawId) => {
var url = pickUrl(ossUrl)
if (url) {
return url
}
if (!rawId) {
return ''
}
var idText = (rawId + '').trim().replace(/'/g, "''")
if (!idText) {
return ''
}
var lookupSql = """
SELECT url FROM sys_oss
WHERE oss_id::text = '""" + idText + """'
OR ext1 = '""" + idText + """'
OR file_name ILIKE '%""" + idText + """%'
OR original_name ILIKE '%""" + idText + """%'
OR url ILIKE '%""" + idText + """%'
ORDER BY
CASE
WHEN oss_id::text = '""" + idText + """' THEN 0
WHEN ext1 = '""" + idText + """' THEN 1
ELSE 2
END
LIMIT 1
"""
var rows = db.select(lookupSql)
if (rows && rows.length > 0) {
return pickUrl(rows[0].url)
}
return pickUrl(idText)
}
var baseRows = db.select(baseSql)
if (!baseRows || baseRows.length == 0) {
exit 404, '未找到供应记录'
}
var base = baseRows[0]
var yakCount = base.yakCount ? base.yakCount : (base.yak_count ? base.yak_count : 0)
var soldCount = base.soldCount ? base.soldCount : (base.sold_count ? base.sold_count : 0)
var orderCount = base.orderCount ? base.orderCount : (base.order_count ? base.order_count : 0)
var progress = 0
if (yakCount > 0) {
progress = Math.round(soldCount * 100 / yakCount)
}
var entryPhotoId = base.entryPhotoId ? base.entryPhotoId : (base.entry_photo_id ? base.entry_photo_id : '')
var certFileId = base.certificateImageFileId ? base.certificateImageFileId : (base.certificate_image_file_id ? base.certificate_image_file_id : '')
var entryPhoto = resolveOssUrl(base.entryOssUrl ? base.entryOssUrl : base.entry_oss_url, entryPhotoId)
if (!entryPhoto) {
entryPhoto = pickUrl(base.carEntryImage ? base.carEntryImage : base.car_entry_image)
}
var quarantineCert = resolveOssUrl(base.certOssUrl ? base.certOssUrl : base.cert_oss_url, certFileId)
var orderRows = db.select(orderSql)
var orders = []
for (order in orderRows) {
var weightPhotoId = order.weightPhotoId ? order.weightPhotoId : (order.weight_photo_id ? order.weight_photo_id : '')
var weightPhoto = resolveOssUrl(order.weightPhotoUrl ? order.weightPhotoUrl : order.weight_photo_url, weightPhotoId)
orders.push({
id: order.id,
buyer: order.buyer ? order.buyer : '',
seller: order.seller ? order.seller : '',
tradeTime: order.tradeTime ? order.tradeTime : (order.trade_time ? order.trade_time : ''),
quantity: order.quantity ? order.quantity : 0,
weight: order.weight ? order.weight : 0,
weightPhoto: weightPhoto,
weightPhotoId: weightPhotoId
})
}
return {
id: base.id,
name: base.name ? base.name : '',
licensePlate: base.vehicleNo ? base.vehicleNo : (base.vehicle_no ? base.vehicle_no : ''),
yakCount: yakCount,
contact: base.phone ? base.phone : '',
origin: base.origin ? base.origin : '',
quarantineNo: base.quarantineNo ? base.quarantineNo : (base.quarantine_no ? base.quarantine_no : ''),
entryTime: base.entryTime ? base.entryTime : (base.entry_time ? base.entry_time : ''),
progress: progress,
tradedCount: soldCount,
pendingCount: yakCount - soldCount,
orderCount: orderCount,
entryPhoto: entryPhoto,
entryPhotoId: entryPhotoId,
quarantineCert: quarantineCert,
quarantineCertId: certFileId,
orders: orders
}