增加每日助理人数查询并调整登录及对账展示
This commit is contained in:
1 parent
8a4d1df248
commit
8d9039aacf
9 files changed
+207
-43
No files matched your search
+18
-4
@@ -625,16 +625,30 @@ class Ledger {
|
||||
return {from,date,total:totals.total,recordedDays:totals.recordedDays,
|
||||
assistant1:{total:totals.assistant1Total,recordedDays:totals.assistant1Days},assistant2:{total:totals.assistant2Total,recordedDays:totals.assistant2Days}};
|
||||
}
|
||||
peopleCountHistory(query={}){
|
||||
const current=today(),from=day(query.from??current.slice(0,7)+'-01'),date=day(query.date??current);
|
||||
const first=Date.parse(from+'T00:00:00Z'),last=Date.parse(date+'T00:00:00Z'),days=(last-first)/86400000+1;
|
||||
if(from>date)fail('开始日期不能晚于结束日期');if(days>366)fail('每次最多查看366天,请缩小日期范围');
|
||||
const saved=new Map(this.rows('SELECT date,assistant1_count,assistant2_count FROM daily_people_counts WHERE date>=? AND date<=? ORDER BY date',from,date).map(row=>[row.date,row]));
|
||||
const rows=[],summary={assistant1:{total:0,recordedDays:0},assistant2:{total:0,recordedDays:0}};
|
||||
for(let i=0;i<days;i++){
|
||||
const date=new Date(first+i*86400000).toISOString().slice(0,10),record=saved.get(date),row={date,assistant1Count:record?.assistant1_count??null,assistant2Count:record?.assistant2_count??null};
|
||||
rows.push(row);for(const key of ['assistant1','assistant2'])if(row[key+'Count']!==null){summary[key].total+=row[key+'Count'];summary[key].recordedDays++;}
|
||||
}
|
||||
return {from,date,today:current,rows,summary};
|
||||
}
|
||||
savePeopleCount(input,actor){
|
||||
if(!actor||!['owner','assistant'].includes(actor.role))fail('请使用麦麦或助理账号登记人数',403);
|
||||
if(!input||typeof input!=='object'||Array.isArray(input))fail('人数记录格式无效');
|
||||
const date=day(input.date),count=peopleNumber(input.count,'人数');
|
||||
const date=day(input.date),submittedCount=Object.hasOwn(input,'count')?peopleNumber(input.count,'人数'):undefined;
|
||||
if(!Number.isSafeInteger(input.revision)||input.revision<0)fail('人数记录版本无效,请刷新后再保存');
|
||||
const previous=this.one('SELECT count,assistant1_count,assistant2_count,revision FROM daily_people_counts WHERE date=?',date);
|
||||
if(input.revision!==(previous?.revision??0))fail('该日人数已被更新,请刷新后核对再保存',409);
|
||||
// 旧页面或旧中断操作只有 count;缺省时保留分项,显式 null/空白才清为未记录。
|
||||
const assistant1Count=Object.hasOwn(input,'assistant1Count')?peopleNumber(input.assistant1Count,'助理1号人数',true):previous?.assistant1_count??null;
|
||||
const assistant2Count=Object.hasOwn(input,'assistant2Count')?peopleNumber(input.assistant2Count,'助理2号人数',true):previous?.assistant2_count??null;
|
||||
// 新页面只填写两位助理;原人数保留旧值,新行用0满足旧表约束,不由分项求和。
|
||||
const count=submittedCount??previous?.count??0;
|
||||
// 旧页面或中断操作缺省的助理项仍保留,显式 null/空白才清为未记录。
|
||||
const assistant1Count=Object.hasOwn(input,'assistant1Count')?peopleNumber(input.assistant1Count,'助理1人数',true):previous?.assistant1_count??null;
|
||||
const assistant2Count=Object.hasOwn(input,'assistant2Count')?peopleNumber(input.assistant2Count,'助理2人数',true):previous?.assistant2_count??null;
|
||||
const revision=input.revision+1,timestamp=now();
|
||||
const changed=this.run('INSERT INTO daily_people_counts(date,count,assistant1_count,assistant2_count,revision,actor,created,updated) VALUES (?,?,?,?,?,?,?,?) ON CONFLICT(date) DO UPDATE SET count=excluded.count,assistant1_count=excluded.assistant1_count,assistant2_count=excluded.assistant2_count,revision=excluded.revision,actor=excluded.actor,updated=excluded.updated WHERE daily_people_counts.revision=?',date,count,assistant1Count,assistant2Count,revision,actor.name,timestamp,timestamp,input.revision);
|
||||
if(changed.changes!==1)fail('该日人数已被更新,请刷新后核对再保存',409);
|
||||
|
||||
Reference in new issue
Block a user