base_from_radish_research_classify.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromRadishResearchClassify 萝卜投研原始数据分类表
  10. type BaseFromRadishResearchClassify struct {
  11. BaseFromRadishResearchClassifyId int `orm:"column(base_from_radish_research_classify_id);pk" gorm:"primaryKey"`
  12. ClassifyName string `description:"分类名称"`
  13. ParentId int `description:"父级id"`
  14. Level int `description:"层级"`
  15. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  16. SysUserId int `description:"创建人id"`
  17. SysUserRealName string `description:"创建人姓名"`
  18. LevelPath string `description:"层级路径"`
  19. RootId int `description:"顶级分类ID"`
  20. UniqueCode string `description:"唯一编码"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. CreateTime time.Time `description:"创建时间"`
  23. }
  24. func (m *BaseFromRadishResearchClassify) TableName() string {
  25. return "base_from_radish_research_classify"
  26. }
  27. type BaseFromRadishResearchClassifyCols struct {
  28. PrimaryId string
  29. ClassifyName string
  30. ParentId string
  31. SysUserId string
  32. SysUserRealName string
  33. Level string
  34. Sort string
  35. RootId string
  36. LevelPath string
  37. UniqueCode string
  38. CreateTime string
  39. ModifyTime string
  40. }
  41. func (m *BaseFromRadishResearchClassify) Cols() BaseFromRadishResearchClassifyCols {
  42. return BaseFromRadishResearchClassifyCols{
  43. PrimaryId: "base_from_radish_research_classify_id",
  44. ClassifyName: "classify_name",
  45. ParentId: "parent_id",
  46. SysUserId: "sys_user_id",
  47. SysUserRealName: "sys_user_real_name",
  48. Level: "level",
  49. Sort: "sort",
  50. RootId: "root_id",
  51. LevelPath: "level_path",
  52. UniqueCode: "unique_code",
  53. CreateTime: "create_time",
  54. ModifyTime: "modify_time",
  55. }
  56. }
  57. func (m *BaseFromRadishResearchClassify) Create() (err error) {
  58. o := global.DbMap[utils.DbNameIndex]
  59. err = o.Create(m).Error
  60. return
  61. }
  62. func (m *BaseFromRadishResearchClassify) CreateMulti(items []*BaseFromRadishResearchClassify) (err error) {
  63. if len(items) == 0 {
  64. return
  65. }
  66. o := global.DbMap[utils.DbNameIndex]
  67. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  68. return
  69. }
  70. func (m *BaseFromRadishResearchClassify) Update(cols []string) (err error) {
  71. o := global.DbMap[utils.DbNameIndex]
  72. err = o.Select(cols).Updates(m).Error
  73. return
  74. }
  75. func (m *BaseFromRadishResearchClassify) Remove() (err error) {
  76. o := global.DbMap[utils.DbNameIndex]
  77. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  78. err = o.Exec(sql, m.BaseFromRadishResearchClassifyId).Error
  79. return
  80. }
  81. func (m *BaseFromRadishResearchClassify) MultiRemove(ids []int) (err error) {
  82. if len(ids) == 0 {
  83. return
  84. }
  85. o := global.DbMap[utils.DbNameIndex]
  86. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  87. err = o.Exec(sql, ids).Error
  88. return
  89. }
  90. func (m *BaseFromRadishResearchClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
  91. if condition == "" {
  92. return
  93. }
  94. o := global.DbMap[utils.DbNameIndex]
  95. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  96. err = o.Exec(sql, pars...).Error
  97. return
  98. }
  99. func (m *BaseFromRadishResearchClassify) GetItemById(id int) (item *BaseFromRadishResearchClassify, err error) {
  100. o := global.DbMap[utils.DbNameIndex]
  101. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  102. err = o.Raw(sql, id).First(&item).Error
  103. return
  104. }
  105. func (m *BaseFromRadishResearchClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromRadishResearchClassify, err error) {
  106. o := global.DbMap[utils.DbNameIndex]
  107. order := ``
  108. if orderRule != "" {
  109. order = ` ORDER BY ` + orderRule
  110. }
  111. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  112. err = o.Raw(sql, pars...).First(&item).Error
  113. return
  114. }
  115. func (m *BaseFromRadishResearchClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  116. o := global.DbMap[utils.DbNameIndex]
  117. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  118. err = o.Raw(sql, pars...).Scan(&count).Error
  119. return
  120. }
  121. func (m *BaseFromRadishResearchClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromRadishResearchClassify, err error) {
  122. o := global.DbMap[utils.DbNameIndex]
  123. fields := strings.Join(fieldArr, ",")
  124. if len(fieldArr) == 0 {
  125. fields = `*`
  126. }
  127. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  128. if orderRule != "" {
  129. order = ` ORDER BY ` + orderRule
  130. }
  131. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  132. err = o.Raw(sql, pars...).Find(&items).Error
  133. return
  134. }
  135. func (m *BaseFromRadishResearchClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromRadishResearchClassify, err error) {
  136. o := global.DbMap[utils.DbNameIndex]
  137. fields := strings.Join(fieldArr, ",")
  138. if len(fieldArr) == 0 {
  139. fields = `*`
  140. }
  141. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  142. if orderRule != "" {
  143. order = ` ORDER BY ` + orderRule
  144. }
  145. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  146. pars = append(pars, startSize, pageSize)
  147. err = o.Raw(sql, pars...).Find(&items).Error
  148. return
  149. }
  150. // GetBaseFromRadishResearchClassify 获取所有分类
  151. func GetBaseFromRadishResearchClassify() (items []*BaseFromRadishResearchClassify, err error) {
  152. sql := ` SELECT * FROM base_from_radish_research_classify ORDER BY parent_id ASC, sort ASC, base_from_radish_research_classify_id ASC`
  153. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  154. return
  155. }
  156. // BaseFromRadishResearchClassifyItem 萝卜投研数据分类信息
  157. type BaseFromRadishResearchClassifyItem struct {
  158. ClassifyId int `description:"分类ID"`
  159. ClassifyName string `description:"分类名称"`
  160. ParentId int `description:"父级id"`
  161. Level int `description:"层级"`
  162. Sort int `description:"排序字段"`
  163. LevelPath string `description:"层级路径"`
  164. UniqueCode string `description:"唯一编码"`
  165. CreateTime string `description:"创建时间"`
  166. ModifyTime string `description:"修改时间"`
  167. Children []*BaseFromRadishResearchClassifyItem `description:"子分类"`
  168. }
  169. func (m *BaseFromRadishResearchClassify) Format2Item() (item *BaseFromRadishResearchClassifyItem) {
  170. item = new(BaseFromRadishResearchClassifyItem)
  171. item.ClassifyId = m.BaseFromRadishResearchClassifyId
  172. item.ClassifyName = m.ClassifyName
  173. item.ParentId = m.ParentId
  174. item.Level = m.Level
  175. item.Sort = m.Sort
  176. item.LevelPath = m.LevelPath
  177. item.UniqueCode = m.UniqueCode
  178. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  179. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  180. return
  181. }
  182. type RadishResearchClassifyAddReq struct {
  183. ClassifyName string `description:"分类名称"`
  184. ParentId int `description:"父级ID, 第一级传0"`
  185. Level int `description:"层级, 第一级传0, 其余传上一级的层级"`
  186. }
  187. type RadishResearchClassifyEditReq struct {
  188. ClassifyId int `description:"分类ID"`
  189. ClassifyName string `description:"分类名称"`
  190. }
  191. func (m *BaseFromRadishResearchClassify) GetSortMax(parentId int) (sort int, err error) {
  192. o := global.DbMap[utils.DbNameIndex]
  193. sql := fmt.Sprintf(`SELECT COALESCE(Max(%s), 0) AS sort FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
  194. err = o.Raw(sql, parentId).Scan(&sort).Error
  195. return
  196. }
  197. type RadishResearchClassifyRemoveReq struct {
  198. ClassifyId int `description:"分类ID"`
  199. }
  200. type BaseFromRadishResearchClassifyListItem struct {
  201. NodeType int `description:"节点类型: 0-分类; 1-指标"`
  202. ClassifyId int `description:"分类ID"`
  203. ClassifyName string `description:"分类名称"`
  204. IndexId int `description:"指标ID"`
  205. IndexCode string `description:"指标编码"`
  206. IndexName string `description:"指标名称"`
  207. ParentId int `description:"父级ID"`
  208. Level int `description:"层级"`
  209. Sort int `description:"排序"`
  210. UniqueCode string `description:"唯一编码, 指标的话用indexCode"`
  211. Children []*BaseFromRadishResearchClassifyListItem `description:"子分类"`
  212. }
  213. type BaseFromRadishResearchClassifyMoveReq struct {
  214. ClassifyId int `description:"分类ID"`
  215. ParentClassifyId int `description:"父级分类ID"`
  216. PrevClassifyId int `description:"上一个兄弟节点分类ID"`
  217. NextClassifyId int `description:"下一个兄弟节点分类ID"`
  218. ItemId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  219. PrevItemId int `description:"上一个指标ID"`
  220. NextItemId int `description:"下一个指标ID"`
  221. }
  222. func GetRadishResearchClassifyById(classifyId int) (item *BaseFromRadishResearchClassify, err error) {
  223. o := global.DbMap[utils.DbNameIndex]
  224. sql := `SELECT * FROM base_from_radish_research_classify WHERE base_from_radish_research_classify_id = ?`
  225. err = o.Raw(sql, classifyId).First(&item).Error
  226. return
  227. }
  228. func GetRadishResearchClassifyByRootIdLevel(rootId int, orderStr string) (items []*BaseFromRadishResearchClassify, err error) {
  229. o := global.DbMap[utils.DbNameIndex]
  230. sql := ` SELECT * FROM base_from_radish_research_classify WHERE root_id = ? `
  231. if orderStr != "" {
  232. sql += orderStr
  233. } else {
  234. sql += ` order by level desc, sort asc, base_from_radish_research_classify_id asc`
  235. }
  236. err = o.Raw(sql, rootId).Find(&items).Error
  237. return
  238. }
  239. // UpdateRadishResearchClassifySortByParentId 根据父类id更新排序
  240. func UpdateRadishResearchClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  241. o := global.DbMap[utils.DbNameIndex]
  242. sql := ` update base_from_radish_research_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? `
  243. if classifyId > 0 {
  244. sql += ` or ( base_from_radish_research_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  245. }
  246. err = o.Exec(sql, parentId, nowSort).Error
  247. return
  248. }
  249. // GetFirstRadishResearchClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  250. func GetFirstRadishResearchClassifyByParentId(parentId int) (item *BaseFromRadishResearchClassify, err error) {
  251. o := global.DbMap[utils.DbNameIndex]
  252. sql := ` SELECT * FROM base_from_radish_research_classify WHERE parent_id = ? ORDER BY sort ASC,base_from_radish_research_classify_id ASC LIMIT 1`
  253. err = o.Raw(sql, parentId).First(&item).Error
  254. return
  255. }
  256. func UpdateRadishResearchClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  257. o := global.DbMap[utils.DbNameIndex]
  258. var pars []interface{}
  259. pars = append(pars, rootId, levelStep)
  260. pars = append(pars, classifyIds)
  261. // 更新相关联的二级分类的parentId,和classify_name_second
  262. sql := `update base_from_radish_research_classify SET root_id = ?, level = level+? where base_from_radish_research_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  263. err = o.Exec(sql, pars...).Error
  264. return
  265. }
  266. // UpdateLevelPath 更新层级路径
  267. func (m *BaseFromRadishResearchClassify) UpdateLevelPath(classifyId int, levelPath string) (err error) {
  268. sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().LevelPath, m.Cols().PrimaryId)
  269. err = global.DbMap[utils.DbNameIndex].Exec(sql, levelPath, classifyId).Error
  270. return
  271. }
  272. // GetRadishResearchClassifyByParentIdAndName 根据父级ID和名称获取分类
  273. func GetRadishResearchClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbClassify, err error) {
  274. sql := `SELECT * FROM base_from_radish_research_classify WHERE parent_id = ? AND classify_name = ? AND base_from_radish_research_classify_id <> ? LIMIT 1`
  275. err = global.DbMap[utils.DbNameIndex].Raw(sql, parentId, classifyName, classifyId).First(&item).Error
  276. return
  277. }