using AutoMapper;
using LinqKit;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Repository;
using Ropin.Inspection.Repository.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 TdevDevOpeAccountService : ITdevDevOpeAccountService
    {
        private readonly ITdevDevOpeAccountRepository _repository;
        private readonly IUnitOfWork _unitOfWork;
        
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public TdevDevOpeAccountService(IClaimsAccessor claims, ITdevDevOpeAccountRepository repository, IUnitOfWork unitOfWork, IMapper mapper)
        {
            _repository = repository;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
            _claims = claims;
        }
        public async Task CreateOneAsync(TdevDevOpeAccountViewModel viewModel)
        {
            var content = _mapper.Map<TDEV_DevOpeAccount>(viewModel);
            content.C_ID = Guid.NewGuid().ToString();
            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("数据库中没有此数据");
            }
            //_repository.Delete(content);
            //var result = await _repository.SaveAsync();
            //content.C_LastUpdatedBy = _claims.ApiUserId;
            //content.D_LastUpdatedOn = DateTime.Now;
            //content.C_Status = "0";
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }
        public async Task DeleteAsync(string code)
        {
            var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
            var content = items.FirstOrDefault();
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            //content.C_LastUpdatedBy = _claims.ApiUserId;
            //content.D_LastUpdatedOn = DateTime.Now;
            //content.C_Status = "0";
            _repository.Delete(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }
        public async Task<IEnumerable<TdevDevOpeAccountViewModel>> GetAllAsync()
        {
            var pagedList = await _repository.GetAllAsync();
            var contentDtoList = _mapper.Map<IEnumerable<TdevDevOpeAccountViewModel>>(pagedList.ToList());
            return contentDtoList.ToList();
        }
        public async Task<IEnumerable<TdevDevOpeAccountViewModel>> GetConditionAsync(TdevDevOpeAccountSearchModel searchModel)
        {
            var predicate = PredicateBuilder.New<TDEV_DevOpeAccount>(true);//查询条件,推荐后台使用这种方式灵活筛选
            #region 添加条件查询
            //predicate = predicate.And(i => i.C_Status.Equals("1"));

            if (!string.IsNullOrEmpty(searchModel.C_DevStoreCode))
            {
                predicate = predicate.And(i => i.C_DevStoreCode.Equals(searchModel.C_DevStoreCode));
            }
            if (!string.IsNullOrEmpty(searchModel.C_ID))
            {
                predicate = predicate.And(i => i.C_ID.Equals(searchModel.C_ID));
            }
            if (!string.IsNullOrEmpty(searchModel.C_DevOpeAccountConfigCode))
            {
                predicate = predicate.And(i => i.C_DevOpeAccountConfigCode==searchModel.C_DevOpeAccountConfigCode);
            }
            if (searchModel.D_Start != null && searchModel.D_End != null)
            {
                predicate = predicate.And(i => i.D_CreateOn>=(searchModel.D_Start));
                predicate = predicate.And(i => i.D_CreateOn <= (searchModel.D_End.Value.AddDays(1)));
            }

            #endregion
            var list = await _repository.GetPageAsync(predicate, "-D_CreateOn", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);
            searchModel.TotalCount = list.Totals;
            var dtoList = _mapper.Map<List<TDEV_DevOpeAccount>, List<TdevDevOpeAccountViewModel>>(list.Rows);
            return dtoList;
        }

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


        public async Task UpdateAsync(string code, TdevDevOpeAccountUpdateModel updateModel)
        {
            var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
            var content = items.FirstOrDefault();
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            //content.C_LastUpdatedBy = _claims.ApiUserId;
            //content.D_LastUpdatedOn = DateTime.Now;
            _mapper.Map(updateModel, content, typeof(TdevDevOpeAccountUpdateModel), typeof(TDEV_DevOpeAccount));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }
        public async Task<List<DevOpeAccountConfigSelect>> GetDevOpeAccountConfigSelectAsync(TdevDevOpeAccountSearchModel searchModel)
        {
            var result = await _repository.GetDevOpeAccountConfigSelectAsync(searchModel);
            return result;
        }

        public async Task UpdateDevOpeAccountAsync(TdevDevOpeAccountUpdateModel updateModel)
        {
            var result = false;
            //_unitOfWork.BeginTransaction();
            try
            {

               

            }
            catch (Exception ex)
            {
                throw new Exception("创建失败");
            }
            finally
            {
                //if (result)
                //    await _unitOfWork.CommitAsync();
                //else
                //    _unitOfWork.Rollback();
            }

        }

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

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

        public Task<IEnumerable<TdevDevOpeAccountViewModel>> GetByConditionAsync(Expression<Func<TdevDevOpeAccountViewModel, bool>> expression)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(string code, TpntAreaUpdateModel updateModel)
        {
            throw new NotImplementedException();
        }

        public Task<IEnumerable<TdevDevOpeAccountViewModel>> GetConditionAsync(TpntAreaSearchModel searchModel)
        {
            throw new NotImplementedException();
        }
    }
}