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

namespace Ropin.Inspection.Service.DEV
{
    public class dev_HandService: IDev_HandService
    {
        private readonly Idev_HandRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        private readonly InspectionDbContext _sqlDBContext;
        public dev_HandService(IClaimsAccessor claims, InspectionDbContext sqlDBContext, Idev_HandRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
            _sqlDBContext = sqlDBContext;
        }
        public async Task CreateOneAsync(dev_HandModel viewModel)
        {
            var id = Guid.NewGuid();
            var content = _mapper.Map<TDEV_Hand>(viewModel);
            content.C_ID = id;
            var path = QRCoderHelper.RenderQrCode(id.ToString(), "M", _claims.Linsence);
            //if (string.IsNullOrEmpty(content.C_MachineCode))
            //{
            //    content.C_MachineCode = "SC-" + id + "-" +DateTime.Now.ToString("yyyyMMdd");
            //}
            content.C_CreateBy = _claims.ApiUserId;
            content.D_CreateOn = DateTime.Now;
            content.C_QRCode = path;
            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_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 UpdateAsync(Guid code, dev_HandModel updateModel)
        {
            var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
            TDEV_Hand content = items.FirstOrDefault();
            if (content == null)
            {
                throw new Exception("没有此数据");
            }
            content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId : Guid.Parse("6e864cbc-5252-11ec-8681-fa163e02b3e4");
            content.D_LastUpdatedOn = DateTime.Now;
            _mapper.Map(updateModel, content, typeof(dev_HandModel), typeof(TDEV_Hand));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }

        public async Task<IEnumerable<devHandViewModel>> GetConditionAsync(TdevHandSearchModel searchModel)
        {
            var dtoList = await _repository.GetConditionAsync(searchModel);
            return dtoList;
        }
        public async Task<devHandViewModel> GetConditionByIdAsync(Guid code)
        {
            TdevHandSearchModel searchModel = new TdevHandSearchModel();
            searchModel.C_ID = code;
            searchModel.IsPagination = false;
            var items = await _repository.GetConditionAsync(searchModel);
            var content = items.FirstOrDefault();
            return content;
        }
        public Task<dev_HandModel> GetByIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }

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

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