using AutoMapper;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model;
using Ropin.Inspection.Repository;
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;
using Ropin.Inspection.Model.ViewModel.SYS;
using Ropin.Inspection.Model.ViewModel;

namespace Ropin.Inspection.Service.SYS
{
   public class TsysPrivService: ITsysPrivService
    {
        private readonly ITsysPrivRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        private readonly ITsysLicenseTypePrivRepository _sysLicenseTypePrivRepository;
        public TsysPrivService(IClaimsAccessor claims, ITsysPrivRepository repository, IMapper mapper, ITsysLicenseTypePrivRepository sysLicenseTypePrivRepository)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
            _sysLicenseTypePrivRepository = sysLicenseTypePrivRepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task CreateOneAsync(TsysPrivViewModel viewModel)
        {
            var pagedList = await _repository.GetAllAsync();
            TSYS_Priv content = _mapper.Map<TSYS_Priv>(viewModel);
            if (string.IsNullOrEmpty(viewModel.C_Code))
            {
                //content.C_Code = Guid.NewGuid().ToString();
                throw new Exception("请输入编号!");
            }
            int num = pagedList.Where(t => t.C_Code == viewModel.C_Code).Count();
            if (num >= 1)
            {
                throw new Exception("编号已存在!");
            }
            content.C_Status = "1";
            _repository.Create(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }

        /// <summary>
        /// 真删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task DeleteAsync(string id)
        {
            var content = _repository.GetPrivEntity(id);
            if (content == null)
            {
                throw new Exception("数据库中没有此数据");
            }
            var delList = await _repository.GetByConditionAsync(t=>t.C_ParentCode==id);
            if (delList != null&&delList.Any())
            {
                throw new Exception("该数据下存在子级,删除失败!");
            }
            var icenseTypePriv = await _sysLicenseTypePrivRepository.GetByConditionAsync(t => t.C_PrivilegeCode == content.C_Code);
            if (icenseTypePriv?.Count()>0)
            {
                throw new Exception("该关联其它表,删除失败!");
            }
            _repository.Delete(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }
        /// <summary>
        /// 假删除-修改状态为禁用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task UpdateStatusAsync(string id)
        {
            var content = _repository.GetPrivEntity(id);
            if (content == null)
            {
                throw new Exception("数据库中没有此数据");
            }
            content.C_Status = "0";
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task UpdateOneAsync(string id, TsysPrivViewModel updateModel)
        {
            var content =  _repository.GetPrivEntity(id);
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            _mapper.Map(updateModel, content, typeof(TsysPrivViewModel), typeof(TSYS_Priv));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }
        /// <summary>
        /// 树型列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<List<TsysPrivTreeModel>> GetPrivTree(TsysPrivSearch model)
        {
            var list = await _repository.GetPrivTree(model);
            if (!string.IsNullOrWhiteSpace(model.C_Name))
            {
                if (list != null&&list.Count>0)
                {
                    list = list.Where(t => t.C_Name.Contains(model.C_Name)).ToList();
                }
            }
            return list;
        }

        public async Task<TsysPrivViewModel> GetByIdAsync(string id)
        {
            var content = _repository.GetPrivEntity(id);
            var contentDto = _mapper.Map<TsysPrivViewModel>(content);
            return contentDto;
        }
        public Task<bool> IsExistAsync(string id)
        {
            throw new NotImplementedException();
        }
    }
}