using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LYFZ.WeixinServiceDate.DAL;
using LYFZ.WeixinServiceDate.Model;
using LYFZ.WXLibrary;

namespace LYFZ.WeixinServers.WxAuthorize
{
    public partial class DataAPI : System.Web.UI.Page
    {
         string eid = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["eid"] != null)
            {
                eid = Request.QueryString["eid"];
            }
           
            if (Request.QueryString["type"] != null)
            {
                string type = Request.QueryString["type"];
                switch (type)
                {
                    case "edit":
                        EditOfficialAccount(eid);
                        break;
                }
               
            }
        }
        public Model_CustomerInterfaces cusModel = new Model_CustomerInterfaces();
        DAL_CustomerInterfaces cusDal = new DAL_CustomerInterfaces();
        string postinfoddomaine = "post";
        string retMsg = "";
        void EditOfficialAccount(string eid)
        {
            if (!String.IsNullOrEmpty(eid))
            {
                string OfficialAccountName = "";
                string OfficialAccount = "";
                string APPID = "";
                int id = -1;
                if (Request.QueryString["id"] != null)
                {
                    id = Convert.ToInt32(Request.QueryString["id"]);
                }
                if (Request["OfficialAccountName"] != null)
                {
                    OfficialAccountName = Request["OfficialAccountName"];
                }

                if (Request["OfficialAccount"] != null)
                {
                    OfficialAccount = Request["OfficialAccount"];
                }

                if (Request["APPID"] != null)
                {
                    APPID = Request["APPID"];
                }
                if (String.IsNullOrEmpty(APPID))
                {
                    Response.Write("{\"code\":-100,\"info\":\"请求失败\",\"data\":\"APPID不能为空!\"}");
                }
                else
                {
                    if (id > 0)
                    {
                        cusModel = cusDal.GetModel(id);
                    }
                    string JMGDomainName = String.Format("{0}{1}", eid, APPID);
                    if (cusModel != null && cusModel.ID > 0)
                    {
                        try
                        {


                            cusModel.JMGDomainName = JMGDomainName;
                            cusModel.CompanyName = OfficialAccountName;
                            cusModel.RefreshTime = DateTime.Now;
                            cusModel.Token = eid;
                            cusModel.AppID = APPID;
                            cusModel.AppSecret = OfficialAccount;
                            cusModel.EncodingAESKey = "";
                            cusModel.OfficialAccount = OfficialAccount;
                            cusModel.OfficialAccountName = OfficialAccountName;
                            cusModel.ISEnabled = 1;
                            if (cusDal.Update(cusModel))
                            {
                                //  cusModel = (LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces)cusDal.GetModelObject("JMGDomainName", JMGDomainName);

                                Global.UpdateCustInfo(JMGDomainName, cusModel);
                                try
                                {
                                    Global.UpdateAuthorizedAPPIDHandleJMGDomainName(cusModel.AppID, cusModel.JMGDomainName, cusModel.CompanyName);
                                }
                                catch (Exception ex)
                                {
                                    CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的的客户更新授权处理对象加密锁域名时出错:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
                                }
                                CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交成功", postinfoddomaine, Global.LogsDongleDomainList);
                            }
                            else
                            {
                                retMsg = "Exception:Refresh Failed";
                                CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交失败,数据库更新记录时失败", postinfoddomaine, Global.LogsDongleDomainList);
                            }

                        }
                        catch (Exception ex)
                        {
                            retMsg = "Exception:" + ex.Message;
                            CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交失败,错误信息为:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
                            Global.RefreshLoadCustDaTable();
                        }

                    }
                    else
                    {
                        try
                        {
                            cusModel.JMGDomainName = JMGDomainName;
                            cusModel.ShellDomainName = "http://p.lyfz.net";
                            cusModel.IPAddress = "0.0.0.0";
                            cusModel.PortNumber = 0;
                            cusModel.UseType = 0;
                            cusModel.CompanyName = OfficialAccountName;
                            cusModel.CallFrequency = 0;

                            cusModel.RefreshTime = DateTime.Now;

                            cusModel.ThirdPartyPlatformURL = "";

                            cusModel.Token = eid;
                            cusModel.AppID = APPID;
                            cusModel.AppSecret = OfficialAccount;
                            cusModel.EncodingAESKey = "";
                            cusModel.OfficialAccount = OfficialAccount;
                            cusModel.OfficialAccountName = OfficialAccountName;

                            cusModel.ISEnabled = 1;
                            if (cusDal.Add(cusModel))
                            {
                                cusModel = (LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces)cusDal.GetModelObject("JMGDomainName", JMGDomainName);
                                Global.UpdateCustInfo(JMGDomainName, cusModel);
                                // retMsg += ":" + IPAddress;
                                CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交成功", postinfoddomaine, Global.LogsDongleDomainList);
                            }
                            else
                            {
                                retMsg = "Exception:Add Failed";
                                CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交失败,数据库添加记录时失败", postinfoddomaine, Global.LogsDongleDomainList);
                            }
                        }
                        catch (Exception ex)
                        {
                            retMsg = "Exception:" + ex.Message;
                            CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交失败,错误信息为:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
                            Global.RefreshLoadCustDaTable();
                        }

                    }
                }

               
            }
            else
            {
                retMsg = "企业ID不能为空";
            }
            if (String.IsNullOrEmpty(retMsg))
            {
                Response.Write("{\"code\":200,\"info\":\"请求成功\",\"data\":\"数据提交成功!\"}");
            }
            else
            {
                Response.Write("{\"code\":-100,\"info\":\"请求失败\",\"data\":\"" + retMsg + "\"}");
            }
        }
    }
}