using AutoMapper;
using MathNet.Numerics.Statistics.Mcmc;
using MySqlX.XDevAPI.Common;
using NPOI.SS.Formula.Functions;
using OASystem.Domain;
using OASystem.Domain.AesEncryption;
using OASystem.Domain.Dtos.Groups;
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;
using XAct.Users;
using Result = OASystem.Domain.Result;

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

        public async Task<Result> _Info(int portType, int id)
        {
            if (id< 1) return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };


            string sql = string.Format(@"SELECT
  Id,
  City,
  [Name],
  [Level],
  [Address],
  Tel,
  Fax,
  Contact,
  ContactPhone,
  OtherInformation,
  Remark
FROM
  Res_HotelData
WHERE
  IsDel = 0
  AND Id = {0}", id);

            if (portType == 1 || portType == 2 || portType == 3)
            {
                var info = await _sqlSugar.SqlQueryable<HotelDataInfoView>(sql).FirstAsync();
                
                EncryptionProcessor.DecryptProperties(info);// 解密属性
                if (info != null) return new Result() { Code = 0, Msg = "操作成功!",Data = info };
                return new Result() { Code = -1, Msg = "暂无数据!" };
            }
            return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
        }

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

            var hotelDataDto = _mapper.Map<Res_HotelData>(dto);

            EncryptionProcessor.EncryptProperties(hotelDataDto);// 加密属性
            if (dto.Status == 1)//添加
            {
                string selectSql = string.Format(@"select * from Res_HotelData where Name='{0}' and IsDel='{1}'"
                                                   , dto.Name, 0);
                var HotelData = await _sqlSugar.Queryable<Res_HotelData>().Where(x => x.IsDel == 0 && x.Name.Contains(AesEncryptionHelper.Encrypt(dto.Name))).FirstAsync();//查询是否存在
                if (HotelData != null)
                {
                    return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };

                }
                else//不存在,可添加
                {
                    int id = await AddAsyncReturnId(hotelDataDto);
                    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)//修改
            {
                var res = await _sqlSugar.Updateable(hotelDataDto)
                    .IgnoreColumns(x => new { x.CreateTime, x.CreateUserId, x.DeleteTime, x.DeleteUserId, x.IsDel })
                    .ExecuteCommandAsync();
                if (res < 1)
                {
                    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修改!" };
            }
        }
    }
}