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

namespace MineTec.ProManager.ProductNumber
{
    public class ProductNumberAppService : AsyncCrudAppService<Entitys.Graphic.ProductNumber.ProductNumber, ProductNumberDto, Guid, PagedResultRequestDto,
                        CreateUpdateProductNumberDto, CreateUpdateProductNumberDto>, IProductNumberAppService
    {
        private readonly IRepository<Entitys.Graphic.ProductNumber.ProductNumber, Guid> _ProductNumberRepository;

        public ProductNumberAppService(IRepository<Entitys.Graphic.ProductNumber.ProductNumber, Guid> repository, IRepository<Entitys.Graphic.ProductNumber.ProductNumber, Guid> ProductNumberRepository)
                    : base(repository)
        {
            _ProductNumberRepository = ProductNumberRepository;
        }

        public OutputBase CreateProductNumber(CreateUpdateProductNumberDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _ProductNumberRepository.GetAll().FirstOrDefault(a => a.ProductNum == input.ProductNum && a.IsDelete == 0);
                if (entity == null)
                {
                    _ProductNumberRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Graphic.ProductNumber.ProductNumber>(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 ProductNumberOutput GetAllProductNumber(GetAllProductNumber input)
        {

            var result = new ProductNumberOutput();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.ProductNum), t => t.ProductNum.Contains(input.ProductNum)).WhereIf(!string.IsNullOrEmpty(input.Remark), t => t.Remark.Contains(input.Remark));
                var productnumcount = query.Count();
                var productnumlist = query.ToList();
                var list = ObjectMapper.Map<List<ProductNumberDto>>(productnumlist);
                result.code = 0;
                result.count = productnumcount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateProductNumber(UpdateProductNumber input)
        {

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