using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using MineTec.ProManager.Commd;
using MineTec.ProManager.GalleryFiles.Dto;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MineTec.ProManager.GalleryFiles
{
    public class GalleryFilesAppService : AsyncCrudAppService<Entitys.Graphic.GalleryFiles.GalleryFiles, GalleryFilesDto, Guid, PagedResultRequestDto,
                            CreateUpdateGalleryFilesDto, CreateUpdateGalleryFilesDto>, IGalleryFilesAppService

    {
        private readonly IRepository<Entitys.Graphic.GalleryFiles.GalleryFiles, Guid> _GalleryFilesRepository;

        public GalleryFilesAppService(IRepository<Entitys.Graphic.GalleryFiles.GalleryFiles, Guid> repository, IRepository<Entitys.Graphic.GalleryFiles.GalleryFiles, Guid> GalleryFilesRepository)
            : base(repository)
        {
            _GalleryFilesRepository = GalleryFilesRepository;
        }

        public OutputBase CreateGalleryFiles(CreateUpdateGalleryFilesDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _GalleryFilesRepository.GetAll().Where(a => a.FileNum == input.FileNum && a.IsDelete == 0)
                                                                .WhereIf(!string.IsNullOrEmpty(input.GalleryInformationID), t => t.GalleryInformationID == input.GalleryInformationID).FirstOrDefault();
                if (entity == null)
                {
                    _GalleryFilesRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Graphic.GalleryFiles.GalleryFiles>(input));
                    //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 GalleryFilesOutPut GetAllGalleryFiles(GetAllGalleryFiles input)
        {

            var result = new GalleryFilesOutPut();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1)
                                                            .WhereIf(!string.IsNullOrEmpty(input.GalleryInformationID), t => t.GalleryInformationID == input.GalleryInformationID)
                                                                .WhereIf(!string.IsNullOrEmpty(input.FileNum), t => t.FileNum.Contains(input.FileNum))
                                                                    .WhereIf(!string.IsNullOrEmpty(input.FileName), t => t.FileName.Contains(input.FileName))
                                                                        .WhereIf(!string.IsNullOrEmpty(input.Subcode), t => t.Subcode.Contains(input.Subcode));
                var filescount = query.Count();
                var fileslist = query.ToList();
                var list = ObjectMapper.Map<List<GalleryFilesDto>>(fileslist);
                result.code = 0;
                result.count = filescount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateGalleryFiles(UpdateGalleryFiles input)
        {

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

            return result;
        }

        public OutputBase GetGalleryFilesTypeCount(GetAllGalleryFiles input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _GalleryFilesRepository.GetAll().Where(a => a.FileNum.Contains(input.Type) && a.IsDelete == 0);
                result.count = entity.Count();
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }
    }
}
