You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.3 KiB
C#

using CommonModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhongLianInterface;
using ZhongLianModel;
namespace ZhongLianService
{
public class ParamService : IParamService
{
public ParamService(IServiceProvider services, ISqlSugarClient sqlSugarClient)
{
Services = services;
Db = sqlSugarClient;
Db.CodeFirst.InitTables(typeof(ParamDO));
}
private readonly IServiceProvider Services;
private readonly ISqlSugarClient Db;
/// <summary>
/// 创建
/// </summary>
/// <param name="param">参数</param>
/// <returns></returns>
public async Task<ParamDO> Insert(ParamDO param)
{
await Db.Insertable<ParamDO>(param).ExecuteCommandAsync();
return param;
}
/// <summary>
/// 创建校验
/// </summary>
/// <param name="param">参数</param>
/// <returns></returns>
public string InsertCheck(ParamDO param)
{
if (string.IsNullOrEmpty(param.Name))
{
return "参数不能为空";
}
if (string.IsNullOrEmpty(param.Id))
{
var _MaxId = Db.Queryable<ParamDO>().Max(x => x.Id);
if (string.IsNullOrEmpty(_MaxId))
{
param.Id = $"PM000001";
}
else
{
param.Id = $"PM{(Convert.ToInt32(_MaxId.Substring(2, 6)) + 1).ToString().PadLeft(6, '0')}";
}
}
else
{
if (Db.Queryable<ParamDO>().Any(x => x.Id == param.Id))
{
return "编号已存在";
}
}
param.UseCount = 0;
return "";
}
public async Task<ParamDO> Update(ParamDO param)
{
await Db.Updateable<ParamDO>(param).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == param.Id).ExecuteCommandAsync();
return param;
}
public async Task Use(string id) {
await Db.Updateable<ParamDO>().SetColumns(x => x.UseCount == (x.UseCount + 1)).Where(x => x.Id == id).ExecuteCommandAsync();
}
public string UpdateCheck(ParamDO param)
{
if (string.IsNullOrEmpty(param.Id) || !Db.Queryable<ParamDO>().Any(x => x.Id == param.Id))
{
return "参数不存在";
}
return "";
}
public async Task Delete(string[] ids)
{
await Db.Deleteable<ParamDO>().In(ids).ExecuteCommandAsync();
}
public async Task<List<ParamDO>> List(ParamDO param)
{
var _Result = await Db.Queryable<ParamDO>()
.WhereIF(!string.IsNullOrEmpty(param.Category), x => x.Category == param.Category)
.WhereIF(!string.IsNullOrEmpty(param.Id), x => x.Id == param.Id)
.WhereIF(!string.IsNullOrEmpty(param.Name), x => x.Name!.Contains(param.Name!))
.OrderByDescending(x => x.UseCount)
.ToListAsync();
return _Result;
}
}
}