# All files / models dailyWorkReportModel.js

0% Statements 0/430
0% Branches 0/78
0% Functions 0/41
0% Lines 0/426

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171

     // models/dailyWorkReportModel.js - 누적입력 방식 + 모든 기존 기능 포함
    const { getDb } = require('../dbPool');

    /**
     * 📋 마스터 데이터 조회 함수들
     */
    const getAllWorkTypes = async (callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query('SELECT * FROM work_types ORDER BY name ASC');
        callback(null, rows);
      } catch (err) {
        console.error('작업 유형 조회 오류:', err);
        callback(err);
      }
    };

    const getAllWorkStatusTypes = async (callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query('SELECT * FROM work_status_types ORDER BY id ASC');
        callback(null, rows);
      } catch (err) {
        console.error('업무 상태 유형 조회 오류:', err);
        callback(err);
      }
    };

    const getAllErrorTypes = async (callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query('SELECT * FROM error_types ORDER BY name ASC');
        callback(null, rows);
      } catch (err) {
        console.error('에러 유형 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 🔄 누적 추가 전용 함수 (createDailyReport 대체) - 절대 삭제 안함!
     */
    const createDailyReport = async (reportData, callback) => {
      const { report_date, worker_id, work_entries, created_by, created_by_name, total_hours } = reportData;
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        console.log(`📝 ${created_by_name}이 ${report_date} ${worker_id}번 작업자에게 데이터 추가 중...`);

       // ✅ 수정된 쿼리 (테이블 alias 추가):
    const [existingReports] = await conn.query(
      `SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
       FROM daily_work_reports dwr
       LEFT JOIN users u ON dwr.created_by = u.user_id
       WHERE dwr.report_date = ? AND dwr.worker_id = ?
       GROUP BY dwr.created_by`,
      [report_date, worker_id]
    );

        console.log('기존 데이터 (삭제하지 않음):', existingReports);

        // 2. ✅ 삭제 없이 새로운 데이터만 추가!
        const insertedIds = [];
        for (const entry of work_entries) {
          const { project_id, work_type_id, work_status_id, error_type_id, work_hours } = entry;

          const [insertResult] = await conn.query(
            `INSERT INTO daily_work_reports
             (report_date, worker_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_by, created_at)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())`,
            [report_date, worker_id, project_id, work_type_id, work_status_id || 1, error_type_id || null, work_hours, created_by]
          );

          insertedIds.push(insertResult.insertId);
        }

       // ✅ 수정된 쿼리:
    const [finalReports] = await conn.query(
      `SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
       FROM daily_work_reports dwr
       LEFT JOIN users u ON dwr.created_by = u.user_id
       WHERE dwr.report_date = ? AND dwr.worker_id = ?
       GROUP BY dwr.created_by`,
      [report_date, worker_id]
    );

        const grandTotal = finalReports.reduce((sum, report) => sum + parseFloat(report.total_hours || 0), 0);
        const myTotal = finalReports.find(r => r.created_by === created_by)?.total_hours || 0;

        console.log('최종 결과:');
        finalReports.forEach(report => {
          console.log(`  - ${report.created_by_name}: ${report.total_hours}시간 (${report.count}개 항목)`);
        });
        console.log(`  📊 총합: ${grandTotal}시간`);

        // 4. 감사 로그 추가
        try {
          await conn.query(
            `INSERT INTO work_report_audit_log
             (action, report_id, new_values, changed_by, change_reason, created_at)
             VALUES (?, ?, ?, ?, ?, NOW())`,
            [
              'ADD_ACCUMULATE',
              insertedIds[0] || null,
              JSON.stringify({
                report_date,
                worker_id,
                work_entries_count: work_entries.length,
                added_hours: total_hours,
                my_total: myTotal,
                grand_total: grandTotal,
                contributors: finalReports.map(r => ({ name: r.created_by_name, hours: r.total_hours }))
              }),
              created_by,
              `누적 추가 by ${created_by_name} - 삭제 없음`
            ]
          );
        } catch (auditErr) {
          console.warn('감사 로그 추가 실패:', auditErr.message);
        }

        await conn.commit();

        callback(null, {
          success: true,
          inserted_count: insertedIds.length,
          deleted_count: 0, // 항상 0 (삭제 안함)
          action: 'accumulated',
          message: `${created_by_name}이 ${total_hours}시간 추가했습니다. (개인 총 ${myTotal}시간, 전체 총 ${grandTotal}시간)`,
          final_summary: {
            my_total: parseFloat(myTotal),
            grand_total: grandTotal,
            total_contributors: finalReports.length,
            contributors: finalReports
          }
        });

      } catch (err) {
        await conn.rollback();
        console.error('작업보고서 누적 추가 오류:', err);
        callback(err);
      } finally {
        conn.release();
      }
    };

    /**
     * 📊 특정 날짜 + 작업자 + 작성자의 누적 현황 조회
     */
    const getMyAccumulatedHours = async (date, worker_id, created_by, callback) => {
      try {
        const db = await getDb();

        const sql = `
          SELECT
            SUM(work_hours) as my_total_hours,
            COUNT(*) as my_entry_count,
            GROUP_CONCAT(
              CONCAT(p.project_name, ':', work_hours, 'h')
              ORDER BY created_at
            ) as my_entries
          FROM daily_work_reports dwr
          LEFT JOIN projects p ON dwr.project_id = p.project_id
          WHERE dwr.report_date = ? AND dwr.worker_id = ? AND dwr.created_by = ?
        `;

        const [rows] = await db.query(sql, [date, worker_id, created_by]);
        callback(null, rows[0] || { my_total_hours: 0, my_entry_count: 0, my_entries: null });
      } catch (err) {
        console.error('개인 누적 현황 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 📊 누적 현황 조회 - 날짜+작업자별 (모든 기여자)
     */
    const getAccumulatedReportsByDate = async (date, worker_id, callback) => {
      try {
        const db = await getDb();

        const sql = getSelectQuery() + `
          WHERE dwr.report_date = ? AND dwr.worker_id = ?
          ORDER BY dwr.created_by, dwr.created_at ASC
        `;

        const [rows] = await db.query(sql, [date, worker_id]);
        callback(null, rows);
      } catch (err) {
        console.error('누적 현황 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 📊 기여자별 요약 조회
     */
    const getContributorsByDate = async (date, worker_id, callback) => {
      try {
        const db = await getDb();

        const sql = `
          SELECT
            dwr.created_by,
            u.name as created_by_name,
            COUNT(*) as entry_count,
            SUM(dwr.work_hours) as total_hours,
            MIN(dwr.created_at) as first_entry,
            MAX(dwr.created_at) as last_entry,
            GROUP_CONCAT(
              CONCAT(p.project_name, ':', dwr.work_hours, 'h')
              ORDER BY dwr.created_at SEPARATOR ', '
            ) as entry_details
          FROM daily_work_reports dwr
          LEFT JOIN users u ON dwr.created_by = u.user_id
          LEFT JOIN projects p ON dwr.project_id = p.project_id
          WHERE dwr.report_date = ? AND dwr.worker_id = ?
          GROUP BY dwr.created_by
          ORDER BY total_hours DESC, first_entry ASC
        `;

        const [rows] = await db.query(sql, [date, worker_id]);
        callback(null, rows);
      } catch (err) {
        console.error('기여자별 요약 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 🗑️ 특정 작업 항목만 삭제 (개별 삭제)
     */
    const removeSpecificEntry = async (entry_id, deleted_by, callback) => {
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        // 삭제 전 정보 확인
        const [entryInfo] = await conn.query(
          `SELECT dwr.*, w.worker_name, p.project_name, u.name as created_by_name
           FROM daily_work_reports dwr
           LEFT JOIN workers w ON dwr.worker_id = w.worker_id
           LEFT JOIN projects p ON dwr.project_id = p.project_id
           LEFT JOIN users u ON dwr.created_by = u.user_id
           WHERE dwr.id = ?`,
          [entry_id]
        );

        if (entryInfo.length === 0) {
          await conn.rollback();
          return callback(new Error('삭제할 항목을 찾을 수 없습니다.'));
        }

        const entry = entryInfo[0];

        // 권한 확인: 본인이 작성한 것만 삭제 가능
        if (entry.created_by !== deleted_by) {
          await conn.rollback();
          return callback(new Error('본인이 작성한 항목만 삭제할 수 있습니다.'));
        }

        // 개별 항목 삭제
        const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [entry_id]);

        // 감사 로그 (삭제된 테이블이므로 콘솔 로그로 대체)
        console.log(`[삭제 로그] 작업자: ${entry.worker_name}, 프로젝트: ${entry.project_name}, 작업시간: ${entry.work_hours}시간, 삭제자: ${deleted_by}`);

        await conn.commit();
        callback(null, {
          success: true,
          deleted_entry: {
            worker_name: entry.worker_name,
            project_name: entry.project_name,
            work_hours: entry.work_hours
          }
        });

      } catch (err) {
        await conn.rollback();
        console.error('개별 항목 삭제 오류:', err);
        callback(err);
      } finally {
        conn.release();
      }
    };

    /**
     * 공통 SELECT 쿼리 부분
     */
    const getSelectQuery = () => `
      SELECT
        dwr.id,
        dwr.report_date,
        dwr.worker_id,
        dwr.project_id,
        dwr.work_type_id,
        dwr.work_status_id,
        dwr.error_type_id,
        dwr.work_hours,
        dwr.created_by,
        w.worker_name,
        p.project_name,
        wt.name as work_type_name,
        wst.name as work_status_name,
        et.name as error_type_name,
        u.name as created_by_name,
        dwr.created_at,
        dwr.updated_at
      FROM daily_work_reports dwr
      LEFT JOIN workers w ON dwr.worker_id = w.worker_id
      LEFT JOIN projects p ON dwr.project_id = p.project_id
      LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
      LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
      LEFT JOIN error_types et ON dwr.error_type_id = et.id
      LEFT JOIN users u ON dwr.created_by = u.user_id
    `;

    /**
     * 7. ID로 작업보고서 조회
     */
    const getById = async (id, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + 'WHERE dwr.id = ?';
        const [rows] = await db.query(sql, [id]);
        callback(null, rows[0] || null);
      } catch (err) {
        console.error('ID로 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 8. 일일 작업보고서 조회 (날짜별)
     */
    const getByDate = async (date, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + `
          WHERE dwr.report_date = ?
          ORDER BY w.worker_name ASC, p.project_name ASC, dwr.id ASC
        `;
        const [rows] = await db.query(sql, [date]);
        callback(null, rows);
      } catch (err) {
        console.error('날짜별 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 9. 일일 작업보고서 조회 (날짜 + 작성자별)
     */
    const getByDateAndCreator = async (date, created_by, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + `
          WHERE dwr.report_date = ? AND dwr.created_by = ?
          ORDER BY w.worker_name ASC, p.project_name ASC, dwr.id ASC
        `;
        const [rows] = await db.query(sql, [date, created_by]);
        callback(null, rows);
      } catch (err) {
        console.error('날짜+작성자별 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 10. 일일 작업보고서 조회 (작업자별)
     */
    const getByWorker = async (worker_id, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + `
          WHERE dwr.worker_id = ?
          ORDER BY dwr.report_date DESC, dwr.id ASC
        `;
        const [rows] = await db.query(sql, [worker_id]);
        callback(null, rows);
      } catch (err) {
        console.error('작업자별 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 11. 일일 작업보고서 조회 (날짜 + 작업자)
     */
    const getByDateAndWorker = async (date, worker_id, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + `
          WHERE dwr.report_date = ? AND dwr.worker_id = ?
          ORDER BY dwr.id ASC
        `;
        const [rows] = await db.query(sql, [date, worker_id]);
        callback(null, rows);
      } catch (err) {
        console.error('날짜+작업자별 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 12. 기간별 조회
     */
    const getByRange = async (start_date, end_date, callback) => {
      try {
        const db = await getDb();
        const sql = getSelectQuery() + `
          WHERE dwr.report_date BETWEEN ? AND ?
          ORDER BY dwr.report_date DESC, w.worker_name ASC, dwr.id ASC
        `;
        const [rows] = await db.query(sql, [start_date, end_date]);
        callback(null, rows);
      } catch (err) {
        console.error('기간별 작업보고서 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 13. 상세 검색 (페이지네이션 포함)
     */
    const searchWithDetails = async (params, callback) => {
      const { start_date, end_date, worker_id, project_id, work_status_id, created_by, page, limit } = params;

      try {
        const db = await getDb();

        // 조건 구성
        let whereConditions = ['dwr.report_date BETWEEN ? AND ?'];
        let queryParams = [start_date, end_date];

        if (worker_id) {
          whereConditions.push('dwr.worker_id = ?');
          queryParams.push(worker_id);
        }

        if (project_id) {
          whereConditions.push('dwr.project_id = ?');
          queryParams.push(project_id);
        }

        if (work_status_id) {
          whereConditions.push('dwr.work_status_id = ?');
          queryParams.push(work_status_id);
        }

        if (created_by) {
          whereConditions.push('dwr.created_by = ?');
          queryParams.push(created_by);
        }

        const whereClause = whereConditions.join(' AND ');

        // 총 개수 조회
        const countQuery = `
          SELECT COUNT(*) as total
          FROM daily_work_reports dwr
          WHERE ${whereClause}
        `;
        const [countResult] = await db.query(countQuery, queryParams);
        const total = countResult[0].total;

        // 데이터 조회 (JOIN 포함)
        const offset = (page - 1) * limit;
        const dataQuery = getSelectQuery() + `
          WHERE ${whereClause}
          ORDER BY dwr.report_date DESC, w.worker_name ASC, dwr.created_at DESC
          LIMIT ? OFFSET ?
        `;

        const dataParams = [...queryParams, limit, offset];
        const [rows] = await db.query(dataQuery, dataParams);

        callback(null, { reports: rows, total });
      } catch (err) {
        console.error('상세 검색 오류:', err);
        callback(err);
      }
    };

    /**
     * 14. 일일 근무 요약 조회 (날짜별)
     */
    const getSummaryByDate = async (date, callback) => {
      try {
        const db = await getDb();

        const sql = `
          SELECT
            dwr.worker_id,
            w.worker_name,
            dwr.report_date,
            SUM(dwr.work_hours) as total_hours,
            COUNT(*) as work_entries_count,
            SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
          FROM daily_work_reports dwr
          LEFT JOIN workers w ON dwr.worker_id = w.worker_id
          WHERE dwr.report_date = ?
          GROUP BY dwr.worker_id, dwr.report_date
          ORDER BY w.worker_name ASC
        `;
        const [rows] = await db.query(sql, [date]);
        callback(null, rows);
      } catch (err) {
        console.error('일일 근무 요약 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 15. 일일 근무 요약 조회 (작업자별)
     */
    const getSummaryByWorker = async (worker_id, callback) => {
      try {
        const db = await getDb();

        const sql = `
          SELECT
            dwr.report_date,
            dwr.worker_id,
            w.worker_name,
            SUM(dwr.work_hours) as total_hours,
            COUNT(*) as work_entries_count,
            SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
          FROM daily_work_reports dwr
          LEFT JOIN workers w ON dwr.worker_id = w.worker_id
          WHERE dwr.worker_id = ?
          GROUP BY dwr.report_date, dwr.worker_id
          ORDER BY dwr.report_date DESC
        `;
        const [rows] = await db.query(sql, [worker_id]);
        callback(null, rows);
      } catch (err) {
        console.error('작업자별 근무 요약 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 16. 월간 요약
     */
    const getMonthlySummary = async (year, month, callback) => {
      try {
        const db = await getDb();
        const start = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-01`;
        const end = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-31`;

        const sql = `
          SELECT
            dwr.report_date,
            dwr.worker_id,
            w.worker_name,
            SUM(dwr.work_hours) as total_work_hours,
            COUNT(DISTINCT dwr.project_id) as project_count,
            COUNT(*) as work_entries_count,
            SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count,
            GROUP_CONCAT(DISTINCT p.project_name ORDER BY p.project_name) as projects,
            GROUP_CONCAT(DISTINCT wt.name ORDER BY wt.name) as work_types
          FROM daily_work_reports dwr
          LEFT JOIN workers w ON dwr.worker_id = w.worker_id
          LEFT JOIN projects p ON dwr.project_id = p.project_id
          LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
          WHERE dwr.report_date BETWEEN ? AND ?
          GROUP BY dwr.report_date, dwr.worker_id
          ORDER BY dwr.report_date DESC, w.worker_name ASC
        `;
        const [rows] = await db.query(sql, [start, end]);
        callback(null, rows);
      } catch (err) {
        console.error('월간 요약 조회 오류:', err);
        callback(err);
      }
    };

    /**
     * 17. 작업보고서 수정
     */
    const updateById = async (id, updateData, callback) => {
      try {
        const db = await getDb();

        const setFields = [];
        const values = [];

        if (updateData.work_hours !== undefined) {
          setFields.push('work_hours = ?');
          values.push(updateData.work_hours);
        }

        if (updateData.work_status_id !== undefined) {
          setFields.push('work_status_id = ?');
          values.push(updateData.work_status_id);
        }

        if (updateData.error_type_id !== undefined) {
          setFields.push('error_type_id = ?');
          values.push(updateData.error_type_id);
        }

        if (updateData.project_id !== undefined) {
          setFields.push('project_id = ?');
          values.push(updateData.project_id);
        }

        if (updateData.work_type_id !== undefined) {
          setFields.push('work_type_id = ?');
          values.push(updateData.work_type_id);
        }

        setFields.push('updated_at = NOW()');

        if (updateData.updated_by) {
          setFields.push('updated_by = ?');
          values.push(updateData.updated_by);
        }

        values.push(id);

        const sql = `UPDATE daily_work_reports SET ${setFields.join(', ')} WHERE id = ?`;
        const [result] = await db.query(sql, values);

        callback(null, result.affectedRows);
      } catch (err) {
        console.error('작업보고서 수정 오류:', err);
        callback(err);
      }
    };

    /**
     * 18. 특정 작업보고서 삭제
     */
    const removeById = async (id, deletedBy, callback) => {
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        // 삭제 전 정보 저장 (감사 로그용)
        const [reportInfo] = await conn.query('SELECT * FROM daily_work_reports WHERE id = ?', [id]);

        // 작업보고서 삭제
        const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [id]);

        // 감사 로그 추가
        if (reportInfo.length > 0 && deletedBy) {
          try {
            await conn.query(
              `INSERT INTO work_report_audit_log
               (action, report_id, old_values, changed_by, change_reason, created_at)
               VALUES ('DELETE', ?, ?, ?, 'Manual deletion', NOW())`,
              [id, JSON.stringify(reportInfo[0]), deletedBy]
            );
          } catch (auditErr) {
            console.warn('감사 로그 추가 실패:', auditErr.message);
          }
        }

        await conn.commit();
        callback(null, result.affectedRows);
      } catch (err) {
        await conn.rollback();
        console.error('작업보고서 삭제 오류:', err);
        callback(err);
      } finally {
        conn.release();
      }
    };

    /**
     * 19. 작업자의 특정 날짜 전체 삭제
     */
    const removeByDateAndWorker = async (date, worker_id, deletedBy, callback) => {
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        // 삭제 전 정보 저장 (감사 로그용)
        const [reportInfos] = await conn.query(
          'SELECT * FROM daily_work_reports WHERE report_date = ? AND worker_id = ?',
          [date, worker_id]
        );

        // 작업보고서 삭제
        const [result] = await conn.query(
          'DELETE FROM daily_work_reports WHERE report_date = ? AND worker_id = ?',
          [date, worker_id]
        );

        // 감사 로그 추가
        if (reportInfos.length > 0 && deletedBy) {
          try {
            await conn.query(
              `INSERT INTO work_report_audit_log
               (action, old_values, changed_by, change_reason, created_at)
               VALUES ('DELETE_BATCH', ?, ?, 'Batch deletion by date and worker', NOW())`,
              [JSON.stringify({ deleted_reports: reportInfos, count: reportInfos.length }), deletedBy]
            );
          } catch (auditErr) {
            console.warn('감사 로그 추가 실패:', auditErr.message);
          }
        }

        await conn.commit();
        callback(null, result.affectedRows);
      } catch (err) {
        await conn.rollback();
        console.error('작업보고서 전체 삭제 오류:', err);
        callback(err);
      } finally {
        conn.release();
      }
    };

    /**
     * 20. 통계 조회 (Promise 기반)
     */
    const getStatistics = async (start_date, end_date) => {
      try {
        const db = await getDb();

        const overallSql = `
          SELECT
            COUNT(*) as total_reports,
            SUM(work_hours) as total_hours,
            COUNT(DISTINCT worker_id) as unique_workers,
            COUNT(DISTINCT project_id) as unique_projects
          FROM daily_work_reports
          WHERE report_date BETWEEN ? AND ?
        `;
        const [overallRows] = await db.query(overallSql, [start_date, end_date]);

        const dailyStatsSql = `
          SELECT
            report_date,
            SUM(work_hours) as daily_hours,
            COUNT(DISTINCT worker_id) as daily_workers
          FROM daily_work_reports
          WHERE report_date BETWEEN ? AND ?
          GROUP BY report_date
          ORDER BY report_date DESC
        `;
        const [dailyStats] = await db.query(dailyStatsSql, [start_date, end_date]);

        return {
          overall: overallRows[0],
          daily_breakdown: dailyStats
        };
      } catch (err) {
        console.error('통계 조회 오류:', err);
        throw new Error('데이터베이스에서 통계 정보를 조회하는 중 오류가 발생했습니다.');
      }
    };

    /**
     * [V2] 여러 작업 보고서 항목을 트랜잭션으로 생성합니다. (Promise 기반)
     * @param {object} modelData - 서비스 레이어에서 전달된 데이터
     * @returns {Promise<object>} 삽입된 항목의 ID 배열과 개수
     */
    const createReportEntries = async ({ report_date, worker_id, entries }) => {
      const db = await getDb();
      const conn = await db.getConnection();
      try {
        await conn.beginTransaction();

        const insertedIds = [];
        const sql = `
          INSERT INTO daily_work_reports
          (report_date, worker_id, project_id, work_type_id, work_hours, work_status_id, error_type_id, created_by)
          VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        `;

        for (const entry of entries) {
          const { project_id, work_type_id, work_hours, work_status_id, error_type_id, created_by } = entry;
          const [result] = await conn.query(sql, [
            report_date,
            worker_id,
            project_id,
            work_type_id,
            work_hours,
            work_status_id || 1,
            error_type_id,
            created_by
          ]);
          insertedIds.push(result.insertId);
        }

        await conn.commit();

        console.log(`[Model] ${insertedIds.length}개 작업 항목 생성 완료.`);
        return {
          inserted_ids: insertedIds,
          inserted_count: insertedIds.length
        };

      } catch (err) {
        await conn.rollback();
        console.error('[Model] 작업 보고서 생성 중 오류 발생:', err);
        // 여기서 발생한 에러는 서비스 레이어로 전파됩니다.
        throw new Error('데이터베이스에 작업 보고서를 생성하는 중 오류가 발생했습니다.');
      } finally {
        conn.release();
      }
    };

    /**
     * [V2] 공통 SELECT 쿼리 (새로운 스키마 기준)
     */
    const getSelectQueryV2 = () => `
      SELECT
        dwr.id,
        dwr.report_date,
        dwr.worker_id,
        dwr.project_id,
        dwr.work_type_id,
        dwr.work_status_id,
        dwr.error_type_id,
        dwr.work_hours,
        dwr.created_by,
        w.worker_name,
        p.project_name,
        wt.name as work_type_name,
        wst.name as work_status_name,
        et.name as error_type_name,
        u.name as created_by_name,
        dwr.created_at
      FROM daily_work_reports dwr
      LEFT JOIN workers w ON dwr.worker_id = w.worker_id
      LEFT JOIN projects p ON dwr.project_id = p.project_id
      LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
      LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
      LEFT JOIN error_types et ON dwr.error_type_id = et.id
      LEFT JOIN users u ON dwr.created_by = u.user_id
    `;

    /**
     * [V2] 옵션 기반으로 작업 보고서를 조회합니다. (Promise 기반)
     * @param {object} options - 조회 조건 (date, worker_id, created_by_user_id 등)
     * @returns {Promise<Array>} 조회된 작업 보고서 배열
     */
    const getReportsWithOptions = async (options) => {
      const db = await getDb();
      let whereConditions = [];
      let queryParams = [];

      // 날짜 조건 처리 (단일 날짜 또는 날짜 범위)
      if (options.date) {
        whereConditions.push('dwr.report_date = ?');
        queryParams.push(options.date);
      } else if (options.start_date && options.end_date) {
        whereConditions.push('dwr.report_date BETWEEN ? AND ?');
        queryParams.push(options.start_date, options.end_date);
      }

      if (options.worker_id) {
        whereConditions.push('dwr.worker_id = ?');
        queryParams.push(options.worker_id);
      }
      if (options.created_by_user_id) {
        whereConditions.push('dwr.created_by = ?');
        queryParams.push(options.created_by_user_id);
      }
      // 필요에 따라 다른 조건 추가 가능 (project_id 등)

      if (whereConditions.length === 0) {
        throw new Error('조회 조건이 하나 이상 필요합니다.');
      }

      const whereClause = whereConditions.join(' AND ');
      const sql = `${getSelectQueryV2()} WHERE ${whereClause} ORDER BY w.worker_name ASC, p.project_name ASC`;

      try {
        const [rows] = await db.query(sql, queryParams);
        return rows;
      } catch (err) {
        console.error('[Model] 작업 보고서 조회 오류:', err);
        throw new Error('데이터베이스에서 작업 보고서를 조회하는 중 오류가 발생했습니다.');
      }
    };

    /**
     * [V2] ID를 기준으로 특정 작업 보고서 항목을 수정합니다. (Promise 기반)
     * @param {string} reportId - 수정할 보고서의 ID
     * @param {object} updateData - 수정할 필드와 값
     * @returns {Promise<number>} 영향을 받은 행의 수
     */
    const updateReportById = async (reportId, updateData) => {
      const db = await getDb();

      // 허용된 필드 목록 (보안 및 안정성) - 실제 테이블 컬럼명 사용
      const allowedFields = ['project_id', 'work_type_id', 'work_hours', 'work_status_id', 'error_type_id'];
      const setClauses = [];
      const queryParams = [];

      for (const field of allowedFields) {
        if (updateData[field] !== undefined) {
          setClauses.push(`${field} = ?`);
          queryParams.push(updateData[field]);
        }
      }

      // updated_by_user_id는 항상 업데이트
      if (updateData.updated_by_user_id) {
          setClauses.push('updated_by_user_id = ?');
          queryParams.push(updateData.updated_by_user_id);
      }

      if (setClauses.length === 0) {
        throw new Error('수정할 데이터가 없습니다.');
      }

      queryParams.push(reportId);

      const sql = `UPDATE daily_work_reports SET ${setClauses.join(', ')} WHERE id = ?`;

      try {
        const [result] = await db.query(sql, queryParams);
        return result.affectedRows;
      } catch (err) {
        console.error(`[Model] 작업 보고서 수정 오류 (id: ${reportId}):`, err);
        throw new Error('데이터베이스에서 작업 보고서를 수정하는 중 오류가 발생했습니다.');
      }
    };

    /**
     * [V2] ID를 기준으로 특정 작업 보고서를 삭제합니다. (Promise 기반)
     * @param {string} reportId - 삭제할 보고서 ID
     * @param {number} deletedByUserId - 삭제를 수행하는 사용자 ID
     * @returns {Promise<number>} 영향을 받은 행의 수
     */
    const removeReportById = async (reportId, deletedByUserId) => {
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        // 감사 로그를 위해 삭제 전 정보 조회
        const [reportInfo] = await conn.query('SELECT * FROM daily_work_reports WHERE id = ?', [reportId]);

        // 실제 삭제 작업
        const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [reportId]);

        // 감사 로그 (삭제된 테이블이므로 콘솔 로그로 대체)
        if (reportInfo.length > 0 && deletedByUserId) {
          console.log(`[삭제 로그] 보고서 ID: ${reportId}, 삭제자: ${deletedByUserId}, 사유: Manual deletion by user`);
        }

        await conn.commit();
        return result.affectedRows;

      } catch (err) {
        await conn.rollback();
        console.error(`[Model] 작업 보고서 삭제 오류 (id: ${reportId}):`, err);
        throw new Error('데이터베이스에서 작업 보고서를 삭제하는 중 오류가 발생했습니다.');
      } finally {
        conn.release();
      }
    };

    // ========== 마스터 데이터 CRUD 메서드들 ==========

    /**
     * 📝 작업 유형 생성
     */
    const createWorkType = async (data, callback) => {
      try {
        const db = await getDb();
        const { name, description, category } = data;
        const [result] = await db.query(
          'INSERT INTO work_types (name, description, category) VALUES (?, ?, ?)',
          [name, description, category]
        );
        callback(null, { id: result.insertId, ...data });
      } catch (err) {
        console.error('작업 유형 생성 오류:', err);
        callback(err);
      }
    };

    /**
     * ✏️ 작업 유형 수정
     */
    const updateWorkType = async (id, data, callback) => {
      try {
        const db = await getDb();
        const { name, description, category } = data;
        const [result] = await db.query(
          'UPDATE work_types SET name = ?, description = ?, category = ? WHERE id = ?',
          [name, description, category, id]
        );
        callback(null, result);
      } catch (err) {
        console.error('작업 유형 수정 오류:', err);
        callback(err);
      }
    };

    /**
     * 🗑️ 작업 유형 삭제
     */
    const deleteWorkType = async (id, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query('DELETE FROM work_types WHERE id = ?', [id]);
        callback(null, result);
      } catch (err) {
        console.error('작업 유형 삭제 오류:', err);
        callback(err);
      }
    };

    /**
     * 📝 작업 상태 생성
     */
    const createWorkStatus = async (data, callback) => {
      try {
        const db = await getDb();
        const { name, description, is_error } = data;
        const [result] = await db.query(
          'INSERT INTO work_status_types (name, description, is_error) VALUES (?, ?, ?)',
          [name, description, is_error || 0]
        );
        callback(null, { id: result.insertId, ...data });
      } catch (err) {
        console.error('작업 상태 생성 오류:', err);
        callback(err);
      }
    };

    /**
     * ✏️ 작업 상태 수정
     */
    const updateWorkStatus = async (id, data, callback) => {
      try {
        const db = await getDb();
        const { name, description, is_error } = data;
        const [result] = await db.query(
          'UPDATE work_status_types SET name = ?, description = ?, is_error = ? WHERE id = ?',
          [name, description, is_error || 0, id]
        );
        callback(null, result);
      } catch (err) {
        console.error('작업 상태 수정 오류:', err);
        callback(err);
      }
    };

    /**
     * 🗑️ 작업 상태 삭제
     */
    const deleteWorkStatus = async (id, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query('DELETE FROM work_status_types WHERE id = ?', [id]);
        callback(null, result);
      } catch (err) {
        console.error('작업 상태 삭제 오류:', err);
        callback(err);
      }
    };

    /**
     * 📝 오류 유형 생성
     */
    const createErrorType = async (data, callback) => {
      try {
        const db = await getDb();
        const { name, description, severity } = data;
        const [result] = await db.query(
          'INSERT INTO error_types (name, description, severity) VALUES (?, ?, ?)',
          [name, description, severity || 'medium']
        );
        callback(null, { id: result.insertId, ...data });
      } catch (err) {
        console.error('오류 유형 생성 오류:', err);
        callback(err);
      }
    };

    /**
     * ✏️ 오류 유형 수정
     */
    const updateErrorType = async (id, data, callback) => {
      try {
        const db = await getDb();
        const { name, description, severity } = data;
        const [result] = await db.query(
          'UPDATE error_types SET name = ?, description = ?, severity = ? WHERE id = ?',
          [name, description, severity || 'medium', id]
        );
        callback(null, result);
      } catch (err) {
        console.error('오류 유형 수정 오류:', err);
        callback(err);
      }
    };

    /**
     * 🗑️ 오류 유형 삭제
     */
    const deleteErrorType = async (id, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query('DELETE FROM error_types WHERE id = ?', [id]);
        callback(null, result);
      } catch (err) {
        console.error('오류 유형 삭제 오류:', err);
        callback(err);
      }
    };

    // 모든 함수 내보내기 (Promise 기반 함수 위주로 재구성)
    module.exports = {
      // 새로 추가된 V2 함수 (Promise 기반)
      createReportEntries,
      getReportsWithOptions,
      updateReportById,
      removeReportById,

      // Promise 기반으로 리팩토링된 함수
      getStatistics,
      getSummaryByDate,
      getSummaryByWorker,

      // 아직 리팩토링되지 않았지만 필요한 기존 함수들...
      // (점진적으로 아래 함수들도 Promise 기반으로 전환해야 함)
      getAllWorkTypes,
      getAllWorkStatusTypes,
      getAllErrorTypes,

      // 마스터 데이터 CRUD
      createWorkType,
      updateWorkType,
      deleteWorkType,
      createWorkStatus,
      updateWorkStatus,
      deleteWorkStatus,
      createErrorType,
      updateErrorType,
      deleteErrorType,
      createDailyReport,
      getMyAccumulatedHours,
      getAccumulatedReportsByDate,
      getContributorsByDate,
      removeSpecificEntry,
      getById,
      getByDate,
      getByDateAndCreator,
      getByWorker,
      getByDateAndWorker,
      getByRange,
      searchWithDetails,
      getMonthlySummary,
      updateById,
      removeById,
      removeByDateAndWorker,
    };