using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Dtos.Resource;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Resource
{
    public class CountryFeeRepository : BaseRepository<Res_CountryFeeCost, CountryFeeCostView>
    {
        private readonly IMapper _mapper;
        public CountryFeeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }

        public async Task<Res_CountryFeeCost> _InfoByCountryName(string CountryName)
        {
            Res_CountryFeeCost _CountryFeeCost = new Res_CountryFeeCost();

            if (string.IsNullOrEmpty(CountryName)) return _CountryFeeCost;

            _CountryFeeCost = _sqlSugar.Queryable< Res_CountryFeeCost >().Where(it => it.VisaCountry.Equals(CountryName)).First();

            return _CountryFeeCost;

        }


        public async Task<Result> OperationCountryFeeCost(OperationCountryFeeCostDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };

            Res_CountryFeeCost _CountryFeeCost = _mapper.Map<Res_CountryFeeCost>(dto);

            _CountryFeeCost.LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (dto.Status == 1)//添加
            {
                string selectSql = string.Format(@"select * from Res_CountryFeeCost where VisaContinent='{0}' and VisaCountry='{1}' and IsDel='{2}'"
                                                   , dto.VisaContinent, dto.VisaCountry, 0);
                var CountryFeeCost = await _sqlSugar.SqlQueryable<Res_CountryFeeCost>(selectSql).FirstAsync();//查询是否存在
                if (CountryFeeCost != null)
                {
                    return result = new Result() { Code = -1, Msg = "该国家已存在,请勿重复添加!" };

                }
                else//不存在,可添加
                {

                    int id = await AddAsyncReturnId(_CountryFeeCost);
                    if (id == 0)
                    {
                        return result = new Result() { Code = -1, Msg = "添加失败!" };

                    }
                    result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
                }
            }
            else if (dto.Status == 2)//修改
            {
                var update = await _sqlSugar.Updateable(_CountryFeeCost)
                                            .IgnoreColumns(it => new { it.Id, it.DeleteUserId, it.DeleteTime, it.CreateUserId, it.CreateTime, it.IsDel })
                                            .Where(it => it.Id == _CountryFeeCost.Id)
                                            .ExecuteCommandAsync();
                //bool res = await UpdateAsync(a => a.Id == dto.Id, a => _CountryFeeCost);
                if (update < 1)
                {
                    return result = new Result() { Code = -1, Msg = "修改失败!" };
                }
                result = new Result() { Code = 0, Msg = "修改成功!" };
            }
            else
            {
                return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
            }

            return result;
        }
    }
}