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;
///
/// 创建
///
/// 参数
///
public async Task Insert(ParamDO param)
{
await Db.Insertable(param).ExecuteCommandAsync();
return param;
}
///
/// 创建校验
///
/// 参数
///
public string InsertCheck(ParamDO param)
{
if (string.IsNullOrEmpty(param.Name))
{
return "参数不能为空";
}
if (string.IsNullOrEmpty(param.Id))
{
var _MaxId = Db.Queryable().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().Any(x => x.Id == param.Id))
{
return "编号已存在";
}
}
param.UseCount = 0;
return "";
}
public async Task Update(ParamDO param)
{
await Db.Updateable(param).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == param.Id).ExecuteCommandAsync();
return param;
}
public async Task Use(string id) {
await Db.Updateable().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().Any(x => x.Id == param.Id))
{
return "参数不存在";
}
return "";
}
public async Task Delete(string[] ids)
{
await Db.Deleteable().In(ids).ExecuteCommandAsync();
}
public async Task> List(ParamDO param)
{
var _Result = await Db.Queryable()
.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;
}
}
}