ai_summary_classify.go 10 KB

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