134 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
using langguanApi.Extensions.AutoDI;
 | 
						|
using langguanApi.Model;
 | 
						|
using langguanApi.Model.Dto;
 | 
						|
using Mapster;
 | 
						|
using System.Linq.Expressions;
 | 
						|
 | 
						|
namespace langguanApi.Service
 | 
						|
{
 | 
						|
    [ServiceInjection(InjectionType.Transient)]
 | 
						|
    public class DeviceService : BaseService<Device>
 | 
						|
    {
 | 
						|
        public DeviceService(IConfiguration config) : base(config, nameof(Device))
 | 
						|
        {
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 新加
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> Add(DeviceAddDto input)
 | 
						|
        {
 | 
						|
            if (await Exist(input))
 | 
						|
            {
 | 
						|
                return new ApiResult { code = 1, msg = $"已经存在名称为:{input.NickName}" };
 | 
						|
            }
 | 
						|
            var entity = input.Adapt<Device>();
 | 
						|
            if (entity != null)
 | 
						|
            {
 | 
						|
                await base.CreateAsync(entity);
 | 
						|
                return new ApiResult { code = 0, msg = "" };
 | 
						|
            }
 | 
						|
            return new ApiResult { code = -1, msg = "" }; ;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 是否存在
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> Exist(DeviceAddDto input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<Device>();
 | 
						|
            Expression<Func<Device, bool>> exp = filter => filter.deviceMN == entity.deviceMN;
 | 
						|
            return await base.Exist(exp);
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 更新
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> update(DeviceUpdateDto input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<Device>();
 | 
						|
            await base.UpdateAsync(entity.Id, entity);
 | 
						|
            return new ApiResult { code = 0, msg = "" };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 取单个
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<Device> FindeById(string id)
 | 
						|
        {
 | 
						|
            return await GetAsync(id);
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// remove
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="ids"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> remove(IEnumerable<string> ids)
 | 
						|
        {
 | 
						|
            if (ids.Any())
 | 
						|
            {
 | 
						|
                foreach (var item in ids)
 | 
						|
                {
 | 
						|
                    var entity = await base.GetAsync(item);
 | 
						|
                    entity.IsDelete = true;
 | 
						|
                    await base.UpdateAsync(entity.Id, entity);
 | 
						|
                }
 | 
						|
                return new ApiResult { code = 0, msg = "" };
 | 
						|
            }
 | 
						|
            return new ApiResult { code = -1, msg = "" };
 | 
						|
        }
 | 
						|
        // 通过devicemn获取设备信息
 | 
						|
        public async Task<Device> GetByDeviceMN(string deviceMN)
 | 
						|
        {
 | 
						|
            Expression<Func<Device, bool>> exp = filter => filter.deviceMN == deviceMN && filter.IsDelete == false;
 | 
						|
            return (await base.GetListWithExp(exp)).FirstOrDefault();
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 通过orgid获取设备信息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="ids"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<List<Device>> GetDeviceByOrgids(IEnumerable<string> ids)
 | 
						|
        {
 | 
						|
            Expression<Func<Device, bool>> exp = filter => ids.Contains(filter.OrgId) && filter.IsDelete == false;
 | 
						|
            var list = (await base.GetListWithExp(exp)).ToList();
 | 
						|
            return list;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 获取设备类型
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public  Task<Dictionary<int, string>> GetDeviceTypes()
 | 
						|
        {
 | 
						|
            //1 voc,2 cems,3,tsp,4 video
 | 
						|
            Dictionary<int, string> dic = new Dictionary<int, string>
 | 
						|
            {
 | 
						|
                { 1, "voc" },
 | 
						|
                { 2, "cems" },
 | 
						|
                { 3, "tsp" },
 | 
						|
                { 4, "video" }
 | 
						|
            };
 | 
						|
            return Task.FromResult(dic);
 | 
						|
        }
 | 
						|
    /// <summary>
 | 
						|
    /// 分页取数据
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="input"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    public async Task<object> GetPage(reqpage input)
 | 
						|
    {
 | 
						|
        Expression<Func<Device, bool>> exp = filter => filter.NickName.Contains(input.key) && filter.IsDelete == false;
 | 
						|
        return await base.GetPager(new ReqPaing()
 | 
						|
        {
 | 
						|
            pageSize = input.pageSize,
 | 
						|
            current = input.current
 | 
						|
        }, exp);
 | 
						|
    }
 | 
						|
}
 | 
						|
}
 |