using AutoMapper;
using LinqKit;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
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.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service
{
    public class TispSpotContentService : ITispSpotContentService
    {
        private readonly ITispSpotContentRepository _repository;
        private readonly ITispContentRepository _tispContentRepository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public TispSpotContentService(IClaimsAccessor claims, ITispSpotContentRepository repository, ITispContentRepository tispContentRepository, IMapper mapper)
        {
            _repository = repository;
            _tispContentRepository = tispContentRepository;
            _mapper = mapper;
            _claims = claims;
        }

        public async Task CreateOneAsync(TispSpotContentViewModel viewModel)
        {
            var content = _mapper.Map<TISP_SpotContent>(viewModel);
            content.C_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            //content.C_LastUpdatedBy = _claims.ApiUserId;
            //content.D_LastUpdatedOn = 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("没有此巡检点内容");
            }
            _repository.Delete(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }

        public async Task<IEnumerable<TispSpotContentViewModel>> GetAllAsync()
        {
            var pagedList = await _repository.GetAllAsync();
            var contentDtoList = _mapper.Map<IEnumerable<TispSpotContentViewModel>>(pagedList);
            return contentDtoList.ToList();
        }

        public Task<IEnumerable<TispSpotContentViewModel>> GetByConditionAsync(Expression<Func<TispSpotContentViewModel, bool>> expression)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<TispContentViewModel> GetBySpotIdAsync(Guid id)
        {
            IEnumerable<TISP_Content> pagedList = _repository.GetBySpotCodeAsync(id);
            var contentDtoList = _mapper.Map<IEnumerable<TispContentViewModel>>(pagedList);
            return contentDtoList.ToList();
        }

        public async Task<TispContentViewModel> GetByIdAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            var contentDto = _mapper.Map<TispContentViewModel>(content);
            return contentDto;
        }

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

        public async Task UpdateAsync(Guid id, TispSpotContentUpdateViewModel updateModel)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此巡检点内容");
            }
            //content.C_LastUpdatedBy = _claims.ApiUserId;
            //content.D_LastUpdatedOn = DateTime.Now;
            _mapper.Map(updateModel, content, typeof(TispSpotContentUpdateViewModel), typeof(TISP_SpotContent));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }

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

        Task<TispSpotContentViewModel> IBaseServiceById<TispSpotContentViewModel, Guid>.GetByIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }
    }
}