using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Utilities;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.ViewModel.SYS;
using Ropin.Inspection.Repository;
using Ropin.Inspection.Repository.Interface;
using Ropin.Inspection.Repository.SYS.Interface;
using Ropin.Inspection.Service.SYS.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service.SYS
{
    internal class TsysRoleHandServices : ITsysRoleHandServices
    {
        private readonly IsysRoleHandRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public TsysRoleHandServices(IClaimsAccessor claims, IsysRoleHandRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
        }
        public async Task CreateOneAsync(TSYSRoleHandViewModel viewModel)
        {
            var content = _mapper.Map<TSYS_RoleHand>(viewModel);
            content.C_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            _repository.Create(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }
        public async Task CreateAsync(AddRoleHandModel viewModel)
        {
            var del = await _repository.DeleteByRoleIdAsync(viewModel.C_RoleCode);

            foreach (var item in viewModel.HandList)
            {
                TSYS_RoleHand entity = new TSYS_RoleHand()
                {
                    C_RoleCode = viewModel.C_RoleCode,
                    C_HandCode = item,
                    C_CreateBy = _claims.ApiUserId,
                    D_CreateOn = DateTime.Now,
                };
                await _repository.CreateRoleHandAsync(entity);                
            }
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }
        public async Task DeleteRoleHandAsync(Guid handCode,Guid roleCode)
        {
            var items = await _repository.GetByConditionAsync(x=>x.C_RoleCode==roleCode&&x.C_HandCode==handCode);
            var content = items.FirstOrDefault();
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            var result = await _repository.DeleteRoleHandAsync(roleCode, handCode);
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }
        public async Task<IEnumerable<TSYSRoleHandModel>> GetRoleHandListAsync(Guid roleId)
        {
            var result = await _repository.GetList(roleId);
            return result;
        }
        public Task DeleteAsync(Guid id)
        {
            throw new NotImplementedException();
        }

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

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

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