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.UavCustomerService;
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.UavCustomerService;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
namespace NCC.Extend.UavCustomerService
{
///
/// 客服信息表服务
///
[ApiDescriptionSettings(Tag = "客服信息表服务", Name = "UavCustomerService", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavCustomerServiceService : IUavCustomerServiceService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _uavCustomerServiceRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public UavCustomerServiceService(ISqlSugarRepository uavCustomerServiceRepository, IUserManager userManager)
{
_uavCustomerServiceRepository = uavCustomerServiceRepository;
_db = _uavCustomerServiceRepository.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] UavCustomerServiceListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.agentId), p => p.AgentId.Contains(input.agentId))
.Select(it => new UavCustomerServiceListOutput
{
id = it.Id,
agentId = it.AgentId,
phone = it.Phone,
remark = it.Remark,
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 新建客服信息表
///
/// 新建客服信息表
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] UavCustomerServiceCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
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 更新客服信息表
///
/// 更新客服信息表
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavCustomerServiceUpInput 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
#region 代理商添加客服
///
/// 代理商添加客服
///
/// 参数
///
[HttpPost("AddAgentCustomerService")]
public async Task AddAgentCustomerService([FromBody] UavCustomerServiceCrInput input)
{
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.AgentId = _userManager.UserId;
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 根据当前代理商获取客服信息表列表
///
/// 根据当前代理商获取客服信息表列表
///
///
[HttpGet("GetAgentCustomerServiceList")]
public async Task GetAgentCustomerServiceList([FromQuery] UavCustomerServiceListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.Where(it=> it.AgentId == _userManager.UserId)
.Select(it => new UavCustomerServiceListOutput
{
id = it.Id,
agentId = it.AgentId,
phone = it.Phone,
remark = it.Remark,
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
}
}