wr.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package models
  2. // 仓单服务
  3. import (
  4. "mtp2_if/db"
  5. "time"
  6. )
  7. // Wrcategory 现货分类表
  8. type Wrcategory struct {
  9. Categoryid int32 `json:"categoryid" xorm:"'CATEGORYID'" binding:"required"` // 类别ID(SEQ_WRCATEGORY)
  10. Categoryname string `json:"categoryname" xorm:"'CATEGORYNAME'"` // 类别名称
  11. Parentcategoryid int32 `json:"parentcategoryid" xorm:"'PARENTCATEGORYID'"` // 父类别ID
  12. Categorydesc string `json:"categorydesc" xorm:"'CATEGORYDESC'"` // 类别描述
  13. Isvalid int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  14. Iconurl string `json:"iconurl" xorm:"'ICONURL'"` // 图标地址
  15. Creatorid int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  16. Createtime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  17. Updatorid int64 `json:"updatorid" xorm:"'UPDATORID'"` // 更新人
  18. Updatetime time.Time `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  19. Orderindex int32 `json:"orderindex" xorm:"'ORDERINDEX'"` // 顺序
  20. Areauserid int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  21. }
  22. // TableName is WRCATEGORY
  23. func (Wrcategory) TableName() string {
  24. return "WRCATEGORY"
  25. }
  26. // WRCategoryTree 现货分类
  27. type WRCategoryTree struct {
  28. Categoryid int32 `json:"categoryid" xorm:"'CATEGORYID'" binding:"required"` // 类别ID(SEQ_WRCATEGORY)
  29. Categoryname string `json:"categoryname" xorm:"'CATEGORYNAME'"` // 类别名称
  30. Parentcategoryid int32 `json:"parentcategoryid" xorm:"'PARENTCATEGORYID'"` // 父类别ID
  31. Categorydesc string `json:"categorydesc" xorm:"'CATEGORYDESC'"` // 类别描述
  32. Iconurl string `json:"iconurl" xorm:"'ICONURL'"` // 图标地址
  33. Orderindex int32 `json:"orderindex" xorm:"'ORDERINDEX'"` // 顺序
  34. Areauserid int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  35. SubCategory []WRCategoryTree `json:"subcategory" xorm:"-"` // 子分类
  36. }
  37. // CreateCategoryTree 生成现货分类信息
  38. func CreateCategoryTree(parentCategoryID int) ([]WRCategoryTree, error) {
  39. engine := db.GetEngine()
  40. wrCategoryTrees := make([]WRCategoryTree, 0)
  41. session := engine.Table("WRCATEGORY WR")
  42. if parentCategoryID != 0 {
  43. session = session.Where("WR.PARENTCATEGORYID = ?", parentCategoryID)
  44. } else {
  45. session = session.Where("WR.PARENTCATEGORYID is null")
  46. }
  47. session = session.Asc("WR.ORDERINDEX")
  48. if err := session.Find(&wrCategoryTrees); err != nil {
  49. return nil, err
  50. }
  51. for i := range wrCategoryTrees {
  52. category := &wrCategoryTrees[i]
  53. // 递归生成子分类
  54. subs, err := CreateCategoryTree(int(category.Categoryid))
  55. if err != nil {
  56. return nil, err
  57. }
  58. category.SubCategory = subs
  59. }
  60. return wrCategoryTrees, nil
  61. }