LqMdGeneralManagerLifelineService.cs 19.9 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqMdGeneralManagerLifeline;
using NCC.Extend.Entitys.Enum;
using NCC.Extend.Entitys.lq_md_general_manager_lifeline;
using NCC.Extend.Entitys.lq_md_target;
using NCC.Extend.Interfaces.LqMdGeneralManagerLifeline;
using NCC.FriendlyException;
using SqlSugar;
using Yitter.IdGenerator;

namespace NCC.Extend.LqMdGeneralManagerLifeline
{
    /// <summary>
    /// 门店总经理生命线设置服务
    /// </summary>
    [ApiDescriptionSettings(Tag = "绿纤门店总经理生命线设置服务", Name = "LqMdGeneralManagerLifeline", Order = 201)]
    [Route("api/Extend/[controller]")]
    public class LqMdGeneralManagerLifelineService : ILqMdGeneralManagerLifelineService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<LqMdGeneralManagerLifelineEntity> _repository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;

        /// <summary>
        /// 初始化一个<see cref="LqMdGeneralManagerLifelineService"/>类型的新实例
        /// </summary>
        public LqMdGeneralManagerLifelineService(ISqlSugarRepository<LqMdGeneralManagerLifelineEntity> repository, IUserManager userManager)
        {
            _repository = repository;
            _db = _repository.Context;
            _userManager = userManager;
        }

        #region 获取门店总经理生命线设置信息
        /// <summary>
        /// 获取门店总经理生命线设置信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<dynamic> GetInfo(string id)
        {
            var entity = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().FirstAsync(p => p.Id == id);
            _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
            var output = entity.Adapt<LqMdGeneralManagerLifelineInfoOutput>();
            return output;
        }
        #endregion

