add grade grouping
This commit is contained in:
@@ -29,10 +29,14 @@ class Link2Handler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const record = this.generateRecord(dateString, currentAppRecords);
|
const groupedRecords = this.groupingAndSort(currentAppRecords);
|
||||||
// console.log(record);
|
|
||||||
|
|
||||||
const result = await this.insertOrUpdateData(api, record, dateString);
|
const records = [];
|
||||||
|
for (const item of groupedRecords) {
|
||||||
|
records.push(this.generateRecord(dateString, item));
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await this.insertOrUpdateData(api, records, dateString);
|
||||||
if (result) {
|
if (result) {
|
||||||
// showSuccess(true, "日誌データ連携作成完了");
|
// showSuccess(true, "日誌データ連携作成完了");
|
||||||
this.showSuccessDialog(result, year, month, date);
|
this.showSuccessDialog(result, year, month, date);
|
||||||
@@ -41,15 +45,17 @@ class Link2Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
showSuccessDialog(result, year, month, date) {
|
showSuccessDialog(result, year, month, date) {
|
||||||
const dateString = year + '年' + month + '月' + date + '日';
|
const dateString = year + '年' + month + '月' + date + '日';
|
||||||
const contentEl = document.createElement('div');
|
const contentEl = document.createElement('div');
|
||||||
contentEl.style.fontSize = '16px';
|
contentEl.style.fontSize = '16px';
|
||||||
if (!result.size) {
|
if (!result.size) {
|
||||||
contentEl.innerHTML = `${dateString}の園児別テーブルに追加するデータはありませんでした。`;
|
contentEl.innerHTML = `${dateString}の園児別テーブルに追加するデータはありませんでした。`;
|
||||||
} else if (result.type === 'update') {
|
} else if (!result.insert) {
|
||||||
contentEl.innerHTML = `${dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`;
|
contentEl.innerHTML = `${dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`;
|
||||||
} else {
|
} else if (!result.update) {
|
||||||
contentEl.innerHTML = `${dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`;
|
contentEl.innerHTML = `${dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`;
|
||||||
|
} else {
|
||||||
|
contentEl.innerHTML = `${dateString}の${result.update}人の園児の日誌を更新し、${result.insert}人の園児の日誌を新規作成しました。`
|
||||||
}
|
}
|
||||||
showDialog({
|
showDialog({
|
||||||
title: '日誌データ連携作成完了',
|
title: '日誌データ連携作成完了',
|
||||||
@@ -57,7 +63,7 @@ class Link2Handler {
|
|||||||
ok: 'アプリへ行く',
|
ok: 'アプリへ行く',
|
||||||
cancel: '閉じる',
|
cancel: '閉じる',
|
||||||
dataHolder: result,
|
dataHolder: result,
|
||||||
onOk: (dataHolder) => { window.open(`${location.origin}/k/${env["2歳以上日誌出力用"].appId}/show#record=` + dataHolder.id) },
|
onOk: () => { window.open(`${location.origin}/k/${env["2歳以上日誌出力用"].appId}/`) },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +71,7 @@ class Link2Handler {
|
|||||||
try {
|
try {
|
||||||
return await api.record.getAllRecordsWithId({
|
return await api.record.getAllRecordsWithId({
|
||||||
app: env["園児別出欠簿入力"].appId,
|
app: env["園児別出欠簿入力"].appId,
|
||||||
fields: ['クラス', "園児名", "園での様子_伝達事項", "評価反省"],
|
fields: ['クラス', '学年', "園児名", "園での様子_伝達事項", "評価反省"],
|
||||||
condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"`
|
condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"`
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -74,11 +80,49 @@ class Link2Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateRecord = (todayString, currentAppRecords) => {
|
groupingAndSort = (data) => {
|
||||||
|
const groupedByGrade = {};
|
||||||
|
// 1. group by 学年
|
||||||
|
data.forEach((record) => {
|
||||||
|
const grade = record['学年'].value;
|
||||||
|
if (!groupedByGrade[grade]) {
|
||||||
|
groupedByGrade[grade] = [];
|
||||||
|
}
|
||||||
|
groupedByGrade[grade].push(record);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. sort by クラス
|
||||||
|
const classOrder = {};
|
||||||
|
classItems.forEach((item, index) => {
|
||||||
|
classOrder[item.value] = index;
|
||||||
|
});
|
||||||
|
for (const grade in groupedByGrade) {
|
||||||
|
groupedByGrade[grade].sort((a, b) => {
|
||||||
|
return classOrder[a["クラス"].value] - classOrder[b["クラス"].value];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. return a list
|
||||||
|
return termItems.reduce((acc, term) => {
|
||||||
|
const grade = term.value;
|
||||||
|
// Skip 0歳児 and 1歳児
|
||||||
|
if (grade === "0歳児" || grade === "1歳児") {
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
acc.push({
|
||||||
|
grade: grade,
|
||||||
|
items: groupedByGrade[grade] || []
|
||||||
|
});
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
generateRecord = (todayString, groupedRecords) => {
|
||||||
return {
|
return {
|
||||||
'登園日': { 'value': todayString },
|
'登園日': { 'value': todayString },
|
||||||
|
'学年': { 'value': groupedRecords.grade },
|
||||||
'園児別テーブル': {
|
'園児別テーブル': {
|
||||||
'value': currentAppRecords.map((record) => {
|
'value': groupedRecords.items.map((record) => {
|
||||||
return {
|
return {
|
||||||
'value': {
|
'value': {
|
||||||
'クラス名': { 'value': record['クラス'].value },
|
'クラス名': { 'value': record['クラス'].value },
|
||||||
@@ -92,39 +136,66 @@ class Link2Handler {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
insertOrUpdateData = async (api, record, dateString) => {
|
insertOrUpdateData = async (api, records, dateString) => {
|
||||||
try {
|
const results = [];
|
||||||
const generatedRecords = await api.record.getAllRecordsWithId({
|
const generatedRecords = await api.record.getAllRecordsWithId({
|
||||||
app: env["2歳以上日誌出力用"].appId,
|
app: env["2歳以上日誌出力用"].appId,
|
||||||
fields: ["$id", "登園日"],
|
fields: ["$id", "学年"],
|
||||||
condition: `登園日 = "${dateString}"`
|
condition: `登園日 = "${dateString}"`
|
||||||
})
|
})
|
||||||
const generatedRecordId = generatedRecords[0]?.$id.value;
|
const existIdMap = generatedRecords.reduce((map, item) => {
|
||||||
|
const grade = item['学年'].value;
|
||||||
|
const id = item['$id'].value;
|
||||||
|
map[grade] = id;
|
||||||
|
return map;
|
||||||
|
}, {});
|
||||||
|
|
||||||
const param = {
|
for (const record of records) {
|
||||||
app: env["2歳以上日誌出力用"].appId,
|
try {
|
||||||
record
|
const generatedRecordId = existIdMap[record["学年"].value];
|
||||||
}
|
const param = {
|
||||||
const result = {
|
app: env["2歳以上日誌出力用"].appId,
|
||||||
id: generatedRecordId,
|
record
|
||||||
type: generatedRecordId ? 'update' : 'insert',
|
}
|
||||||
size: record["園児別テーブル"].value.length,
|
const result = {
|
||||||
};
|
id: generatedRecordId,
|
||||||
|
type: generatedRecordId ? 'update' : 'insert',
|
||||||
|
size: record["園児別テーブル"].value.length,
|
||||||
|
};
|
||||||
|
|
||||||
let awaitResult;
|
let awaitResult;
|
||||||
if (result.type === 'update') {
|
|
||||||
param.id = generatedRecordId;
|
if (result.type === 'update') {
|
||||||
// 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します
|
param.id = generatedRecordId;
|
||||||
awaitResult = await api.record.updateRecord(param);
|
// 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します
|
||||||
} else {
|
awaitResult = await api.record.updateRecord(param);
|
||||||
awaitResult = await api.record.addRecord(param);
|
} else {
|
||||||
|
awaitResult = await api.record.addRecord(param);
|
||||||
|
}
|
||||||
|
Object.assign(result, awaitResult);
|
||||||
|
results.push(result);
|
||||||
|
} catch (e) {
|
||||||
|
showError(true, '日誌データ連携作成失敗\n - ' + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results.reduce((result, item) => {
|
||||||
|
// Sum all sizes
|
||||||
|
result.size += item.size;
|
||||||
|
|
||||||
|
// Sum based on type
|
||||||
|
if (item.type === 'insert') {
|
||||||
|
result.insert += item.size;
|
||||||
|
} else if (item.type === 'update') {
|
||||||
|
result.update += item.size;
|
||||||
}
|
}
|
||||||
Object.assign(result, awaitResult);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e) {
|
}, {
|
||||||
showError(true, '日誌データ連携作成失敗\n - ' + e);
|
size: 0, // Total sum of all sizes
|
||||||
}
|
insert: 0, // Sum of insert sizes
|
||||||
|
update: 0 // Sum of update sizes
|
||||||
|
});;
|
||||||
};
|
};
|
||||||
|
|
||||||
static getInstance(headerSpace) {
|
static getInstance(headerSpace) {
|
||||||
|
|||||||
Reference in New Issue
Block a user