using AutoMapper;
using MySqlX.XDevAPI.Common;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Domain.ViewModels.QiYeWeChat;
using OASystem.Infrastructure.Repositories.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Result = OASystem.Domain.Result;
namespace OASystem.Infrastructure.Repositories.Groups
{
    /// 
    /// 酒店询价 仓储
    /// 
    public class HotelInquiryRepository:BaseRepository
    {
        private readonly IMapper _mapper;
        private readonly SetDataRepository _setDataRep;
        /// 
        /// Init
        /// 
        /// 
        public HotelInquiryRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRep) 
            : base(sqlSugar)
        {
            _mapper = mapper;
            _setDataRep = setDataRep;
        }
        /// 
        /// 酒店询价
        /// 初始化数据
        /// 
        /// 
        public async Task _Init() 
        {
            var currDataResult = await _setDataRep.GetSetDataBySTId(_setDataRep, 66);
            dynamic currDatas = null;
            if (currDataResult.Code == 0) currDatas = currDataResult.Data;
            var data = new { 
                CurrData = currDatas
            };
            return new Result() { Code = 0 ,Msg="操作成功!",Data = data };
        }
        /// 
        /// 酒店询价
        /// Page 列表
        /// 
        /// 
        /// 
        /// 
        public async Task _PageItem(int pageIndex,int pageSize, int portType, int diId)
        {
            if (portType < 1 || portType > 3) return new Result() { Code = -1, Msg =  MsgTips.Port};
            if (diId < 1) return new Result() { Code = -1, Msg = MsgTips.DiId };
            if (pageIndex < 1) return new Result() { Code = -1, Msg = MsgTips.PageIndex };
            if (pageIndex < 1) return new Result() { Code = -1, Msg = MsgTips.PageSize };
            string sql = string.Format(@$"SELECT
  hi.Id,
  hi.DiId,
  hi.City,
  hi.[Name],
  hi.[SelectDt],
  hi.CheckInDate,
  hi.CheckOutDate,
  hi.SinglePrice,
  hi.SingleQuantity,
  sd1.[Name] AS SingleCurrency,
  hi.DoublePrice,
  hi.DoubleQuantity,
  sd2.[Name] AS DoubleCurrency,
  hi.SuitePrice,
  hi.SuiteQuantity,
  sd3.[Name] AS SuiteCurrency,
  hi.OtherPrice,
  hi.OtherQuantity,
  sd4.[Name] AS OtherCurrency,
  hi.Remark,
  u.CnName AS CreateUserName,
  hi.CreateTime
FROM
  Grp_HotelInquiry hi
WITH
  (NoLock)
  LEFT JOIN Sys_Users u
WITH
  (NoLock) ON hi.CreateUserId = u.Id
  LEFT JOIN Sys_SetData sd1
WITH
  (NoLock) ON hi.SingleCurrency = sd1.Id
  LEFT JOIN Sys_SetData sd2
WITH
  (NoLock) ON hi.DoubleCurrency = sd2.Id
  LEFT JOIN Sys_SetData sd3
WITH
  (NoLock) ON hi.SuiteCurrency = sd3.Id
  LEFT JOIN Sys_SetData sd4
WITH
  (NoLock) ON hi.OtherCurrency = sd4.Id
WHERE
  hi.Isdel = 0
  AND hi.DiId = {diId}");
            if (portType == 1 || portType == 2 || portType == 3)
            {
                RefAsync total = 0;
                var eqquiryDatas = await _sqlSugar.SqlQueryable(sql).OrderBy(it => it.checkInDate).ToPageListAsync(pageIndex, pageSize, total);
                return new Result() { Code = 0, Msg = MsgTips.Succeed, Data = new PageDataViewBase { Data = eqquiryDatas, Total = total } };
            }
            return new Result() { Code = -1, Msg = MsgTips.Fail  };
        }
        
        /// 
        /// info 
        /// 
        /// 
        /// 
        /// 
        public async Task _Info(int portType, int id)
        {
            if (portType < 1 || portType > 3) return new Result() { Code = -1, Msg = MsgTips.Port };
            if (id < 1 ) return new Result() { Code = -1, Msg = MsgTips.Id };
            var info = await _sqlSugar.Queryable()
                                      .Where(it => it.IsDel == 0 && it.Id == id)
                                      .Select(it =>
                                            new { 
                                                it.Id,
                                                it.DiId, 
                                                it.City,
                                                it.Name,
                                                it.Address,
                                                it.SelectDt,
                                                it.CheckInDate,
                                                it.CheckOutDate,
                                                it.SinglePrice,
                                                it.SingleQuantity,
                                                it.SingleCurrency,
                                                it.DoublePrice,
                                                it.DoubleQuantity,
                                                it.DoubleCurrency,
                                                it.SuitePrice,
                                                it.SuiteQuantity,
                                                it.SuiteCurrency,
                                                it.OtherPrice,
                                                it.OtherQuantity,
                                                it.OtherCurrency,
                                                it.Remark,
                                            })
                                      .FirstAsync();
            return new Result() { Code = 0, Msg = MsgTips.Succeed,Data = info };
        }
        /// 
        /// Add Or Edit
        /// 
        /// 
        /// 
        public async Task _AddOrEdit(HotelInquiryAddOrEditDto _dto)
        {
            #region 参数验证
            if (_dto.DiId < 1) return new Result() { Code = -1, Msg = MsgTips.DiId };
            if (_dto.Status < 1 || _dto.Status > 2) return new Result() { Code = -1, Msg = MsgTips.Status };
            #endregion
            Grp_HotelInquiry _HotelInquiry = _mapper.Map(_dto);
            if (_dto.Status == 1)
            {
                var add = await _sqlSugar.Insertable(_HotelInquiry).ExecuteReturnIdentityAsync();
                if (add >  0 ) return new Result() { Code = 0, Msg = MsgTips.Succeed };
            }
            else if (_dto.Status == 2)
            {
                var update = await _sqlSugar.Updateable(_HotelInquiry)
                                            .IgnoreColumns(it =>
                                                new
                                                {
                                                    it.CreateUserId,
                                                    it.CreateTime,
                                                    it.DeleteUserId,
                                                    it.DeleteTime,
                                                    it.IsDel
                                                })
                                            .WhereColumns(it =>
                                                new
                                                {
                                                    it.Id
                                                })
                                            .ExecuteCommandAsync();
                if (update > 0) return new Result() { Code = 0, Msg = MsgTips.Succeed };
            }
            return new Result() { Code = -1, Msg = MsgTips.Fail };
        }
        /// 
        /// Del
        /// 
        /// 
        /// 
        public async Task _Del(int id,int userId)
        {
            #region 参数验证
            if (id < 1) return new Result() { Code = -1, Msg = MsgTips.Id };
            if (userId < 0) return new Result() { Code = -1, Msg = MsgTips.UserId };
            #endregion
            var del = await SoftDeleteByIdAsync(id.ToString(), userId);
            if (del) return new Result() { Code = 0, Msg = MsgTips.Succeed };
            return new Result() { Code = -1, Msg = MsgTips.Fail };
        }
    }
}