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.

217 lines
5.8 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 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. res = &v1.CreateRes{}
  78. //id, err = dao.Category.Ctx(ctx).InsertAndGetId(in)
  79. res.Id, err = dao.Category.Ctx(ctx).InsertAndGetId(m)
  80. return err
  81. })
  82. return
  83. }
  84. // Update 更新
  85. func (s *sCategory) Update(ctx context.Context, in *v1.UpdateReq) (err error) {
  86. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  87. if in.Id == 0 {
  88. return gerror.New("编号不能为空")
  89. }
  90. if in.Union == "" {
  91. in.Union, _ = pinyin.New(in.Name).Split("").Mode(pinyin.InitialsInCapitals).Convert()
  92. }
  93. ump := g.Map{}
  94. ump = gconv.Map(in)
  95. ump[dao.Category.Columns().Extend], err = gjson.EncodeString(in.CategoryExtend)
  96. if err != nil {
  97. return nil
  98. }
  99. //_, err = dao.Category.Ctx(ctx).OmitEmpty().Data(in).Where(dao.Category.Columns().Id, in.Id).Update()
  100. _, err = dao.Category.Ctx(ctx).OmitEmpty().Data(ump).Where(dao.Category.Columns().Id, in.Id).Update()
  101. return err
  102. })
  103. }
  104. // Delete 删除
  105. func (s *sCategory) Delete(ctx context.Context, in *v1.DeleteReq) (err error) {
  106. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  107. if in.Id < 1 {
  108. return gerror.New("编号不能为空")
  109. }
  110. _, err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, in.Id).Delete()
  111. return nil
  112. })
  113. }
  114. // BatchDelete 批量删除
  115. func (s *sCategory) BatchDelete(ctx context.Context, in *v1.BatchDeleteReq) (err error) {
  116. return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  117. if len(in.Ids) < 1 {
  118. return gerror.New("编号不能为空")
  119. }
  120. _, err = dao.Category.Ctx(ctx).WhereIn(dao.Category.Columns().Id, in.Ids).Delete()
  121. return nil
  122. })
  123. }
  124. func (s *sCategory) Drop(ctx context.Context, source int, target int, action string) (err error) {
  125. targetEntity := entity.Category{}
  126. err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, target).Scan(&targetEntity)
  127. err = dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
  128. if err != nil {
  129. return err
  130. }
  131. if action == "inner" {
  132. _, err = dao.Category.Ctx(ctx).
  133. Data(dao.Category.Columns().ParentId, target).
  134. Where(dao.Category.Columns().Id, source).Update()
  135. if err != nil {
  136. return err
  137. }
  138. }
  139. if action == "before" {
  140. if err != nil {
  141. return err
  142. }
  143. _, err = dao.Category.Ctx(ctx).
  144. Data(g.Map{
  145. dao.Category.Columns().ParentId: targetEntity.ParentId,
  146. dao.Category.Columns().Sort: targetEntity.Sort - 5,
  147. }).
  148. Where(dao.Category.Columns().Id, source).Update()
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. if action == "after" {
  154. _, err = dao.Category.Ctx(ctx).
  155. Data(g.Map{
  156. dao.Category.Columns().ParentId: targetEntity.ParentId,
  157. dao.Category.Columns().Sort: targetEntity.Sort + 5,
  158. }).
  159. Where(dao.Category.Columns().Id, source).Update()
  160. if err != nil {
  161. return err
  162. }
  163. }
  164. return
  165. })
  166. var menus []entity.Category
  167. err = dao.Category.Ctx(ctx).
  168. Where(dao.Category.Columns().ParentId, targetEntity.ParentId).
  169. OrderAsc(dao.Category.Columns().Sort).
  170. Scan(&menus)
  171. if err != nil {
  172. return
  173. }
  174. for i, menu := range menus {
  175. _, _ = dao.Category.Ctx(ctx).
  176. Data(g.Map{
  177. dao.Category.Columns().Sort: (i + 1) * 10,
  178. }).
  179. Where(dao.Category.Columns().Id, menu.Id).Update()
  180. }
  181. return err
  182. }
  183. // getSubCategory 获取子分类
  184. func (s *sCategory) getSubCategory(rs []*model.Category, id int) []*model.Category {
  185. var children []*model.Category
  186. for _, r := range rs {
  187. if r.ParentId == id {
  188. r.Children = s.getSubCategory(rs, r.Id)
  189. children = append(children, r)
  190. }
  191. }
  192. return children
  193. }
  194. // GetCategoryNameById 获取分类名称
  195. func (s *sCategory) GetCategoryNameById(ctx context.Context, id uint) (name string) {
  196. var category entity.Category
  197. err := dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, id).Scan(&category)
  198. if err != nil {
  199. return
  200. }
  201. return category.Name
  202. }