You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.5 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. package category
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/database/gdb"
  5. "github.com/gogf/gf/v2/encoding/gjson"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. v1 "xgit.pub/module/cms/app/api/category/v1"
  10. "xgit.pub/module/cms/app/dao"
  11. "xgit.pub/module/cms/app/model"
  12. "xgit.pub/module/cms/app/model/entity"
  13. "xgit.pub/module/cms/app/service"
  14. "xgit.pub/st52/xcore/util/pinyin"
  15. )
  16. type sCategory struct {
  17. }
  18. func init() {
  19. Category := New()
  20. service.RegisterCategory(Category)
  21. }
  22. func New() *sCategory {
  23. return &sCategory{}
  24. }
  25. // GetTree 获取树
  26. func (s *sCategory) GetTree(ctx context.Context, req *v1.GetTreeReq) (categories []*model.Category, err error) {
  27. var rs []*model.Category
  28. err = dao.Category.Ctx(ctx).OrderAsc(dao.Category.Columns().Sort).Scan(&rs)
  29. if err != nil {
  30. return
  31. }
  32. for _, r := range rs {
  33. if r.Extend != "" {
  34. if err = gjson.DecodeTo(r.Extend, &r.CategoryExtend); err != nil {
  35. g.Log().Error(ctx, err)
  36. return
  37. }
  38. }
  39. if r.ParentId == 0 {
  40. r.Children = s.getSubCategory(rs, r.Id)
  41. categories = append(categories, r)
  42. }
  43. }
  44. return
  45. }
  46. // GetList 获取列表
  47. func (s *sCategory) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
  48. res = &v1.GetListRes{}
  49. tx := dao.Category.Ctx(ctx)
  50. res.Total, err = tx.Count()
  51. if err != nil {
  52. return
  53. }
  54. var list []*entity.Category
  55. err = tx.OrderAsc(dao.Category.Columns().Sort).Scan(list)
  56. if err != nil {
  57. return
  58. }
  59. res.Page = req.Page
  60. res.PageSize = req.PageSize
  61. res.Rows = list
  62. return
  63. }
  64. // Create 创建
  65. func (s *sCategory) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  66. err = dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  67. if req.Union == "" {
  68. req.Union, _ = pinyin.New(req.Name).Split("").Mode(pinyin.InitialsInCapitals).Convert()
  69. }
  70. m := entity.Category{}
  71. if err = gconv.Struct(req, &m); err != nil {
  72. return nil
  73. }
  74. if m.Extend, err = gjson.EncodeString(req.CategoryExtend); err != nil {
  75. return nil
  76. }
  77. //id, err = dao.Category.Ctx(ctx).InsertAndGetId(in)
  78. _, err = dao.Category.Ctx(ctx).InsertAndGetId(m)
  79. return err
  80. })
  81. return
  82. }
  83. // Update 更新
  84. func (s *sCategory) Update(ctx context.Context, in *v1.UpdateReq) (err error) {
  85. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  86. if in.Id == 0 {
  87. return gerror.New("编号不能为空")
  88. }
  89. if in.Union == "" {
  90. in.Union, _ = pinyin.New(in.Name).Split("").Mode(pinyin.InitialsInCapitals).Convert()
  91. }
  92. ump := g.Map{}
  93. ump = gconv.Map(in)
  94. ump[dao.Category.Columns().Extend], err = gjson.EncodeString(in.CategoryExtend)
  95. if err != nil {
  96. return nil
  97. }
  98. //_, err = dao.Category.Ctx(ctx).OmitEmpty().Data(in).Where(dao.Category.Columns().Id, in.Id).Update()
  99. _, err = dao.Category.Ctx(ctx).OmitEmpty().Data(ump).Where(dao.Category.Columns().Id, in.Id).Update()
  100. return err
  101. })
  102. }
  103. // Delete 删除
  104. func (s *sCategory) Delete(ctx context.Context, in *v1.DeleteReq) (err error) {
  105. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  106. if in.Id < 1 {
  107. return gerror.New("编号不能为空")
  108. }
  109. _, err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, in.Id).Delete()
  110. return nil
  111. })
  112. }
  113. // BatchDelete 批量删除
  114. func (s *sCategory) BatchDelete(ctx context.Context, in *v1.BatchDeleteReq) (err error) {
  115. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  116. if len(in.Ids) < 1 {
  117. return gerror.New("编号不能为空")
  118. }
  119. _, err = dao.Category.Ctx(ctx).WhereIn(dao.Category.Columns().Id, in.Ids).Delete()
  120. return nil
  121. })
  122. }
  123. func (s *sCategory) Drop(ctx context.Context, source int, target int, action string) (err error) {
  124. targetEntity := entity.Category{}
  125. err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, target).Scan(&targetEntity)
  126. err = dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
  127. if err != nil {
  128. return err
  129. }
  130. if action == "inner" {
  131. _, err = dao.Category.Ctx(ctx).
  132. Data(dao.Category.Columns().ParentId, target).
  133. Where(dao.Category.Columns().Id, source).Update()
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. if action == "before" {
  139. if err != nil {
  140. return err
  141. }
  142. _, err = dao.Category.Ctx(ctx).
  143. Data(g.Map{
  144. dao.Category.Columns().ParentId: targetEntity.ParentId,
  145. dao.Category.Columns().Sort: targetEntity.Sort - 5,
  146. }).
  147. Where(dao.Category.Columns().Id, source).Update()
  148. if err != nil {
  149. return err
  150. }
  151. }
  152. if action == "after" {
  153. _, err = dao.Category.Ctx(ctx).
  154. Data(g.Map{
  155. dao.Category.Columns().ParentId: targetEntity.ParentId,
  156. dao.Category.Columns().Sort: targetEntity.Sort + 5,
  157. }).
  158. Where(dao.Category.Columns().Id, source).Update()
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. return
  164. })
  165. var menus []entity.Category
  166. err = dao.Category.Ctx(ctx).
  167. Where(dao.Category.Columns().ParentId, targetEntity.ParentId).
  168. OrderAsc(dao.Category.Columns().Sort).
  169. Scan(&menus)
  170. if err != nil {
  171. return
  172. }
  173. for i, menu := range menus {
  174. _, _ = dao.Category.Ctx(ctx).
  175. Data(g.Map{
  176. dao.Category.Columns().Sort: (i + 1) * 10,
  177. }).
  178. Where(dao.Category.Columns().Id, menu.Id).Update()
  179. }
  180. return err
  181. }
  182. // getSubCategory 获取子分类
  183. func (s *sCategory) getSubCategory(rs []*model.Category, id int) []*model.Category {
  184. var children []*model.Category
  185. for _, r := range rs {
  186. if r.ParentId == id {
  187. r.Children = s.getSubCategory(rs, r.Id)
  188. children = append(children, r)
  189. }
  190. }
  191. return children
  192. }