using langguanApi.Extensions;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using Mapster;
using System.Linq.Expressions;
namespace langguanApi.Service
{
    /// 
    /// OrganizedService
    /// 
    [ServiceInjection(InjectionType.Transient)]
    public class OrganizedService : BaseService
    {
        private readonly DeviceService _deviceService;
        public OrganizedService(IConfiguration config, DeviceService device) : base(config, nameof(Organized))
        {
            _deviceService = device;
        }
        /// 
        /// Add 工序
        /// 
        /// 
        /// 
        public async Task Add(OrganizedDto input)
        {
            var entity = input.Adapt();
            await CreateAsync(entity);
            return new ApiResult() { code = 0 };
        }
        /// 
        /// Update
        /// 
        /// 
        /// 
        public async Task Update(OrganizedUpdateDto input)
        {
            var entity = input.Adapt();
            await UpdateAsync(input.Id, entity);
            return new ApiResult() { code = 0 };
        }
        /// 
        /// Delete
        /// 
        /// 
        /// 
        public async Task Delete(IEnumerable ids)
        {
            if (ids.Any())
            {
                foreach (var id in ids)
                {
                    await RemoveAsync(id);
                }
            }
            else
            {
                return new ApiResult() { code = 1, msg = "请选择要删除的记录" };
            }
            return new ApiResult() { code = 0 };
        }
        /// 
        /// ListAndDevice
        /// 
        /// 1,有组织,2无组织
        /// 1,voc,2cems
        /// 组织Id
        /// 
        public async Task ListAndDevice(int OrganizedType, int DeviceType, string OrgId)
        {
            Expression> exp = filter => filter.OrganizedType == OrganizedType && filter.IsDelete == false;
            if (!string.IsNullOrEmpty(OrgId))
            {
                exp = exp.And(filter => filter.Id == OrgId);
            }
            var result = (await base.GetListWithExp(exp)).OrderByDescending(x => x.Order).ToList();
            List list = new List();
            Expression> deviceExp = filter => filter.IsDelete == false;
            if (DeviceType > 0)
            {
                deviceExp = deviceExp.And(filter => filter.DeviceType == DeviceType);
            }
            if (!string.IsNullOrEmpty(OrgId))
            {
                deviceExp= deviceExp.And(filter => filter.OrgId == OrgId);
            }
            var devices = (await _deviceService.GetListWithExp(deviceExp)).ToList();
            foreach (var item in result)
            {
                list.Add(new OrganizedByDeviceDto
                {
                    Items = devices.Where(s => s.OrgId == item.Id).ToList(),
                    Order = item.Order,
                    Name = item.Name,
                    OrganizedType = item.OrganizedType,
                    Id = item.Id,
                });
            }
            return new ApiResult() { code = 0, data = list };
        }
        /// 
        /// List
        /// 
        /// 1,有组织,2无组织
        /// 
        public async Task List(int OrganizedType = 1)
        {
            List dto = new List();
            Expression> exp = filter => filter.OrganizedType == OrganizedType && filter.IsDelete == false;
            var result = (await base.GetListWithExp(exp)).OrderByDescending(x => x.Order).ToList();
            return new ApiResult() { code = 0, data = result };
        }
    }
}