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

namespace MineTec.ProManager.MaterialNumber
{
    public class MaterialNumberAppService : AsyncCrudAppService<Entitys.Graphic.MaterialNumber.MaterialNumber, MaterialNumberDto, Guid, PagedResultRequestDto,
                        CreateUpdateMaterialNumberDto, CreateUpdateMaterialNumberDto>, IMaterialNumberAppService
    {
        private readonly IRepository<Entitys.Graphic.MaterialNumber.MaterialNumber, Guid> _MaterialNumberRepository;

        public MaterialNumberAppService(IRepository<Entitys.Graphic.MaterialNumber.MaterialNumber, Guid> repository, IRepository<Entitys.Graphic.MaterialNumber.MaterialNumber, Guid> MaterialNumberRepository)
            : base(repository)
        {
            _MaterialNumberRepository = MaterialNumberRepository;
        }

        public OutputBase CreateMaterialNumber(CreateUpdateMaterialNumberDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _MaterialNumberRepository.GetAll().FirstOrDefault(a => a.MaterialNum == input.MaterialNum && a.IsDelete == 0);
                if (entity == null)
                {
                    _MaterialNumberRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Graphic.MaterialNumber.MaterialNumber>(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 MaterialNumberOutput GetAllMaterialNumber(GetAllMaterialNumber input)
        {

            var result = new MaterialNumberOutput();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.MaterialNum), t => t.MaterialNum.Contains(input.MaterialNum)).WhereIf(!string.IsNullOrEmpty(input.Remark), t => t.Remark.Contains(input.Remark));
                var materialnumcount = query.Count();
                var materialnumlist = query.ToList();
                var list = ObjectMapper.Map<List<MaterialNumberDto>>(materialnumlist);
                result.code = 0;
                result.count = materialnumcount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateMaterialNumber(UpdateMaterialNumber input)
        {

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