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

namespace MineTec.ProManager.BOMDetails
{

    public class BOMDetailsAppService : AsyncCrudAppService<Entitys.Order.BOM.BOMDetails, BOMDetailsDto, Guid, PagedResultRequestDto,
                        CreateUpdateBOMDetailsDto, CreateUpdateBOMDetailsDto>, IBOMDetailsAppService

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

        public BOMDetailsAppService(IRepository<Entitys.Order.BOM.BOMDetails, Guid> repository, IRepository<Entitys.Order.BOM.BOMDetails, Guid> BOMDetailsRepository)
            : base(repository)
        {
            _BOMDetailsRepository = BOMDetailsRepository;
        }

        public OutputBase CreateBOMDetails(CreateUpdateBOMDetailsDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _BOMDetailsRepository.GetAll().Where(a => a.CodeOrFigureNum == input.CodeOrFigureNum && a.IsDelete == 0)
                                                                .WhereIf(!string.IsNullOrEmpty(input.BOMID), t => t.BOMID == input.BOMID).FirstOrDefault();
                if (entity == null)
                {
                    _BOMDetailsRepository.InsertAndGetId(ObjectMapper.Map<Entitys.Order.BOM.BOMDetails>(input));
                    //base.CreateAsync(input);
                    result.code = 1;//成功
                    result.msg = "保存成功！";
                }
                else
                {
                    result.code = 2;//重复
                    result.msg = "已经存在相同的BOM代号(图号)，请修改！";
                }
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }

        public OutputBase CheckBOMDetails(CreateUpdateBOMDetailsDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _BOMDetailsRepository.GetAll().Where(a => a.CodeOrFigureNum == input.CodeOrFigureNum && a.IsDelete == 0)
                                                                .WhereIf(!string.IsNullOrEmpty(input.BOMID), t => t.BOMID == input.BOMID).FirstOrDefault();
                if (entity == null)
                {
                    result.code = 1;//成功
                    result.msg = "无重复数据！";
                }
                else
                {
                    result.code = 2;//重复
                    result.msg = "已经存在相同的BOM代号(图号)，请修改！";
                }
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }

        public BOMDetailsOutPut GetAllBOMDetails(GetAllBOMDetails input)
        {

            var result = new BOMDetailsOutPut();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1)
                                                            .WhereIf(!string.IsNullOrEmpty(input.BOMID), t => t.BOMID == input.BOMID)
                                                                .WhereIf(!string.IsNullOrEmpty(input.CodeOrFigureNum), t => t.CodeOrFigureNum.Contains(input.CodeOrFigureNum))
                                                                    .WhereIf(!string.IsNullOrEmpty(input.NameAndSpecs), t => t.NameAndSpecs.Contains(input.NameAndSpecs))
                                                                        .WhereIf(!string.IsNullOrEmpty(input.Materials), t => t.Materials.Contains(input.Materials));
                var bometailscount = query.Count();
                var bometailslist = query.ToList();
                var list = ObjectMapper.Map<List<BOMDetailsDto>>(bometailslist);
                result.code = 0;
                result.count = bometailscount;
                result.data = list;

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

            return result;
        }

        public OutputBase UpdateBOMDetails(UpdateBOMDetails input)
        {

            var result = new OutputBase();
            try
            {
                var query = _BOMDetailsRepository.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.RelationshipID))
                {
                    query.RelationshipID = input.RelationshipID;
                }
                if (!string.IsNullOrEmpty(input.RelationshipName))
                {
                    query.RelationshipName = input.RelationshipName;
                }
                if (!string.IsNullOrEmpty(input.NameAndSpecs))
                {
                    query.NameAndSpecs = input.NameAndSpecs;
                }
                if (!string.IsNullOrEmpty(input.Materials))
                {
                    query.Materials = input.Materials;
                }
                if (!string.IsNullOrEmpty(input.SuppliersID))
                {
                    query.SuppliersID = input.SuppliersID;
                }
                if (!string.IsNullOrEmpty(input.SuppliersName))
                {
                    query.SuppliersName = input.SuppliersName;
                }

                query.AmountOfUnit = input.AmountOfUnit;

                if (!string.IsNullOrEmpty(input.Unit))
                {
                    query.Unit = input.Unit;
                }

                query.Price = input.Price;

                query.Amount = input.Amount;

                if (!string.IsNullOrEmpty(input.Borrowings))
                {
                    query.Borrowings = input.Borrowings;
                }
                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;
        }
    }
}
