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.FriendlyException; using NCC.Extend.Interfaces.UavRelation; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using NCC.Extend.Entitys; using NCC.Extend.Entitys.Dto.UavRelation; using Yitter.IdGenerator; using NCC.Common.Helper; using NCC.JsonSerialization; namespace NCC.Extend.UavRelation { /// /// 套餐绑定服务 /// [ApiDescriptionSettings(Tag = "Extend", Name = "UavRelation", Order = 200)] [Route("api/Extend/[controller]")] public class UavRelationService : IUavRelationService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _uavRelationRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public UavRelationService( ISqlSugarRepository uavRelationRepository, IUserManager userManager) { _uavRelationRepository = uavRelationRepository; _db = _uavRelationRepository.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] UavRelationListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; var data = await _db.Queryable() .WhereIF(!string.IsNullOrEmpty(input.siteId), p => p.SiteId.Contains(input.siteId)) .WhereIF(!string.IsNullOrEmpty(input.feeRuleId), p => p.FeeRuleId.Contains(input.feeRuleId)) .Select(it => new UavRelationListOutput { id = it.Id, siteId = it.SiteId, feeRuleId = it.FeeRuleId, }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } #endregion #region 新建套餐绑定 /// /// 新建套餐绑定 /// /// 参数 /// [HttpPost("")] public async Task Create([FromBody] UavRelationCrInput input) { var userInfo = await _userManager.GetUserInfo(); //通过场地id和套餐id查询套餐绑定 var data = await _db.Queryable().Where(p => p.SiteId == input.siteId && p.FeeRuleId == input.feeRuleId).AnyAsync(); //如果已经查询到绑定,则提示套餐绑定已存在 if (data == false) { var entity = input.Adapt(); entity.Id = YitIdHelper.NextId().ToString(); var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000); } } #endregion #region 套餐绑定 /// /// 套餐绑定 /// /// /// /// [HttpPost("BindRelation/{id}")] public async Task BindRelation(string id, [FromBody] List input) { var result = await _db.Ado.UseTranAsync(async () => { // 1. 删除所有旧的绑定关系 await _db.Deleteable().Where(d => d.SiteId == id).ExecuteCommandAsync(); // 2. 插入新的绑定关系 foreach (var item in input) { var entity = new UavRelationEntity { Id = YitIdHelper.NextId().ToString(), SiteId = id, FeeRuleId = item, CreatorUserId = _userManager.UserId, CreatorTime = DateTime.Now }; await _db.Insertable(entity).ExecuteCommandAsync(); } }); if (!result.IsSuccess) { throw new Exception("绑定失败:" + result.ErrorMessage); } } #endregion #region 获取当前场所绑定的套餐 /// /// 获取当前场所绑定的套餐 /// /// 参数 /// [HttpGet("GetSiteBindFeeRule/{id}")] public async Task GetSiteBindFeeRule(string id) { var data = await _db.Queryable().Where(p => p.SiteId == id).Select(p=>p.FeeRuleId).ToListAsync(); return data; } #endregion #region 更新套餐绑定 /// /// 更新套餐绑定 /// /// 主键 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] UavRelationUpInput input) { var entity = input.Adapt(); 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 } }