jy-plc/Services/EmployeeService.cs

105 lines
3.9 KiB
C#
Raw Permalink Normal View History

2024-07-24 13:30:21 +00:00
using LangGuan.Command.Model;
using LangGuan.Command.Model.EntityModel;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace LangGuan.Services
{
/// <summary>
///
/// </summary>
public class EmployeeService : BaseService<Employee>
{
/// <summary>
///
/// </summary>
/// <param name="config"></param>
public EmployeeService(IConfiguration config) : base(config, nameof(Employee))
{
}
/// <summary>
/// 登陆
/// </summary>
/// <param name="Account"></param>
/// <param name="Pwd"></param>
/// <returns></returns>
public async Task<ApiResult> Login(string Account, string Pwd)
{
Expression<Func<Employee, bool>> expression = filter => filter.Account == Account && filter.Pwd == Pwd;
var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
if (entity != null) return new ApiResult() { code = 0, data = new { account = Account, nickName = entity.NickName }, msg = "" };
else
return new ApiResult() { code = 1, msg = "登陆失败,请检查用户名跟密码" };
}
/// <summary>
/// 更新密码
/// </summary>
/// <param name="Account"></param>
/// <param name="Pwd"></param>
/// <param name="NewPwd"></param>
/// <returns></returns>
public async Task<ApiResult> Update(string Account, string Pwd, string NewPwd)
{
Expression<Func<Employee, bool>> expression = filter => filter.Account == Account && filter.Pwd == Pwd;
var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
if (entity != null)
{
entity.Pwd = NewPwd;
await base.UpdateAsync(entity.Id, entity);
return new ApiResult() { code = 0, msg = "" };
}
return new ApiResult() { code = 1, msg = "旧密码有误" };
}
/// <summary>
/// 新加用户
/// </summary>
/// <param name="Account"></param>
/// <param name="Pwd"></param>
/// <param name="NickName"></param>
/// <returns></returns>
public async Task<ApiResult> Add(string Account, string Pwd, string NickName)
{
Expression<Func<Employee, bool>> expression = filter => filter.Account == Account;
var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
if (entity != null)
{
return new ApiResult() { code = 1, msg = $"已经存在{Account},无法重复添加" };
}
else
{
await base.CreateAsync(new Employee()
{
Account = Account,
Pwd = Pwd,
NickName = NickName
});
return new ApiResult() { code = 0, msg = "" };
}
}
/// <summary>
/// 列表页
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<ApiResult> GetList(RqeustPaging request)
{
request.pageSize = request.pageSize == 0 ? 10 : request.pageSize;
Expression<Func<Employee, bool>> exp = num => true;
var query = await base.GetListWithExp(exp);
var total = query.Count();
var items = query.OrderByDescending(s => s.CreateDateTime)
.Skip(request.pageSize * (request.current - 1)).Take(request.pageSize)
.Select(s => new { s.NickName, s.Account, s.Id }).ToList();
return new ApiResult()
{
code = 0,
data = new { total = total, items = items }
};
}
}
}