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.Groups
{
    public class AirTicketAgentRepository : BaseRepository<Res_AirTicketAgent, Res_AirTicketAgent>
    {
        private readonly IMapper _mapper;
        public AirTicketAgentRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }

        public async Task<Result> OpAirTicketAgent(OpAirTicketAgentDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };
            try
            {
                if (dto.Status == 1)//添加
                {
                   Res_AirTicketAgent res_AirTicket=await Query<Res_AirTicketAgent>(a=>a.IsDel==0 && a.Account==dto.Account && a.Name==dto.Name && a.Bank==dto.Bank).FirstAsync();
                    if (res_AirTicket!=null)
                    {
                        return result = new Result() { Code = -1, Msg = "该代理商已存在，请勿重复添加！" };
                    }
                    else
                    {
                        Res_AirTicketAgent _AirTicketAgent = _mapper.Map<Res_AirTicketAgent>(dto);
                        int id = await AddAsyncReturnId(_AirTicketAgent);
                        if (id == 0)
                        {
                            return result = new Result() { Code = -1, Msg = "添加失败！" };

                        }
                        return result = new Result() { Code = 0, Msg = "添加成功！", Data = new { Id = id } };
                    }
                }
                else if (dto.Status == 2)//修改
                {
                    bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_AirTicketAgent
                    {
                        Name = dto.Name,
                        Bank = dto.Bank,
                        Account = dto.Account,
                        CreateUserId = dto.CreateUserId,
                        Remark = dto.Remark,
                    });
                    if (!res)
                    {
                        return result = new Result() { Code = -1, Msg = "修改失败！" };
                    }
                    return result = new Result() { Code = 0, Msg = "修改成功！" };
                }
                else
                {
                    return result = new Result() { Code = -1, Msg = "请传入Status参数，1添加 2修改！" };
                }
            }
            catch (Exception ex)
            {
                return result = new Result() { Code = -2, Msg = "程序错误！" };
            }
        }
    }
}
