using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Dtos.Resource;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Resource;
using SqlSugar;

namespace OASystem.Infrastructure.Repositories.Resource
{
    public class ThreeCodeRepository:BaseRepository<Res_ThreeCode,ThreeCodeView>
    {
        private readonly IMapper _mapper;
        public ThreeCodeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }
        /// <summary>
        /// sqlSugar分页查询
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>

        public async Task<JsonView> QuerThreeCode(int PageIndex, int PageSize)
        {
            JsonView result = new JsonView();
            try
            {
               
                int totalCount = 0;
                List<Res_ThreeCode> page = _sqlSugar.Queryable<Res_ThreeCode>().ToPageList(PageIndex, PageSize, ref totalCount);
                if (page.Count != 0)
                {
                    result=new JsonView(){Code=200,Msg = "查询成功!",Data = page,Count = totalCount };
                }
                else
                {
                    result = new JsonView(){ Code = 400, Msg = "查询失败!", Data = page, Count = totalCount };
                }
            }
            catch (Exception)
            {
                result = new JsonView() { Code = 400, Msg = "程序错误!", Data = null, Count = 0 };
                throw;
            }
            
           
            return result;
        }

        /// <summary>
        /// 添加、编辑操作
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Result> ThreeCodeOperation(ThreeCodeOperationDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };
            try
            {
                if (dto.Status == 1)//添加
                {
                    string selectSql = string.Format(@"select * from Res_ThreeCode where Three='{0}' and Country='{1}' and City='{2}' and AirPort='{3}'"
                                                       , dto.Three, dto.Country, dto.City, dto.AirPort);
                    var ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(selectSql).FirstAsync();//查询是否存在
                    if (ThreeCode != null)
                    {
                        return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };

                    }
                    else//不存在,可添加
                    {
                        Res_ThreeCode _ThreeCodeDto = _mapper.Map<Res_ThreeCode>(dto);
                        int id = await AddAsyncReturnId(_ThreeCodeDto);
                        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)//修改
                {
                    bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_ThreeCode
                    {
                        Three=dto.Three,
                        Four=dto.Four,
                        Country=dto.Country,
                        City = dto.City,
                        AirPort = dto.AirPort,
                        AirPort_En = dto.AirPort_En,
                        Remark = dto.Remark,
                    });
                    if (!res)
                    {
                        return result = new Result() { Code = -1, Msg = "修改失败!" };
                    }
                    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 = "程序错误!" };
            }
            return result;
        }
    }
}