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

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

        public async Task CreateOneAsync(TsysUserPostViewModel viewModel)
        {
            await _repository.DeleteByUserAsync(viewModel.G_UserCode);
            var content = _mapper.Map<TSYS_UserPost>(viewModel);
            content.G_ID = Guid.NewGuid();
            content.G_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            _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<TsysUserPostViewModel> GetByIdAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            var contentDto = _mapper.Map<TsysUserPostViewModel>(content);
            return contentDto;
        }

        public async Task<TsysUserPostViewModel> GetUserPostsByUserIdAsync(Guid id)
        {
            var content = await _repository.GetUserPostsByUserIdAsync(id);
            var contentDto = _mapper.Map<TsysUserPostViewModel>(content);
            return contentDto;
        }
        public Task<bool> IsExistAsync(Guid id)
        {
            throw new NotImplementedException();
        }

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