using AutoMapper;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel.LGS;
using Ropin.Inspection.Model.ViewModel.LGS;
using Ropin.Inspection.Repository.LGS.Interface;
using Ropin.Inspection.Service.LGS.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service.LGS
{
    public class LGSLargeScreenService : ILGSLargeScreenService
    {
        private readonly ILGSLargeScreenRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        private readonly InspectionDbContext _sqlDBContext;
        public LGSLargeScreenService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, ILGSLargeScreenRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
            _sqlDBContext = sqlDBContext;
        }
        public async Task<IEnumerable<LargeScreenViewModel>> GetConditionAsync(LargeScreenSearch searchModel)
        {
            var list = await _repository.GetConditionAsync(searchModel);
            return list;
        }

        public async Task CreateOneAsync(LargeScreenViewModel viewModel)
        {
            var content = _mapper.Map<TLGS_LargeScreen>(viewModel);
            content.C_ID = Guid.NewGuid().ToString();
            content.C_CreateBy = _claims.ApiUserId.ToString();
            content.C_Creator = _claims.ApiUserName;
            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(string id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("数据库中没有此数据");
            }
            content.C_LastUpdatedBy = _claims.ApiUserId.ToString();
            content.D_LastUpdatedOn = DateTime.Now;
            content.C_Modifier = _claims.ApiUserName;
            content.C_Status = "0";
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }

        public async Task<LargeScreenViewModel> GetByIdAsync(string id)
        {
            LargeScreenSearch searchModel = new LargeScreenSearch();
            searchModel.IsPagination = false;
            searchModel.C_ID = id;
            var list = await _repository.GetConditionAsync(searchModel);
            return list?.FirstOrDefault();
        }

        public async Task UpdateAsync(LargeScreenViewModel viewModel, string Id)
        {
            var content = await _repository.GetByIdAsync(Id);
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            _mapper.Map(viewModel, content, typeof(LargeScreenViewModel), typeof(TLGS_LargeScreen));
            content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId.ToString() : "6e864cbc-5252-11ec-8681-fa163e02b3e4";
            content.D_LastUpdatedOn = DateTime.Now;
            content.C_Modifier = _claims.ApiUserName;
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }

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

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