using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Mapster; using Microsoft.AspNetCore.Mvc; using NCC.ClayObject; using NCC.Common.Configuration; using NCC.Common.Core.Manager; using NCC.Common.Enum; using NCC.Common.Extension; using NCC.Common.Filter; using NCC.Common.Helper; using NCC.Common.Model.NPOI; using NCC.DataEncryption; using NCC.Dependency; using NCC.DynamicApiController; using NCC.Extend.Entitys.Dto.LqMdXdbhsj; using NCC.Extend.Entitys.lq_md_xdbhsj; using NCC.Extend.Interfaces.LqMdXdbhsj; using NCC.FriendlyException; using NCC.JsonSerialization; using SqlSugar; using Yitter.IdGenerator; namespace NCC.Extend.LqMdXdbhsj { /// /// 门店新店保护时间服务 /// [ApiDescriptionSettings(Tag = "绿纤门店信息服务", Name = "LqMdXdbhsj", Order = 201)] [Route("api/Extend/[controller]")] public class LqMdXdbhsjService : ILqMdXdbhsjService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _lqMdXdbhsjRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public LqMdXdbhsjService(ISqlSugarRepository lqMdXdbhsjRepository, IUserManager userManager) { _lqMdXdbhsjRepository = lqMdXdbhsjRepository; _db = _lqMdXdbhsjRepository.Context; _userManager = userManager; } #region 门店新店保护时间信息 /// /// 获取门店新店保护时间信息 /// /// 参数 /// [HttpGet("{id}")] public async Task GetInfo(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); var output = entity.Adapt(); return output; } #endregion #region 门店新店保护时间列表 /// /// 获取门店新店保护时间列表 /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] LqMdXdbhsjListQueryInput input) { var sidx = input.sidx == null ? "F_Id" : input.sidx; var data = await _db.Queryable() .WhereIF(!string.IsNullOrEmpty(input.mdid), p => p.Mdid.Contains(input.mdid)) .WhereIF(input.bhkssj.HasValue, p => p.Bhkssj >= input.bhkssj) .WhereIF(input.bhjssj.HasValue, p => p.Bhjssj <= input.bhjssj) .WhereIF(input.sfqy.HasValue, p => p.Sfqy == input.sfqy) .Select(it => new LqMdXdbhsjListOutput { F_Id = it.Id, mdid = it.Mdid, bhkssj = it.Bhkssj, bhjssj = it.Bhjssj, sm = it.Sm, cjsj = it.Cjsj, sfqy = it.Sfqy, stage = it.Stage, }) .MergeTable() .OrderBy(sidx + " " + input.sort) .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } #endregion #region 新建门店新店保护时间 /// /// 新建门店新店保护时间 /// /// 参数 /// [HttpPost("")] public async Task Create([FromBody] LqMdXdbhsjCrInput input) { var userInfo = await _userManager.GetUserInfo(); var entity = input.Adapt(); entity.Id = YitIdHelper.NextId().ToString(); entity.Cjsj = DateTime.Now; // 验证时间逻辑 if (entity.Bhkssj >= entity.Bhjssj) { throw NCCException.Oh("保护开始时间必须早于保护结束时间"); } // 检查是否存在重叠的保护时间 var exists = await _db.Queryable() .Where(p => p.Mdid == entity.Mdid && p.Sfqy == 1) .Where(p => (p.Bhkssj <= entity.Bhkssj && p.Bhjssj > entity.Bhkssj) || (p.Bhkssj < entity.Bhjssj && p.Bhjssj >= entity.Bhjssj) || (p.Bhkssj >= entity.Bhkssj && p.Bhjssj <= entity.Bhjssj)) .AnyAsync(); if (exists) { throw NCCException.Oh("该门店在指定时间段内已存在保护时间设置"); } var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000); } #endregion #region 更新门店新店保护时间 /// /// 更新门店新店保护时间 /// /// 参数 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] LqMdXdbhsjUpInput input) { var entity = input.Adapt(); entity.Id = id; // 验证时间逻辑 if (entity.Bhkssj >= entity.Bhjssj) { throw NCCException.Oh(ErrorCode.COM1000, "保护开始时间必须早于保护结束时间"); } // 检查是否存在重叠的保护时间(排除当前记录) var exists = await _db.Queryable() .Where(p => p.Mdid == entity.Mdid && p.Sfqy == 1 && p.Id != id) .Where(p => (p.Bhkssj <= entity.Bhkssj && p.Bhjssj > entity.Bhkssj) || (p.Bhkssj < entity.Bhjssj && p.Bhjssj >= entity.Bhjssj) || (p.Bhkssj >= entity.Bhkssj && p.Bhjssj <= entity.Bhjssj) ) .AnyAsync(); if (exists) { throw NCCException.Oh(ErrorCode.COM1000, "该门店在指定时间段内已存在保护时间设置"); } var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001); } #endregion #region 删除门店新店保护时间 /// /// 删除门店新店保护时间 /// /// [HttpDelete("{id}")] public async Task Delete(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005); var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002); } #endregion #region 获取门店新店保护时间下拉选择数据 /// /// 获取门店新店保护时间下拉选择数据 /// /// [HttpGet("Selector")] public async Task GetSelector() { var list = await _db.Queryable() .Where(p => p.Sfqy == 1) .Select(it => new { id = it.Id, fullName = it.Mdid, enCode = it.Id, stage = it.Stage, }) .ToListAsync(); return new { list = list }; } #endregion #region 根据门店ID获取当前有效的保护时间 /// /// 根据门店ID获取当前有效的保护时间 /// /// 门店ID /// [HttpGet("GetByStoreId/{mdid}")] public async Task GetByStoreId(string mdid) { var now = DateTime.Now; var entity = await _db.Queryable().Where(p => p.Mdid == mdid && p.Sfqy == 1).Where(p => p.Bhkssj <= now && p.Bhjssj >= now).FirstAsync(); if (entity == null) { return new { hasProtection = false, message = "该门店当前无保护时间设置" }; } var output = entity.Adapt(); return new { hasProtection = true, data = output }; } #endregion } }