scene.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package dao
  2. import (
  3. "strconv"
  4. "strings"
  5. "go-common/app/admin/ep/melloi/conf"
  6. "go-common/app/admin/ep/melloi/model"
  7. )
  8. // AddScene Add Scene
  9. func (d *Dao) AddScene(scene *model.Scene) (sceneId int, err error) {
  10. scene.IsActive = true
  11. err = d.DB.Table("scene").Create(scene).Error
  12. sceneId = scene.ID
  13. return
  14. }
  15. // QueryDraft Query Draft
  16. func (d *Dao) QueryDraft(scene *model.Scene) (res *model.QueryDraft, err error) {
  17. res = &model.QueryDraft{}
  18. err = d.DB.Table("scene").Select("id as scene_id, scene_name").Where(scene).Where("is_draft = 1 and is_active = 1").Count(&res.Total).Order("mtime desc").Find(&res.Drafts).Error
  19. return
  20. }
  21. // UpdateScene Update Scene
  22. func (d *Dao) UpdateScene(scene *model.Scene, scriptIDList []int) (fusing int, err error) {
  23. if err = d.DB.Exec("update scene set fusing = ? where id = ?", scene.Fusing, scene.ID).Error; err != nil {
  24. return
  25. }
  26. if err = d.DB.Model(&model.Scene{}).Where("ID = ?", scene.ID).Update(scene).Error; err != nil {
  27. return
  28. }
  29. if scene.IsBatch { //选择已有接口点击确定时执行
  30. if scene.Fusing == 0 {
  31. err = d.DB.Model(&model.Script{}).Where("scene_id = ? and id in (?)", scene.ID, scriptIDList).Update("fusing", conf.Conf.Melloi.DefaultFusing).Error
  32. } else {
  33. err = d.DB.Model(&model.Script{}).Where("scene_id = ? and id in (?)", scene.ID, scriptIDList).Update("fusing", scene.Fusing).Error
  34. }
  35. } else if scene.IsUpdate { //
  36. err = d.DB.Model(&model.Script{}).Where("scene_id = ?", scene.ID).Update("fusing", scene.Fusing).Error
  37. }
  38. fusing = scene.Fusing
  39. return
  40. }
  41. // SaveScene Save Scene
  42. func (d *Dao) SaveScene(scene *model.Scene) error {
  43. return d.DB.Model(&model.Scene{}).Where("ID = ?", scene.ID).Update("is_draft", 0).Update("scene_type", scene.SceneType).Error
  44. }
  45. // SaveOrder Save Order
  46. func (d *Dao) SaveOrder(reqList []*model.GroupOrder, scene *model.Scene) (err error) {
  47. for _, req := range reqList {
  48. if scene.SceneType == 1 {
  49. err = d.DB.Model(&model.Script{}).Where("ID = ?", req.ID).Update("group_id", req.GroupID).Error
  50. } else {
  51. err = d.DB.Model(&model.Script{}).Where("ID = ?", req.ID).Update("run_order", req.RunOrder).Error
  52. }
  53. }
  54. return
  55. }
  56. // QueryGroupId Query GroupId
  57. func (d *Dao) QueryGroupId(script *model.Script) (res *model.QueryRelation, groupId int, err error) {
  58. res = &model.QueryRelation{}
  59. //select group_id, count(group_id) as count from script where scene_id = 1 and group_id in (select group_id from script where id = 777) group by group_id
  60. err = d.DB.Table("script").Select("group_id").Where("id = ?", script.ID).Find(&res.RelationList).Error
  61. groupId = res.RelationList[0].GroupID
  62. return
  63. }
  64. // QueryRelation Query Relation
  65. func (d *Dao) QueryRelation(groupId int, script *model.Script) (res *model.QueryRelation, err error) {
  66. res = &model.QueryRelation{}
  67. err = d.DB.Table("script").Select("group_id, count(group_id) as count").Where("scene_id = ? and group_id = ?", script.SceneID, groupId).Find(&res.RelationList).Error
  68. return
  69. }
  70. // QueryAPI Query API
  71. func (d *Dao) QueryAPI(scene *model.Scene) (res *model.QueryAPIs, err error) {
  72. res = &model.QueryAPIs{}
  73. if err = d.DB.Table(model.Script{}.TableName()).Select("id").Where("scene_id = ? and active = 1", scene.ID).Count(&res.Total).Error; err != nil {
  74. return
  75. }
  76. if res.Total > 0 {
  77. //select scene.scene_name, scene.scene_type, scene.department, scene.project, scene.app, script.group_id, script.run_order, script.id as test_id, script.test_name
  78. //from scene INNER JOIN script on scene.id = script.scene_id and script.active = 1 where script.scene_id = 1
  79. gDB := d.DB.Table(model.Scene{}.TableName()).Select("scene.id as scene_id, scene.scene_name, scene.scene_type, scene.department, " +
  80. "scene.project, scene.app, script.group_id, script.run_order, script.id, script.test_name, script.url, script.output_params, script.threads_sum, script.load_time").
  81. Joins("inner join script on scene.id = script.scene_id and script.active = 1")
  82. if scene.SceneType == 2 {
  83. err = gDB.Where(scene).Count(&res.Total).Order("group_id, run_order").Find(&res).Find(&res.APIs).Error
  84. res.SceneType = 2
  85. } else {
  86. err = gDB.Where(scene).Count(&res.Total).Order("script.id").Find(&res).Find(&res.APIs).Error
  87. res.SceneType = 1
  88. }
  89. } else {
  90. gDB := d.DB.Table(model.Scene{}.TableName()).Select("id as scene_id, scene_name, department, project, app")
  91. err = gDB.Where("id = ?", scene.ID).Find(&res).Error
  92. //查询无结果时,APIs返回空数组
  93. res.APIs = []*model.TestAPI{}
  94. }
  95. return
  96. }
  97. // DeleteAPI Delete API
  98. func (d *Dao) DeleteAPI(script *model.Script) error {
  99. return d.DB.Model(&model.Script{}).Where("ID = ?", script.ID).Update("active", 0).Error
  100. }
  101. // AddConfig Add Config
  102. func (d *Dao) AddConfig(script *model.Script) error {
  103. return d.DB.Exec("update script set threads_sum = ?, load_time = ?, ready_time = ? where scene_id = ? and group_id = ?", script.ThreadsSum, script.LoadTime, script.ReadyTime, script.SceneID, script.GroupID).Error
  104. }
  105. // QueryTree Query Tree
  106. func (d *Dao) QueryTree(script *model.Script) (res *model.ShowTree, err error) {
  107. res = &model.ShowTree{}
  108. err = d.DB.Table(model.Script{}.TableName()).Select("department, project, app").Where(script).Count(&res.IsShow).Order("id").Limit(1).Find(&res.Tree).Error
  109. if res.IsShow > 1 {
  110. res.IsShow = 1
  111. }
  112. return
  113. }
  114. //QueryScenesByPage query scripts by page
  115. func (d *Dao) QueryScenesByPage(scene *model.Scene, pn, ps int32, treeNodes []string) (qsr *model.QuerySceneResponse, err error) {
  116. qsr = &model.QuerySceneResponse{}
  117. gDB := d.DB.Table(model.Scene{}.TableName()).Where("app in (?)", treeNodes).Where("is_draft = 0")
  118. if scene.Department != "" && scene.Project != "" && scene.APP != "" {
  119. gDB = gDB.Where("department = ? and project = ? and app = ?", scene.Department, scene.Project, scene.APP)
  120. }
  121. if scene.Department != "" && scene.Project != "" {
  122. gDB = gDB.Where("department = ? and project = ?", scene.Department, scene.Project)
  123. }
  124. if scene.Department != "" {
  125. gDB = gDB.Where("department = ?", scene.Department)
  126. }
  127. if scene.SceneName != "" {
  128. gDB = gDB.Where("scene_name LIKE ? ", "%"+scene.SceneName+"%")
  129. }
  130. if scene.UserName != "" {
  131. gDB = gDB.Where("user_name LIKE ? ", "%"+scene.UserName+"%")
  132. }
  133. if scene.SceneName == "" && scene.UserName == "" && scene.Department == "" && scene.Project == "" && scene.APP == "" {
  134. gDB = gDB.Where(scene)
  135. }
  136. err = gDB.Where("is_active = 1").Count(&qsr.TotalSize).Order("ctime desc").Offset((pn - 1) * ps).Limit(ps).
  137. Find(&qsr.Scenes).Error
  138. qsr.PageSize = ps /**/
  139. qsr.PageNum = pn
  140. return
  141. }
  142. //QueryScenesByPageWhiteName query scripts by page white name
  143. func (d *Dao) QueryScenesByPageWhiteName(scene *model.Scene, pn, ps int32) (qsr *model.QuerySceneResponse, err error) {
  144. qsr = &model.QuerySceneResponse{}
  145. gDB := d.DB.Table(model.Scene{}.TableName()).Where("is_draft = 0")
  146. if scene.SceneName != "" {
  147. gDB = gDB.Where("scene_name LIKE ? ", "%"+scene.SceneName+"%")
  148. }
  149. if scene.UserName != "" {
  150. gDB = gDB.Where("user_name LIKE ? ", "%"+scene.UserName+"%")
  151. }
  152. if scene.SceneName == "" && scene.UserName == "" {
  153. gDB = gDB.Where(scene)
  154. }
  155. err = gDB.Where("is_active = 1").Count(&qsr.TotalSize).Order("mtime desc").Offset((pn - 1) * ps).Limit(ps).
  156. Find(&qsr.Scenes).Error
  157. qsr.PageSize = ps /**/
  158. qsr.PageNum = pn
  159. return
  160. }
  161. //QueryScenes query scene
  162. func (d *Dao) QueryScenes(scene *model.Scene, pn int, ps int) (scenes []*model.Scene, err error) {
  163. err = d.DB.Table(model.Scene{}.TableName()).Where(scene).Order("mtime desc").Offset((pn - 1) * ps).Limit(ps).Find(&scenes).Error
  164. return
  165. }
  166. // QueryExistAPI Query Exist API
  167. func (d *Dao) QueryExistAPI(script *model.Script, pageNum int32, pageSize int32, sceneId int, treeNodes []string) (res *model.APIInfoList, err error) {
  168. res = &model.APIInfoList{}
  169. gDB := d.DB.Table(model.Script{}.TableName()).Where("app in (?)", treeNodes)
  170. if pageSize == 0 && pageNum == 0 {
  171. err = gDB.Where(script).Where("active = 1 and scene_id = 0").Count(&res.TotalSize).Order("id desc").Find(&res.ScriptList).Error
  172. } else {
  173. err = gDB.Where(script).Where("active = 1 and scene_id = 0").Count(&res.TotalSize).Order("id desc").Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&res.ScriptList).Error
  174. res.PageNum = pageNum
  175. res.PageSize = pageSize
  176. }
  177. res.SceneID = sceneId
  178. return
  179. }
  180. // QueryGroup Query Group
  181. func (d *Dao) QueryGroup(sceneId int) (res *model.GroupList, err error) {
  182. res = &model.GroupList{}
  183. gDB := d.DB.Table(model.Script{}.TableName())
  184. //select group_id, count(group_id) from script where scene_id = 1 GROUP BY group_id order by group_id
  185. err = gDB.Select("group_id, threads_sum, load_time, ready_time").Where("scene_id = ? and active = 1", sceneId).Group("group_id").Order("group_id").Find(&res.GroupList).Error
  186. return
  187. }
  188. // QueryPreview Query Preview
  189. func (d *Dao) QueryPreview(sceneId, groupId int) (res *model.PreviewList, err error) {
  190. res = &model.PreviewList{}
  191. gDB := d.DB.Table(model.Script{}.TableName())
  192. //select id as test_id, test_name, run_order from script where scene_id = 1 and group_id = 1 order by run_order
  193. err = gDB.Select("id, test_name, run_order, group_id, const_timer, random_timer").Where("scene_id = ? and group_id = ? and active = 1", sceneId, groupId).Order("run_order").Find(&res.PreList).Error
  194. return
  195. }
  196. // QueryUsefulParams Query Useful Params
  197. func (d *Dao) QueryUsefulParams(sceneId int) (res *model.UsefulParamsList, err error) {
  198. res = &model.UsefulParamsList{}
  199. gDB := d.DB.Table(model.Script{}.TableName())
  200. err = gDB.Select("output_params").Where("scene_id = ?", sceneId).Where("active = 1 and output_params != '[{\"\":\"\"}]'").Order("id desc").Find(&res.ParamsList).Error
  201. return
  202. }
  203. // UpdateBindScene Update Bind Scene
  204. func (d *Dao) UpdateBindScene(bindScene *model.BindScene) (err error) {
  205. var id int
  206. tempList := strings.Split(bindScene.ID, ",")
  207. for _, tempId := range tempList {
  208. if id, err = strconv.Atoi(tempId); err != nil {
  209. return err
  210. }
  211. err = d.DB.Model(&model.Script{}).Where("id =?", id).Updates(model.Script{SceneID: bindScene.SceneID}).Error
  212. }
  213. return
  214. }
  215. // QueryDrawRelation Query Draw Relation
  216. func (d *Dao) QueryDrawRelation(scene *model.Scene) (res *model.SaveOrderReq, err error) {
  217. res = &model.SaveOrderReq{}
  218. gDB := d.DB.Table(model.Script{}.TableName())
  219. err = gDB.Select("id, test_name, group_id, run_order").Where("scene_id = ? and active = 1", scene.ID).Order("group_id, run_order").Find(&res.GroupOrderList).Error
  220. return
  221. }
  222. // DeleteDraft Delete Draft
  223. func (d *Dao) DeleteDraft(scene *model.Scene) error {
  224. if scene.ID == 0 {
  225. return d.DB.Model(&model.Scene{}).Where("user_name = ? and is_draft = 1", scene.UserName).Update("is_active", 0).Error
  226. }
  227. return d.DB.Model(&model.Scene{}).Where("user_name = ? and id = ? and is_draft = 1", scene.UserName, scene.ID).Update("is_active", 0).Error
  228. }
  229. // QueryConfig Query Config
  230. func (d *Dao) QueryConfig(script *model.Script) (res *model.GroupInfo, err error) {
  231. res = &model.GroupInfo{}
  232. gDB := d.DB.Table(model.Script{}.TableName())
  233. // select distinct group_id, threads_sum, ready_time, load_time from script where scene_id = 282 and active = 1 and group_id = 2
  234. err = gDB.Select("distinct group_id, threads_sum, ready_time, load_time").Where("scene_id = ? and group_id = ? and active = 1", script.SceneID, script.GroupID).Find(&res).Error
  235. return
  236. }
  237. // DeleteScene Delete Scene
  238. func (d *Dao) DeleteScene(scene *model.Scene) error {
  239. return d.DB.Model(&model.Scene{}).Where("id = ?", scene.ID).Update("is_active", 0).Error
  240. }
  241. // QueryFusing Query Fusing
  242. func (d *Dao) QueryFusing(script *model.Script) (res *model.FusingInfoList, err error) {
  243. res = &model.FusingInfoList{}
  244. gDB := d.DB.Table(model.Script{}.TableName())
  245. //select id as test_id, test_name, run_order from script where scene_id = 1 and group_id = 1 order by run_order
  246. err = gDB.Select("fusing").Where("scene_id = ? and active = 1", script.SceneID).Find(&res.FusingList).Error
  247. return
  248. }