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

namespace MineTec.ProManager.FigureNumber
{
    public class FigureNumberAppService : AsyncCrudAppService<Entitys.Graphic.FigureNumber.FigureNumber, FigureNumberDto, Guid, PagedResultRequestDto,
                            CreateUpdateFigureNumberDto, CreateUpdateFigureNumberDto>, IFigureNumberAppService

    {
        private readonly IRepository<Entitys.Graphic.FigureNumber.FigureNumber, Guid> _FigureNumberRepository;

        public FigureNumberAppService(IRepository<Entitys.Graphic.FigureNumber.FigureNumber, Guid> repository, IRepository<Entitys.Graphic.FigureNumber.FigureNumber, Guid> FigureNumberRepository)
            : base(repository)
        {
            _FigureNumberRepository = FigureNumberRepository;
        }

        public OutputBase CreateFigureNumber(CreateUpdateFigureNumberDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _FigureNumberRepository.GetAll().FirstOrDefault(a => a.FigureNum == input.FigureNum && a.IsDelete == 0);
                if (entity == null)
                {
                    _FigureNumberRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Graphic.FigureNumber.FigureNumber>(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 FigureNumberOutPut GetAllFigureNumber(GetAllFigureNumber input)
        {

            var result = new FigureNumberOutPut();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.FigureNum), t => t.FigureNum.Contains(input.FigureNum)).WhereIf(!string.IsNullOrEmpty(input.Remark), t => t.Remark.Contains(input.Remark));
                var figurenumcount = query.Count();
                var figurenumlist = query.ToList();
                var list = ObjectMapper.Map<List<FigureNumberDto>>(figurenumlist);
                result.code = 0;
                result.count = figurenumcount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateFigureNumber(UpdateFigureNumber input)
        {

            var result = new OutputBase();
            try
            {
                var query = _FigureNumberRepository.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;
        }
    }
}