using ASHermed.AdminInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Repository.MySql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{
    public class OnlineOrderSourceRepository : BaseRepositoryToASHermed_SinoInterface<Online_Order_Source>, IOnlineOrderSourceRepository
    {
        public int CreateOrder(Online_Order_Source order)
        {
            var sql = @"INSERT INTO `online_order_source`(`OrderNo`, `PatId`, `OrderStatus`, `CreateTime`, `AptPrice`, `AptType`, `IsUseCoupon`, `CouponId`, `CouponPrice`, `PayPrice`, `PayWay`, `PayTime`, `TradeNo`, `Remark`, `UpdateTime`, `UpdateUser`, `IsEnglish`) 
                        VALUES (@OrderNo, @PatId, @OrderStatus, @CreateTime, @AptPrice, @AptType, @IsUseCoupon, @CouponId, @CouponPrice, @PayPrice, @PayWay, @PayTime, @TradeNo, @Remark, @UpdateTime, @UpdateUser, @IsEnglish)";
            return Execute(sql, order);
        }

        public Online_Order_Source GetOrderSource(string orderNo)
        {
            var sql = @" SELECT * FROM online_order_source Where OrderNo=@OrderNo ";
            return Query<Online_Order_Source>(sql, new { OrderNo = orderNo })?.FirstOrDefault();
        }

        public List<string> GetCancelOrderSource()
        {
            var sql = @"select OrderNo from online_order_source WHERE OrderStatus = 1 AND TIMESTAMPDIFF(MINUTE, CreateTime, now()) > 30 ";
            return Query<string>(sql).ToList();
        }

        public int UpdateOrderSourceStatus(string orderNo, int status, string payWay, decimal payPrice)
        {
            var sql = @" UPDATE online_order_source SET  ";
            if (status != 0)
            {
                sql += " `OrderStatus` = @OrderStatus,";
            }
            if (!string.IsNullOrEmpty(payWay))
            {
                sql += " `PayWay` = @PayWay,";
            }
            if (payPrice != 0M)
            {
                sql += " `PayPrice` = @PayPrice,";
            }
            if (sql.Substring(sql.Length - 1, 1) == ",")
            {
                sql = sql.Substring(0, sql.Length - 1);
            }
            sql += " WHERE `OrderNo`=@OrderNo ";
            return Execute(sql, new { OrderStatus = status, PayWay = payWay, PayPrice = payPrice, OrderNo = orderNo });
        }

        public int PerfPayInfoByNo(string orderNo, string PayType, string payTime, string tradeNo, string tradeStatus)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update online_order_source set ");
            str.Append(@"OrderStatus=@OrderStatus,
                            PayTime=@PayTime,
                            TradeNo=@TradeNo,
                            PayWay=@PayWay ");
            str.Append(@" where OrderNo=@OrderNo");
            var parms = new Dictionary<string, object>();
            parms.Add("@OrderNo", orderNo);
            parms.Add("@OrderStatus", tradeStatus);
            parms.Add("@PayTime", payTime);
            parms.Add("@PayWay", PayType);
            parms.Add("@TradeNo", tradeNo);
            var result = Execute(str.ToString(), parms);
            return result;
        }
    }
}
