LqAssistantSalaryService.cs 60.4 KB
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 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NCC.Common.Filter;
using NCC.Common.Helper;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqAssistantSalary;
using NCC.Extend.Entitys.Dto.LqSalary;
using NCC.Extend.Entitys.lq_assistant_salary_statistics;
using NCC.Extend.Entitys.lq_attendance_summary;
using NCC.Extend.Entitys.lq_hytk_hytk;
using NCC.Extend.Entitys.lq_kd_kdjlb;
using NCC.Extend.Entitys.lq_md_target;
using NCC.Extend.Entitys.lq_md_xdbhsj;
using NCC.Extend.Entitys.lq_mdxx;
using NCC.Extend.Entitys.lq_xh_hyhk;
using NCC.Extend.Entitys.lq_xh_jksyj;
using NCC.FriendlyException;
using NCC.System.Entitys.Permission;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace NCC.Extend
{
    /// <summary>
    /// 店助薪酬服务
    /// </summary>
    [ApiDescriptionSettings(Tag = "店助、店助主任薪酬服务", Name = "LqAssistantSalary", Order = 301)]
    [Route("api/Extend/[controller]")]
    public class LqAssistantSalaryService : IDynamicApiController, ITransient
    {
        private readonly ISqlSugarClient _db;
        private readonly ILogger<LqAssistantSalaryService> _logger;

        /// <summary>
        /// 初始化一个<see cref="LqAssistantSalaryService"/>类型的新实例
        /// </summary>
        public LqAssistantSalaryService(ISqlSugarClient db, ILogger<LqAssistantSalaryService> logger)
        {
            _db = db;
            _logger = logger;
        }

        /// <summary>
        /// 获取店助工资列表
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>店助工资分页列表</returns>
        [HttpGet("assistant")]
        public async Task<dynamic> GetAssistantSalaryList([FromQuery] AssistantSalaryInput input)
        {
            var monthStr = $"{input.Year}{input.Month:D2}";

            // 查询数据
            var query = _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                .Where(x => x.StatisticsMonth == monthStr);

            if (!string.IsNullOrEmpty(input.StoreId))
            {
                query = query.Where(x => x.StoreId == input.StoreId);
            }

            if (!string.IsNullOrEmpty(input.Keyword))
            {
                query = query.Where(x => x.EmployeeName.Contains(input.Keyword) || x.EmployeeId.Contains(input.Keyword));
            }

            var list = await query.Select(x => new AssistantSalaryOutput
            {
                Id = x.Id,
                StoreName = x.StoreName,
                EmployeeName = x.EmployeeName,
                Position = x.Position,
                StoreTotalPerformance = x.StoreTotalPerformance,
                StoreBillingPerformance = x.StoreBillingPerformance,
                StoreRefundPerformance = x.StoreRefundPerformance,
                StoreLifeline = x.StoreLifeline,
                PerformanceCompletionRate = x.PerformanceCompletionRate,
                CommissionRate = x.CommissionRate,
                CommissionAmount = x.CommissionAmount,
                HeadCount = x.HeadCount,
                Stage1TargetHeadCount = x.Stage1TargetHeadCount,
                Stage2TargetHeadCount = x.Stage2TargetHeadCount,
                ReachedStage1 = x.ReachedStage1,
                ReachedStage2 = x.ReachedStage2,
                StageRewardAmount = x.StageRewardAmount,
                Stage1Reward = x.Stage1Reward,
                Stage2Reward = x.Stage2Reward,
                BaseSalary = x.BaseSalary,
                PhoneManagementFee = x.PhoneManagementFee,
                WorkingDays = x.WorkingDays,
                LeaveDays = x.LeaveDays,
                GrossSalary = x.GrossSalary,
                ActualSalary = x.ActualSalary,
                TotalDeduction = x.TotalDeduction,
                TotalSubsidy = x.TotalSubsidy,
                Bonus = x.Bonus,
                ReturnPhoneDeposit = x.ReturnPhoneDeposit,
                ReturnAccommodationDeposit = x.ReturnAccommodationDeposit,
                MonthlyPaymentStatus = x.MonthlyPaymentStatus,
                PaidAmount = x.PaidAmount,
                PendingAmount = x.PendingAmount,
                LastMonthSupplement = x.LastMonthSupplement,
                MonthlyTotalPayment = x.MonthlyTotalPayment,
                IsLocked = x.IsLocked,
                EmployeeConfirmStatus = x.EmployeeConfirmStatus,
                UpdateTime = x.UpdateTime,
                StoreType = x.StoreType,
                StoreCategory = x.StoreCategory,
                IsNewStore = x.IsNewStore,
                NewStoreProtectionStage = x.NewStoreProtectionStage
            })
            .ToPagedListAsync(input.currentPage, input.pageSize);

            return PageResult<AssistantSalaryOutput>.SqlSugarPageResult(list);
        }

        /// <summary>
        /// 通过月份和员工ID查询工资(仅查询已锁定且未确认的工资)
        /// </summary>
        /// <remarks>
        /// **重要**:此接口只能查询已锁定(IsLocked=1)且未确认(EmployeeConfirmStatus=0或null)的工资记录
        /// **注意**:已确认的工资记录无法通过此接口查询
        /// </remarks>
        [HttpGet("query-by-employee")]
        public async Task<AssistantSalaryOutput> GetSalaryByEmployee([FromQuery] SalaryQueryByEmployeeInput input)
        {
            if (input.Year <= 0 || input.Month <= 0 || input.Month > 12)
                throw NCCException.Oh("年份和月份参数不正确");
            if (string.IsNullOrWhiteSpace(input.EmployeeId))
                throw NCCException.Oh("员工ID不能为空");
            var monthStr = $"{input.Year}{input.Month:D2}";
            var salary = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                .Where(x => x.StatisticsMonth == monthStr && x.EmployeeId == input.EmployeeId && x.IsLocked == 1 && x.EmployeeConfirmStatus != 1)
                .Select(x => new AssistantSalaryOutput
                {
                    Id = x.Id,
                    StoreName = x.StoreName,
                    EmployeeName = x.EmployeeName,
                    Position = x.Position,
                    StoreTotalPerformance = x.StoreTotalPerformance,
                    StoreBillingPerformance = x.StoreBillingPerformance,
                    StoreRefundPerformance = x.StoreRefundPerformance,
                    StoreLifeline = x.StoreLifeline,
                    PerformanceCompletionRate = x.PerformanceCompletionRate,
                    CommissionRate = x.CommissionRate,
                    CommissionAmount = x.CommissionAmount,
                    HeadCount = x.HeadCount,
                    Stage1TargetHeadCount = x.Stage1TargetHeadCount,
                    Stage2TargetHeadCount = x.Stage2TargetHeadCount,
                    ReachedStage1 = x.ReachedStage1,
                    ReachedStage2 = x.ReachedStage2,
                    StageRewardAmount = x.StageRewardAmount,
                    Stage1Reward = x.Stage1Reward,
                    Stage2Reward = x.Stage2Reward,
                    BaseSalary = x.BaseSalary,
                    PhoneManagementFee = x.PhoneManagementFee,
                    WorkingDays = x.WorkingDays,
                    LeaveDays = x.LeaveDays,
                    GrossSalary = x.GrossSalary,
                    ActualSalary = x.ActualSalary,
                    TotalDeduction = x.TotalDeduction,
                    TotalSubsidy = x.TotalSubsidy,
                    Bonus = x.Bonus,
                    ReturnPhoneDeposit = x.ReturnPhoneDeposit,
                    ReturnAccommodationDeposit = x.ReturnAccommodationDeposit,
                    MonthlyPaymentStatus = x.MonthlyPaymentStatus,
                    PaidAmount = x.PaidAmount,
                    PendingAmount = x.PendingAmount,
                    LastMonthSupplement = x.LastMonthSupplement,
                    MonthlyTotalPayment = x.MonthlyTotalPayment,
                    IsLocked = x.IsLocked,
                    EmployeeConfirmStatus = x.EmployeeConfirmStatus,
                    UpdateTime = x.UpdateTime,
                    StoreType = x.StoreType,
                    StoreCategory = x.StoreCategory,
                    IsNewStore = x.IsNewStore,
                    NewStoreProtectionStage = x.NewStoreProtectionStage
                })
                .FirstAsync();
            if (salary == null)
                throw NCCException.Oh($"未找到员工{input.EmployeeId}在{input.Year}年{input.Month}月的工资记录");
            return salary;
        }

        /// <summary>
        /// 计算店助工资
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        [HttpPost("calculate/assistant")]
        public async Task CalculateAssistantSalary(int year, int month)
        {
            var startDate = new DateTime(year, month, 1);
            var endDate = startDate.AddMonths(1).AddDays(-1);
            var monthStr = $"{year}{month:D2}";
            var daysInMonth = DateTime.DaysInMonth(year, month); // 当月天数

            // 1. 获取基础数据

            // 1.1 获取店助员工列表(从BASE_USER表,岗位为"店助"或"店助主任")
            var assistantUserList = await _db.Queryable<UserEntity>()
                .Where(x => (x.Gw == "店助" || x.Gw == "店助主任") && x.DeleteMark == null && x.EnabledMark == 1)
                .Select(x => new { x.Id, x.RealName, x.Mdid, x.Gw })
                .ToListAsync();

            if (!assistantUserList.Any())
            {
                // 如果没有店助员工,直接返回
                return;
            }

            // 1.2 门店信息 (lq_mdxx)
            var storeList = await _db.Queryable<LqMdxxEntity>().ToListAsync();
            var storeDict = storeList.Where(x => !string.IsNullOrEmpty(x.Id)).ToDictionary(x => x.Id, x => x);

            // 1.4 门店目标信息 (lq_md_target) - 包含门店生命线和阶段目标
            var storeTargets = await _db.Queryable<LqMdTargetEntity>()
                .Where(x => x.Month == monthStr)
                .ToListAsync();
            var storeTargetDict = storeTargets.Where(x => !string.IsNullOrEmpty(x.StoreId))
                .ToDictionary(x => x.StoreId, x => x);

            // 1.5 门店新店保护信息 (lq_md_xdbhsj)
            var newStoreProtectionList = await _db.Queryable<LqMdXdbhsjEntity>()
                .Where(x => x.Sfqy == 1)
                .ToListAsync();
            var newStoreProtectionDict = newStoreProtectionList
                .Where(x => x.Bhkssj <= startDate && x.Bhjssj >= startDate)
                .GroupBy(x => x.Mdid)
                .ToDictionary(g => g.Key, g => g.First());

            // 1.6 门店总业绩计算 (开单实付 - 退卡金额)
            // 开单实付
            var storeBillingList = await _db.Queryable<LqKdKdjlbEntity>()
                .Where(x => x.Kdrq >= startDate && x.Kdrq <= endDate.AddDays(1) && x.IsEffective == 1)
                .Select(x => new { x.Djmd, x.Sfyj })
                .ToListAsync();
            var storeBillingDict = storeBillingList
                .Where(x => !string.IsNullOrEmpty(x.Djmd))
                .GroupBy(x => x.Djmd)
                .ToDictionary(g => g.Key, g => g.Sum(x => x.Sfyj));

            // 退卡金额(使用F_ActualRefundAmount,如果没有则使用tkje)
            var storeRefundList = await _db.Queryable<LqHytkHytkEntity>()
                .Where(x => x.Tksj >= startDate && x.Tksj <= endDate.AddDays(1) && x.IsEffective == 1)
                .Select(x => new { x.Md, x.ActualRefundAmount, x.Tkje })
                .ToListAsync();
            var storeRefundDict = storeRefundList
                .Where(x => !string.IsNullOrEmpty(x.Md))
                .GroupBy(x => x.Md)
                .ToDictionary(g => g.Key, g => g.Sum(x => x.ActualRefundAmount ?? x.Tkje ?? 0));

            // 1.7 进店消耗人数统计(有消费金额的,按门店按月去重客户数)
            // 使用SQL查询优化性能
            var headcountSql = $@"
                SELECT 
                    hyhk.md as StoreId,
                    COUNT(DISTINCT hyhk.hy) as HeadCount
                FROM lq_xh_hyhk hyhk
                WHERE hyhk.F_IsEffective = 1
                    AND DATE_FORMAT(hyhk.hksj, '%Y%m') = @monthStr
                    AND EXISTS (
                        SELECT 1 
                        FROM lq_xh_jksyj jksyj 
                        WHERE jksyj.glkdbh = hyhk.F_Id 
                            AND jksyj.F_IsEffective = 1 
                            AND jksyj.jksyj > 0
                    )
                GROUP BY hyhk.md";

            var headcountData = await _db.Ado.SqlQueryAsync<dynamic>(headcountSql, new { monthStr });
            var headcountDict = headcountData
                .Where(x => x.StoreId != null)
                .ToDictionary(x => x.StoreId.ToString(), x => Convert.ToInt32(x.HeadCount));

            // 1.8 考勤数据 (lq_attendance_summary)
            var attendanceList = await _db.Queryable<LqAttendanceSummaryEntity>()
                .Where(x => x.Year == year && x.Month == month && x.IsEffective == 1)
                .ToListAsync();
            var attendanceDict = attendanceList.ToDictionary(x => x.UserId, x => x);

            // 2. 计算每个店助的工资
            var assistantSalaryList = new List<LqAssistantSalaryStatisticsEntity>();

            foreach (var assistantUser in assistantUserList)
            {
                var salary = new LqAssistantSalaryStatisticsEntity
                {
                    Id = YitIdHelper.NextId().ToString(),
                    EmployeeId = assistantUser.Id,
                    EmployeeName = assistantUser.RealName,
                    StatisticsMonth = monthStr,
                    Position = assistantUser.Gw ?? "店助", // 使用Gw字段,如果为空则默认为"店助"
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsLocked = 0,
                    MonthlyPaymentStatus = "未发放"
                };

                // 2.1 填充门店信息
                string storeId = assistantUser.Mdid;
                if (string.IsNullOrEmpty(storeId))
                {
                    // 如果用户没有门店ID,跳过
                    continue;
                }

                salary.StoreId = storeId;

                if (storeDict.ContainsKey(storeId))
                {
                    var store = storeDict[storeId];
                    salary.StoreName = store.Dm;
                    salary.StoreType = store.StoreType;
                    salary.StoreCategory = store.StoreCategory;

                    // 数据校验:门店分类必须设置
                    if (!salary.StoreCategory.HasValue)
                    {
                        throw new Exception($"门店【{store.Dm}】的门店分类未设置,无法计算店助工资");
                    }
                }

                // 2.2 填充新店保护信息
                if (newStoreProtectionDict.ContainsKey(storeId))
                {
                    var protection = newStoreProtectionDict[storeId];
                    salary.IsNewStore = "是";
                    salary.NewStoreProtectionStage = protection.Stage;
                }
                else
                {
                    salary.IsNewStore = "否";
                    salary.NewStoreProtectionStage = 0;
                }

                // 2.3 获取门店目标信息(门店生命线和阶段目标)
                // 如果没有设置门店目标,则相关字段设为0(适用于新店未开张等情况)
                if (!storeTargetDict.ContainsKey(storeId))
                {
                    // 门店目标未设置时,所有目标相关字段设为0
                    salary.StoreLifeline = 0;
                    salary.Stage1TargetHeadCount = 0;
                    salary.Stage2TargetHeadCount = 0;
                }
                else
                {
                    var storeTarget = storeTargetDict[storeId];
                    salary.StoreLifeline = storeTarget.StoreLifeline;

                    // 阶段目标设置(如果未设置,则奖励金额为0)
                    salary.Stage1TargetHeadCount = storeTarget.AssistantHeadcountTargetStage1 > 0
                        ? (int)storeTarget.AssistantHeadcountTargetStage1
                        : 0;
                    salary.Stage2TargetHeadCount = storeTarget.AssistantHeadcountTargetStage2 > 0
                        ? (int)storeTarget.AssistantHeadcountTargetStage2
                        : 0;
                }

                // 2.4 计算门店业绩
                decimal billing = storeBillingDict.ContainsKey(storeId) ? storeBillingDict[storeId] : 0;
                decimal refund = storeRefundDict.ContainsKey(storeId) ? storeRefundDict[storeId] : 0;
                salary.StoreBillingPerformance = billing;
                salary.StoreRefundPerformance = refund;
                salary.StoreTotalPerformance = billing - refund;

                // 计算业绩完成率
                if (salary.StoreLifeline > 0)
                {
                    salary.PerformanceCompletionRate = salary.StoreTotalPerformance / salary.StoreLifeline;
                }
                else
                {
                    salary.PerformanceCompletionRate = 0;
                }

                // 2.5 计算提成比例(固定比例,不随在店天数变化)
                // 判断岗位类型:店助 或 店助主任
                bool isDirector = salary.Position == "店助主任";

                decimal commissionRate = 0;

                // 如果门店生命线未设置(<=0),则没有提成
                if (salary.StoreLifeline <= 0)
                {
                    commissionRate = 0;
                }
                else
                {
                    // 计算业绩完成率
                    decimal performanceRatio = salary.StoreTotalPerformance / salary.StoreLifeline;

                    // 根据岗位类型确定提成比例
                    // 店助和店助主任使用相同的分段提成规则,但100%以上部分比例不同
                    decimal totalCommission;
                    if (isDirector)
                    {
                        // 店助主任提成规则(分段式):
                        // 1. 前提条件:门店业绩必须达到门店生命线的70%,否则无提成
                        // 2. 70% ≤ 业绩 < 100%:整个业绩按0.4%
                        // 3. 业绩 ≥ 100%:分段式
                        //    - 0-100%部分(整个生命线):0.4%
                        //    - 100%以上部分:1.6%(与店助的0.6%不同)
                        totalCommission = CalculateDirectorCommission(salary.StoreTotalPerformance, salary.StoreLifeline);
                    }
                    else
                    {
                        // 店助提成规则(分段式):
                        // 1. 前提条件:门店业绩必须达到门店生命线的70%,否则无提成
                        // 2. 70% ≤ 业绩 < 100%:整个业绩按0.4%
                        // 3. 业绩 ≥ 100%:分段式
                        //    - 0-100%部分(整个生命线):0.4%
                        //    - 100%以上部分:0.6%
                        totalCommission = CalculateAssistantCommission(salary.StoreTotalPerformance, salary.StoreLifeline);
                    }

                    // 计算平均提成比例(用于显示)
                    if (salary.StoreTotalPerformance > 0)
                    {
                        commissionRate = totalCommission / salary.StoreTotalPerformance;
                    }
                    else
                    {
                        commissionRate = 0;
                    }
                }

                salary.CommissionRate = commissionRate;

                // 2.6 统计进店消耗人数
                salary.HeadCount = headcountDict.ContainsKey(storeId) ? headcountDict[storeId] : 0;

                // 2.7 计算门店总阶段奖励(先计算门店级别的奖励)
                decimal storeTotalStageReward = 0;
                bool reachedStage1 = false;
                bool reachedStage2 = false;

                // 如果阶段目标未设置(为0),则没有奖励考核,奖励金额为0
                if (salary.Stage1TargetHeadCount <= 0 && salary.Stage2TargetHeadCount <= 0)
                {
                    // 阶段目标未设置,没有奖励考核
                    salary.ReachedStage1 = "否";
                    salary.ReachedStage2 = "否";
                    storeTotalStageReward = 0m;
                }
                else
                {
                    // 阶段目标已设置,进行奖励考核
                    reachedStage1 = salary.Stage1TargetHeadCount > 0 && salary.HeadCount >= salary.Stage1TargetHeadCount;
                    reachedStage2 = salary.Stage2TargetHeadCount > 0 && salary.HeadCount >= salary.Stage2TargetHeadCount;

                    salary.ReachedStage1 = reachedStage1 ? "是" : "否";
                    salary.ReachedStage2 = reachedStage2 ? "是" : "否";

                    // 阶段奖励计算规则:
                    // - 如果达到第二阶段,获得400元(第一阶段200 + 第二阶段200)
                    // - 如果只达到第一阶段,获得200元
                    // - 如果都没达到,获得0元
                    if (reachedStage2)
                    {
                        storeTotalStageReward = 400m;
                    }
                    else if (reachedStage1)
                    {
                        storeTotalStageReward = 200m;
                    }
                    else
                    {
                        storeTotalStageReward = 0m;
                    }
                }

                // 2.8 考勤数据
                int workingDays = 0;
                if (attendanceDict.ContainsKey(assistantUser.Id))
                {
                    var attendance = attendanceDict[assistantUser.Id];
                    workingDays = (int)attendance.WorkDays;
                    salary.WorkingDays = workingDays;
                    salary.LeaveDays = (int)attendance.LeaveDays;
                }
                else
                {
                    salary.WorkingDays = 0;
                    salary.LeaveDays = 0;
                }

                // 2.9 计算底薪(店助和店助主任底薪规则相同,都根据门店分类确定,按在店天数比例计算)
                decimal baseSalaryFull = CalculateBaseSalary(salary.StoreCategory.Value);
                if (daysInMonth > 0 && workingDays > 0)
                {
                    salary.BaseSalary = baseSalaryFull / daysInMonth * workingDays;
                }
                else
                {
                    salary.BaseSalary = 0;
                }

                // 2.10 计算手机管理费(按在店天数比例计算)
                decimal phoneManagementFeeFull = 150m;
                if (daysInMonth > 0 && workingDays > 0)
                {
                    salary.PhoneManagementFee = phoneManagementFeeFull / daysInMonth * workingDays;
                }
                else
                {
                    salary.PhoneManagementFee = 0;
                }

                // 2.11 按在店天数比例计算店助的提成和奖励
                // 逻辑:提成金额 = 门店总提成(阶梯计算) / 当月天数 × 在店天数
                //       阶段奖励 = 门店总奖励 / 当月天数 × 在店天数
                if (daysInMonth > 0 && workingDays > 0)
                {
                    // 先计算门店总提成(阶梯计算)- 根据岗位类型使用不同规则
                    decimal storeTotalCommission;
                    if (isDirector)
                    {
                        storeTotalCommission = CalculateDirectorCommission(salary.StoreTotalPerformance, salary.StoreLifeline);
                    }
                    else
                    {
                        storeTotalCommission = CalculateAssistantCommission(salary.StoreTotalPerformance, salary.StoreLifeline);
                    }

                    // 按比例计算提成:门店总提成 / 当月天数 × 在店天数
                    salary.CommissionAmount = storeTotalCommission / daysInMonth * workingDays;

                    // 按比例计算奖励
                    salary.StageRewardAmount = storeTotalStageReward / daysInMonth * workingDays;

                    // 计算阶段奖励的明细(按比例分配)
                    if (reachedStage2)
                    {
                        // 达到第二阶段:第一阶段200 + 第二阶段200
                        salary.Stage1Reward = 200m / daysInMonth * workingDays;
                        salary.Stage2Reward = 200m / daysInMonth * workingDays;
                    }
                    else if (reachedStage1)
                    {
                        // 只达到第一阶段:第一阶段200
                        salary.Stage1Reward = 200m / daysInMonth * workingDays;
                        salary.Stage2Reward = 0m;
                    }
                    else
                    {
                        // 都没达到
                        salary.Stage1Reward = 0m;
                        salary.Stage2Reward = 0m;
                    }
                }
                else
                {
                    // 如果当月天数为0或在店天数为0,则提成和奖励为0
                    salary.CommissionAmount = 0;
                    salary.StageRewardAmount = 0;
                    salary.Stage1Reward = 0;
                    salary.Stage2Reward = 0;
                }

                // 2.12 计算应发工资
                salary.GrossSalary = salary.BaseSalary + salary.CommissionAmount + salary.StageRewardAmount + salary.PhoneManagementFee;

                // 2.13 初始化扣款、补贴、奖金字段(默认值为0)
                salary.MissingCard = 0;
                salary.LateArrival = 0;
                salary.LeaveDeduction = 0;
                salary.SocialInsuranceDeduction = 0;
                salary.RewardDeduction = 0;
                salary.AccommodationDeduction = 0;
                salary.StudyPeriodDeduction = 0;
                salary.WorkClothesDeduction = 0;
                salary.TotalDeduction = 0;

                salary.MonthlyTrainingSubsidy = 0;
                salary.MonthlyTransportSubsidy = 0;
                salary.LastMonthTrainingSubsidy = 0;
                salary.LastMonthTransportSubsidy = 0;
                salary.TotalSubsidy = 0;

                salary.Bonus = 0;
                salary.ReturnPhoneDeposit = 0;
                salary.ReturnAccommodationDeposit = 0;

                // 2.14 计算实发工资
                salary.ActualSalary = salary.GrossSalary - salary.TotalDeduction + salary.TotalSubsidy + salary.Bonus;

                // 2.15 初始化支付相关字段
                salary.PaidAmount = 0;
                salary.PendingAmount = salary.ActualSalary;
                salary.LastMonthSupplement = 0;
                salary.MonthlyTotalPayment = 0;

                assistantSalaryList.Add(salary);
            }

            // 3. 保存数据
            if (assistantSalaryList.Any())
            {
                // 3.1 先删除计算月的未锁定且未确认的工资记录
                var deletedCount = await _db.Deleteable<LqAssistantSalaryStatisticsEntity>()
                    .Where(x => x.StatisticsMonth == monthStr 
                        && x.IsLocked == 0 
                        && x.EmployeeConfirmStatus == 0)
                    .ExecuteCommandAsync();
                
                if (deletedCount > 0)
                {
                    _logger.LogInformation($"计算工资前删除了 {deletedCount} 条未锁定且未确认的记录(月份:{monthStr})");
                }

                // 3.2 查询已存在的记录(只查询已锁定或已确认的记录)
                var existingRecords = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                    .Where(x => x.StatisticsMonth == monthStr 
                        && (x.IsLocked == 1 || x.EmployeeConfirmStatus == 1))
                    .ToListAsync();

                var existingDict = existingRecords
                    .Where(x => !string.IsNullOrEmpty(x.EmployeeId))
                    .GroupBy(x => x.EmployeeId)
                    .ToDictionary(g => g.Key, g => g.First());

                // 分离需要插入的新记录和需要更新的记录
                var recordsToInsert = new List<LqAssistantSalaryStatisticsEntity>();
                var recordsToUpdate = new List<LqAssistantSalaryStatisticsEntity>();
                var updatedCount = 0;
                var skippedCount = 0;

                foreach (var salary in assistantSalaryList)
                {
                    if (existingDict.ContainsKey(salary.EmployeeId))
                    {
                        // 检查记录是否已锁定或已确认
                        var existing = existingDict[salary.EmployeeId];
                        
                        // 如果已锁定或已确认,跳过不更新(保留所有原有数据,包括扣款项目)
                        if (existing.IsLocked == 1 || existing.EmployeeConfirmStatus == 1)
                        {
                            skippedCount++;
                            continue; // 跳过,不进行任何更新
                        }
                        
                        // 未锁定且未确认的记录,可以做更新操作
                        salary.Id = existing.Id;
                        salary.EmployeeConfirmStatus = existing.EmployeeConfirmStatus; // 应该是0
                        salary.EmployeeConfirmTime = existing.EmployeeConfirmTime;
                        salary.EmployeeConfirmRemark = existing.EmployeeConfirmRemark;
                        salary.IsLocked = existing.IsLocked; // 保留锁定状态(应该是0)
                        salary.CreateTime = existing.CreateTime;
                        salary.CreateUser = existing.CreateUser;
                        recordsToUpdate.Add(salary);
                        updatedCount++;
                    }
                    else
                    {
                        // 不存在的记录,做插入操作
                        salary.Id = YitIdHelper.NextId().ToString();
                        salary.EmployeeConfirmStatus = 0;
                        salary.IsLocked = 0;
                        salary.CreateTime = DateTime.Now;
                        salary.CreateUser = ""; // 新记录,创建人为空或系统
                        recordsToInsert.Add(salary);
                    }
                }

                // 批量插入新记录
                if (recordsToInsert.Any())
                {
                    await _db.Insertable(recordsToInsert).ExecuteCommandAsync();
                    _logger.LogInformation($"插入了 {recordsToInsert.Count} 条新的工资记录(月份:{monthStr})");
                }

                // 批量更新现有记录
                if (recordsToUpdate.Any())
                {
                    await _db.Updateable(recordsToUpdate).ExecuteCommandAsync();
                    _logger.LogInformation($"更新了 {recordsToUpdate.Count} 条未锁定且未确认的工资记录(月份:{monthStr})");
                }

                if (skippedCount > 0)
                {
                    _logger.LogInformation($"跳过了 {skippedCount} 条已锁定或已确认的工资记录,保留原有数据(月份:{monthStr})");
                }
            }
        }

        /// <summary>
        /// 计算店助提成(分段阶梯提成模式)
        /// </summary>
        /// <param name="storePerformance">门店业绩</param>
        /// <param name="storeLifeline">门店生命线</param>
        /// <returns>提成金额</returns>
        /// <remarks>
        /// 提成规则:
        /// 1. 前提条件:门店业绩必须达到门店生命线的70%,否则无提成
        /// 2. 0-100%部分:整个0-100%部分(整个生命线)按0.4%计算
        /// 3. 100%以上部分:按0.6%计算
        /// 4. 分段计算:不同区间按不同比例分别计算后累加
        /// 
        /// 计算公式:
        /// - 如果业绩 < 70%:提成 = 0
        /// - 如果 70% ≤ 业绩 < 100%:提成 = 业绩 × 0.4%
        /// - 如果业绩 ≥ 100%:提成 = 生命线 × 0.4% + (业绩 - 生命线) × 0.6%
        /// </remarks>
        private decimal CalculateAssistantCommission(decimal storePerformance, decimal storeLifeline)
        {
            if (storeLifeline <= 0)
            {
                return 0;
            }

            decimal ratio = storePerformance / storeLifeline;

            // 前提条件:必须达到70%才有提成
            if (ratio < 0.7m)
            {
                // 门店业绩 < 门店生命线 × 70% → 0%(无提成)
                return 0;
            }
            else if (ratio < 1.0m)
            {
                // 门店生命线 × 70% ≤ 门店业绩 < 门店生命线 × 100% → 整个业绩按0.4%计算
                return storePerformance * 0.004m;
            }
            else
            {
                // 门店业绩 ≥ 门店生命线 × 100% → 分段计算
                // 0-100%部分(整个生命线):按0.4%计算
                // 100%以上部分:按0.6%计算
                decimal commissionBelow100 = storeLifeline * 0.004m;  // 0-100%部分(整个生命线)按0.4%
                decimal commissionAbove100 = (storePerformance - storeLifeline) * 0.006m;  // 100%以上部分按0.6%

                return commissionBelow100 + commissionAbove100;
            }
        }

        /// <summary>
        /// 计算店助主任提成(分段提成模式)
        /// </summary>
        /// <param name="storePerformance">门店业绩</param>
        /// <param name="storeLifeline">门店生命线</param>
        /// <returns>提成金额</returns>
        /// <remarks>
        /// 店助主任提成规则(分段式,与店助相同,但100%以上部分比例不同):
        /// 1. 前提条件:门店业绩必须达到门店生命线的70%,否则无提成
        /// 2. 70% ≤ 业绩 < 100%:整个业绩按0.4%计算
        /// 3. 业绩 ≥ 100%:分段式提成
        ///    - 0-100%部分(整个生命线):按0.4%计算
        ///    - 100%以上部分:按1.6%计算(与店助的0.6%不同)
        /// 
        /// 计算公式:
        /// - 如果业绩 < 70%:提成 = 0
        /// - 如果 70% ≤ 业绩 < 100%:提成 = 业绩 × 0.4%
        /// - 如果业绩 ≥ 100%:提成 = 生命线 × 0.4% + (业绩 - 生命线) × 1.6%
        /// </remarks>
        private decimal CalculateDirectorCommission(decimal storePerformance, decimal storeLifeline)
        {
            if (storeLifeline <= 0)
            {
                return 0;
            }

            decimal ratio = storePerformance / storeLifeline;

            // 前提条件:必须达到70%才有提成
            if (ratio < 0.7m)
            {
                // 门店业绩 < 门店生命线 × 70% → 0%(无提成)
                return 0;
            }
            else if (ratio < 1.0m)
            {
                // 门店生命线 × 70% ≤ 门店业绩 < 门店生命线 × 100% → 整个业绩按0.4%计算
                return storePerformance * 0.004m;
            }
            else
            {
                // 门店业绩 ≥ 门店生命线 × 100% → 分段式提成
                // 0-100%部分(整个生命线):按0.4%计算
                // 100%以上部分:按1.6%计算(店助主任与店助的区别)
                decimal commissionBelow100 = storeLifeline * 0.004m;  // 0-100%部分(整个生命线)按0.4%
                decimal commissionAbove100 = (storePerformance - storeLifeline) * 0.016m;  // 100%以上部分按1.6%

                return commissionBelow100 + commissionAbove100;
            }
        }

        /// <summary>
        /// 计算底薪(店助)
        /// </summary>
        /// <param name="storeCategory">门店分类(1=A类,2=B类,3=C类)</param>
        /// <returns>底薪金额</returns>
        private decimal CalculateBaseSalary(int storeCategory)
        {
            return storeCategory switch
            {
                1 => 3000m, // A类门店
                2 => 3100m, // B类门店
                3 => 3200m, // C类门店
                _ => throw new Exception($"门店分类值无效:{storeCategory},有效值为1(A类)、2(B类)、3(C类)")
            };
        }

        #region 员工工资确认

        /// <summary>
        /// 员工确认工资条
        /// </summary>
        /// <remarks>
        /// 员工确认自己的工资条,确认后工资数据不可再修改
        /// 
        /// 示例请求:
        /// <code>
        /// {
        ///   "id": "工资记录ID",
        ///   "employeeId": "员工ID",
        ///   "remark": "确认备注(可选)"
        /// }
        /// </code>
        /// 
        /// 参数说明:
        /// - id: 工资记录ID(必填)
        /// - employeeId: 员工ID(必填)
        /// - remark: 确认备注(可选)
        /// 
        /// 注意事项:
        /// - 只能确认自己的工资条
        /// - 只能确认已锁定的工资条(IsLocked = 1)
        /// - 已确认的工资条不能重复确认
        /// </remarks>
        /// <param name="input">确认参数</param>
        /// <returns>操作结果</returns>
        /// <response code="200">确认成功</response>
        /// <response code="400">参数错误或记录不存在</response>
        [HttpPost("confirm")]
        public async Task<string> ConfirmSalary([FromBody] SalaryConfirmInput input)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(input.Id))
                {
                    throw NCCException.Oh("工资记录ID不能为空");
                }

                if (string.IsNullOrWhiteSpace(input.EmployeeId))
                {
                    throw NCCException.Oh("员工ID不能为空");
                }

                var salary = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                    .Where(s => s.Id == input.Id && s.EmployeeId == input.EmployeeId)
                    .FirstAsync();

                if (salary == null)
                {
                    throw NCCException.Oh("工资记录不存在或不属于该员工");
                }

                if (salary.EmployeeConfirmStatus == 1)
                {
                    throw NCCException.Oh("该工资条已确认,不能重复确认");
                }

                if (salary.IsLocked != 1)
                {
                    throw NCCException.Oh("该工资条尚未锁定,请等待管理员锁定后再确认");
                }

                salary.EmployeeConfirmStatus = 1;
                salary.EmployeeConfirmTime = DateTime.Now;
                salary.EmployeeConfirmRemark = input.Remark;
                salary.UpdateTime = DateTime.Now;

                await _db.Updateable(salary).ExecuteCommandAsync();

                return "确认成功";
            }
            catch (Exception ex)
            {
                throw NCCException.Oh($"确认工资条失败: {ex.Message}");
            }
        }

        #endregion

        #region 工资锁定/解锁

        /// <summary>
        /// 批量锁定/解锁工资条
        /// </summary>
        [HttpPost("lock")]
        public async Task<string> LockSalary([FromBody] SalaryLockInput input)
        {
            try
            {
                if (input == null || input.Ids == null || !input.Ids.Any())
                    throw NCCException.Oh("工资记录ID列表不能为空");
                var salaries = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                    .Where(s => input.Ids.Contains(s.Id))
                    .ToListAsync();
                if (!salaries.Any())
                    throw NCCException.Oh("未找到指定的工资记录");
                var lockedCount = 0;
                var unlockedCount = 0;
                var skippedCount = 0;
                foreach (var salary in salaries)
                {
                    if (salary.EmployeeConfirmStatus == 1 && !input.IsLocked)
                    {
                        skippedCount++;
                        continue;
                    }
                    salary.IsLocked = input.IsLocked ? 1 : 0;
                    salary.UpdateTime = DateTime.Now;
                    if (input.IsLocked) lockedCount++; else unlockedCount++;
                }
                await _db.Updateable(salaries).ExecuteCommandAsync();
                var action = input.IsLocked ? "锁定" : "解锁";
                var count = input.IsLocked ? lockedCount : unlockedCount;
                var message = $"{action}成功:{count}条";
                if (skippedCount > 0)
                    message += $",跳过{skippedCount}条(已确认的记录不能解锁)";
                return message;
            }
            catch (Exception ex)
            {
                throw NCCException.Oh($"锁定/解锁工资条失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 批量锁定当月所有工资
        /// </summary>
        /// <param name="input">批量锁定输入参数</param>
        /// <returns>锁定结果</returns>
        [HttpPost("lock-by-month")]
        public async Task<dynamic> LockSalaryByMonth([FromBody] SalaryLockByMonthInput input)
        {
            try
            {
                if (input == null)
                    throw NCCException.Oh("参数不能为空");

                if (input.Year <= 0 || input.Month <= 0 || input.Month > 12)
                    throw NCCException.Oh("年份和月份参数不正确");

                var monthStr = $"{input.Year}{input.Month:D2}";

                var salaries = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                    .Where(s => s.StatisticsMonth == monthStr)
                    .ToListAsync();

                if (!salaries.Any())
                    throw NCCException.Oh($"未找到{input.Year}年{input.Month}月的工资记录");

                var lockedCount = 0;
                var unlockedCount = 0;
                var skippedCount = 0;
                var alreadyLockedCount = 0;

                foreach (var salary in salaries)
                {
                    if (salary.EmployeeConfirmStatus == 1 && !input.IsLocked)
                    {
                        skippedCount++;
                        continue;
                    }

                    if (salary.IsLocked == 1 && input.IsLocked)
                    {
                        alreadyLockedCount++;
                        continue;
                    }

                    if (salary.IsLocked == 0 && !input.IsLocked)
                    {
                        alreadyLockedCount++;
                        continue;
                    }

                    salary.IsLocked = input.IsLocked ? 1 : 0;
                    salary.UpdateTime = DateTime.Now;

                    if (input.IsLocked)
                        lockedCount++;
                    else
                        unlockedCount++;
                }

                if (lockedCount > 0 || unlockedCount > 0)
                {
                    var salariesToUpdate = salaries.Where(s =>
                        (input.IsLocked && s.IsLocked == 0) ||
                        (!input.IsLocked && s.IsLocked == 1 && s.EmployeeConfirmStatus != 1)
                    ).ToList();

                    if (salariesToUpdate.Any())
                    {
                        await _db.Updateable(salariesToUpdate)
                            .UpdateColumns(s => new { s.IsLocked, s.UpdateTime })
                            .ExecuteCommandAsync();
                    }
                }

                var action = input.IsLocked ? "锁定" : "解锁";
                var count = input.IsLocked ? lockedCount : unlockedCount;
                var message = $"{action}成功:{count}条";

                if (alreadyLockedCount > 0)
                    message += $",跳过{alreadyLockedCount}条(已是{action}状态)";

                if (skippedCount > 0)
                    message += $",跳过{skippedCount}条(已确认的记录不能解锁)";

                return new
                {
                    success = true,
                    message = message,
                    total = salaries.Count,
                    locked = lockedCount,
                    unlocked = unlockedCount,
                    skipped = skippedCount,
                    alreadyLocked = alreadyLockedCount
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量锁定当月工资失败");
                var action = input?.IsLocked == true ? "锁定" : "解锁";
                throw NCCException.Oh($"批量{action}当月工资失败: {ex.Message}");
            }
        }

        #endregion

        #region 导入工资

        /// <summary>
        /// 从Excel导入店助工资数据
        /// </summary>
        /// <param name="file">Excel文件</param>
        /// <returns>导入结果</returns>
        [HttpPost("import")]
        public async Task<dynamic> ImportSalaryFromExcel(IFormFile file)
        {
            try
            {
                if (file == null || file.Length == 0)
                    throw NCCException.Oh("请选择要上传的Excel文件");

                var allowedExtensions = new[] { ".xlsx", ".xls" };
                var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant();
                if (!allowedExtensions.Contains(fileExtension))
                    throw NCCException.Oh("只支持.xlsx和.xls格式的Excel文件");

                var recordsToInsert = new List<LqAssistantSalaryStatisticsEntity>();
                var recordsToUpdate = new List<LqAssistantSalaryStatisticsEntity>();
                var errorMessages = new List<string>();
                var successCount = 0;
                var failCount = 0;
                var skippedCount = 0;

                var tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(file.FileName));
                try
                {
                    using (var stream = new FileStream(tempFilePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    var dataTable = ExcelImportHelper.ToDataTable(tempFilePath, 0, 0);
                    if (dataTable.Rows.Count == 0)
                        throw NCCException.Oh("Excel文件中没有数据行");

                    Func<string, decimal> ParseDecimal = (str) =>
                    {
                        if (string.IsNullOrWhiteSpace(str)) return 0;
                        var cleaned = str.Trim().Replace(",", "").Replace(",", "").Replace("¥", "").Replace("$", "").Replace("元", "").Replace("%", "").Replace(" ", "");
                        return decimal.TryParse(cleaned, out decimal result) ? result : 0;
                    };

                    Func<string, int> ParseInt = (str) =>
                    {
                        if (string.IsNullOrWhiteSpace(str)) return 0;
                        var cleaned = str.Trim().Replace(",", "").Replace(",", "").Replace(" ", "");
                        return int.TryParse(cleaned, out int result) ? result : 0;
                    };

                    // ExcelImportHelper.ToDataTable(filePath, 0, 0)会将第一行作为标题行,数据从第二行开始
                    // 所以DataTable.Rows[0]是Excel的第一条数据行,应该从i=0开始循环
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        try
                        {
                            var row = dataTable.Rows[i];
                            Func<int, string> GetColumnValue = (colIndex) => colIndex < row.ItemArray.Length && row[colIndex] != null ? row[colIndex].ToString().Trim() : "";

                            var firstColumnValue = GetColumnValue(0);
                            bool isOldFormat = !string.IsNullOrWhiteSpace(firstColumnValue) && (firstColumnValue == "门店名称" || (!long.TryParse(firstColumnValue, out _) && firstColumnValue.Length > 20));

                            int storeNameIndex = isOldFormat ? 0 : 1;
                            int employeeNameIndex = isOldFormat ? 1 : 2;
                            int offset = isOldFormat ? 0 : 1;

                            var id = isOldFormat ? "" : GetColumnValue(0);
                            var employeeName = GetColumnValue(employeeNameIndex);
                            var storeName = GetColumnValue(storeNameIndex);

                            if (string.IsNullOrWhiteSpace(id) && string.IsNullOrWhiteSpace(employeeName))
                                continue;

                            if (string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(employeeName))
                            {
                                var matchedRecord = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                                    .Where(x => x.EmployeeName == employeeName)
                                    .WhereIF(!string.IsNullOrWhiteSpace(storeName), x => x.StoreName == storeName)
                                    .OrderBy(x => x.CreateTime, OrderByType.Desc)
                                    .FirstAsync();
                                if (matchedRecord != null) id = matchedRecord.Id;
                            }

                            if (string.IsNullOrWhiteSpace(employeeName))
                            {
                                errorMessages.Add($"第{i + 1}行:员工姓名不能为空");
                                failCount++;
                                continue;
                            }

                            LqAssistantSalaryStatisticsEntity existing = null;
                            if (!string.IsNullOrWhiteSpace(id))
                            {
                                existing = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                                    .Where(x => x.Id == id).FirstAsync();

                                if (existing != null && (existing.IsLocked == 1 || existing.EmployeeConfirmStatus == 1))
                                {
                                    skippedCount++;
                                    failCount++;
                                    continue;
                                }
                            }

                            var entity = existing ?? new LqAssistantSalaryStatisticsEntity
                            {
                                Id = string.IsNullOrWhiteSpace(id) ? YitIdHelper.NextId().ToString() : id,
                                EmployeeConfirmStatus = 0,
                                IsLocked = 0,
                                CreateTime = DateTime.Now,
                                CreateUser = ""
                            };

                            entity.StoreName = storeName;
                            entity.EmployeeName = employeeName;
                            entity.Position = GetColumnValue(2 + offset);
                            entity.StoreTotalPerformance = ParseDecimal(GetColumnValue(3 + offset));
                            entity.StoreBillingPerformance = ParseDecimal(GetColumnValue(4 + offset));
                            entity.StoreRefundPerformance = ParseDecimal(GetColumnValue(5 + offset));
                            entity.StoreLifeline = ParseDecimal(GetColumnValue(6 + offset));
                            entity.PerformanceCompletionRate = ParseDecimal(GetColumnValue(7 + offset));
                            entity.CommissionRate = ParseDecimal(GetColumnValue(8 + offset));
                            entity.CommissionAmount = ParseDecimal(GetColumnValue(9 + offset));
                            entity.HeadCount = ParseInt(GetColumnValue(10 + offset));
                            entity.Stage1TargetHeadCount = ParseInt(GetColumnValue(11 + offset));
                            entity.Stage2TargetHeadCount = ParseInt(GetColumnValue(12 + offset));
                            entity.ReachedStage1 = GetColumnValue(13 + offset);
                            entity.ReachedStage2 = GetColumnValue(14 + offset);
                            entity.StageRewardAmount = ParseDecimal(GetColumnValue(15 + offset));
                            entity.Stage1Reward = ParseDecimal(GetColumnValue(16 + offset));
                            entity.Stage2Reward = ParseDecimal(GetColumnValue(17 + offset));
                            entity.BaseSalary = ParseDecimal(GetColumnValue(18 + offset));
                            entity.PhoneManagementFee = ParseDecimal(GetColumnValue(19 + offset));
                            entity.WorkingDays = ParseInt(GetColumnValue(20 + offset));
                            entity.LeaveDays = ParseInt(GetColumnValue(21 + offset));
                            entity.GrossSalary = ParseDecimal(GetColumnValue(22 + offset));
                            entity.ActualSalary = ParseDecimal(GetColumnValue(23 + offset));
                            entity.TotalDeduction = ParseDecimal(GetColumnValue(24 + offset));
                            entity.TotalSubsidy = ParseDecimal(GetColumnValue(25 + offset));
                            entity.Bonus = ParseDecimal(GetColumnValue(26 + offset));
                            entity.ReturnPhoneDeposit = ParseDecimal(GetColumnValue(27 + offset));
                            entity.ReturnAccommodationDeposit = ParseDecimal(GetColumnValue(28 + offset));
                            entity.LastMonthSupplement = ParseDecimal(GetColumnValue(29 + offset));
                            entity.MonthlyPaymentStatus = GetColumnValue(30 + offset);
                            entity.PaidAmount = ParseDecimal(GetColumnValue(31 + offset));
                            entity.PendingAmount = ParseDecimal(GetColumnValue(32 + offset));
                            entity.MonthlyTotalPayment = ParseDecimal(GetColumnValue(33 + offset));
                            entity.IsNewStore = GetColumnValue(34 + offset) == "是" ? "是" : "否";
                            entity.NewStoreProtectionStage = ParseInt(GetColumnValue(35 + offset));

                            if (existing != null)
                            {
                                entity.StoreId = existing.StoreId;
                                entity.EmployeeId = existing.EmployeeId;
                                entity.StatisticsMonth = existing.StatisticsMonth;
                            }
                            else
                            {
                                // 对于新记录,尝试通过员工姓名和门店名称匹配已有记录以获取统计月份
                                LqAssistantSalaryStatisticsEntity matchedRecord = null;
                                if (!string.IsNullOrWhiteSpace(employeeName))
                                {
                                    var user = await _db.Queryable<UserEntity>()
                                        .Where(u => u.RealName == employeeName).FirstAsync();
                                    if (user != null)
                                    {
                                        entity.EmployeeId = user.Id;
                                        if (!string.IsNullOrEmpty(user.Mdid) && string.IsNullOrWhiteSpace(storeName))
                                            entity.StoreId = user.Mdid;

                                        // 尝试通过员工ID匹配已有记录获取统计月份
                                        matchedRecord = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                                            .Where(x => x.EmployeeId == user.Id)
                                            .WhereIF(!string.IsNullOrWhiteSpace(storeName), x => x.StoreName == storeName)
                                            .OrderBy(x => x.CreateTime, OrderByType.Desc)
                                            .FirstAsync();
                                    }

                                    if (!string.IsNullOrWhiteSpace(storeName) && string.IsNullOrEmpty(entity.StoreId))
                                    {
                                        var store = await _db.Queryable<LqMdxxEntity>()
                                            .Where(s => s.Dm == storeName).FirstAsync();
                                        if (store != null)
                                        {
                                            entity.StoreId = store.Id;
                                            // 如果没有匹配到记录,再通过门店ID尝试
                                            if (matchedRecord == null)
                                            {
                                                matchedRecord = await _db.Queryable<LqAssistantSalaryStatisticsEntity>()
                                                    .Where(x => x.StoreId == store.Id)
                                                    .WhereIF(!string.IsNullOrWhiteSpace(employeeName), x => x.EmployeeName == employeeName)
                                                    .OrderBy(x => x.CreateTime, OrderByType.Desc)
                                                    .FirstAsync();
                                            }
                                        }
                                    }
                                }

                                // 如果有匹配的记录,使用其统计月份;否则使用当前年月作为默认值
                                if (matchedRecord != null && !string.IsNullOrWhiteSpace(matchedRecord.StatisticsMonth))
                                {
                                    entity.StatisticsMonth = matchedRecord.StatisticsMonth;
                                }
                                else
                                {
                                    // 如果没有匹配记录,使用当前年月(YYYYMM格式)
                                    var now = DateTime.Now;
                                    entity.StatisticsMonth = $"{now.Year}{now.Month:D2}";
                                }
                            }

                            entity.UpdateTime = DateTime.Now;
                            if (existing != null) recordsToUpdate.Add(entity);
                            else recordsToInsert.Add(entity);
                            successCount++;
                        }
                        catch (Exception ex)
                        {
                            errorMessages.Add($"第{i + 1}行数据处理失败: {ex.Message}");
                            failCount++;
                        }
                    }
                }
                finally
                {
                    if (File.Exists(tempFilePath)) File.Delete(tempFilePath);
                }

                if (recordsToInsert.Any()) await _db.Insertable(recordsToInsert).ExecuteCommandAsync();
                if (recordsToUpdate.Any())
                {
                    // 使用IgnoreColumns排除CreateTime和CreateUser,确保其他所有字段都被更新
                    await _db.Updateable(recordsToUpdate)
                        .IgnoreColumns(x => x.CreateTime)
                        .IgnoreColumns(x => x.CreateUser)
                        .ExecuteCommandAsync();
                }

                return new
                {
                    success = true,
                    message = $"导入完成:成功 {successCount} 条,失败 {failCount} 条,跳过 {skippedCount} 条(已锁定或已确认)",
                    successCount,
                    failCount,
                    skippedCount,
                    errors = errorMessages
                };
            }
            catch (Exception ex)
            {
                throw NCCException.Oh($"导入店助工资数据失败: {ex.Message}");
            }
        }

        #endregion
    }
}