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.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

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

        public Task CreateOneAsync(TispSpotsRouteViewModel viewModel)
        {
            throw new NotImplementedException();
        }

        public async Task CreateSpotsRoutesAsync(TispSpotRouteCreateViewModel viewModel)
        {
            foreach (Guid routeId in viewModel.routeIdList)
            {
                await _repository.DeleteByRouteIdAsync(routeId);
                foreach (Guid spotId in viewModel.spotIdList)
                {
                    TISP_SpotRoute spotRoute = new TISP_SpotRoute()
                    {
                        C_SpotCode = spotId,
                        C_RouteCode = routeId,
                        C_CreateBy = _claims.ApiUserId,
                        D_CreateOn = DateTime.Now,
                        C_LastUpdatedBy = _claims.ApiUserId,
                        D_LastUpdatedOn = DateTime.Now,
                        C_Status = '1',
                    };
                    _repository.Create(spotRoute);
                }

            }
            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 Task<IEnumerable<TispSpotsRouteViewModel>> GetByConditionAsync(Expression<Func<TispSpotsRouteViewModel, bool>> expression)
        {
            throw new NotImplementedException();
        }

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

        public async Task<IEnumerable<TispSpotsRouteViewModel>> GetSpotRoutesAsyncByPage(TispSpotRouteSearchModel searchViewModel)
        {
            var pagedList = await _repository.GetSpotRoutesAsyncByPage(searchViewModel);
            searchViewModel.TotalCount = pagedList==null? 0:pagedList.Totals;
            return pagedList?.Rows;
        }
        public async Task<IEnumerable<TispSpotsRouteViewModel>> GetSpotsRouteAsyncByPage(TispSpotRouteSearchModel searchViewModel)
        {
            var pagedList = await _repository.GetSpotsRouteAsyncByPage(searchViewModel);
            searchViewModel.TotalCount = pagedList == null ? 0 : pagedList.Totals;
            return pagedList?.Rows;
        }
        public Task<bool> IsExistAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(Guid id, TispContentUpdateViewModel updateModel)
        {
            throw new NotImplementedException();
        }

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