0,1歳日誌データ連携

This commit is contained in:
2025-02-13 16:43:24 +08:00
parent fe23e8e8b3
commit 41a6388d73
4 changed files with 197 additions and 27 deletions

Binary file not shown.

View File

@@ -136,7 +136,7 @@ function createBtnGroupArea(groupId, btnLabel, btnOnClick, { btnElId = false, ye
function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElId }) { function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElId }) {
const year = yearElId && map[yearElId].value; const year = yearElId && map[yearElId].value;
const month = monthElId && map[monthElId].value; const month = monthElId && map[monthElId].value;
const date = dateElId && (map[dateElId].value === 'end' ? getLastDate(year, month) : map[dateElId].value); const date = dateElId && (map[dateElId].value === 'end' ? getLastDate(year, month).getDate() : map[dateElId].value);
const className = classElId && map[classElId].value; const className = classElId && map[classElId].value;
const errorMsgs = []; const errorMsgs = [];
@@ -170,7 +170,7 @@ function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElI
} }
function getLastDate(year, month) { function getLastDate(year, month) {
return new Date(year, month, 0).getDate(); return new Date(year, month, 0);
} }
function createBtn(id, label, onClick) { function createBtn(id, label, onClick) {
@@ -239,9 +239,9 @@ function getFormatDateString(dateObjOrYear, month, date) {
} }
const formatY = year; const formatY = year;
const formatM = String(month).padStart(2, '0'); const formatM = String(month).padStart(2, '0');
const formatD = date ? String(date).padStart(2, '0') : ''; const formatD = String(date).padStart(2, '0');
return formatY + '-' + formatM + (formatD && ('-' + formatD)); return `${formatY}-${formatM}-${formatD}`;
} }
function loading(show, text) { function loading(show, text) {

View File

@@ -17,24 +17,194 @@ class Link1Handler {
showError(false); showError(false);
const api = new KintoneRestAPIClient(); const api = new KintoneRestAPIClient();
const dateString = getFormatDateString(year, month);
// 本アプリからデータを読み取る // 本アプリからデータを読み取る
// const currentAppRecords = await this.getRecords(api, dateString); const currentAppRecords = await this.getRecords(api, year, month);
// if (!currentAppRecords) { if (!currentAppRecords) {
// // エラー // エラー
// loading(false); loading(false);
// return; return;
// } }
// const record = this.generateRecord(dateString, currentAppRecords); // console.log(currentAppRecords);
// console.log(record); const recordsWrapper = await this.generateRecords(api, year, month, currentAppRecords);
// console.log(recordsWrapper);
// const result = await this.insertOrUpdateData(api, record, dateString); const result = await this.insertOrUpdateData(api, recordsWrapper);
// if (result) { if (result) {
// showSuccess(true, "日誌データ連携作成完了"); // TODO dialog
// } showSuccess(true, "日誌データ連携作成完了");
// loading(false); this.showSuccessDialog(result, year, month);
}
loading(false);
}
showSuccessDialog(result, year, month) {
const dateString = year + '年' + month + '月';
const contentEl = document.createElement('div');
contentEl.style.fontSize = '16px';
if (result.updateResult && result.insertResult) {
contentEl.innerHTML = `${dateString}${result.updateResult.records.length}人の園児の日誌を更新し、${result.insertResult.records.length}人の園児の日誌を新規作成しました。`
} else if (result.updateResult) {
contentEl.innerHTML = `${dateString}${result.updateResult.records.length}人の園児の日誌が更新されました。`;
} else if (result.insertResult) {
contentEl.innerHTML = `${dateString}${result.insertResult.records.length}人の園児の日誌が生成されました`;
} else {
contentEl.innerHTML = `${dateString}の園児の日誌に生成するデータはありませんでした。`
}
showDialog({
title: '日誌データ連携作成完了',
content: contentEl,
ok: 'アプリへ行く',
cancel: '閉じる',
onOk: () => { window.open(`${location.origin}/k/${env["0,1歳日誌出力用"].appId}/`) },
});
}
insertOrUpdateData = async (api, recordsWrapper) => {
try {
const param = {
app: env["0,1歳日誌出力用"].appId,
}
let insertResult;
if (recordsWrapper['insert'].length) {
param.records = recordsWrapper['insert'];
insertResult = await api.record.addAllRecords(param);
}
let updateResult;
if (recordsWrapper['update'].length) {
param.records = recordsWrapper['update'];
// 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します
updateResult = await api.record.updateAllRecords(param);
}
return {
recordsWrapper,
insertResult,
updateResult
};
} catch (e) {
showError(true, '日誌データ連携作成失敗\n - ' + e);
}
};
generateRecords = async (api, year, month, records) => {
const generatedRecordIdMap = await this.getGeneratedRecordIdMap(api, year, month);
if (!generatedRecordIdMap) {
// エラー
loading(false);
return;
}
const kidsMap = {};
records.forEach((record) => {
const uniqueKey = record['園児ユニークキー'].value;
let map = kidsMap[uniqueKey];
if (!map) {
map = {
existId: generatedRecordIdMap[uniqueKey],
list: [],
'年': { 'value': Number(year) },
'月': { 'value': Number(month) },
'クラス': { 'value': record['クラス'].value },
'出席番号': { 'value': record['出席番号'].value },
'園児名': { 'value': record['園児名'].value },
'園児ユニークキー': { 'value': uniqueKey },
'帳票出力用_テーブル': { 'value': undefined },
};
kidsMap[uniqueKey] = map
}
record['date'] = Number(record['登園日'].value.split('-')[2]);
map.list.push(record);
});
// console.log(generatedRecordIdMap);
// console.log(kidsMap);
const result = {
'update': [],
'insert': []
}
Object.values(kidsMap).forEach((recordWrapper) => {
const subtable = Array.from({ length: 31 }, (_e, i) => ({
value: {
'日付': { 'value': i + 1 },
'出欠': { 'value': '出席停止' },
}
}));
recordWrapper.list.forEach((record) => {
subtable[record['date'] - 1] = {
value: {
'日付': { 'value': record['date'] },
'出欠': { 'value': record['出欠'].value },
'降園': { 'value': record['帰園時刻'].value },
'検温時刻1': { 'value': record['検温時刻1'].value },
'体温1': { 'value': record['体温1'].value },
'検温時刻2': { 'value': record['検温時刻2'].value },
'体温2': { 'value': record['体温2'].value },
'検温時刻3': { 'value': record['検温時刻3'].value },
'体温3': { 'value': record['体温3'].value },
'食事量': { 'value': record['食事量_結合'].value },
'排便': { 'value': record['排便'].value },
'睡眠開始時間1': { 'value': record['睡眠開始時間1'].value },
'睡眠終了時間1': { 'value': record['睡眠終了時間1'].value },
'睡眠開始時間2': { 'value': record['睡眠開始時間2'].value },
'睡眠終了時間2': { 'value': record['睡眠終了時間2'].value },
'保護者から': { 'value': record['保護者から'].value },
'園での様子_伝達事項': { 'value': record['園での様子_伝達事項'].value },
'評価反省': { 'value': record['評価反省'].value },
}
}
})
recordWrapper['帳票出力用_テーブル'].value = subtable;
const id = recordWrapper['existId'];
delete recordWrapper['list'];
delete recordWrapper['existId'];
if (id) {
result['update'].push({
id,
record: recordWrapper
});
} else {
result['insert'].push(recordWrapper);
}
});
return result;
}
getRecords = async (api, year, month) => {
const firstDate = getFormatDateString(year, month, 1)
const lastDate = getFormatDateString(getLastDate(year, month));
try {
return await api.record.getAllRecordsWithId({
app: env["園児別出欠簿入力"].appId,
condition: `学年 in ("0歳児", "1歳児") and 登園日 >= "${firstDate}" and 登園日 <= "${lastDate}"`
});
} catch (e) {
showError(true, '本アプリのデータ読み取りエラー\n - ' + e);
loading(false);
}
}
getGeneratedRecordIdMap = async (api, year, month) => {
try {
const result = await api.record.getAllRecordsWithId({
app: env["0,1歳日誌出力用"].appId,
fields: ["$id", "園児ユニークキー"],
condition: `年 = ${year} and 月 = ${month}`
});
const map = {}
result.forEach((record) => {
const uniqueKey = record['園児ユニークキー'].value;
map[uniqueKey] = record['$id'].value;
});
return map;
} catch (e) {
showError(true, '日誌データ読み取りエラー\n - ' + e);
loading(false);
}
} }
static getInstance(headerSpace) { static getInstance(headerSpace) {

View File

@@ -30,25 +30,26 @@ class Link2Handler {
} }
const record = this.generateRecord(dateString, currentAppRecords); const record = this.generateRecord(dateString, currentAppRecords);
console.log(record); // console.log(record);
const result = await this.insertOrUpdateData(api, record, dateString); const result = await this.insertOrUpdateData(api, record, dateString);
if (result) { if (result) {
// showSuccess(true, "日誌データ連携作成完了"); // showSuccess(true, "日誌データ連携作成完了");
this.showSuccessDialog(result); this.showSuccessDialog(result, year, month, date);
} }
loading(false); loading(false);
} }
showSuccessDialog(result) { showSuccessDialog(result, 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 = `${result.dateString}園児別テーブルに追加するデータはありませんでした。`; contentEl.innerHTML = `${dateString}園児別テーブルに追加するデータはありませんでした。`;
} else if (result.type === 'update') { } else if (result.type === 'update') {
contentEl.innerHTML = `${result.dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`; contentEl.innerHTML = `${dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`;
} else { } else {
contentEl.innerHTML = `${result.dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`; contentEl.innerHTML = `${dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`;
} }
showDialog({ showDialog({
title: '日誌データ連携作成完了', title: '日誌データ連携作成完了',
@@ -68,7 +69,7 @@ class Link2Handler {
condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"` condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"`
}); });
} catch (e) { } catch (e) {
showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e); showError(true, 'アプリのデータ読み取りエラー\n - ' + e);
loading(false); loading(false);
} }
} }
@@ -108,7 +109,6 @@ class Link2Handler {
id: generatedRecordId, id: generatedRecordId,
type: generatedRecordId ? 'update' : 'insert', type: generatedRecordId ? 'update' : 'insert',
size: record["園児別テーブル"].value.length, size: record["園児別テーブル"].value.length,
dateString
}; };
let awaitResult; let awaitResult;