using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;

using System.Web;
using MySystem.MainModels;
using LitJson;
using Library;
using System.IO;
using System.Text;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/[controller]/[action]")]
    public class FeeController : BaseController
    {
        public FeeController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }



        #region 设置费率
        public string Set()
        {
            string content = "";
            try
            {
                StreamReader sr = new StreamReader(Request.Body);
                string requestMes = sr.ReadToEnd();
                JsonData json = JsonMapper.ToObject(requestMes);
                string value = AesDecrypt(json["content"].ToString(), "j7rKHxMIpz5w0XRU");
                JsonData data = JsonMapper.ToObject(value);
                string sn = data["sn"].ToString();
                string rate = data["rate"].ToString();
                // string fee = data["fee"].ToString();

                WebCMSEntities db = new WebCMSEntities();
                PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == sn);
                if(pos != null)
                {
                    int Kind = rate == "0.63" ? 1 : 2;
                    string info = "{\"RecordId\":\"\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"" + rate + "\",\"Kind\": \"" + Kind + "\",\"OpMan\": \"创业帮\"}";
                    RedisDbconn.Instance.AddList("SetDepositPostQueue", info);
                }
            }
            catch(Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "设置费率异常");
            }
            return content;
        }
        public string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7,
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }
        #endregion


    }
}

