mysteel_chemical_classify.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  7. )
  8. // BaseFromMysteelChemicalClassify 钢联化工分类表
  9. type BaseFromMysteelChemicalClassify struct {
  10. BaseFromMysteelChemicalClassifyId int `orm:"column(base_from_mysteel_chemical_classify_id);pk"`
  11. ClassifyName string `description:"分类名称"`
  12. ParentId int `description:"父级id"`
  13. SysUserId int `description:"创建人id"`
  14. SysUserRealName string `description:"创建人姓名"`
  15. Level int `description:"层级"`
  16. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. CreateTime time.Time `description:"创建时间"`
  19. }
  20. // AddBaseFromMysteelChemicalClassify 添加钢联化工分类
  21. func AddBaseFromMysteelChemicalClassify(item *BaseFromMysteelChemicalClassify) (lastId int64, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. // GetBaseFromMysteelChemicalClassifyCount 获取分类名称的个数
  27. func GetBaseFromMysteelChemicalClassifyCount(classifyName string, parentId int) (count int, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := `SELECT COUNT(1) AS count FROM base_from_mysteel_chemical_classify WHERE classify_name=? AND parent_id=? `
  30. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  31. return
  32. }
  33. // GetBaseFromMysteelChemicalClassifyById 通过分类id的获取分类信息
  34. func GetBaseFromMysteelChemicalClassifyById(classifyId int) (item *BaseFromMysteelChemicalClassify, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := `SELECT * FROM base_from_mysteel_chemical_classify WHERE base_from_mysteel_chemical_classify_id=? `
  37. err = o.Raw(sql, classifyId).QueryRow(&item)
  38. return
  39. }
  40. // EditBaseFromMysteelChemicalClassify 修改钢联化工分类
  41. func EditBaseFromMysteelChemicalClassify(classifyId int, classifyName string) (err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. sql := `UPDATE base_from_mysteel_chemical_classify SET classify_name=?,modify_time=NOW() WHERE base_from_mysteel_chemical_classify_id=? `
  44. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  45. return
  46. }
  47. type BaseFromMysteelChemicalClassifyItems struct {
  48. BaseFromMysteelChemicalClassifyId int `orm:"column(base_from_mysteel_chemical_classify_id)"`
  49. BaseFromMysteelChemicalIndexId int `orm:"column(base_from_mysteel_chemical_index_id)"`
  50. ClassifyName string `description:"分类名称"`
  51. ParentId int `description:"父级id"`
  52. SysUserId int `description:"创建人id"`
  53. SysUserRealName string `description:"创建人姓名"`
  54. Level int `description:"层级"`
  55. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  56. UniqueCode string `description:"唯一编码"`
  57. ModifyTime time.Time `description:"修改时间"`
  58. CreateTime time.Time `description:"创建时间"`
  59. Button BaseFromMysteelChemicalClassifyItemsButton `description:"操作权限"`
  60. Children []*BaseFromMysteelChemicalClassifyItems `description:"下级"`
  61. }
  62. type BaseFromMysteelChemicalClassifyItemsButton struct {
  63. AddButton bool `description:"是否可添加"`
  64. OpButton bool `description:"是否可编辑"`
  65. DeleteButton bool `description:"是否可删除"`
  66. MoveButton bool `description:"是否可移动"`
  67. }
  68. // GetBaseFromMysteelChemicalClassifyByParentId 根据上级id获取当下的分类列表数据
  69. func GetBaseFromMysteelChemicalClassifyByParentId(parentId int) (items []*BaseFromMysteelChemicalClassifyItems, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. sql := ` SELECT * FROM base_from_mysteel_chemical_classify WHERE parent_id=? order by sort asc,base_from_mysteel_chemical_classify_id asc`
  72. _, err = o.Raw(sql, parentId).QueryRows(&items)
  73. return
  74. }
  75. // GetAllBaseFromMysteelChemicalClassify 获取所有的分类列表数据
  76. func GetAllBaseFromMysteelChemicalClassify() (items []*BaseFromMysteelChemicalClassifyItems, err error) {
  77. o := orm.NewOrmUsingDB("data")
  78. sql := ` SELECT * FROM base_from_mysteel_chemical_classify WHERE parent_id <> 0 order by sort asc,base_from_mysteel_chemical_classify_id asc`
  79. _, err = o.Raw(sql).QueryRows(&items)
  80. return
  81. }
  82. type DeleteBaseFromMysteelChemicalClassifyReq struct {
  83. ClassifyId int `description:"分类id"`
  84. EdbInfoId int `description:"指标id"`
  85. }
  86. type BaseFromMysteelChemicalClassifyListResp struct {
  87. AllNodes []*BaseFromMysteelChemicalClassifyItems
  88. CanOpClassify bool `description:"是否允许操作分类"`
  89. }
  90. type BaseFromMysteelChemicalClassifySimplify struct {
  91. ClassifyId int `description:"分类id"`
  92. ClassifyName string `description:"分类名称"`
  93. ParentId int
  94. }
  95. // GetFirstBaseFromMysteelChemicalClassify 获取当前分类下,且排序数相同 的排序第一条的数据
  96. func GetFirstBaseFromMysteelChemicalClassify() (item *BaseFromMysteelChemicalClassify, err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. sql := ` SELECT * FROM base_from_mysteel_chemical_classify order by sort asc,base_from_mysteel_chemical_classify_id asc limit 1`
  99. err = o.Raw(sql).QueryRow(&item)
  100. return
  101. }
  102. // UpdateBaseFromMysteelChemicalClassifySortByClassifyId 根据分类id更新排序
  103. func UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. sql := ` update base_from_mysteel_chemical_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  106. if classifyId > 0 {
  107. sql += ` or ( base_from_mysteel_chemical_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  108. }
  109. _, err = o.Raw(sql, parentId, nowSort).Exec()
  110. return
  111. }
  112. // Update 更新分类基础信息
  113. func (BaseFromMysteelChemicalClassify *BaseFromMysteelChemicalClassify) Update(cols []string) (err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. _, err = o.Update(BaseFromMysteelChemicalClassify, cols...)
  116. return
  117. }
  118. // GetBaseFromMysteelChemicalClassifyMaxSort 获取分类下最大的排序数
  119. func GetBaseFromMysteelChemicalClassifyMaxSort(parentId int) (sort int, err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. sql := `SELECT Max(sort) AS sort FROM base_from_mysteel_chemical_classify WHERE parent_id=? `
  122. err = o.Raw(sql, parentId).QueryRow(&sort)
  123. return
  124. }
  125. // DeleteMysteelChemicalByClassifyId 根据分类id删除对应的指标分类、指标、指标数据
  126. func DeleteMysteelChemicalByClassifyId(classifyIdList []int, mysteelChemicalIndexIdList []int) (err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. to, err := o.Begin()
  129. if err != nil {
  130. return
  131. }
  132. defer func() {
  133. if err != nil {
  134. _ = to.Rollback()
  135. } else {
  136. _ = to.Commit()
  137. }
  138. }()
  139. num := len(classifyIdList)
  140. if num <= 0 {
  141. return
  142. }
  143. //删除分类
  144. sql := `DELETE FROM base_from_mysteel_chemical_classify WHERE base_from_mysteel_chemical_classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  145. _, err = to.Raw(sql, classifyIdList).Exec()
  146. if err != nil {
  147. return
  148. }
  149. //删除指标
  150. sql = `DELETE FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  151. _, err = to.Raw(sql, classifyIdList).Exec()
  152. if err != nil {
  153. return
  154. }
  155. //删除指标数据
  156. indexNum := len(mysteelChemicalIndexIdList)
  157. if indexNum > 0 {
  158. sql = `DELETE FROM base_from_mysteel_chemical_data WHERE base_from_mysteel_chemical_index_id in (` + utils.GetOrmInReplace(indexNum) + `) `
  159. _, err = to.Raw(sql, mysteelChemicalIndexIdList).Exec()
  160. if err != nil {
  161. return
  162. }
  163. }
  164. return
  165. }
  166. // DeleteMysteelChemical 根据指标id删除对应的指标指标、指标数据
  167. func DeleteMysteelChemical(mysteelChemicalIndexId int) (err error) {
  168. o := orm.NewOrmUsingDB("data")
  169. to, err := o.Begin()
  170. if err != nil {
  171. return
  172. }
  173. defer func() {
  174. if err != nil {
  175. _ = to.Rollback()
  176. } else {
  177. _ = to.Commit()
  178. }
  179. }()
  180. //删除指标
  181. sql := `DELETE FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_index_id = ? `
  182. _, err = to.Raw(sql, mysteelChemicalIndexId).Exec()
  183. if err != nil {
  184. return
  185. }
  186. //删除指标数据
  187. sql = `DELETE FROM base_from_mysteel_chemical_data WHERE base_from_mysteel_chemical_index_id = ? `
  188. _, err = to.Raw(sql, mysteelChemicalIndexId).Exec()
  189. if err != nil {
  190. return
  191. }
  192. return
  193. }