using AutoMapper;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Repository.Interface;
using Ropin.Inspection.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service
{
    public class TispContentGroupService : ITispContentGroupService
    {
        private readonly ITispContentGroupRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public TispContentGroupService(IClaimsAccessor claims, ITispContentGroupRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
        }

        public async Task<IEnumerable<TispContentGroupViewModel>> GetAllAsync()
        {
            var pagedList = await _repository.GetAllAsync();
            var contentDtoList = _mapper.Map<IEnumerable<TispContentGroupViewModel>>(pagedList);
            return contentDtoList.ToList();
        }
        public async Task<IEnumerable<TispContentGroupViewModel>> GetContentGroupsAsync(TispContentGroupsSearchModel searchModel)
        {
            var pagedList = await _repository.GetAllAsync();
            var contentDtoList = _mapper.Map<IEnumerable<TispContentGroupViewModel>>(pagedList.Where(i => i.C_StoreCode == searchModel.C_StoreCode)).OrderBy(t=>t.I_Sort);

            if (!string.IsNullOrEmpty(searchModel.C_Name))
            {
               return contentDtoList.Where(i=>i.C_Name.Contains(searchModel.C_Name)).ToList();
            }
            return contentDtoList.ToList();
        }

        public async Task CreateOneAsync(TispContentGroupViewModel viewModel)
        {
            var content = _mapper.Map<TISP_ContentGroup>(viewModel);
            content.G_ID = Guid.NewGuid();
            content.G_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            content.C_Status = "1";
            _repository.Create(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }

        public async Task DeleteAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此巡检内容分组");
            }
            content.G_LastUpdatedBy = _claims.ApiUserId;
            content.D_LastUpdatedOn = DateTime.Now;
            content.C_Status = "0";
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            //_repository.delete(content);
            //var result = await _repository.saveasync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }

        public Task<TispContentGroupViewModel> GetByIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<bool> IsExistAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<int> UpdateOneAsync(TispContentUpdateGroupViewModel viewModel, params string[] fields)
        {
            throw new NotImplementedException();
        }
        public async Task UpdateAsync(Guid id, TispContentUpdateGroupViewModel updateModel)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此巡检内容分组");
            }
            content.G_LastUpdatedBy = _claims.ApiUserId;
            content.D_LastUpdatedOn = DateTime.Now;
            _mapper.Map(updateModel, content, typeof(TispContentUpdateGroupViewModel), typeof(TISP_ContentGroup));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }

        public Task<int> UpdateOneAsync(TispContentGroupViewModel viewModel, params string[] fields)
        {
            throw new NotImplementedException();
        }
    }
}