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 DepositController : BaseController
    {
        public DepositController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }



        #region 设置押金
        public Dictionary<string, object> Set()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            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 serviceFee = data["serviceFee"].ToString();

                WebCMSEntities db = new WebCMSEntities();
                PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == sn);
                if(pos != null)
                {
                    Dictionary<string, object> req = new Dictionary<string, object>();
                    req.Add("PosSnId", pos.Id);
                    req.Add("BrandId", 12);
                    req.Add("Deposit", serviceFee);
                    RedisDbconn.Instance.AddList("ChangePosFeeQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
                    result.Add("code", 0);
                    result.Add("status", "success");
                    result.Add("msg", "成功");
                }
                else
                {
                    result.Add("code", 1);
                    result.Add("status", "fail");
                    result.Add("msg", "机具号不存在");
                }
            }
            catch(Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "设置押金异常");
                result.Add("code", 1);
                result.Add("status", "fail");
                result.Add("msg", "异常");
            }
            return result;
        }
        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

    }
}

