ai_summary_classify.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package ai_summary
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type AiSummaryClassify struct {
  9. AiSummaryClassifyId int `orm:"column(ai_summary_classify_id);pk"` // ai纪要分类id
  10. ClassifyName string // 分类名称
  11. ParentId int // 父级id
  12. CreateTime time.Time // 创建时间
  13. ModifyTime time.Time // 修改时间
  14. SysUserId int // 创建人id,使用指针表示可为空
  15. SysUserRealName string // 创建人姓名
  16. Level int // 层级
  17. Sort int // 排序字段,越小越靠前,默认值:10
  18. RootId int // 顶级ID
  19. HasData int `description:"是否含有指标数据"`
  20. }
  21. func AddAiSummaryClassify(item *AiSummaryClassify) (lastId int64, err error) {
  22. o := orm.NewOrm()
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. type AiSummaryClassifyItems struct {
  27. AiSummaryClassifyId int
  28. ClassifyName string `description:"分类名称"`
  29. ParentId int `description:"父级id"`
  30. HasData int `description:"是否含有指标数据"`
  31. CreateTime time.Time `description:"创建时间"`
  32. ModifyTime time.Time `description:"修改时间"`
  33. SysUserId int `description:"创建人id"`
  34. SysUserRealName string `description:"创建人姓名"`
  35. Level int `description:"层级"`
  36. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  37. AiSummaryId int `description:"纪要id"`
  38. UniqueCode string `description:"唯一编码"`
  39. Children []*AiSummaryClassifyItems
  40. }
  41. type AiSummaryClassifyListResp struct {
  42. AllNodes []*AiSummaryClassifyItems
  43. }
  44. // GetAiSummaryClassifyByParentId
  45. func GetAiSummaryClassifyByParentId(parentId int) (items []*AiSummaryClassifyItems, err error) {
  46. o := orm.NewOrm()
  47. sql := ` SELECT * FROM ai_summary_classify WHERE parent_id=? order by sort asc,ai_summary_classify_id asc`
  48. _, err = o.Raw(sql, parentId).QueryRows(&items)
  49. return
  50. }
  51. // GetAiSummaryInfoByAdminId 获取所有我创建的纪要,用于分类展示
  52. func GetAiSummaryInfoByAdminId(adminId int) (items []*AiSummaryClassifyItems, err error) {
  53. o := orm.NewOrm()
  54. sql := ` SELECT
  55. a.*,
  56. b.ai_summary_classify_id,
  57. b.classify_name
  58. FROM
  59. ai_summary AS a
  60. JOIN ai_summary_classify AS b ON a.classify_id = b.ai_summary_classify_id
  61. WHERE
  62. a.sys_user_id = ?
  63. ORDER BY
  64. a.sort ASC,
  65. a.create_time ASC `
  66. _, err = o.Raw(sql, adminId).QueryRows(&items)
  67. return
  68. }
  69. // GetAiSummaryClassifyAndInfoByParentId
  70. func GetAiSummaryClassifyAndInfoByParentId(parentId int) (items []*AiSummaryClassifyItems, err error) {
  71. o := orm.NewOrm()
  72. sql := ` SELECT
  73. 0 AS ai_summary_id,
  74. ai_summary_classify_id,
  75. classify_name,
  76. parent_id,
  77. create_time,
  78. modify_time,
  79. sys_user_id,
  80. sys_user_real_name AS sys_user_real_name,
  81. sort,
  82. level
  83. FROM
  84. ai_summary_classify
  85. WHERE
  86. parent_id = ? UNION ALL
  87. SELECT
  88. ai_summary_id,
  89. classify_id as ai_summary_classify_id,
  90. title AS classify_name,
  91. 0 AS parent_id,
  92. create_time,
  93. modify_time,
  94. sys_user_id,
  95. sys_user_real_name,
  96. sort,
  97. 0 AS level
  98. FROM
  99. ai_summary
  100. WHERE
  101. classify_id = ?
  102. ORDER BY
  103. sort ASC,
  104. ai_summary_classify_id ASC`
  105. _, err = o.Raw(sql, parentId, parentId).QueryRows(&items)
  106. return
  107. }
  108. type AddAiSummaryClassifyReq struct {
  109. ClassifyName string `description:"分类名称"`
  110. ParentId int `description:"父级id,第一级传0"`
  111. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  112. }
  113. func GetAiSummaryClassifyCount(classifyName string, parentId int) (count int, err error) {
  114. o := orm.NewOrm()
  115. sql := `SELECT COUNT(1) AS count FROM ai_summary_classify WHERE parent_id=? AND classify_name=? `
  116. err = o.Raw(sql, parentId, classifyName).QueryRow(&count)
  117. return
  118. }
  119. // GetAiSummaryClassifyMaxSort 获取ai纪要下最大的排序数
  120. func GetAiSummaryClassifyMaxSort(parentId int) (sort int, err error) {
  121. o := orm.NewOrm()
  122. sql := `SELECT Max(sort) AS sort FROM ai_summary_classify WHERE parent_id=? `
  123. err = o.Raw(sql, parentId).QueryRow(&sort)
  124. return
  125. }
  126. type EditAiSummaryClassifyReq struct {
  127. ClassifyName string `description:"分类名称"`
  128. AiSummaryClassifyId int `description:"分类id"`
  129. }
  130. func EditSandboxClassify(classifyId int, classifyName string) (err error) {
  131. o := orm.NewOrm()
  132. sql := `UPDATE ai_summary_classify SET classify_name=?, modify_time=NOW() WHERE ai_summary_classify_id=? `
  133. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  134. return
  135. }
  136. type AiSummaryClassifyDeleteCheckReq struct {
  137. AiSummaryClassifyId int `description:"分类id"`
  138. }
  139. func GetAiSummaryInfoCountByClassifyId(classifyId int) (count int, err error) {
  140. o := orm.NewOrm()
  141. sql := ` SELECT COUNT(1) AS count FROM ai_summary AS a
  142. WHERE a.classify_id IN(
  143. SELECT t.ai_summary_classify_id FROM
  144. (
  145. SELECT rd.*
  146. FROM (SELECT * FROM ai_summary_classify WHERE parent_id IS NOT NULL) rd,
  147. (SELECT @pid := ?) pd
  148. WHERE FIND_IN_SET(parent_id, @pid) > 0
  149. AND @pid := CONCAT(@pid, ',', ai_summary_classify_id)
  150. UNION SELECT * FROM ai_summary_classify WHERE ai_summary_classify_id = @pid
  151. )AS t
  152. ) `
  153. err = o.Raw(sql, classifyId).QueryRow(&count)
  154. return
  155. }
  156. type DeleteAiSummaryClassifyReq struct {
  157. AiSummaryClassifyId int `description:"分类id"`
  158. AiSummaryId int `description:"纪要id"`
  159. }
  160. func DeleteAiSummaryClassify(classifyId int) (err error) {
  161. o := orm.NewOrm()
  162. sql := ` DELETE FROM ai_summary_classify
  163. WHERE ai_summary_classify_id IN(
  164. SELECT t.ai_summary_classify_id FROM
  165. (
  166. SELECT rd.*
  167. FROM (SELECT * FROM ai_summary_classify WHERE parent_id IS NOT NULL) rd,
  168. (SELECT @pid := ?) pd
  169. WHERE FIND_IN_SET(parent_id, @pid) > 0
  170. AND @pid := CONCAT(@pid, ',', ai_summary_classify_id)
  171. UNION SELECT * FROM ai_summary_classify WHERE ai_summary_classify_id = @pid
  172. )AS t
  173. ) `
  174. _, err = o.Raw(sql, classifyId).Exec()
  175. return
  176. }
  177. // MoveAiSummaryClassifyReq 移动纪要分类请求参数
  178. type MoveAiSummaryClassifyReq struct {
  179. AiSummaryClassifyId int `description:"分类id"`
  180. AiSummaryId int `description:"纪要ID"`
  181. ParentClassifyId int `description:"父级分类id 移动纪要时为目标分类id"`
  182. PrevId int `description:"上一个兄弟节点分类id"`
  183. NextId int `description:"下一个兄弟节点分类id"`
  184. PrevType int `description:"上一个兄弟节点类型 1分类 2纪要 "`
  185. NextType int `description:"上一个兄弟节点类型 1分类 2纪要 "`
  186. }
  187. func GetAiSummaryClassifyById(classifyId int) (item *AiSummaryClassify, err error) {
  188. o := orm.NewOrm()
  189. sql := `SELECT * FROM ai_summary_classify WHERE ai_summary_classify_id=? `
  190. err = o.Raw(sql, classifyId).QueryRow(&item)
  191. return
  192. }
  193. func GetAiSummaryClassifyCountById(classifyId int) (count int, err error) {
  194. o := orm.NewOrm()
  195. sql := `SELECT count(1) AS count FROM ai_summary_classify WHERE ai_summary_classify_id=? `
  196. err = o.Raw(sql, classifyId).QueryRow(&count)
  197. return
  198. }
  199. // Update 更新沙盘分类基础信息
  200. func (aiSummaryClassify *AiSummaryClassify) Update(cols []string) (err error) {
  201. o := orm.NewOrm()
  202. _, err = o.Update(aiSummaryClassify, cols...)
  203. return
  204. }
  205. // GetFirstAiSummaryClassifyByParentId 获取当前父级沙盘分类下的排序第一条的数据
  206. func GetFirstAiSummaryClassifyByParentId(parentId int) (item *AiSummaryClassify, err error) {
  207. o := orm.NewOrm()
  208. sql := ` SELECT * FROM ai_summary_classify WHERE parent_id=? order by sort asc,ai_summary_classify_id asc limit 1`
  209. err = o.Raw(sql, parentId).QueryRow(&item)
  210. return
  211. }
  212. // UpdateSandboxClassifySortByParentId 根据沙盘父类id更新排序
  213. func UpdateAiSummaryClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  214. o := orm.NewOrm()
  215. sql := ` update ai_summary_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  216. if classifyId > 0 {
  217. sql += ` or ( ai_summary_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  218. }
  219. _, err = o.Raw(sql, parentId, nowSort).Exec()
  220. return
  221. }
  222. // 获取所有子级分类id
  223. func GetAiSummaryClassifySubcategories(classifyId int) (Ids string, err error) {
  224. o := orm.NewOrm()
  225. sql := `SELECT GROUP_CONCAT(ai_summary_classify_id) AS ids
  226. FROM (
  227. SELECT @pv := ? AS ai_summary_classify_id
  228. UNION ALL
  229. SELECT sc.ai_summary_classify_id
  230. FROM ai_summary_classify sc
  231. JOIN (SELECT @pv := ?) initial
  232. WHERE sc.parent_id = @pv
  233. ) subcategories; `
  234. err = o.Raw(sql, classifyId, classifyId).QueryRow(&Ids)
  235. return
  236. }
  237. func GetAiSummaryClassify(aiSummaryClassify int) (aiSummaryClassifyIds string, err error) {
  238. o := orm.NewOrm()
  239. sql := `SELECT GROUP_CONCAT(t.ai_summary_classify_id) AS ai_summary_classify_id FROM (
  240. SELECT a.ai_summary_classify_id FROM ai_summary_classify AS a
  241. WHERE a.ai_summary_classify_id=?
  242. UNION ALL
  243. SELECT a.ai_summary_classify_id FROM ai_summary_classify AS a
  244. WHERE a.parent_id=? UNION ALL
  245. SELECT
  246. ai_summary_classify_id
  247. FROM
  248. ai_summary_classify
  249. WHERE
  250. parent_id IN ( SELECT ai_summary_classify_id FROM ai_summary_classify WHERE parent_id = ? )
  251. )AS t`
  252. err = o.Raw(sql, aiSummaryClassify, aiSummaryClassify, aiSummaryClassify).QueryRow(&aiSummaryClassifyIds)
  253. return
  254. }
  255. func GetAiSummaryAllParentByClassifyId(aiSummaryClassifyId int) (ids string, err error) {
  256. o := orm.NewOrm()
  257. sql := `SELECT
  258. GROUP_CONCAT(DISTINCT m.ai_summary_classify_id ORDER BY m.level) AS ids
  259. FROM
  260. (
  261. SELECT
  262. @id AS _id,(
  263. SELECT
  264. @id := parent_id
  265. FROM
  266. ai_summary_classify
  267. WHERE
  268. ai_summary_classify_id = _id
  269. )
  270. FROM
  271. (
  272. SELECT
  273. @id :=(
  274. SELECT
  275. parent_id
  276. FROM
  277. ai_summary_classify
  278. WHERE
  279. ai_summary_classify_id = ?
  280. )) vm,
  281. ai_summary_classify m
  282. WHERE
  283. @id IS NOT NULL
  284. ) vm
  285. INNER JOIN ai_summary_classify m
  286. WHERE
  287. ai_summary_classify_id = vm._id `
  288. err = o.Raw(sql, aiSummaryClassifyId).QueryRow(&ids)
  289. return
  290. }
  291. type AiSummaryListResp struct {
  292. Paging *paging.PagingItem
  293. List []*AiSummaryItems
  294. }
  295. // GetAiSummaryClassifyAll
  296. func GetAiSummaryClassifyAll() (items []*AiSummaryClassifyItems, err error) {
  297. o := orm.NewOrm()
  298. sql := ` SELECT * FROM ai_summary_classify WHERE parent_id<>0 order by sort asc,ai_summary_classify_id asc`
  299. _, err = o.Raw(sql).QueryRows(&items)
  300. return
  301. }