This commit is contained in:
seb
2026-09-29 18:26:42 +02:00
parent 30820ba8c7
commit 69bd0935cc
14 changed files with 710 additions and 20 deletions

View File

@@ -102,6 +102,50 @@ SET is_father = 1
WHERE id = @articleId
`;
export const TOGGLE_FATHER = `
UPDATE article
SET is_father = @isFather,
fatherattributename_id = @fatherAttributeNameId
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
)
OUTPUT INSERTED.id
VALUES (
@companyId, @sku, @unitId, @unitamount, @weight, @trackStock, @brandId, 0, @fatherArticleId, @fathersort
)
`;
export const UNLINK_CHILD = `
UPDATE article
SET fatherarticle_id = NULL,
fathersort = 0
WHERE id = @articleId AND fatherarticle_id = @fatherArticleId
`;
export const LIST_CHILDREN_BY_FATHER_ID = `
SELECT
child.id,
child.sku,
child.fathersort,
child.track_stock AS trackStock,
child.unitamount,
child.weight,
at_child.name,
at_child.slug,
av.id AS attributeValueId,
av.value AS attributeValue
FROM article child
LEFT JOIN articletext at_child ON at_child.article_id = child.id AND at_child.language_id = @languageId
LEFT JOIN articleattributevalue aav ON aav.article_id = child.id
LEFT JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = @attributeId
WHERE child.fatherarticle_id = @fatherArticleId
ORDER BY child.fathersort ASC, child.id ASC
`;
export const SET_FATHER_SORT = `
UPDATE article
SET fathersort = @fathersort
@@ -406,8 +450,28 @@ SELECT
FROM article child
WHERE child.fatherarticle_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,
(
SELECT
child.id,
child.sku,
child.fathersort,
at_child.name,
av.id AS attributeValueId,
av.value AS attributeValue
FROM article child
LEFT JOIN articletext at_child ON at_child.article_id = child.id AND at_child.language_id = auth.languageId
LEFT JOIN articleattributevalue aav ON aav.article_id = child.id
LEFT JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = a.fatherattributename_id
WHERE child.fatherarticle_id = a.id
ORDER BY child.fathersort ASC, child.id ASC
FOR JSON PATH
) AS childrenJson,
(
SELECT ca2.category_id
FROM categoryarticle ca2
@@ -510,6 +574,7 @@ SELECT
) 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
WHERE a.id = auth.articleId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS articleJson

View File

