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

namespace MineTec.ProManager.Supplier
{
    public class SupplierAppService : AsyncCrudAppService<Entitys.Supplier.Supplier, SupplierDto, Guid, PagedResultRequestDto,
                           CreateUpdateSupplierDto, CreateUpdateSupplierDto>, ISupplierAppService

    {
        private readonly IRepository<Entitys.Supplier.Supplier, Guid> _SupplierRepository;

        public SupplierAppService(IRepository<Entitys.Supplier.Supplier, Guid> repository, IRepository<Entitys.Supplier.Supplier, Guid> SupplierRepository)
            : base(repository)
        {
            _SupplierRepository = SupplierRepository;
        }

        public OutputBase CreateSupplier(CreateUpdateSupplierDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _SupplierRepository.GetAll().FirstOrDefault(a => a.SupplierCode == input.SupplierCode && a.IsDelete == 0);
                if (entity == null)
                {
                    _SupplierRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Supplier.Supplier>(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 SupplierOutPut GetAllSupplier(GetAllSupplier input)
        {

            var result = new SupplierOutPut();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.SupplierCode), t => t.SupplierCode.Contains(input.SupplierCode))
                                                                                       .WhereIf(!string.IsNullOrEmpty(input.SupplierName), t => t.SupplierName.Contains(input.SupplierName))
                                                                                       .WhereIf(!string.IsNullOrEmpty(input.Mainbusiness), t => t.Mainbusiness.Contains(input.Mainbusiness))
                                                                                       .WhereIf(!string.IsNullOrEmpty(input.Supplygoods_services), t => t.Supplygoods_services.Contains(input.Supplygoods_services));
                var suppliernumcount = query.Count();
                var suppliernumlist = query.ToList();
                var list = ObjectMapper.Map<List<SupplierDto>>(suppliernumlist);
                result.code = 0;
                result.count = suppliernumcount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateSupplier(UpdateSupplier input)
        {

            var result = new OutputBase();
            try
            {
                var entity = _SupplierRepository.GetAll().FirstOrDefault(a => a.SupplierCode == input.SupplierCode && a.IsDelete == 0);
                if (entity == null)
                {
                    var query = _SupplierRepository.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;

                    query.SupplierCode = input.SupplierCode;
                    query.SupplierName = input.SupplierName;
                    query.Mainbusiness = input.Mainbusiness;
                    query.Contacts = input.Contacts;
                    query.Supplygoods_services = input.Supplygoods_services;
                    query.Price_old = input.Price_old;
                    query.Price = input.Price;
                    query.Mobilephone = input.Mobilephone;
                    query.Fixedtelephone = input.Fixedtelephone;
                    query.Email = input.Email;
                    query.Address = input.Address;
                    query.Website = input.Website;
                    query.Remark = input.Remark;

                    result.code = 1;//1成功0失败
                    result.msg = "保存成功！";
                }
                else
                {
                    result.code = 0;//重复
                    result.msg = "已经存在相同的图号编码，请修改！";
                }
            }
            catch (Exception ex)
            {
                result.code = 0;
                result.msg = ex.Message;
            }

            return result;
        }
    }
}
