123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package controllers
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "Cold_Api/models/Account"
- "Cold_Api/models/DataSource"
- "Cold_Api/models/System"
- "fmt"
- beego "github.com/beego/beego/v2/server/web"
- "math"
- "strconv"
- "strings"
- )
- type DataSourceController struct {
- beego.Controller
- Admin_r Account.Admin // 登陆的用户
- }
- func (c *DataSourceController) Prepare() {
- GetCookie := c.Ctx.GetCookie("User_tokey")
- GetString := c.GetString("User_tokey")
- User_tokey := GetCookie
- if len(User_tokey) == 0 {
- User_tokey = GetString
- }
- c.Admin_r = *Account.Admin_r
- }
- func (c *DataSourceController) DataSource_Exec() {
- type R_JSONS struct {
- //必须的大写开头
- Data interface{}
- }
- var r_jsons R_JSONS
- var T_data_list []string
- T_dataSource_id := c.GetString("T_dataSource_id")
- T_data := c.GetString("T_data")
- T_pid, _ := c.GetInt("T_pid")
- T_data = strings.TrimLeft(T_data, "[")
- T_data = strings.TrimRight(T_data, "]")
- if len(T_data) > 0 {
- T_data_list = strings.Split(T_data, ",")
- }
- // 操作pid
- company_r, err := Account.Read_Company_ById(T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- visit := false
- if c.Admin_r.T_pid > 0 && (c.Admin_r.T_pid == T_pid || strings.Contains(company_r.T_path, fmt.Sprintf("/%d/", c.Admin_r.T_pid))) {
- visit = true
- }
- if c.Admin_r.T_pid == 0 {
- if c.Admin_r.T_pids == "*" {
- visit = true
- }
- pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P")
- for _, v := range pids {
- if T_pid == v {
- visit = true
- break
- }
- }
- }
- if !visit {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
- c.ServeJSON()
- return
- }
- var data []interface{}
- for _, v := range T_data_list {
- data = append(data, v)
- }
- dataSource, err := DataSource.Read_DataSource_ByT_id(T_dataSource_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_dataSource_id Err!"}
- c.ServeJSON()
- return
- }
- if len(T_data_list) != strings.Count(dataSource.T_sql, "?") {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sql: expected %d arguments, got %d!", strings.Count(dataSource.T_sql, "?"), len(T_data_list))}
- c.ServeJSON()
- return
- }
- sql := strings.Replace(dataSource.T_sql, "@pid", fmt.Sprintf("t_pid = %d", T_pid), -1)
- r_jsons.Data = DataSource.Exec_DataSource_Sql(sql, data)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DataSourceController) DataSource_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []DataSource.DataSource_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- T_name := c.GetString("T_name")
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- r_jsons.Data, r_jsons.Num = DataSource.Read_DataSource_List(T_name, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DataSourceController) DataSource_Add() {
- T_name := c.GetString("T_name")
- T_sql := c.GetString("T_sql")
- var_ := DataSource.DataSource{
- T_name: T_name,
- T_sql: T_sql,
- }
- Id, err := DataSource.Add_DataSource(var_)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "数据源管理", "添加", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
- c.ServeJSON()
- return
- }
- func (c *DataSourceController) DataSource_Edit() {
- id, _ := c.GetInt("T_id")
- T_name := c.GetString("T_name")
- T_sql := c.GetString("T_sql")
- dataSource, err := DataSource.Read_DataSource_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if len(T_name) > 0 {
- dataSource.T_name = T_name
- }
- if len(T_sql) > 0 {
- dataSource.T_sql = T_sql
- }
- if err = DataSource.Update_DataSource(dataSource, "T_name", "T_sql"); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "数据源管理", "修改", strconv.Itoa(id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DataSourceController) DataSource_Del() {
- id, _ := c.GetInt("T_id")
- _, err := DataSource.Read_DataSource_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if err = DataSource.Delete_DataSource_ById(id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "数据源管理", "删除", strconv.Itoa(id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
|