using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using MineTec.ProManager.Commd;
using MineTec.ProManager.GalleryInformation.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MineTec.ProManager.GalleryInformation
{
    public class GalleryInformationAppService : AsyncCrudAppService<Entitys.Graphic.GalleryInformation.GalleryInformation, GalleryInformationDto, Guid, PagedResultRequestDto,
                            CreateUpdateGalleryInformationDto, CreateUpdateGalleryInformationDto>, IGalleryInformationAppService

    {
        private readonly IRepository<Entitys.Graphic.GalleryInformation.GalleryInformation, Guid> _GalleryInformationRepository;

        private readonly IRepository<Entitys.Graphic.GalleryInformation.ViewGalleryInformation, Guid> _ViewGalleryInformationRepository;

        public GalleryInformationAppService(IRepository<Entitys.Graphic.GalleryInformation.GalleryInformation, Guid> repository, IRepository<Entitys.Graphic.GalleryInformation.GalleryInformation, Guid> GalleryInformationRepository, IRepository<Entitys.Graphic.GalleryInformation.ViewGalleryInformation, Guid> ViewGalleryInformationRepository)
            : base(repository)
        {
            _GalleryInformationRepository = GalleryInformationRepository;
            _ViewGalleryInformationRepository = ViewGalleryInformationRepository;
        }

        public OutputBase CreateGalleryInformation(CreateUpdateGalleryInformationDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _GalleryInformationRepository.GetAll().FirstOrDefault(a => a.GalleryNum == input.GalleryNum && a.IsDelete == 0);
                if (entity == null)
                {
                    base.CreateAsync(input);
                    result.code = 1;//成功
                    result.msg = "保存成功！";
                }
                else
                {
                    result.code = 2;//重复
                    result.msg = "已经存在相同的图库编码，请修改！";
                }
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }

        public async Task<GalleryInformationOutPut> GetAllGalleryInformation(GetAllGalleryInformation input)
        {

            var result = new GalleryInformationOutPut();
            try
            {
                var query = _ViewGalleryInformationRepository.GetAll().Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.GalleryNum), t => t.GalleryNum.Contains(input.GalleryNum)).WhereIf(!string.IsNullOrEmpty(input.Remark), t => t.Remark.Contains(input.Remark));
                var gallerycount = query.Count();
                var gallerylist = query.ToList();
                var list = ObjectMapper.Map<List<ViewGalleryInformationDto>>(gallerylist);
                result.code = 0;
                result.count = gallerycount;
                result.data = list;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return result;
        }

        public async Task<OutputBase> UpdateGalleryInformation(UpdateGalleryInformation input)
        {

            var result = new OutputBase();
            try
            {
                var query = _GalleryInformationRepository.GetAll().FirstOrDefault(a => a.Id == input.id);
                if (input.IsDelete == 1)
                {
                    query.IsDelete = 1;
                }
                query.UpdateTime = DateTime.Now;
                query.UpdateUserID = input.UpdateUserID;
                query.UpdateUserName = input.UpdateUserName;
                if (!string.IsNullOrEmpty(input.Remark))
                {
                    query.Remark = input.Remark;
                }
                result.code = 1;//1成功0失败
            }
            catch (Exception ex)
            {
                result.code = 0;
                result.msg = ex.Message;
            }

            return result;
        }

        public OutputBase GetGalleryTypeCount(GetAllGalleryInformation input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _GalleryInformationRepository.GetAll().Where(a => a.GalleryNum.Substring(0, 1) == input.Type && a.IsDelete == 0);
                result.count = entity.Count();
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }
    }
}