        #region 获取门店总经理生命线设置列表
        /// <summary>
        /// 获取门店总经理生命线设置列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<dynamic> GetList([FromQuery] LqMdGeneralManagerLifelineListQueryInput input)
        {
            var sidx = input.sidx == null ? "id" : input.sidx;
            var data = await _db.Queryable<LqMdGeneralManagerLifelineEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.storeId), p => p.StoreId.Contains(input.storeId))
                .WhereIF(!string.IsNullOrEmpty(input.month), p => p.Month == input.month)
                .WhereIF(!string.IsNullOrEmpty(input.generalManagerId), p => p.GeneralManagerId.Contains(input.generalManagerId))
                .Select(it => new LqMdGeneralManagerLifelineListOutput
                {
                    id = it.Id,
                    storeId = it.StoreId,
                    month = it.Month,
                    generalManagerId = it.GeneralManagerId,
                    managerType = it.ManagerType,
                    lifeline1 = it.Lifeline1,
                    commissionRate1 = it.CommissionRate1,
                    lifeline2 = it.Lifeline2,
                    commissionRate2 = it.CommissionRate2,
                    lifeline3 = it.Lifeline3,
                    commissionRate3 = it.CommissionRate3,
                    remark = it.Remark,
                    createTime = it.CreateTime,
                    createUserId = it.CreateUserId,
                    updateTime = it.UpdateTime,
                    updateUserId = it.UpdateUserId,
                })
                .MergeTable()
                .OrderBy(sidx + " " + input.sort)
                .ToPagedListAsync(input.currentPage, input.pageSize);

            // 设置经理类型中文名称
            foreach (var item in data.list)
            {
                if (Enum.IsDefined(typeof(ManagerTypeEnum), item.managerType))
                {
                    var enumValue = (ManagerTypeEnum)item.managerType;
                    item.managerTypeName = enumValue.GetDescription();
                }
                else
                {
                    item.managerTypeName = string.Empty;
                }
            }

            return PageResult<LqMdGeneralManagerLifelineListOutput>.SqlSugarPageResult(data);
        }
        #endregion

        #region 新建门店总经理生命线设置
        /// <summary>
        /// 新建门店总经理生命线设置
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPost("")]
        public async Task Create([FromBody] LqMdGeneralManagerLifelineCrInput input)
        {
            var userInfo = await _userManager.GetUserInfo();

            // 验证月份格式
            if (input.month.Length != 6 || !Regex.IsMatch(input.month, @"^\d{6}$"))
            {
                throw NCCException.Oh(ErrorCode.COM1000, "月份格式必须为YYYYMM(如:202501)");
            }

            // 验证门店ID、月份、总经理ID的唯一性
            var exists = await _db.Queryable<LqMdGeneralManagerLifelineEntity>()
                .Where(p => p.StoreId == input.storeId && p.Month == input.month && p.GeneralManagerId == input.generalManagerId)
                .AnyAsync();
            if (exists)
            {
                throw NCCException.Oh(ErrorCode.COM1000, "该门店在该月份该总经理已存在生命线设置记录");
            }

            var entity = input.Adapt<LqMdGeneralManagerLifelineEntity>();
            entity.Id = YitIdHelper.NextId().ToString();
            entity.CreateTime = DateTime.Now;
            entity.CreateUserId = userInfo.userId;

            var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1000);
        }
        #endregion

        #region 更新门店总经理生命线设置
        /// <summary>
        /// 更新门店总经理生命线设置
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task Update(string id, [FromBody] LqMdGeneralManagerLifelineUpInput input)
        {
            var userInfo = await _userManager.GetUserInfo();

            // 验证记录是否存在
            var entity = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().FirstAsync(p => p.Id == id);
            _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);

            // 验证月份格式
            if (input.month.Length != 6 || !Regex.IsMatch(input.month, @"^\d{6}$"))
            {
                throw NCCException.Oh("月份格式必须为YYYYMM(如:202501)");
            }

            // 如果门店ID、月份或总经理ID发生变化,需要验证唯一性
            if (entity.StoreId != input.storeId || entity.Month != input.month || entity.GeneralManagerId != input.generalManagerId)
            {
                var exists = await _db.Queryable<LqMdGeneralManagerLifelineEntity>()
                    .Where(p => p.StoreId == input.storeId && p.Month == input.month && p.GeneralManagerId == input.generalManagerId && p.Id != id)
                    .AnyAsync();
                if (exists)
                {
                    throw NCCException.Oh("该门店在该月份该总经理已存在生命线设置记录");
                }
            }

            var updateEntity = input.Adapt<LqMdGeneralManagerLifelineEntity>();
            updateEntity.Id = id;
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUserId = userInfo.userId;

            var isOk = await _db.Updateable(updateEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1001);
        }
        #endregion

        #region 删除门店总经理生命线设置
        /// <summary>
        /// 删除门店总经理生命线设置
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task Delete(string id)
        {
            var entity = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().FirstAsync(p => p.Id == id);
            _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
            var isOk = await _db.Deleteable<LqMdGeneralManagerLifelineEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1002);
        }
        #endregion

        #region 批量创建门店总经理生命线设置(从门店目标表批量创建)
        /// <summary>
        /// 批量创建门店总经理生命线设置(从门店目标表批量创建)
        /// </summary>
        /// <remarks>
        /// 从 lq_md_target 表中读取数据,根据 F_BusinessUnitGeneralManager 和 F_BusinessUnitManager 字段,
        /// 批量创建对应的总经理生命线设置记录。
        /// 
        /// 示例请求:
        /// POST /api/Extend/LqMdGeneralManagerLifeline/BatchCreateFromTarget?month=202501
        /// 
        /// 业务逻辑:
        /// 1. 从 lq_md_target 表中查询指定月份的数据
        /// 2. 提取 F_StoreId(门店ID)、F_Month(月份)、F_BusinessUnitGeneralManager(总经理ID)、F_BusinessUnitManager(经理ID)
        /// 3. 对于每个门店目标记录,创建对应的总经理和经理的生命线设置记录
        /// 4. 过滤掉已存在的记录,避免重复创建
        /// 5. 批量插入新记录
        /// </remarks>
        /// <param name="month">月份(YYYYMM格式)</param>
        /// <returns>批量创建结果</returns>
        /// <response code="200">成功返回创建结果</response>
        /// <response code="400">参数错误</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost("BatchCreateFromTarget")]
        public async Task<dynamic> BatchCreateFromTarget([FromQuery] string month)
        {
            var userInfo = await _userManager.GetUserInfo();

            // 验证月份格式
            if (string.IsNullOrEmpty(month) || month.Length != 6 || !Regex.IsMatch(month, @"^\d{6}$"))
            {
                throw NCCException.Oh(ErrorCode.COM1000, "月份格式必须为YYYYMM(如:202501)");
            }

            // 从门店目标表中查询数据
            var targetData = await _db.Queryable<LqMdTargetEntity>()
                .Where(p => p.Month == month)
                .Select(p => new
                {
                    p.StoreId,
                    p.Month,
                    p.BusinessUnitGeneralManager,
                    p.BusinessUnitManager
                })
                .ToListAsync();

            if (targetData == null || targetData.Count == 0)
            {
                return new
                {
                    success = true,
                    message = $"月份 {month} 没有门店目标设置",
                    createdCount = 0,
                    skippedCount = 0
                };
            }

            // 查询已存在的记录
            var existingRecords = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().Where(p => p.Month == month).Select(p => new { p.StoreId, p.GeneralManagerId }).ToListAsync();

            var existingKeys = existingRecords.Select(p => $"{p.StoreId}_{p.GeneralManagerId}").ToHashSet();

            // 构建要创建的生命线设置记录
            var entities = new List<LqMdGeneralManagerLifelineEntity>();

            foreach (var target in targetData)
            {
                // 创建总经理的生命线设置
                if (!string.IsNullOrEmpty(target.BusinessUnitGeneralManager))
                {
                    var key = $"{target.StoreId}_{target.BusinessUnitGeneralManager}";
                    if (!existingKeys.Contains(key))
                    {
                        entities.Add(new LqMdGeneralManagerLifelineEntity
                        {
                            Id = YitIdHelper.NextId().ToString(),
                            StoreId = target.StoreId,
                            Month = target.Month,
                            ManagerType = ManagerTypeEnum.总经理.GetHashCode(),
                            GeneralManagerId = target.BusinessUnitGeneralManager,
                            Lifeline1 = 0,
                            CommissionRate1 = 0,
                            Lifeline2 = 0,
                            CommissionRate2 = 0,
                            Lifeline3 = 0,
                            CommissionRate3 = 0,
                            Remark = "",
                            CreateTime = DateTime.Now,
                            CreateUserId = userInfo.userId,
                        });
                    }
                }

                // 创建经理的生命线设置(如果经理ID不为空且与总经理ID不同)
                if (!string.IsNullOrEmpty(target.BusinessUnitManager) &&
                    target.BusinessUnitManager != target.BusinessUnitGeneralManager)
                {
                    var key = $"{target.StoreId}_{target.BusinessUnitManager}";
                    if (!existingKeys.Contains(key))
                    {
                        entities.Add(new LqMdGeneralManagerLifelineEntity
                        {
                            Id = YitIdHelper.NextId().ToString(),
                            StoreId = target.StoreId,
                            Month = target.Month,
                            ManagerType = ManagerTypeEnum.经理.GetHashCode(),
                            GeneralManagerId = target.BusinessUnitManager,
                            Lifeline1 = 0,
                            CommissionRate1 = 0,
                            Lifeline2 = 0,
                            CommissionRate2 = 0,
                            Lifeline3 = 0,
                            CommissionRate3 = 0,
                            Remark = "",
                            CreateTime = DateTime.Now,
                            CreateUserId = userInfo.userId,
                        });
                    }
                }
            }

            int createdCount = 0;
            int skippedCount = targetData.Count * 2 - entities.Count; // 理论最大记录数 - 实际创建数

            if (entities.Count > 0)
            {
                createdCount = await _db.Insertable(entities).ExecuteCommandAsync();
            }

            return new
            {
                success = true,
                message = $"批量创建完成:成功创建 {createdCount} 条记录,跳过 {skippedCount} 条已存在的记录",
                createdCount = createdCount,
                skippedCount = skippedCount,
                month = month
            };
        }
        #endregion

        #region 复制上月设置
        /// <summary>
        /// 复制上月设置
        /// </summary>
        /// <remarks>
        /// 从上个月复制生命线设置数据到目标月份
        /// 
        /// 示例请求:
        /// POST /api/Extend/LqMdGeneralManagerLifeline/CopyLastMonthData?targetMonth=202502
        /// 
        /// 业务逻辑:
        /// 1. 确定目标月份(如果未指定,使用当前月份)
        /// 2. 计算上个月份
        /// 3. 查询上个月的所有生命线设置数据
        /// 4. 查询目标月份已存在的记录
        /// 5. 过滤掉已存在的记录,只复制新记录
        /// 6. 批量插入新记录
        /// </remarks>
        /// <param name="targetMonth">目标月份(YYYYMM格式),如果为空则复制到当前月份</param>
        /// <returns>复制结果</returns>
        /// <response code="200">成功返回复制结果</response>
        /// <response code="400">参数错误</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost("CopyLastMonthData")]
        public async Task<dynamic> CopyLastMonthData([FromQuery] string targetMonth = null)
        {
            var userInfo = await _userManager.GetUserInfo();

            // 确定目标月份
            string targetMonthStr;
            if (string.IsNullOrEmpty(targetMonth))
            {
                // 如果未指定,使用当前月份
                var now = DateTime.Now;
                targetMonthStr = now.ToString("yyyyMM");
            }
            else
            {
                // 验证月份格式
                if (targetMonth.Length != 6 || !Regex.IsMatch(targetMonth, @"^\d{6}$"))
                {
                    throw NCCException.Oh(ErrorCode.COM1000, "月份格式必须为YYYYMM(如:202501)");
                }
                targetMonthStr = targetMonth;
            }

            // 计算上个月份
            var targetDate = DateTime.ParseExact(targetMonthStr, "yyyyMM", null);
            var lastMonthDate = targetDate.AddMonths(-1);
            var lastMonthStr = lastMonthDate.ToString("yyyyMM");
            // 查询上个月的所有生命线设置数据
            var lastMonthData = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().Where(p => p.Month == lastMonthStr).ToListAsync();
            if (lastMonthData == null || lastMonthData.Count == 0)
            {
                return new
                {
                    success = true,
                    message = $"上个月({lastMonthStr})没有数据可复制",
                    copiedCount = 0,
                    skippedCount = 0
                };
            }
            // 查询目标月份已存在的记录
            var existingRecords = await _db.Queryable<LqMdGeneralManagerLifelineEntity>().Where(p => p.Month == targetMonthStr).Select(p => new { p.StoreId, p.GeneralManagerId }).ToListAsync();
            var existingKeys = existingRecords.Select(p => $"{p.StoreId}_{p.GeneralManagerId}").ToHashSet();
            // 过滤掉已存在的记录,只复制新记录
            var newEntities = lastMonthData.Where(p => !existingKeys.Contains($"{p.StoreId}_{p.GeneralManagerId}"))
                .Select(p => new LqMdGeneralManagerLifelineEntity
                {
                    Id = YitIdHelper.NextId().ToString(),
                    StoreId = p.StoreId,
                    Month = targetMonthStr,
                    ManagerType = p.ManagerType,
                    GeneralManagerId = p.GeneralManagerId,
                    Lifeline1 = p.Lifeline1,
                    CommissionRate1 = p.CommissionRate1,
                    Lifeline2 = p.Lifeline2,
                    CommissionRate2 = p.CommissionRate2,
                    Lifeline3 = p.Lifeline3,
                    CommissionRate3 = p.CommissionRate3,
                    Remark = p.Remark,
                    CreateTime = DateTime.Now,
                    CreateUserId = userInfo.userId,
                }).ToList();
            int copiedCount = 0;
            int skippedCount = lastMonthData.Count - newEntities.Count;
            if (newEntities.Count > 0)
            {
                copiedCount = await _db.Insertable(newEntities).ExecuteCommandAsync();
            }
            return new
            {
                success = true,
                message = $"复制完成:成功复制{copiedCount}条记录,跳过{skippedCount}条已存在的记录",
                copiedCount = copiedCount,
                skippedCount = skippedCount,
                lastMonth = lastMonthStr,
                targetMonth = targetMonthStr
            };
        }
        #endregion
    }
}