// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using System.ComponentModel;
using Admin.NET.Core;
using Bodk.NET.Customer.Entities;
using Bodk.NET.Customer.Models;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Bodk.NET.Customer.Services;
///
/// 客户接口控制器
///
[ApiDescriptionSettings(Groups = new[] { "Bodk Groups" }, Name = "Customer", Description = "客户接口")]
public class CustomerService(SqlSugarRepository repository) : IDynamicApiController, ITransient
{
///
/// 新增客户
///
///
[ApiDescriptionSettings(Name = "Add"), HttpPost]
[DisplayName("Add")]
[Authorize(AuthenticationSchemes =
JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
public async Task AddCustomer(AddCustomerInput input)
{
var entity = input.Adapt();
bool exist = await CheckCustomerExist(entity);
if (exist)
throw Oops.Oh("该用户已存在");
return await repository.InsertReturnSnowflakeIdAsync(entity);
}
///
/// 更新客户
///
///
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("Update")]
[Authorize(AuthenticationSchemes =
JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
public async Task UpdateCustomer(UpdateCustomerInput input)
{
var entity = input.Adapt();
bool exist = await CheckCustomerExist(input.Id);
if (!exist)
throw Oops.Oh("该用户不存在");
await repository.UpdateAsync(entity);
}
///
/// 获取客户列表
///
///
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("Update")]
[Authorize(AuthenticationSchemes =
JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
public async Task PageCustomer(UpdateCustomerInput input)
{
var entity = input.Adapt();
bool exist = await CheckCustomerExist(input.Id);
if (!exist)
throw Oops.Oh("该用户不存在");
await repository.UpdateAsync(entity);
}
private async Task CheckCustomerExist(BodkCustomer entity)
{
return await repository.AsQueryable().AnyAsync(u => u.IdCard == entity.IdCard)
|| await repository.AsQueryable().AnyAsync(u => u.Phone == entity.Phone);
}
private Task CheckCustomerExist(long id)
{
return repository.AsQueryable().AnyAsync(u => u.Id == id);
}
}