DataSource.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package controllers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "Cold_Api/models/DataSource"
  7. "Cold_Api/models/System"
  8. "fmt"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "math"
  11. "strconv"
  12. "strings"
  13. )
  14. type DataSourceController struct {
  15. beego.Controller
  16. Admin_r Account.Admin // 登陆的用户
  17. }
  18. func (c *DataSourceController) Prepare() {
  19. GetCookie := c.Ctx.GetCookie("User_tokey")
  20. GetString := c.GetString("User_tokey")
  21. User_tokey := GetCookie
  22. if len(User_tokey) == 0 {
  23. User_tokey = GetString
  24. }
  25. c.Admin_r = *Account.Admin_r
  26. }
  27. func (c *DataSourceController) DataSource_Exec() {
  28. type R_JSONS struct {
  29. //必须的大写开头
  30. Data interface{}
  31. }
  32. var r_jsons R_JSONS
  33. var T_data_list []string
  34. T_dataSource_id := c.GetString("T_dataSource_id")
  35. T_data := c.GetString("T_data")
  36. T_pid, _ := c.GetInt("T_pid")
  37. T_data = strings.TrimLeft(T_data, "[")
  38. T_data = strings.TrimRight(T_data, "]")
  39. if len(T_data) > 0 {
  40. T_data_list = strings.Split(T_data, ",")
  41. }
  42. // 操作pid
  43. company_r, err := Account.Read_Company_ById(T_pid)
  44. if err != nil {
  45. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  46. c.ServeJSON()
  47. return
  48. }
  49. visit := false
  50. 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))) {
  51. visit = true
  52. }
  53. if c.Admin_r.T_pid == 0 {
  54. if c.Admin_r.T_pids == "*" {
  55. visit = true
  56. }
  57. pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P")
  58. for _, v := range pids {
  59. if T_pid == v {
  60. visit = true
  61. break
  62. }
  63. }
  64. }
  65. if !visit {
  66. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  67. c.ServeJSON()
  68. return
  69. }
  70. var data []interface{}
  71. for _, v := range T_data_list {
  72. data = append(data, v)
  73. }
  74. dataSource, err := DataSource.Read_DataSource_ByT_id(T_dataSource_id)
  75. if err != nil {
  76. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_dataSource_id Err!"}
  77. c.ServeJSON()
  78. return
  79. }
  80. if len(T_data_list) != strings.Count(dataSource.T_sql, "?") {
  81. 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))}
  82. c.ServeJSON()
  83. return
  84. }
  85. sql := strings.Replace(dataSource.T_sql, "@pid", fmt.Sprintf("t_pid = %d", T_pid), -1)
  86. r_jsons.Data = DataSource.Exec_DataSource_Sql(sql, data)
  87. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  88. c.ServeJSON()
  89. return
  90. }
  91. func (c *DataSourceController) DataSource_List() {
  92. type R_JSONS struct {
  93. //必须的大写开头
  94. Data []DataSource.DataSource_R
  95. Num int64
  96. Page int
  97. Page_size int
  98. }
  99. var r_jsons R_JSONS
  100. T_name := c.GetString("T_name")
  101. page, _ := c.GetInt("page")
  102. if page < 1 {
  103. page = 1
  104. }
  105. page_z, _ := c.GetInt("page_z")
  106. if page_z < 1 {
  107. page_z = conf.Page_size
  108. }
  109. r_jsons.Data, r_jsons.Num = DataSource.Read_DataSource_List(T_name, page, page_z)
  110. r_jsons.Page = page
  111. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  112. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  113. c.ServeJSON()
  114. return
  115. }
  116. func (c *DataSourceController) DataSource_Add() {
  117. T_name := c.GetString("T_name")
  118. T_sql := c.GetString("T_sql")
  119. var_ := DataSource.DataSource{
  120. T_name: T_name,
  121. T_sql: T_sql,
  122. }
  123. Id, err := DataSource.Add_DataSource(var_)
  124. if err != nil {
  125. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  126. c.ServeJSON()
  127. return
  128. }
  129. System.Add_UserLogs_T(c.Admin_r.T_uuid, "数据源管理", "添加", var_)
  130. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  131. c.ServeJSON()
  132. return
  133. }
  134. func (c *DataSourceController) DataSource_Edit() {
  135. id, _ := c.GetInt("T_id")
  136. T_name := c.GetString("T_name")
  137. T_sql := c.GetString("T_sql")
  138. dataSource, err := DataSource.Read_DataSource_ById(id)
  139. if err != nil {
  140. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  141. c.ServeJSON()
  142. return
  143. }
  144. if len(T_name) > 0 {
  145. dataSource.T_name = T_name
  146. }
  147. if len(T_sql) > 0 {
  148. dataSource.T_sql = T_sql
  149. }
  150. if err = DataSource.Update_DataSource(dataSource, "T_name", "T_sql"); err != nil {
  151. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  152. c.ServeJSON()
  153. return
  154. }
  155. System.Add_UserLogs(c.Admin_r.T_uuid, "数据源管理", "修改", strconv.Itoa(id))
  156. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  157. c.ServeJSON()
  158. return
  159. }
  160. func (c *DataSourceController) DataSource_Del() {
  161. id, _ := c.GetInt("T_id")
  162. _, err := DataSource.Read_DataSource_ById(id)
  163. if err != nil {
  164. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  165. c.ServeJSON()
  166. return
  167. }
  168. if err = DataSource.Delete_DataSource_ById(id); err != nil {
  169. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  170. c.ServeJSON()
  171. return
  172. }
  173. System.Add_UserLogs(c.Admin_r.T_uuid, "数据源管理", "删除", strconv.Itoa(id))
  174. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  175. c.ServeJSON()
  176. return
  177. }