@@ -323,6 +323,7 @@ BEGIN
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,
created_at DATETIME2 NOT NULL CONSTRAINT DF_article_created_at DEFAULT SYSUTCDATETIME(),
updated_at DATETIME2 NOT NULL CONSTRAINT DF_article_updated_at DEFAULT SYSUTCDATETIME(),
@@ -332,6 +333,7 @@ BEGIN
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 UQ_article_company_sku UNIQUE (company_id, sku)
);
@@ -341,6 +343,14 @@ BEGIN
ON article (fatherarticle_id, fathersort)
WHERE fatherarticle_id IS NOT NULL;
END
ELSE
BEGIN
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('article') AND name = 'fatherattributename_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;
END
END
ELSE IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('article') AND name = 'track_stock')
BEGIN
ALTER TABLE article ADD track_stock BIT NOT NULL CONSTRAINT DF_article_track_stock DEFAULT 1;
@@ -988,6 +998,9 @@ export const SCHEMA_SCRIPTS = [
INIT_OPTIONSLIST,
INIT_ARTICLELIST,
INIT_BRAND,
INIT_PROPERTYNAME,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLE,
INIT_ARTICLEEAN,
INIT_ARTICLEPIC,
@@ -1000,10 +1013,7 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLENAME,
INIT_PRICE,
INIT_ARTICLE_SUPPLIER,
INIT_PROPERTYNAME,
INIT_ARTICLEPROPERTY,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLEATTRIBUTEVALUE,
INIT_MANIFEST,
INIT_MANIFESTARTICLE,

View File

@@ -406,6 +406,11 @@ export class BetterSqlite3Provider extends BetterSqlite3Executor {
`ALTER TABLE article ADD COLUMN track_stock INTEGER NOT NULL DEFAULT 1`,
);
}
if (articleCols.length > 0 && !articleCols.includes('fatherattributename_id')) {
this.db.exec(
`ALTER TABLE article ADD COLUMN fatherattributename_id INTEGER REFERENCES attributename(id) ON DELETE NO ACTION`,
);
}
const warehouseCols = this.db.pragma('table_info("warehouse")').map((c) => c.name);
if (warehouseCols.length > 0 && !warehouseCols.includes('sort')) {

View File

@@ -3,12 +3,13 @@ import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'article');
export const {
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CREATE,
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CREATE, CREATE_CHILD,
DELETE_CATEGORIES_BY_ARTICLE, DELETE_EANS_BY_ARTICLE, EAN_EXISTS,
GET_BRAND_BY_COMPANY_AND_ID,
GET_CATEGORY_ID_BY_SKU, GET_CATEGORY_TREE_FLAT, GET_ID_BY_SKU,
LIST_CHILDREN_BY_FATHER_ID,
SET_FATHER_SORT,
SET_IS_FATHER, UPDATE_BRAND, UPDATE_FATHER_BY_SKU, UPDATE_TRACK_STOCK, UPDATE_UNIT,
SET_IS_FATHER, TOGGLE_FATHER, UNLINK_CHILD, UPDATE_BRAND, UPDATE_FATHER_BY_SKU, UPDATE_TRACK_STOCK, UPDATE_UNIT,
UPDATE_UNITAMOUNT, UPDATE_UNIT_AND_AMOUNT, UPDATE_WEIGHT,
} = queries;
@@ -133,7 +134,24 @@ SELECT auth.*,
'optionsCount', (SELECT COUNT(*) FROM optionslistarticle WHERE optionslist_id = a.optionslist_id),
'articleListCount', (SELECT COUNT(*) FROM articlelistarticle WHERE articlelist_id = a.articlelist_id),
'childCount', (SELECT COUNT(*) FROM article WHERE fatherarticle_id = a.id),
'isFather', a.is_father,
'fatherAttributeNameId', a.fatherattributename_id,
'fatherAttributeName', fan.name,
'fatherAttributeExclusive', fan.exclusive,
'fatherArticleId', a.fatherarticle_id, 'fatherSku', father.sku,
'childrenJson', json((SELECT COALESCE(json_group_array(json_object(
'id', child.id,
'sku', child.sku,
'fathersort', child.fathersort,
'name', at_child.name,
'attributeValueId', av.id,
'attributeValue', av.value
)), '[]') FROM article child
LEFT JOIN articletext at_child ON at_child.article_id = child.id AND at_child.language_id = auth.languageId
LEFT JOIN articleattributevalue aav ON aav.article_id = child.id
LEFT JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = a.fatherattributename_id
WHERE child.fatherarticle_id = a.id
ORDER BY child.fathersort ASC, child.id ASC)),
'categoryIdsJson', json(${JSON_CATEGORIES}),
'photosJson', json((SELECT COALESCE(json_group_array(json_object(
'sort', sort, 'fullHash', hash, 'fullW', w, 'fullH', h,
@@ -197,4 +215,5 @@ LEFT JOIN pic bp ON bp.id = (
SELECT pic_id FROM brandpic WHERE brand_id = b.id ORDER BY sort, id LIMIT 1
)
LEFT JOIN article father ON father.id = a.fatherarticle_id
LEFT JOIN attributename fan ON fan.id = a.fatherattributename_id
`;

View File

@@ -223,6 +223,7 @@ CREATE TABLE IF NOT EXISTS article (
is_father INTEGER NOT NULL DEFAULT 0,
fatherarticle_id INTEGER,
fathersort INTEGER NOT NULL DEFAULT 0,
fatherattributename_id INTEGER,
created_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
updated_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
UNIQUE (company_id, sku),
@@ -231,7 +232,8 @@ CREATE TABLE IF NOT EXISTS article (
FOREIGN KEY (brand_id) REFERENCES brand(id) ON DELETE SET NULL,
FOREIGN KEY (articlelist_id) REFERENCES articlelist(id) ON DELETE SET NULL,
FOREIGN KEY (optionslist_id) REFERENCES optionslist(id) ON DELETE SET NULL,
FOREIGN KEY (fatherarticle_id) REFERENCES article(id) ON DELETE NO ACTION
FOREIGN KEY (fatherarticle_id) REFERENCES article(id) ON DELETE NO ACTION,
FOREIGN KEY (fatherattributename_id) REFERENCES attributename(id) ON DELETE NO ACTION
);
CREATE UNIQUE INDEX IF NOT EXISTS UQ_article_fatherarticle_fathersort
ON article(fatherarticle_id, fathersort)
@@ -699,6 +701,9 @@ export const SCHEMA_SCRIPTS = [
INIT_OPTIONSLIST,
INIT_ARTICLELIST,
INIT_BRAND,
INIT_PROPERTYNAME,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLE,
INIT_ARTICLEEAN,
INIT_ARTICLEPIC,
@@ -711,10 +716,7 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLENAME,
INIT_PRICE,
INIT_ARTICLE_SUPPLIER,
INIT_PROPERTYNAME,
INIT_ARTICLEPROPERTY,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLEATTRIBUTEVALUE,
INIT_MANIFEST,
INIT_MANIFESTARTICLE,