using AutoMapper;
using Ropin.Inspection.Common;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Repository.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
{
    public class TsysRoleService : ITsysRoleService
    {
        private readonly ITsysRoleRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public TsysRoleService(IClaimsAccessor claims, ITsysRoleRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
        }

        public async Task CreateOneAsync(TsysRoleViewModel viewModel)
        {
            var content = _mapper.Map<TSYS_Role>(viewModel);
            //content.C_LicenseCode = _claims.Linsence;
            content.C_Code = Guid.NewGuid();
            content.C_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            content.C_Status = "1";
            _repository.Create(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }
        public async Task CreateAsync(TsysRoleCreateViewModel viewModel)
        {
            var content = _mapper.Map<TSYS_Role>(viewModel);
            //content.C_LicenseCode = _claims.Linsence;
            content.C_Code = Guid.NewGuid();
            content.C_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = 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("没有此数据");
            }
            content.C_Status = "0";
            content.C_LastUpdatedBy = _claims.ApiUserId;
            content.D_LastUpdatedOn = DateTime.Now;
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }

        public async Task<IEnumerable<TsysRoleViewModel>> GetAllAsync()
        {
            IEnumerable<TSYS_Role> pagedList;
            //if (_claims.OrgTypeCode == "PARTY_TYPE_005")
            if (_claims.LicenseTypeCode == TsysLicenseType.SYSTEM)
            {
                pagedList = await _repository.GetAllAsync();
            }
            else
            {
                pagedList = await _repository.GetByConditionAsync(a => a.C_LicenseCode.Equals(_claims.Linsence));
            }
            
            
            var dtoList = _mapper.Map<IEnumerable<TsysRoleViewModel>>(pagedList);
            return dtoList;
        }
        public async Task<IEnumerable<TsysRoleOrgLicenseViewModel>> GetBylicenseCodeAsync(string licenseCode, string status)
        {
            IEnumerable<TsysRoleOrgLicenseViewModel> pagedList;
            //if (_claims.OrgTypeCode == "PARTY_TYPE_005")
            //if (_claims.LicenseTypeCode == TsysLicenseType.SYSTEM)
            //{
            //    pagedList = await _repository.GetBylicenseCodeAsync(null);
            //}
            //else
            //{
                pagedList = await _repository.GetBylicenseCodeAsync(licenseCode, status);
            //}
            return pagedList;
        }

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

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

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

        public async Task UpdateAsync(Guid id, TsysRoleUpdateViewModel updateModel)
        {
            var outlet = await _repository.GetByIdAsync(id);
            if (outlet == null)
            {
                throw new Exception("没有数据");
            }
            outlet.C_LastUpdatedBy = _claims.ApiUserId;
            outlet.D_LastUpdatedOn = DateTime.Now;
            _mapper.Map(updateModel, outlet, typeof(TsysRoleUpdateViewModel), typeof(TSYS_Role));
            _repository.Update(outlet);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }
    }
}