script.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package dao
  2. import (
  3. "time"
  4. "go-common/app/admin/ep/melloi/model"
  5. )
  6. //QueryScripts query script
  7. func (d *Dao) QueryScripts(script *model.Script, pn int, ps int) (scripts []*model.Script, err error) {
  8. script.Active = 1
  9. err = d.DB.Table(model.Script{}.TableName()).Where(script).Order("id asc").Offset((pn - 1) * ps).Limit(ps).Find(&scripts).Error
  10. return
  11. }
  12. //QueryScriptsByPage query scripts by page
  13. func (d *Dao) QueryScriptsByPage(script *model.Script, pn, ps int32, treeNodes []string) (qsr *model.QueryScriptResponse, err error) {
  14. script.TestType = 1
  15. qsr = &model.QueryScriptResponse{}
  16. gDB := d.DB.Table(model.Script{}.TableName()).Where("app in (?)", treeNodes)
  17. if script.Department != "" && script.Project != "" && script.App != "" {
  18. gDB = gDB.Where("department = ? and project = ? and app = ?", script.Department, script.Project, script.App)
  19. }
  20. if script.Department != "" && script.Project != "" {
  21. gDB = gDB.Where("department = ? and project = ?", script.Department, script.Project)
  22. }
  23. if script.Department != "" {
  24. gDB = gDB.Where("department = ?", script.Department)
  25. }
  26. if script.TestName != "" {
  27. gDB = gDB.Where("test_name LIKE ? and test_type = ?", "%"+script.TestName+"%", 1)
  28. }
  29. if script.UpdateBy != "" {
  30. gDB = gDB.Where("update_by LIKE ? and test_type = ?", "%"+script.UpdateBy+"%", 1)
  31. }
  32. if script.TestName == "" && script.UpdateBy == "" {
  33. gDB = gDB.Where(script)
  34. }
  35. err = gDB.Where("active = 1").Count(&qsr.TotalSize).Order("ctime desc").Offset((pn - 1) * ps).Limit(ps).
  36. Find(&qsr.Scripts).Error
  37. qsr.PageSize = ps /**/
  38. qsr.PageNum = pn
  39. return
  40. }
  41. //QueryScriptsByPageWhiteName query by whiteName
  42. func (d *Dao) QueryScriptsByPageWhiteName(script *model.Script, pn, ps int32) (qsr *model.QueryScriptResponse, err error) {
  43. qsr = &model.QueryScriptResponse{}
  44. script.TestType = 1
  45. gDB := d.DB.Table(model.Script{}.TableName())
  46. if script.TestName != "" {
  47. gDB = gDB.Where("test_name LIKE ? and test_type = ?", "%"+script.TestName+"%", 1)
  48. }
  49. if script.UpdateBy != "" {
  50. gDB = gDB.Where("update_by LIKE ? and test_type = ?", "%"+script.UpdateBy+"%", 1)
  51. }
  52. if script.TestName == "" && script.UpdateBy == "" {
  53. gDB = gDB.Where(script)
  54. }
  55. err = gDB.Where("active = 1").Count(&qsr.TotalSize).Order("ctime desc").Offset((pn - 1) * ps).Limit(ps).
  56. Find(&qsr.Scripts).Error
  57. qsr.PageSize = ps /**/
  58. qsr.PageNum = pn
  59. return
  60. }
  61. //QueryScriptByID get script by id
  62. func (d *Dao) QueryScriptByID(id int) (script *model.Script, err error) {
  63. script = &model.Script{}
  64. err = d.DB.Table(model.Script{}.TableName()).Where("id = ? and active = 1", id).First(script).Error
  65. return
  66. }
  67. //QueryScriptsInID get script by id
  68. func (d *Dao) QueryScriptsInID(id []int) (scripts []*model.Script, err error) {
  69. err = d.DB.Table(model.Script{}.TableName()).Where("id in (?) and active = 1", id).Find(&scripts).Error
  70. return
  71. }
  72. //CountQueryScripts count query scripts
  73. func (d *Dao) CountQueryScripts(script *model.Script) (total int) {
  74. d.DB.Table(model.Script{}.TableName()).Where(script).Count(&total)
  75. return
  76. }
  77. //AddScript add script
  78. func (d *Dao) AddScript(script *model.Script) (id int, groupId int, runOrder int, err error) {
  79. if script.OutputParams == "[]" || script.OutputParams == "" {
  80. script.OutputParams = "[{\"\":\"\"}]"
  81. }
  82. if script.APIHeader == "[]" || script.APIHeader == "" {
  83. script.APIHeader = "[{\"\":\"\"}]"
  84. }
  85. if script.ArgumentString == "[]" || script.AssertionString == "" {
  86. script.ArgumentString = "[{\"\":\"\"}]"
  87. }
  88. script.Active = 1
  89. err = d.DB.Create(script).Error
  90. id = script.ID
  91. groupId = script.GroupID
  92. runOrder = script.RunOrder
  93. return
  94. }
  95. //QueryParams query params
  96. func (d *Dao) QueryParams(script *model.Script, scene *model.Scene) (paramList *model.ParamList, err error) {
  97. paramList = new(model.ParamList)
  98. if scene.SceneType == 1 || scene.SceneType == 0 {
  99. err = d.DB.Table(model.Script{}.TableName()).Select("id, group_id, run_order, output_params").Where("scene_id = ? and active = 1", script.SceneID).Order("group_id, run_order").Find(&paramList.ParamList).Error
  100. } else if scene.SceneType == 2 {
  101. err = d.DB.Table(model.Script{}.TableName()).Select("id, group_id, run_order, output_params").Where("scene_id = ? and group_id = ? and active = 1", script.SceneID, script.GroupID).Order("group_id, run_order").Find(&paramList.ParamList).Error
  102. }
  103. return
  104. }
  105. //AddScriptSnap add script snap
  106. func (d *Dao) AddScriptSnap(scriptSnap *model.ScriptSnap) (id int, err error) {
  107. scriptSnap.Ctime = time.Now()
  108. scriptSnap.Mtime = time.Now()
  109. err = d.DB.Create(scriptSnap).Error
  110. id = scriptSnap.ID
  111. return
  112. }
  113. //UpdateScript update script
  114. func (d *Dao) UpdateScript(script *model.Script) (err error) {
  115. script.Mtime = time.Now()
  116. if err = d.DB.Exec("update script set is_async = ?, login = ?, keep_alive = ?, use_sign = ?, assertion = ?, conn_time_out = ?, resp_time_out = ?, "+
  117. "use_data_file = ?, file_name = ?, params_name = ?, delimiter = ?, fusing = ?, use_business_stop = ?, business_stop_percent = ? where id = ?",
  118. script.IsAsync, script.Login, script.KeepAlive, script.UseSign, script.Assertion, script.ConnTimeOut, script.RespTimeOut,
  119. script.UseDataFile, script.FileName, script.ParamsName, script.Delimiter, script.Fusing, script.UseBusinessStop, script.BusinessStopPercent, script.ID).Error; err != nil {
  120. return
  121. }
  122. if script.OutputParams == "[]" {
  123. script.OutputParams = "[{\"\":\"\"}]"
  124. }
  125. if script.APIHeader == "[]" {
  126. script.APIHeader = "[{\"\":\"\"}]"
  127. }
  128. if script.ArgumentString == "[]" {
  129. script.ArgumentString = "[{\"\":\"\"}]"
  130. }
  131. //script.Active = 1
  132. //return d.DB.Model(&model.Script{}).Save(script).Error
  133. return d.DB.Model(&model.Script{}).Updates(script).Error
  134. }
  135. //UpdateScriptPart update scriptPart
  136. func (d *Dao) UpdateScriptPart(script *model.Script) (err error) {
  137. script.Mtime = time.Now()
  138. if script.OutputParams == "[]" {
  139. script.OutputParams = "[{\"\":\"\"}]"
  140. }
  141. if script.APIHeader == "[]" {
  142. script.APIHeader = "[{\"\":\"\"}]"
  143. }
  144. if script.ArgumentString == "[]" {
  145. script.ArgumentString = "[{\"\":\"\"}]"
  146. }
  147. //script.Active = 1
  148. //return d.DB.Model(&model.Script{}).Save(script).Error
  149. return d.DB.Model(&model.Script{}).Updates(script).Error
  150. }
  151. //AddScriptTimer add script timer
  152. func (d *Dao) AddScriptTimer(script *model.Script) error {
  153. return d.DB.Exec("update script set const_timer = ?, random_timer = ? where id = ?", script.ConstTimer, script.RandomTimer, script.ID).Error
  154. }
  155. //DelScript delete script
  156. func (d *Dao) DelScript(id int) error {
  157. return d.DB.Model(&model.Script{}).Where("id = ?", id).Update("active", 0).Error
  158. }
  159. //QueryScriptSnap query script snap
  160. func (d *Dao) QueryScriptSnap(snap *model.ScriptSnap) (scriptSnap []*model.ScriptSnap, err error) {
  161. err = d.DB.Table(model.ScriptSnap{}.TableName()).Where(snap).Find(&scriptSnap).Error
  162. return
  163. }
  164. //ScriptHost query api domain name in script
  165. func (d *Dao) ScriptHost(script *model.Script) (host string, err error) {
  166. err = d.DB.Create(script).Error
  167. host = script.Domain
  168. return
  169. }
  170. ////UpdateRunOrder Update Run Order
  171. //func (d *Dao) UpdateRunOrder(runOrder int) error {
  172. // return d.DB.Model(&model.Script{}).Where("ID=?", script.ID).Updates(script).Error
  173. //}