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 { private DeviceSerive _deviceSerive; public GroupService(IConfiguration config, DeviceSerive deviceSerive) : base(config, nameof(Group)) { _deviceSerive = deviceSerive; } /// /// 新加分组 /// /// /// public async Task Add(Group input) { Expression> 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 = "已经存在同名的组" }; } /// /// 更新 /// /// /// public async Task update(Group input) { Expression> 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 = "对象为空" }; } /// /// /// /// /// public async Task remove(IEnumerable ids) { if (ids.Any()) { foreach (var item in ids) { await base.RemoveAsync(item); } } return new ApiResult() { code = 0, msg = "" }; } /// /// 取单条 /// /// /// public async Task FindOne(string id) { Expression> exp = num => num.Id == id; var entity = (await base.GetListWithExp(exp)).FirstOrDefault(); Expression> 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 } }; } /// /// /// /// /// public async Task GetList(RqeustPaging request) { request.pageSize = request.pageSize == 0 ? 10 : request.pageSize; Expression> 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 } }; } /// /// group 取全部的设备id /// /// /// public async Task> GetDeviceIds(IEnumerable ids) { Expression> exp = num => ids.Contains(num.Id); List result = new List(); (await base.GetListWithExp(exp)).ToList().Select(s => s.DeviceIds).ToList().ForEach(o => { result.AddRange(o); }); return result; } } }