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

namespace MineTec.ProManager.BOM
{
    public class BOMAppService : AsyncCrudAppService<Entitys.Order.BOM.BOM, BOMDto, Guid, PagedResultRequestDto,
                        CreateUpdateBOMDto, CreateUpdateBOMDto>, IBOMAppService

    {
        private readonly IRepository<Entitys.Order.BOM.BOM, Guid> _BOMRepository;

        public BOMAppService(IRepository<Entitys.Order.BOM.BOM, Guid> repository, IRepository<Entitys.Order.BOM.BOM, Guid> BOMRepository)
            : base(repository)
        {
            _BOMRepository = BOMRepository;
        }

        public OutputBase CreateBOM(CreateUpdateBOMDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _BOMRepository.GetAll().FirstOrDefault(a => a.DesignAssyNum == input.DesignAssyNum && a.IsDelete == 0);
                if (entity == null)
                {
                    _BOMRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Order.BOM.BOM>(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 OutputBase CheckBOM(CreateUpdateBOMDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _BOMRepository.GetAll().FirstOrDefault(a => a.DesignAssyNum == input.DesignAssyNum && a.IsDelete == 0);
                if (entity == null)
                {
                    result.code = 1;//成功
                    result.msg = "无重复数据！";
                }
                else
                {
                    result.code = 2;//重复
                    result.msg = "已经存在相同的设计总成图号，请修改！";
                }
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }

        public BOMOutPut GetAllBOM(GetAllBOM input)
        {

            var result = new BOMOutPut();
            try
            {
                var query = _BOMRepository.GetAll().Where(t => t.IsDelete != 1).WhereIf(!string.IsNullOrEmpty(input.DesignAssyNum), t => t.DesignAssyNum.Contains(input.DesignAssyNum)).WhereIf(!string.IsNullOrEmpty(input.Name), t => t.Name.Contains(input.Name));
                var bomcount = query.Count();
                var bomlist = query.ToList();
                var list = ObjectMapper.Map<List<BOMDto>>(bomlist);
                result.code = 0;
                result.count = bomcount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateBOM(UpdateBOM input)
        {

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

    }
}
