u
This commit is contained in:
@@ -21,7 +21,7 @@ OUTER APPLY (
|
||||
ORDER BY bp.sort ASC, bp.id ASC
|
||||
) first_brand_pic
|
||||
LEFT JOIN pic brand_pic ON brand_pic.id = first_brand_pic.pic_id
|
||||
LEFT JOIN article father ON father.id = a.fatherarticle_id
|
||||
LEFT JOIN article parent ON parent.id = a.parentarticle_id
|
||||
`;
|
||||
|
||||
export const ARTICLE_LIST_COLUMNS = `
|
||||
@@ -32,7 +32,7 @@ export const ARTICLE_LIST_COLUMNS = `
|
||||
pr.net,
|
||||
t.rate AS taxRate,
|
||||
a.track_stock,
|
||||
a.is_father AS isFather,
|
||||
a.is_parent AS isParent,
|
||||
CASE
|
||||
WHEN a.optionslist_id IS NOT NULL THEN (
|
||||
SELECT COUNT(*)
|
||||
@@ -52,10 +52,10 @@ export const ARTICLE_LIST_COLUMNS = `
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM article child
|
||||
WHERE child.fatherarticle_id = a.id
|
||||
WHERE child.parentarticle_id = a.id
|
||||
) AS childCount,
|
||||
a.fatherarticle_id AS fatherArticleId,
|
||||
father.sku AS fatherSku,
|
||||
a.parentarticle_id AS parentArticleId,
|
||||
parent.sku AS parentSku,
|
||||
pic.hash AS fullHash,
|
||||
pic.thumbw AS thumbW,
|
||||
pic.thumbh AS thumbH,
|
||||
@@ -75,7 +75,7 @@ INNER JOIN categoryarticle ca ON ca.article_id = a.id
|
||||
WHERE a.sku = @sku AND (@companyId IS NULL OR a.company_id = @companyId)
|
||||
`;
|
||||
|
||||
/** Resolve an article id by sku (used to wire father/child compound links). */
|
||||
/** Resolve an article id by sku (used to wire parent/child compound links). */
|
||||
export const GET_ID_BY_SKU = `
|
||||
SELECT TOP 1 id AS articleId
|
||||
FROM article
|
||||
@@ -89,49 +89,49 @@ FROM articleean
|
||||
WHERE ean = @ean
|
||||
`;
|
||||
|
||||
/** Link an article to its father article and mark the father. */
|
||||
export const UPDATE_FATHER_BY_SKU = `
|
||||
/** Link an article to its parent article and mark the parent. */
|
||||
export const UPDATE_PARENT_BY_SKU = `
|
||||
UPDATE article
|
||||
SET fatherarticle_id = @fatherArticleId,
|
||||
fathersort = COALESCE(@fathersort, 0)
|
||||
SET parentarticle_id = @parentArticleId,
|
||||
parentsort = COALESCE(@parentsort, 0)
|
||||
WHERE sku = @sku AND (@companyId IS NULL OR company_id = @companyId)
|
||||
`;
|
||||
|
||||
export const SET_IS_FATHER = `
|
||||
export const SET_IS_PARENT = `
|
||||
UPDATE article
|
||||
SET is_father = 1
|
||||
SET is_parent = 1
|
||||
WHERE id = @articleId
|
||||
`;
|
||||
|
||||
export const TOGGLE_FATHER = `
|
||||
export const TOGGLE_PARENT = `
|
||||
UPDATE article
|
||||
SET is_father = @isFather,
|
||||
fatherattributename_id = @fatherAttributeNameId
|
||||
SET is_parent = @isParent,
|
||||
parentattributename_id = @parentAttributeNameId
|
||||
WHERE id = @articleId
|
||||
`;
|
||||
|
||||
export const CREATE_CHILD = `
|
||||
INSERT INTO article (
|
||||
company_id, sku, unit_id, unitamount, weight, track_stock, brand_id, is_father, fatherarticle_id, fathersort
|
||||
company_id, sku, unit_id, unitamount, weight, track_stock, brand_id, is_parent, parentarticle_id, parentsort
|
||||
)
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (
|
||||
@companyId, @sku, @unitId, @unitamount, @weight, @trackStock, @brandId, 0, @fatherArticleId, @fathersort
|
||||
@companyId, @sku, @unitId, @unitamount, @weight, @trackStock, @brandId, 0, @parentArticleId, @parentsort
|
||||
)
|
||||
`;
|
||||
|
||||
export const UNLINK_CHILD = `
|
||||
UPDATE article
|
||||
SET fatherarticle_id = NULL,
|
||||
fathersort = 0
|
||||
WHERE id = @articleId AND fatherarticle_id = @fatherArticleId
|
||||
SET parentarticle_id = NULL,
|
||||
parentsort = 0
|
||||
WHERE id = @articleId AND parentarticle_id = @parentArticleId
|
||||
`;
|
||||
|
||||
export const LIST_CHILDREN_BY_FATHER_ID = `
|
||||
export const LIST_CHILDREN_BY_PARENT_ID = `
|
||||
SELECT
|
||||
child.id,
|
||||
child.sku,
|
||||
child.fathersort,
|
||||
child.parentsort,
|
||||
child.track_stock AS trackStock,
|
||||
child.unitamount,
|
||||
child.weight,
|
||||
@@ -145,14 +145,14 @@ LEFT JOIN (
|
||||
articleattributevalue aav
|
||||
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = @attributeId
|
||||
) ON aav.article_id = child.id
|
||||
WHERE child.fatherarticle_id = @fatherArticleId
|
||||
ORDER BY child.fathersort ASC, child.id ASC
|
||||
WHERE child.parentarticle_id = @parentArticleId
|
||||
ORDER BY child.parentsort ASC, child.id ASC
|
||||
`;
|
||||
|
||||
export const SET_FATHER_SORT = `
|
||||
export const SET_PARENT_SORT = `
|
||||
UPDATE article
|
||||
SET fathersort = @fathersort
|
||||
WHERE id = @articleId AND fatherarticle_id = @fatherArticleId
|
||||
SET parentsort = @parentsort
|
||||
WHERE id = @articleId AND parentarticle_id = @parentArticleId
|
||||
`;
|
||||
|
||||
/** SKU → article/category + company auth in one round-trip. */
|
||||
@@ -484,19 +484,19 @@ SELECT
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM article child
|
||||
WHERE child.fatherarticle_id = a.id
|
||||
WHERE child.parentarticle_id = a.id
|
||||
) AS childCount,
|
||||
a.is_father AS isFather,
|
||||
a.fatherattributename_id AS fatherAttributeNameId,
|
||||
fan.name AS fatherAttributeName,
|
||||
fan.exclusive AS fatherAttributeExclusive,
|
||||
a.fatherarticle_id AS fatherArticleId,
|
||||
fa.sku AS fatherSku,
|
||||
a.is_parent AS isParent,
|
||||
a.parentattributename_id AS parentAttributeNameId,
|
||||
fan.name AS parentAttributeName,
|
||||
fan.exclusive AS parentAttributeExclusive,
|
||||
a.parentarticle_id AS parentArticleId,
|
||||
fa.sku AS parentSku,
|
||||
(
|
||||
SELECT
|
||||
child.id,
|
||||
child.sku,
|
||||
child.fathersort,
|
||||
child.parentsort,
|
||||
at_child.name,
|
||||
av.id AS attributeValueId,
|
||||
av.value AS attributeValue
|
||||
@@ -504,10 +504,10 @@ SELECT
|
||||
LEFT JOIN articletext at_child ON at_child.article_id = child.id AND at_child.language_id = auth.languageId
|
||||
LEFT JOIN (
|
||||
articleattributevalue aav
|
||||
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = a.fatherattributename_id
|
||||
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = a.parentattributename_id
|
||||
) ON aav.article_id = child.id
|
||||
WHERE child.fatherarticle_id = a.id
|
||||
ORDER BY child.fathersort ASC, child.id ASC
|
||||
WHERE child.parentarticle_id = a.id
|
||||
ORDER BY child.parentsort ASC, child.id ASC
|
||||
FOR JSON PATH
|
||||
) AS childrenJson,
|
||||
(
|
||||
@@ -637,8 +637,8 @@ SELECT
|
||||
ORDER BY bp2.sort ASC, bp2.id ASC
|
||||
) first_brand_pic
|
||||
LEFT JOIN pic bp ON bp.id = first_brand_pic.pic_id
|
||||
LEFT JOIN article fa ON fa.id = a.fatherarticle_id
|
||||
LEFT JOIN attributename fan ON fan.id = a.fatherattributename_id
|
||||
LEFT JOIN article fa ON fa.id = a.parentarticle_id
|
||||
LEFT JOIN attributename fan ON fan.id = a.parentattributename_id
|
||||
WHERE a.id = auth.articleId
|
||||
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
|
||||
) AS articleJson
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
export const GET_CHILD_COUNT = `
|
||||
SELECT COUNT(*) AS count
|
||||
FROM article
|
||||
WHERE fatherarticle_id = @articleId
|
||||
WHERE parentarticle_id = @articleId
|
||||
`;
|
||||
|
||||
export const GET_CHILDREN_IDS = `
|
||||
SELECT id
|
||||
FROM article
|
||||
WHERE fatherarticle_id = @id
|
||||
WHERE parentarticle_id = @id
|
||||
`;
|
||||
|
||||
export const GET_LIST_IDS = `
|
||||
|
||||
@@ -36,10 +36,10 @@ export const ARTICLE_DETAIL_COLUMNS = `
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM article child
|
||||
WHERE child.fatherarticle_id = a.id
|
||||
WHERE child.parentarticle_id = a.id
|
||||
) AS childCount,
|
||||
a.fatherarticle_id AS fatherArticleId,
|
||||
father.sku AS fatherSku
|
||||
a.parentarticle_id AS parentArticleId,
|
||||
parent.sku AS parentSku
|
||||
`;
|
||||
|
||||
export const GET_DETAIL_BY_SKU = `
|
||||
@@ -59,7 +59,7 @@ OUTER APPLY (
|
||||
ORDER BY bp.sort ASC, bp.id ASC
|
||||
) first_brand_pic
|
||||
LEFT JOIN pic brand_pic ON brand_pic.id = first_brand_pic.pic_id
|
||||
LEFT JOIN article father ON father.id = a.fatherarticle_id
|
||||
LEFT JOIN article parent ON parent.id = a.parentarticle_id
|
||||
WHERE a.sku = @sku AND (@companyId IS NULL OR a.company_id = @companyId)
|
||||
`;
|
||||
|
||||
@@ -80,7 +80,7 @@ OUTER APPLY (
|
||||
ORDER BY bp.sort ASC, bp.id ASC
|
||||
) first_brand_pic
|
||||
LEFT JOIN pic brand_pic ON brand_pic.id = first_brand_pic.pic_id
|
||||
LEFT JOIN article father ON father.id = a.fatherarticle_id
|
||||
LEFT JOIN article parent ON parent.id = a.parentarticle_id
|
||||
WHERE a.id = @articleId
|
||||
`;
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ FROM categoryarticle ca
|
||||
INNER JOIN article a ON a.id = ca.article_id
|
||||
${ARTICLE_LIST_JOINS}
|
||||
WHERE ca.category_id = @categoryId
|
||||
AND a.fatherarticle_id IS NULL
|
||||
AND a.parentarticle_id IS NULL
|
||||
ORDER BY a.id
|
||||
`;
|
||||
|
||||
|
||||
@@ -109,10 +109,10 @@ VALUES (
|
||||
)
|
||||
`;
|
||||
|
||||
export const CHECK_ARTICLE_IS_FATHER = `
|
||||
export const CHECK_ARTICLE_IS_PARENT = `
|
||||
SELECT
|
||||
a.id,
|
||||
a.is_father AS isFather,
|
||||
a.is_parent AS isParent,
|
||||
COALESCE(
|
||||
(SELECT at.name FROM articletext at WHERE at.article_id = a.id AND at.language_id = co.default_language_id),
|
||||
(SELECT MIN(at2.name) FROM articletext at2 WHERE at2.article_id = a.id),
|
||||
|
||||
@@ -253,7 +253,7 @@ SELECT TOP (@limit)
|
||||
SELECT TOP 1 p2.hash
|
||||
FROM articlepic ap2
|
||||
INNER JOIN pic p2 ON p2.id = ap2.pic_id
|
||||
WHERE a.fatherarticle_id IS NOT NULL AND ap2.article_id = a.fatherarticle_id
|
||||
WHERE a.parentarticle_id IS NOT NULL AND ap2.article_id = a.parentarticle_id
|
||||
ORDER BY ap2.sort ASC, ap2.id ASC
|
||||
)
|
||||
) AS imgHash,
|
||||
@@ -263,15 +263,15 @@ SELECT TOP (@limit)
|
||||
WHERE ae.article_id = a.id
|
||||
ORDER BY ae.id ASC
|
||||
) AS barcode,
|
||||
a.is_father AS isParent,
|
||||
a.fatherarticle_id AS parentArticleId,
|
||||
a.is_parent AS isParent,
|
||||
a.parentarticle_id AS parentArticleId,
|
||||
(
|
||||
SELECT TOP 1 CONCAT(fan.name, '|', av.value, '|', fan.sort, '|', a.fathersort)
|
||||
SELECT TOP 1 CONCAT(fan.name, '|', av.value, '|', fan.sort, '|', a.parentsort)
|
||||
FROM article fa
|
||||
INNER JOIN attributename fan ON fan.id = fa.fatherattributename_id
|
||||
INNER JOIN attributename fan ON fan.id = fa.parentattributename_id
|
||||
INNER JOIN articleattributevalue aav ON aav.article_id = a.id
|
||||
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = fan.id
|
||||
WHERE a.fatherarticle_id IS NOT NULL AND fa.id = a.fatherarticle_id
|
||||
WHERE a.parentarticle_id IS NOT NULL AND fa.id = a.parentarticle_id
|
||||
) AS variantName,
|
||||
CASE
|
||||
WHEN a.articlelist_id IS NOT NULL AND EXISTS (
|
||||
|
||||
@@ -323,10 +323,10 @@ BEGIN
|
||||
brand_id INT NULL,
|
||||
articlelist_id INT NULL,
|
||||
optionslist_id INT NULL,
|
||||
is_father BIT NOT NULL CONSTRAINT DF_article_is_father DEFAULT 0,
|
||||
fatherarticle_id INT NULL,
|
||||
fatherattributename_id INT NULL,
|
||||
fathersort INT NOT NULL CONSTRAINT DF_article_fathersort DEFAULT 0,
|
||||
is_parent BIT NOT NULL CONSTRAINT DF_article_is_parent DEFAULT 0,
|
||||
parentarticle_id INT NULL,
|
||||
parentattributename_id INT NULL,
|
||||
parentsort INT NOT NULL CONSTRAINT DF_article_parentsort DEFAULT 0,
|
||||
created_at DATETIME2 NOT NULL CONSTRAINT DF_article_created_at DEFAULT SYSUTCDATETIME(),
|
||||
updated_at DATETIME2 NOT NULL CONSTRAINT DF_article_updated_at DEFAULT SYSUTCDATETIME(),
|
||||
CONSTRAINT FK_article_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION,
|
||||
@@ -334,23 +334,23 @@ BEGIN
|
||||
CONSTRAINT FK_article_brand FOREIGN KEY (brand_id) REFERENCES brand(id) ON DELETE SET NULL,
|
||||
CONSTRAINT FK_article_articlelist FOREIGN KEY (articlelist_id) REFERENCES articlelist(id) ON DELETE SET NULL,
|
||||
CONSTRAINT FK_article_optionslist FOREIGN KEY (optionslist_id) REFERENCES optionslist(id) ON DELETE SET NULL,
|
||||
CONSTRAINT FK_article_fatherarticle FOREIGN KEY (fatherarticle_id) REFERENCES article(id) ON DELETE NO ACTION,
|
||||
CONSTRAINT FK_article_fatherattributename FOREIGN KEY (fatherattributename_id) REFERENCES attributename(id) ON DELETE NO ACTION,
|
||||
CONSTRAINT FK_article_parentarticle FOREIGN KEY (parentarticle_id) REFERENCES article(id) ON DELETE NO ACTION,
|
||||
CONSTRAINT FK_article_parentattributename FOREIGN KEY (parentattributename_id) REFERENCES attributename(id) ON DELETE NO ACTION,
|
||||
CONSTRAINT UQ_article_company_sku UNIQUE (company_id, sku)
|
||||
);
|
||||
|
||||
-- Only enforce sibling sort uniqueness among children of the same father.
|
||||
-- Only enforce sibling sort uniqueness among children of the same parent.
|
||||
-- (SQL Server UNIQUE treats NULLs as equal, which would block multiple root articles.)
|
||||
CREATE UNIQUE INDEX UQ_article_fatherarticle_fathersort
|
||||
ON article (fatherarticle_id, fathersort)
|
||||
WHERE fatherarticle_id IS NOT NULL;
|
||||
CREATE UNIQUE INDEX UQ_article_parentarticle_parentsort
|
||||
ON article (parentarticle_id, parentsort)
|
||||
WHERE parentarticle_id IS NOT NULL;
|
||||
END
|
||||
ELSE
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('article') AND name = 'fatherattributename_id')
|
||||
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('article') AND name = 'parentattributename_id')
|
||||
BEGIN
|
||||
ALTER TABLE article ADD fatherattributename_id INT NULL;
|
||||
ALTER TABLE article ADD CONSTRAINT FK_article_fatherattributename FOREIGN KEY (fatherattributename_id) REFERENCES attributename(id) ON DELETE NO ACTION;
|
||||
ALTER TABLE article ADD parentattributename_id INT NULL;
|
||||
ALTER TABLE article ADD CONSTRAINT FK_article_parentattributename FOREIGN KEY (parentattributename_id) REFERENCES attributename(id) ON DELETE NO ACTION;
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('article') AND name = 'track_stock')
|
||||
|
||||
Reference in New Issue
Block a user