面包店
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.
 
 
 
 
 

234 line
6.2 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/utils"
  7. "errors"
  8. "github.com/gin-gonic/gin"
  9. "reflect"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. func List(c *gin.Context) ([]map[string]interface{}, error) {
  14. engine := db.Db
  15. //1、获取当前主营类目下所有 一级类目
  16. firstCategoryList, err := db.GetCategoryList(engine, "1")
  17. if err != nil {
  18. return nil, err
  19. }
  20. var firstCategoryListIds []int
  21. for _, v := range *firstCategoryList {
  22. firstCategoryListIds = append(firstCategoryListIds, v.Id)
  23. }
  24. //2、获取所有 二级类目
  25. secondCategoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
  26. "key": "pid",
  27. "value": firstCategoryListIds,
  28. })
  29. var secondCategoryListIds []int
  30. for _, v := range *secondCategoryList {
  31. secondCategoryListIds = append(secondCategoryListIds, v.Id)
  32. }
  33. var tempResult = map[string][]map[string]interface{}{}
  34. for _, v := range *secondCategoryList {
  35. var tmp = map[string]interface{}{}
  36. tmp["id"] = v.Id
  37. tmp["pid"] = v.Pid
  38. tmp["name"] = v.Name
  39. tmp["level"] = v.Level
  40. tmp["create_at"] = v.CreateAt
  41. tmp["update_at"] = v.UpdateAt
  42. tempResult[utils.AnyToString(v.Pid)] = append(tempResult[utils.AnyToString(v.Pid)], tmp)
  43. }
  44. var result []map[string]interface{}
  45. for _, v := range *firstCategoryList {
  46. var temp = map[string]interface{}{}
  47. temp["id"] = v.Id
  48. temp["pid"] = v.Pid
  49. temp["name"] = v.Name
  50. temp["level"] = v.Level
  51. temp["create_at"] = v.CreateAt
  52. temp["update_at"] = v.UpdateAt
  53. temp["child"] = tempResult[utils.AnyToString(v.Id)]
  54. result = append(result, temp)
  55. }
  56. return result, nil
  57. }
  58. func Add(c *gin.Context, name string, pid string) error {
  59. engine := db.Db
  60. var level = 1
  61. if pid != "0" {
  62. fatherCategory, err := db.GetCategoryById(engine, pid)
  63. if err != nil {
  64. return err
  65. }
  66. if fatherCategory == nil {
  67. return errors.New("未找到该分类的父类")
  68. }
  69. level = fatherCategory.Level + 1
  70. }
  71. m := model.GoodsCategory{
  72. Name: name,
  73. Level: level,
  74. Pid: utils.StrToInt(pid),
  75. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  76. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  77. }
  78. _, err := db.CategoryInsert(engine, &m)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func Del(c *gin.Context, categoryId string) error {
  85. engine := db.Db
  86. delModel := new(model.GoodsCategory)
  87. _, err := engine.ID(categoryId).Delete(delModel)
  88. if err != nil {
  89. return err
  90. }
  91. _, err = engine.Table("goods_category").Where("pid = ?", categoryId).Delete(new(model.GoodsCategory))
  92. if err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func Sort(c *gin.Context, ids []string) error {
  98. engine := db.Db
  99. categoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
  100. "key": "id",
  101. "value": ids,
  102. })
  103. for _, v := range *categoryList {
  104. err := db.UpdateCategoryById(engine, &v)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }
  111. func Save(c *gin.Context, data map[string]interface{}) error {
  112. engine := db.Db
  113. //todo::处理一级类目
  114. err := db.UpdateOrInsert(engine, &model.GoodsCategory{
  115. Id: utils.StrToInt(utils.AnyToString(data["id"])),
  116. Pid: utils.StrToInt(utils.AnyToString(data["pid"])),
  117. Name: utils.AnyToString(data["name"]),
  118. Level: 1,
  119. CreateAt: utils.AnyToString(data["create_at"]),
  120. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  121. })
  122. if err != nil {
  123. return err
  124. }
  125. if data["child"] != nil {
  126. //todo::有二级类目
  127. child := reflect.ValueOf(data["child"])
  128. if child.Len() > 0 {
  129. //1、查询出所有三级类目
  130. secondCategoryListData, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
  131. "key": "pid",
  132. "value": data["id"],
  133. })
  134. var secondCategoryList = map[string]interface{}{}
  135. for _, secondCategory := range *secondCategoryListData {
  136. secondCategoryList[utils.AnyToString(secondCategory.Id)] = secondCategory
  137. }
  138. tempSave := map[string]interface{}{}
  139. for i := 0; i < child.Len(); i++ {
  140. tempChild := child.Index(i).Interface().(map[string]interface{})
  141. tempSave[utils.AnyToString(tempChild["id"])] = tempChild
  142. m := &model.GoodsCategory{
  143. Id: utils.StrToInt(utils.AnyToString(tempChild["id"])),
  144. Pid: utils.StrToInt(utils.AnyToString(data["id"])),
  145. Name: utils.AnyToString(tempChild["name"]),
  146. Level: 2,
  147. CreateAt: utils.AnyToString(tempChild["create_at"]),
  148. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  149. }
  150. err := db.UpdateOrInsert(engine, m)
  151. if err != nil {
  152. return err
  153. }
  154. }
  155. for key, _ := range secondCategoryList {
  156. if tempSave[key] == nil {
  157. engine.Where("id=?", key).Delete(model.GoodsCategory{})
  158. }
  159. }
  160. } else {
  161. //删除所有二级类目
  162. engine.Where("pid=?", data["id"]).Delete(model.GoodsCategory{})
  163. }
  164. } else {
  165. //删除所有二级类目
  166. engine.Where("pid=?", data["id"]).Delete(model.GoodsCategory{})
  167. }
  168. return nil
  169. }
  170. func GetAllCategory(c *gin.Context) (interface{}, error) {
  171. engine := db.Db
  172. var result interface{}
  173. result, err := MakeCategoryOptionV2(engine)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return result, nil
  178. }
  179. // 按照父级子级进行分类
  180. func MakeCategoryOptionV2(engine *xorm.Engine) ([]md.CateGoryTreeList, error) {
  181. //1、获取当前主营类目下所有 一级类目
  182. firstCategoryList, err := db.GetCategoryList(engine, "1")
  183. if err != nil {
  184. return nil, err
  185. }
  186. var firstCategoryListIds []int
  187. for _, v := range *firstCategoryList {
  188. firstCategoryListIds = append(firstCategoryListIds, v.Id)
  189. }
  190. //2、获取所有 二级类目
  191. secondCategoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
  192. "key": "pid",
  193. "value": firstCategoryListIds,
  194. })
  195. var tempResult = map[string][]md.CateGoryTreeList{}
  196. for _, v := range *secondCategoryList {
  197. tempResult[utils.AnyToString(v.Pid)] = append(tempResult[utils.AnyToString(v.Pid)], md.CateGoryTreeList{
  198. Id: v.Id,
  199. Pid: v.Pid,
  200. Name: v.Name,
  201. CreateAt: v.CreateAt,
  202. UpdateAt: v.CreateAt,
  203. })
  204. }
  205. var result []md.CateGoryTreeList
  206. for _, v := range *firstCategoryList {
  207. result = append(result, md.CateGoryTreeList{
  208. Id: v.Id,
  209. Pid: v.Pid,
  210. Name: v.Name,
  211. CreateAt: v.CreateAt,
  212. UpdateAt: v.CreateAt,
  213. Children: tempResult[utils.AnyToString(v.Id)],
  214. })
  215. }
  216. return result, nil
  217. }