using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.FrameWork;
using EKETEAM.Data;
using LitJson;

public partial class Plugins_getRecordData : System.Web.UI.Page
{
    public string UserArea = "Application";
    string RecordID = eParameters.QueryString("id");
    private eDataBase _database;
    public eDataBase DataBase
    {
        get
        {
            if (_database == null)
            {
                if (Record["DataSourceID"].ToString().Length > 0)
                {
                    _database = new eDataBase(Record);
                }
                else
                {
                    _database = eConfig.DefaultDataBase;
                }
            }
            return _database;
        }
    }
    public dynamic Record = new eViewObject();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.UrlReferrer == null) return;
        if (Request.Url.Host.ToLower() != Request.UrlReferrer.Host.ToLower()) return;
        if (!eConfig.portMapping && Request.Url.Port != Request.UrlReferrer.Port) return;

        eUser user = new eUser(eBase.getUserArea(UserArea));
        if (!user.Logined) eResult.Error("登录后操作!");
        user.Check();

        Record = eBase.DataBase.getValues("select * from a_eke_sysDataRecords where RecordID='" + RecordID + "'");
        if (Record.outType.ToString() == "0") return;


        string sql = Record.resultSql.Length > 0 ? Record.resultSql : Record.tempSql;
        sql = eParameters.Replace(sql, null, null);
        DataTable dt = DataBase.getDataTable(sql);

        if (Record.outType.ToString() == "1") //对象
        {
            if (dt.Rows.Count > 0)
            {
                eBase.Write(dt.Rows[0].toJSON());
                eBase.End();
            }
            else
            {
                eBase.Write("{}");
                eBase.End();
            }
        }
        if (Record.outType.ToString() == "2") //数组
        {
            if (dt.Rows.Count > 0)
            {
                eBase.Write(dt.toJSON());
                eBase.End();
            }
            else
            {
                eBase.Write("[]");
                eBase.End();
            }
        }
        eBase.End();
    }
}