137 lines
5.1 KiB
C#
137 lines
5.1 KiB
C#
|
|
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
|
|||
|
|
{
|
|||
|
|
public class GroupService : BaseService<Group>
|
|||
|
|
{
|
|||
|
|
private DeviceSerive _deviceSerive;
|
|||
|
|
public GroupService(IConfiguration config, DeviceSerive deviceSerive) : base(config, nameof(Group))
|
|||
|
|
{
|
|||
|
|
_deviceSerive = deviceSerive;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新加分组
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<ApiResult> Add(Group input)
|
|||
|
|
{
|
|||
|
|
Expression<Func<Group, bool>> expression = filter => filter.GroupName == input.GroupName;
|
|||
|
|
var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
|
|||
|
|
if (entity == null)
|
|||
|
|
{
|
|||
|
|
if (await _deviceSerive.CheckGroupState(input.DeviceIds))
|
|||
|
|
{
|
|||
|
|
await base.CreateAsync(input);
|
|||
|
|
return new ApiResult() { code = 0, msg = "" };
|
|||
|
|
}
|
|||
|
|
return new ApiResult() { code = 1, msg = "设备已有分组" };
|
|||
|
|
}
|
|||
|
|
return new ApiResult() { code = 1, msg = "已经存在同名的组" };
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 更新
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<ApiResult> update(Group input)
|
|||
|
|
{
|
|||
|
|
Expression<Func<Group, bool>> exp = num => num.Id == input.Id;
|
|||
|
|
var entity = (await base.GetListWithExp(exp)).FirstOrDefault();
|
|||
|
|
if (entity != null)
|
|||
|
|
{
|
|||
|
|
//更新设备状态为不在组内
|
|||
|
|
await _deviceSerive.UpdataGroupState(entity.DeviceIds, false);
|
|||
|
|
entity.GroupName = input.GroupName;
|
|||
|
|
entity.Description = input.Description;
|
|||
|
|
entity.DeviceIds = input.DeviceIds;
|
|||
|
|
await base.UpdateAsync(entity.Id, entity);
|
|||
|
|
await _deviceSerive.UpdataGroupState(input.DeviceIds, true);
|
|||
|
|
return new ApiResult() { code = 0, msg = "" };
|
|||
|
|
}
|
|||
|
|
return new ApiResult() { code = 1, msg = "对象为空" };
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="ids"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<ApiResult> remove(IEnumerable<string> ids)
|
|||
|
|
{
|
|||
|
|
if (ids.Any())
|
|||
|
|
{
|
|||
|
|
foreach (var item in ids)
|
|||
|
|
{
|
|||
|
|
await base.RemoveAsync(item);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return new ApiResult() { code = 0, msg = "" };
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 取单条
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<ApiResult> FindOne(string id)
|
|||
|
|
{
|
|||
|
|
Expression<Func<Group, bool>> exp = num => num.Id == id;
|
|||
|
|
var entity = (await base.GetListWithExp(exp)).FirstOrDefault();
|
|||
|
|
Expression<Func<Device, bool>> exp1 = filter => entity.DeviceIds.Contains(filter.Id);
|
|||
|
|
var deviceItems = (await _deviceSerive.GetListWithExp(exp1)).ToList();
|
|||
|
|
return new ApiResult()
|
|||
|
|
{
|
|||
|
|
code = 0,
|
|||
|
|
msg = "",
|
|||
|
|
data = new GroupDevice()
|
|||
|
|
{
|
|||
|
|
GroupName = entity.GroupName,
|
|||
|
|
Description = entity.Description,
|
|||
|
|
items = deviceItems
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
/// <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<Group, 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.Id, s.GroupName, s.Description, s.DeviceIds }).ToList();
|
|||
|
|
return new ApiResult()
|
|||
|
|
{
|
|||
|
|
code = 0,
|
|||
|
|
data = new { total = total, items = items }
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// group 取全部的设备id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="ids"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<IEnumerable<string>> GetDeviceIds(IEnumerable<string> ids)
|
|||
|
|
{
|
|||
|
|
Expression<Func<Group, bool>> exp = num => ids.Contains(num.Id);
|
|||
|
|
List<string> result = new List<string>();
|
|||
|
|
(await base.GetListWithExp(exp)).ToList().Select(s => s.DeviceIds).ToList().ForEach(o =>
|
|||
|
|
{
|
|||
|
|
result.AddRange(o);
|
|||
|
|
});
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|