using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Data.Standard.Finance;
using XYY.Model.Standard.Finance;

namespace XYY.Service.Standard.Finance
{
    public interface IEstimateFeeService
    {
        Task<long> Create(Finance_EstimateFee entity);
        Task<bool> Modify(Finance_EstimateFee entity);
        Task<IEnumerable<Finance_EstimateFee>> Query(Expression<Func<Finance_EstimateFee, bool>> expression);
        Task<PageResult<Finance_EstimateFee>> QueryPage(QueryModel queryModel);
        Task<bool> Remove(int id);
    }

    public class EstimateFeeService : IEstimateFeeService
    {
        private readonly IFinance_EstimateFeeRepository _IEstimateFeeRepository;

        public EstimateFeeService(IFinance_EstimateFeeRepository estimateFeeRepository)
        {
            _IEstimateFeeRepository = estimateFeeRepository;
        }

        public async Task<long> Create(Finance_EstimateFee entity)
        {

            var rs = await _IEstimateFeeRepository.InsertAsync(entity);
            //将该渠道其他预估价格设置禁用
            if (rs > 0)
            {
                int channelId = entity.ChannelId;
                int id = (int)rs;
                await _IEstimateFeeRepository.UpdateByConditionAsync(i => i.ChannelId == channelId && i.Id != id, new { Enabled = false });
            }

            return rs;
        }

        public async Task<bool> Modify(Finance_EstimateFee entity)
        {
            var rs = await _IEstimateFeeRepository.UpdateAsync(entity);
            return rs;
        }

        public async Task<IEnumerable<Finance_EstimateFee>> Query(Expression<Func<Finance_EstimateFee, bool>> expression)
        {
            var result = await _IEstimateFeeRepository.QueryAsync(expression);
            return result;
        }

        public async Task<PageResult<Finance_EstimateFee>> QueryPage(QueryModel queryModel)
        {
            queryModel.QuerySort = new List<QuerySort> { new QuerySort { Field = "Id", IsDesc = true } };
            var pageResult = await _IEstimateFeeRepository.QueryPagerAsync(queryModel);
            return pageResult;
        }

        public async Task<bool> Remove(int id)
        {
            return await _IEstimateFeeRepository.DeleteAsync(i => i.Id == id);
        }
    }
}
