using AutoMapper;
using LinqKit;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel;
using Ropin.Inspection.Model.SearchModel.LGS;
using Ropin.Inspection.Model.ViewModel.DEV;
using Ropin.Inspection.Model.ViewModel.LGS;
using Ropin.Inspection.Model.ViewModel.SYS;
using Ropin.Inspection.Repository.DEV.Interface;
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 LgsLargeScreenTemplateService : ILgsLargeScreenTemplateService
    {
        private readonly IlgsLargeScreenTemplateRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        private readonly InspectionDbContext _sqlDBContext;
        public LgsLargeScreenTemplateService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, IlgsLargeScreenTemplateRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
            _sqlDBContext = sqlDBContext;
        }
        public async Task<IEnumerable<LGS_LargeScreenTemplateViewModel>> GetConditionAsync(LargeScreenTemplateSearch searchModel)
        {
            var list = await _repository.GetConditionAsync(searchModel);
            return list;
        }
        public async Task CreateOneAsync(LGS_LargeScreenTemplateViewModel viewModel)
        {
            var content = _mapper.Map<TLGS_LargeScreenTemplate>(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<LGS_LargeScreenTemplateViewModel> GetByIdAsync(string id)
        {
            LargeScreenTemplateSearch searchModel = new LargeScreenTemplateSearch();
            searchModel.IsPagination = false;
            searchModel.C_ID = id;
            var list = await _repository.GetConditionAsync(searchModel);
            return list?.FirstOrDefault();
        }
        public async Task UpdateAsync(LGS_LargeScreenTemplateViewModel viewModel,string Id)
        {
            var content = await _repository.GetByIdAsync(Id);
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            _mapper.Map(viewModel, content, typeof(LGS_LargeScreenTemplateViewModel), typeof(TLGS_LargeScreenTemplate));
            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<int> UpdateOneAsync(LGS_LargeScreenTemplateViewModel viewModel, params string[] fields)
        {
            throw new NotImplementedException();
        }

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

    }
}