ken_nogi/NodeJS/Care7_old/データ解析.js
Kenichiro NOGI 51a95c5e21 NodeJS
2025-07-10 10:12:37 +09:00

364 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { XMLHttpRequest } from 'xmlhttprequest'
//ケアセブン訪問看護データ計算スクリプト
//対象月の指定
let targetMonth;// = '2025-04-01';
//日付データ作成
let startDate;// = new Date(targetMonth);
let endDate;// = new Date(targetMonth);
//endDate.setMonth(endDate.getMonth() + 1, 0);
let startStr;
let endStr;
//HTTPRequestを使うための宣言
//let XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; //XMLHttpRequestの定義
//プリザンターAPIKeyとURL
let apiKey = 'c63feb0027f958152bd376d1fa63fff7c8ee86e0a13de5ab2f2ea14303e6278cf507547a04a83d02d3b32650bac7b85e7e8f31e9ea1ab17ef620b378a2a02f16';
let baseUrl = 'https://nextoffice.next-hd.co.jp/';
//let baseUrl = 'https://neo999.next-hd.net/';
//対象テーブル情報
let tableA = '126841';
let holidayTable = '281723';
let holiDays = [];
let resData = [];
let baseYear;
let baseMonth;
function main() {
if (process.argv.length == 4) {
baseYear = process.argv[2];
baseMonth = process.argv[3];
if (isNaN(baseYear) || isNaN(baseMonth)) {
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
console.log("■ 引数が数字ではありません 実行例↓");
console.log("■ node .\\実行ファイル名 年 月");
console.log("■ 年と月は数字のみ(前0不要)");
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
return false;
} else {
baseYear = parseFloat(baseYear);
baseMonth = parseFloat(baseMonth);
if (baseYear <= 2000 || baseYear > 3000) {
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
console.log("■ 引数が数字ではありません 実行例↓");
console.log("■ node .\\実行ファイル名 年 月");
console.log("■ 年は西暦4桁で指定してください");
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
return false;
}
if (baseMonth < 1 || baseMonth > 12) {
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
console.log("■ 引数が数字ではありません 実行例↓");
console.log("■ node .\\実行ファイル名 年 月");
console.log("■ 月は112で指定してください");
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
return false;
}
}
} else {
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
console.log("■ 引数を正確に指定してください 実行例↓");
console.log("■ node .\\実行ファイル名 年 月");
console.log("■ 年と月は数字のみ(前0不要)");
console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■");
return false;
}
//引数から日付範囲を設定
targetMonth = baseYear + '/' + baseMonth + '/1';
startDate = new Date(targetMonth);
endDate = new Date(targetMonth);
endDate.setMonth(endDate.getMonth() + 1, 0);
startStr = startDate.getFullYear() +
'/' + (startDate.getMonth() + 1).toString().padStart(2, '0') +
'/' + (startDate.getDate()).toString().padStart(2, '0') + ' 00:00:00';
endStr = endDate.getFullYear() +
'/' + (endDate.getMonth() + 1).toString().padStart(2, '0') +
'/' + (endDate.getDate()).toString().padStart(2, '0') + ' 23:59:59';
getHolidayData();
sendRequest(0);
//loadCSVdata();
}
function sendRequest(offset) {
try {
let tableId = tableA;
//送信データ
let json = {
"ApiVersion": 1.1,
"ApiKey": apiKey,
"Offset": offset,
"View": {
"ColumnFilterHash": {
"DateA": "[\"" + startStr + ",\"]",
"DateB": "[\"," + endStr + "\"]"
},
}
};
//データ取得リクエスト
const url = baseUrl + '/pleasanter/api/items/' + tableId + '/get';
const xhr = new XMLHttpRequest(); //XMLHttpRequestの作成
//HTTPリクエスト実行
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
xhr.onload = () => { //XMLHttpRequestのリクエストステータスの確認
if (xhr.status === 200) {
//success
let data = JSON.parse(xhr.responseText);
//結果を配列に結合
resData = resData.concat(data.Response.Data);
//全件取得するまで繰り返す
if ((data.Response.Offset + data.Response.PageSize) <= data.Response.TotalCount) {
sendRequest(data.Response.Offset + data.Response.PageSize);
} else {
//全件取得したら配列の各要素を処理
resData.forEach(function (value) {
//let result =
dataCreate(value);
//console.log(result);
//if (!result) {
// console.log(value);
// console.log("Update Failed!!");
// return;
//}
});
}
} else {
//error
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send(JSON.stringify(json));
} catch (e) {
console.log(e.stack);
}
}
//祝日データの取得
function getHolidayData() {
//116221
try {
let tableId = holidayTable;
//送信データ
let json = {
"ApiVersion": 1.1,
"ApiKey": apiKey,
"View": {
"ColumnFilterHash": {
"DateA": "[\"" + startStr + "," + endStr + "\"]"
},
}
};
//データ取得リクエスト
const url = baseUrl + '/pleasanter/api/items/' + tableId + '/get';
const xhr = new XMLHttpRequest(); //XMLHttpRequestの作成
//HTTPリクエスト実行
xhr.open('POST', url, false); //同期通信
xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
xhr.onload = () => { //XMLHttpRequestのリクエストステータスの確認
if (xhr.status === 200) {
//success
//console.log(xhr.responseText);
let data = JSON.parse(xhr.responseText);
//console.log(data.Response.Data);
data.Response.Data.forEach(function (value) {
let holiday = new Date(value.DateHash.DateA)
let holidayStr = holiday.getFullYear() + '-' +
(holiday.getMonth() + 1).toString().padStart(2, '0') + '-' +
(holiday.getDate()).toString().padStart(2, '0');
holiDays.push(holidayStr);
})
} else {
//error
console.log(xhr.responseText);
}
};
xhr.send(JSON.stringify(json));
} catch (e) {
console.log(e.stack);
}
}
function dataCreate(value) {
//対象レコード
let resultId = value.ResultId;
//時間計算用配列初期化
let timeTable = [];
let i, j, k;
for (i = 0; i < (24 * 60); i++) {
timeTable[i] = 0;
}
//開始時間、終了時間取得
let startTime = new Date(value.DateHash.DateA);
//曜日
let dayOfWeek = startTime.getDay();
//日付のみ取得
let startDate = startTime.getFullYear() + '-' +
(startTime.getMonth() + 1).toString().padStart(2, '0') + '-' +
(startTime.getDate()).toString().padStart(2, '0');
//祝日フラグ
let holidayFlag = 0;
if (holiDays.includes(startDate)) holidayFlag = 1;
//土日判定
if (dayOfWeek == 0 || dayOfWeek == 6) holidayFlag = 1;
let endTime = new Date(value.DateHash.DateB);
let startMin = startTime.getHours() * 60 + startTime.getMinutes();
let endMin = endTime.getHours() * 60 + endTime.getMinutes();
//終了時間が0時を過ぎている場合開始時間より前になっている
if (endTime.getTime() <= startTime.getTime()) {
//タイムテーブルへ反映
for (j = 0; j < endMin; j++) {
timeTable[j] = 1;
}
endMin = endMin + (24 * 60);
}
//タイムテーブルへ反映
for (j = startMin; j < endMin; j++) {
timeTable[j] = 1;
}
//集計
let timeA = 0; //0 ~ 360
let timeB = 0; //360 ~ 510
let timeC = 0; //510 ~ 720
let timeD = 0; //720 ~ 1050
let timeE = 0; //1050 ~ 1320
let timeF = 0; //1320 ~ 1440
for (k = 0; k < 360; k++) {
timeA += timeTable[k];
}
for (k = k; k < 510; k++) {
timeB += timeTable[k];
}
for (k = k; k < 720; k++) {
timeC += timeTable[k];
}
for (k = k; k < 1050; k++) {
timeD += timeTable[k];
}
for (k = k; k < 1320; k++) {
timeE += timeTable[k];
}
for (k = k; k < 1440; k++) {
timeF += timeTable[k];
}
//時間外対応 または休日対応flag
let sumFlag = 0;
if (timeA > 0) sumFlag = 1;
if (timeB > 0) sumFlag = 1;
if (timeE > 0) sumFlag = 1;
if (timeF > 0) sumFlag = 1;
//祝日判定
//
if (holidayFlag == 1) {
sumFlag = 1;
} else {
//平日判定の場合は C,D枠を0設定
timeC = 0;
timeD = 0;
}
//時間外対応時間集計
let timeSum = 0;
if (endTime.getTime() == startTime.getTime()) {
timeSum = 0;
} else {
timeSum = timeA + timeB + timeC + timeD + timeE + timeF;
}
//集計分類が「他」の場合は時間外対応集計フラグ外す
if (value.ClassHash.ClassJ == '99') sumFlag = 0;
//レコード更新用データ作成
let classHash = {
'ClassG': holidayFlag,
'ClassP': sumFlag
}
let numHash = {
'Num001': timeA,
'Num002': timeB,
'Num003': timeC,
'Num004': timeD,
'Num005': timeE,
'Num006': timeF,
'Num007': timeSum,
}
//レコード更新
return recordUpdate(resultId, classHash, numHash);
}
//データ更新
function recordUpdate(resultId, classHash, numHash) {
try {
//let tableId = tableA;
//送信データ
let json = {
"ApiVersion": 1.1,
"ApiKey": apiKey,
"ClassHash": classHash,
"NumHash": numHash
};
//データ更新リクエスト
const url = baseUrl + '/pleasanter/api/items/' + resultId + '/update';
const xhr = new XMLHttpRequest(); //XMLHttpRequestの作成
//HTTPリクエスト実行
xhr.open('POST', url, false); //非同期通信:true 同期通信:false
xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
xhr.onload = () => { //XMLHttpRequestのリクエストステータスの確認
if (xhr.status === 200) {
//success
console.log(JSON.parse(xhr.responseText));
return true;
} else {
//error
console.log(xhr.responseText);
return false;
}
};
xhr.send(JSON.stringify(json));
} catch (e) {
console.log(e.stack);
return false;
}
}
//始動
main();