/*
 * 公众号设置
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.BsModels;
using Library;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class PublicAccountSetController : BaseController
    {
        public PublicAccountSetController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["BsSqlConnStr"].ToString();
        }

        #region 公众号设置列表

        /// <summary>
        /// 根据条件查询公众号设置列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(PublicAccountSet data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            string Condition = "";

            if (!string.IsNullOrEmpty(Condition))
            {
                Condition = Condition.TrimEnd(',');
                Condition = ", where: {" + Condition + "}";
            }
            ViewBag.Condition = Condition;
            return View();
        }

        #endregion

        #region 根据条件查询公众号设置列表

        /// <summary>
        /// 公众号设置列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(PublicAccountSet data, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("PublicAccountSet", Fields, "Id desc", "False", page, limit);
            //List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            //foreach (Dictionary<string, object> dic in diclist)
            //{

            //}
            return Json(obj);
        }

        #endregion

        #region 增加公众号设置

        /// <summary>
        /// 增加或修改公众号设置信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 增加公众号设置

        /// <summary>
        /// 增加或修改公众号设置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(PublicAccountSet data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("AlipayAppId", data.AlipayAppId); //支付宝AppId
            Fields.Add("AlipayPartnerId", data.AlipayPartnerId); //支付宝合作者Id
            Fields.Add("AlipayPrivateKey", data.AlipayPrivateKey); //支付宝私钥
            Fields.Add("AlipayPublicKey", data.AlipayPublicKey); //支付宝公钥
            Fields.Add("WeChatAppId", data.WeChatAppId); //微信公众号AppId
            Fields.Add("WeChatAppSecret", data.WeChatAppSecret); //微信公众号AppSecret
            Fields.Add("WeChatMchId", data.WeChatMchId); //微信商户号
            Fields.Add("WeChatPayKey", data.WeChatPayKey); //微信支付密钥
            Fields.Add("WeChatOpenAppId", data.WeChatOpenAppId); //微信开放平台AppId
            Fields.Add("WeChatOpenAppSecret", data.WeChatOpenAppSecret); //微信开放平台AppSecret
            Fields.Add("WeChatMiniAppId", data.WeChatMiniAppId); //微信小程序AppId
            Fields.Add("WeChatMiniAppSecret", data.WeChatMiniAppSecret); //微信小程序AppSecret

            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Add("PublicAccountSet", Fields, 0);
            AddSysLog(data.Id.ToString(), "PublicAccountSet", "add");
            bsdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改公众号设置

        /// <summary>
        /// 增加或修改公众号设置信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            PublicAccountSet editData = bsdb.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改公众号设置

        /// <summary>
        /// 增加或修改公众号设置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(PublicAccountSet data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("AlipayAppId", data.AlipayAppId); //支付宝AppId
            Fields.Add("AlipayPartnerId", data.AlipayPartnerId); //支付宝合作者Id
            Fields.Add("AlipayPrivateKey", data.AlipayPrivateKey); //支付宝私钥
            Fields.Add("AlipayPublicKey", data.AlipayPublicKey); //支付宝公钥
            Fields.Add("WeChatAppId", data.WeChatAppId); //微信公众号AppId
            Fields.Add("WeChatAppSecret", data.WeChatAppSecret); //微信公众号AppSecret
            Fields.Add("WeChatMchId", data.WeChatMchId); //微信商户号
            Fields.Add("WeChatPayKey", data.WeChatPayKey); //微信支付密钥
            Fields.Add("WeChatOpenAppId", data.WeChatOpenAppId); //微信开放平台AppId
            Fields.Add("WeChatOpenAppSecret", data.WeChatOpenAppSecret); //微信开放平台AppSecret
            Fields.Add("WeChatMiniAppId", data.WeChatMiniAppId); //微信小程序AppId
            Fields.Add("WeChatMiniAppSecret", data.WeChatMiniAppSecret); //微信小程序AppSecret
            Fields.Add("ServerIp", data.ServerIp); //服务器IP
            Fields.Add("QQAndroidAppId", data.QqAndroidAppId);
            Fields.Add("QQAndroidAppKey", data.QqAndroidAppKey);
            Fields.Add("QQIosAppId", data.QqIosAppId);
            Fields.Add("QQIosAppKey", data.QqIosAppKey);
            Fields.Add("AliyunAccessKeyID", data.AliyunAccessKeyId);
            Fields.Add("AliyunAccessKeySecret", data.AliyunAccessKeySecret);
            Fields.Add("AliyunPushAndroidAppId", data.AliyunPushAndroidAppId);
            Fields.Add("AliyunPushAndroidAppSecret", data.AliyunPushAndroidAppSecret);
            Fields.Add("AliyunPushIosAppId", data.AliyunPushIosAppId);
            Fields.Add("AliyunPushIosAppSecret", data.AliyunPushIosAppSecret);
            Fields.Add("MobAppKey", data.MobAppKey);
            Fields.Add("MobAppSecret", data.MobAppSecret);
            Fields.Add("MeiQiaAppKey", data.MeiQiaAppKey);
            Fields.Add("ErpApiUrl", data.ErpApiUrl);
            Fields.Add("ErpAppCode", data.ErpAppCode);
            Fields.Add("SmsApiUserId", data.SmsApiUserId);
            Fields.Add("SmsApiPwd", data.SmsApiPwd);
            Fields.Add("SmsApiSign", data.SmsApiSign);
            Fields.Add("SmsApiDefaultTemplate", data.SmsApiDefaultTemplate);

            PublicAccountSet check = bsdb.PublicAccountSet.FirstOrDefault();
            if (check == null)
            {
                int Id = new AdminContentCustomer(_accessor.HttpContext, PublicFunction.BsTables).Add("PublicAccountSet", Fields, 0);
                AddSysLog(data.Id.ToString(), "PublicAccountSet", "add");
            }
            else
            {
                new AdminContentCustomer(_accessor.HttpContext, PublicFunction.BsTables).Edit("PublicAccountSet", Fields, data.Id);
                AddSysLog(data.Id.ToString(), "PublicAccountSet", "update");
            }

            return "success";
        }

        #endregion

        #region 删除公众号设置信息

        /// <summary>
        /// 删除公众号设置信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "PublicAccountSet", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("PublicAccountSet", id);
            }
            bsdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "PublicAccountSet", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("PublicAccountSet", Fields, id);
            }
            bsdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "PublicAccountSet", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("PublicAccountSet", Fields, id);
            }
            bsdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("PublicAccountSet", Sort, Id);

            AddSysLog(Id.ToString(), "PublicAccountSet", "sort");
            return "success";
        }
        #endregion


    }
}
