excel_classify.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package excel
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. const (
  9. CustomAnalysisMenuShareId = 9000000 // 自定义分析目录ID-我共享的
  10. CustomAnalysisMenuSharedId = 8000000 // 自定义分析目录ID-收到共享
  11. )
  12. // ExcelClassify excel表格分类
  13. type ExcelClassify struct {
  14. ExcelClassifyId int `orm:"column(excel_classify_id);pk"`
  15. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  16. ExcelClassifyName string `description:"分类名称"`
  17. ParentId int `description:"父级id"`
  18. SysUserId int `description:"创建人id"`
  19. SysUserRealName string `description:"创建人姓名"`
  20. Level int `description:"层级"`
  21. UniqueCode string `description:"唯一编码"`
  22. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  23. IsDelete int `description:"排序字段,越小越靠前,默认值:10"`
  24. CreateTime time.Time `description:"创建时间"`
  25. ModifyTime time.Time `description:"修改时间"`
  26. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  27. }
  28. // AddExcelClassify 添加excel分类
  29. func AddExcelClassify(item *ExcelClassify) (lastId int64, err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. lastId, err = o.Insert(item)
  32. if err != nil {
  33. return
  34. }
  35. item.ExcelClassifyId = int(lastId)
  36. return
  37. }
  38. // GetExcelClassifyCount 获取同级分类下存在同名分类的数量
  39. func GetExcelClassifyCount(ExcelClassifyName string, parentId, source int) (count int, err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. sql := `SELECT COUNT(1) AS count FROM excel_classify WHERE parent_id=? AND source = ? AND excel_classify_name=? AND is_delete=0 `
  42. err = o.Raw(sql, parentId, source, ExcelClassifyName).QueryRow(&count)
  43. return
  44. }
  45. func GetExcelClassifyById(classifyId int) (item *ExcelClassify, err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. sql := `SELECT * FROM excel_classify WHERE excel_classify_id=? AND is_delete=0 `
  48. err = o.Raw(sql, classifyId).QueryRow(&item)
  49. return
  50. }
  51. // GetExcelClassifyByIdList
  52. // @Description: 根据分类id列表获取所有分类信息
  53. // @author: Roc
  54. // @datetime 2024-04-07 16:24:04
  55. // @param classifyIdList []int
  56. // @return items []*ExcelClassify
  57. // @return err error
  58. func GetExcelClassifyByIdList(classifyIdList []int) (items []*ExcelClassify, err error) {
  59. num := len(classifyIdList)
  60. if num <= 0 {
  61. return
  62. }
  63. o := orm.NewOrmUsingDB("data")
  64. sql := `SELECT * FROM excel_classify WHERE excel_classify_id in (` + utils.GetOrmInReplace(num) + `) AND is_delete=0 `
  65. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  66. return
  67. }
  68. // GetExcelClassifyBySourceAndIsJoinPermission
  69. // @Description: 根据分类id列表获取所有分类信息
  70. // @author: Roc
  71. // @datetime 2024-04-07 16:24:04
  72. // @param classifyIdList []int
  73. // @return items []*ExcelClassify
  74. // @return err error
  75. func GetExcelClassifyBySourceAndIsJoinPermission(source, isJoinPermission int) (items []*ExcelClassify, err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. sql := `SELECT * FROM excel_classify WHERE source = ? AND is_join_permission = ? `
  78. _, err = o.Raw(sql, source, isJoinPermission).QueryRows(&items)
  79. return
  80. }
  81. func GetChildClassifyById(classifyId int) (items []*ExcelClassify, err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := `SELECT * FROM excel_classify WHERE parent_id=? AND is_delete=0 `
  84. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  85. return
  86. }
  87. func GetExcelClassifyByParentId(parentId, source int) (items []*ExcelClassifyItems, err error) {
  88. o := orm.NewOrmUsingDB("data")
  89. sql := ` SELECT * FROM excel_classify WHERE parent_id=? AND source = ? AND is_delete=0 order by sort asc,excel_classify_id asc`
  90. _, err = o.Raw(sql, parentId, source).QueryRows(&items)
  91. return
  92. }
  93. func GetExcelClassifyBySource(source int) (items []*ExcelClassifyItems, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := ` SELECT * FROM excel_classify WHERE source = ? AND is_delete=0 order by sort asc,excel_classify_id asc`
  96. _, err = o.Raw(sql, source).QueryRows(&items)
  97. return
  98. }
  99. func GetExcelClassifyBySourceOrderByLevel(source int) (items []*ExcelClassifyItems, err error) {
  100. o := orm.NewOrmUsingDB("data")
  101. sql := ` SELECT * FROM excel_classify WHERE source = ? AND is_delete=0 order by level asc, sort asc,excel_classify_id asc`
  102. _, err = o.Raw(sql, source).QueryRows(&items)
  103. return
  104. }
  105. func GetExcelClassifyAll() (items []*ExcelClassifyItems, err error) {
  106. o := orm.NewOrmUsingDB("data")
  107. sql := ` SELECT * FROM excel_classify WHERE parent_id<>0 AND is_delete=0 order by sort asc,excel_classify_id asc`
  108. _, err = o.Raw(sql).QueryRows(&items)
  109. return
  110. }
  111. type ExcelClassifyItems struct {
  112. ExcelClassifyId int `description:"分类id"`
  113. ExcelInfoId int `description:"表格id"`
  114. ExcelClassifyName string
  115. ParentId int
  116. Level int `description:"层级"`
  117. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  118. UniqueCode string `description:"唯一编码"`
  119. SysUserId int `description:"创建人id"`
  120. SysUserRealName string `description:"创建人姓名"`
  121. StartDate string `description:"自定义开始日期"`
  122. Children []*ExcelClassifyItems
  123. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  124. HaveOperaAuth bool `description:"是否有数据权限"`
  125. HasShare bool `description:"是否已共享: true-是; false-否"`
  126. ShowShareBtn bool `description:"是否显示共享按钮: true-是; false-否"`
  127. }
  128. func GetExcelClassifyByCondition(condition string, pars []interface{}) (item *ExcelClassify, err error) {
  129. o := orm.NewOrmUsingDB("data")
  130. sql := ` SELECT * FROM excel_classify WHERE 1=1 AND is_delete=0 `
  131. if condition != "" {
  132. sql += condition
  133. }
  134. err = o.Raw(sql, pars).QueryRow(&item)
  135. return
  136. }
  137. // GetNextExcelClassifyByCondition 获取下一个分类
  138. func GetNextExcelClassifyByCondition(condition string, pars []interface{}) (item *ExcelClassify, err error) {
  139. o := orm.NewOrmUsingDB("data")
  140. sql := ` SELECT * FROM excel_classify WHERE 1=1 AND is_delete=0 `
  141. if condition != "" {
  142. sql += condition
  143. }
  144. sql += " ORDER BY sort asc , create_time ASC LIMIT 1 "
  145. err = o.Raw(sql, pars).QueryRow(&item)
  146. return
  147. }
  148. // GetFirstExcelClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  149. func GetFirstExcelClassifyByParentId(parentId int) (item *ExcelClassify, err error) {
  150. o := orm.NewOrmUsingDB("data")
  151. sql := ` SELECT * FROM excel_classify WHERE parent_id=? AND is_delete=0 order by sort asc,excel_classify_id asc limit 1`
  152. err = o.Raw(sql, parentId).QueryRow(&item)
  153. return
  154. }
  155. // UpdateExcelClassifySortByParentId 根据图表父类id更新排序
  156. func UpdateExcelClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, source int) (err error) {
  157. o := orm.NewOrmUsingDB("data")
  158. sql := ` update excel_classify set sort = ` + updateSort + ` WHERE parent_id=? and source=? and sort > ? AND is_delete=0 `
  159. if classifyId > 0 {
  160. sql += ` or ( excel_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  161. }
  162. _, err = o.Raw(sql, parentId, source, nowSort).Exec()
  163. return
  164. }
  165. // Update 更新图表分类基础信息
  166. func (ExcelClassify *ExcelClassify) Update(cols []string) (err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. _, err = o.Update(ExcelClassify, cols...)
  169. return
  170. }
  171. // GetExcelClassifyMaxSort 获取图表分类下最大的排序数
  172. func GetExcelClassifyMaxSort(parentId int, source int) (sort int, err error) {
  173. o := orm.NewOrmUsingDB("data")
  174. sql := `SELECT Max(sort) AS sort FROM excel_classify WHERE parent_id=? AND source = ? AND is_delete=0 `
  175. err = o.Raw(sql, parentId, source).QueryRow(&sort)
  176. return
  177. }
  178. type ExcelClassifyView struct {
  179. ExcelClassifyId int `orm:"column(excel_classify_id);pk"`
  180. ExcelClassifyName string `description:"分类名称"`
  181. ParentId int `description:"父级id"`
  182. }
  183. func GetExcelClassifyViewById(classifyId int) (item *ExcelClassifyView, err error) {
  184. o := orm.NewOrmUsingDB("data")
  185. sql := `SELECT * FROM excel_classify WHERE excel_classify_id=? AND is_delete=0 `
  186. err = o.Raw(sql, classifyId).QueryRow(&item)
  187. return
  188. }
  189. // ExcelClassifyItemBySort 自定义比较函数,按年龄从小到大排序
  190. func ExcelClassifyItemBySort(p1, p2 *ExcelClassifyItems) bool {
  191. return p1.Sort < p2.Sort
  192. }
  193. // GetClassifyByIdList
  194. // @Description: 根据分类ID列表获取分类列表
  195. // @author: Roc
  196. // @datetime 2024-04-02 19:40:30
  197. // @param classifyIdList []int
  198. // @return items []*ExcelClassify
  199. // @return err error
  200. func GetClassifyByIdList(classifyIdList []int) (items []*ExcelClassify, err error) {
  201. num := len(classifyIdList)
  202. if num <= 0 {
  203. return
  204. }
  205. o := orm.NewOrmUsingDB("data")
  206. sql := `SELECT * FROM excel_classify WHERE excel_classify_id in (` + utils.GetOrmInReplace(num) + `) AND is_delete=0 `
  207. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  208. return
  209. }
  210. func GetAdminExcelClassifyBySource(source, adminId int) (items []*ExcelClassifyItems, err error) {
  211. o := orm.NewOrmUsingDB("data")
  212. sql := `SELECT * FROM excel_classify WHERE source = ? AND sys_user_id = ? AND is_delete = 0 ORDER BY sort ASC, excel_classify_id ASC;`
  213. _, err = o.Raw(sql, source, adminId).QueryRows(&items)
  214. return
  215. }
  216. func GetExcelClassifyModelBySource(source int) (items []*ExcelClassify, err error) {
  217. o := orm.NewOrmUsingDB("data")
  218. sql := `SELECT * FROM excel_classify WHERE source = ? AND is_delete = 0 ORDER BY sort ASC, excel_classify_id ASC`
  219. _, err = o.Raw(sql, source).QueryRows(&items)
  220. return
  221. }
  222. func GetCustomAnalysisExcelClassifyCount(classifyName string, parentId, source, adminId int) (count int, err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := `SELECT COUNT(1) AS count FROM excel_classify WHERE parent_id = ? AND source = ? AND excel_classify_name = ? AND is_delete = 0 AND sys_user_id = ?`
  225. err = o.Raw(sql, parentId, source, classifyName, adminId).QueryRow(&count)
  226. return
  227. }