using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Core.Standard.Data.Infrastructure;
using XYY.Model.Standard.Order;
using XYY.Model.Standard.Order.FBA;

namespace XYY.Data.Standard.Order
{
    public interface IFBAOtherRepository : IBaseRepository<FBA_OtherFee>
    {
        Task SaveOrAdd(FBA_OtherFee otherFee);

        Task<List<FBA_OtherFee>> GetAll();
    }

    public class FBAOtherRepository : BaseRepository<FBA_OtherFee>, IFBAOtherRepository
    {
        public FBAOtherRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }

        public async Task<List<FBA_OtherFee>> GetAll()
        {
            return (await _unitOfWork.QueryAsync<FBA_OtherFee>()).ToList();
        }

        public async Task SaveOrAdd(FBA_OtherFee otherFee)
        {
            if (await _unitOfWork.IsExistsAsync<FBA_OtherFee>(x => x.Name == otherFee.Name && x.Id != otherFee.Id))
                throw new Exception($"系统内已存在名为{otherFee.Name}的附加费品类，请不要重复添加");
            if (otherFee.Id == 0)
                await _unitOfWork.InsertAsync(otherFee);
            else
                await _unitOfWork.UpdateAsync(otherFee);
        }


    }
}
