|
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/utils"
- "errors"
- "github.com/gin-gonic/gin"
- "reflect"
- "time"
- "xorm.io/xorm"
- )
-
- func List(c *gin.Context) ([]map[string]interface{}, error) {
- engine := db.Db
- //1、获取当前主营类目下所有 一级类目
- firstCategoryList, err := db.GetCategoryList(engine, "1")
- if err != nil {
- return nil, err
- }
- var firstCategoryListIds []int
- for _, v := range *firstCategoryList {
- firstCategoryListIds = append(firstCategoryListIds, v.Id)
- }
-
- //2、获取所有 二级类目
- secondCategoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
- "key": "pid",
- "value": firstCategoryListIds,
- })
- var secondCategoryListIds []int
- for _, v := range *secondCategoryList {
- secondCategoryListIds = append(secondCategoryListIds, v.Id)
- }
-
- var tempResult = map[string][]map[string]interface{}{}
- for _, v := range *secondCategoryList {
- var tmp = map[string]interface{}{}
- tmp["id"] = v.Id
- tmp["pid"] = v.Pid
- tmp["name"] = v.Name
- tmp["level"] = v.Level
- tmp["create_at"] = v.CreateAt
- tmp["update_at"] = v.UpdateAt
- tempResult[utils.AnyToString(v.Pid)] = append(tempResult[utils.AnyToString(v.Pid)], tmp)
- }
-
- var result []map[string]interface{}
- for _, v := range *firstCategoryList {
- var temp = map[string]interface{}{}
- temp["id"] = v.Id
- temp["pid"] = v.Pid
- temp["name"] = v.Name
- temp["level"] = v.Level
- temp["create_at"] = v.CreateAt
- temp["update_at"] = v.UpdateAt
- temp["child"] = tempResult[utils.AnyToString(v.Id)]
- result = append(result, temp)
- }
- return result, nil
- }
-
- func Add(c *gin.Context, name string, pid string) error {
- engine := db.Db
- var level = 1
- if pid != "0" {
- fatherCategory, err := db.GetCategoryById(engine, pid)
- if err != nil {
- return err
- }
- if fatherCategory == nil {
- return errors.New("未找到该分类的父类")
- }
- level = fatherCategory.Level + 1
- }
- m := model.GoodsCategory{
- Name: name,
- Level: level,
- Pid: utils.StrToInt(pid),
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- _, err := db.CategoryInsert(engine, &m)
- if err != nil {
- return err
- }
- return nil
- }
-
- func Del(c *gin.Context, categoryId string) error {
- engine := db.Db
- delModel := new(model.GoodsCategory)
- _, err := engine.ID(categoryId).Delete(delModel)
- if err != nil {
- return err
- }
- _, err = engine.Table("goods_category").Where("pid = ?", categoryId).Delete(new(model.GoodsCategory))
- if err != nil {
- return err
- }
- return nil
- }
-
- func Sort(c *gin.Context, ids []string) error {
- engine := db.Db
- categoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
- "key": "id",
- "value": ids,
- })
- for _, v := range *categoryList {
- err := db.UpdateCategoryById(engine, &v)
- if err != nil {
- return err
- }
- }
- return nil
- }
-
- func Save(c *gin.Context, data map[string]interface{}) error {
- engine := db.Db
- //todo::处理一级类目
- err := db.UpdateOrInsert(engine, &model.GoodsCategory{
- Id: utils.StrToInt(utils.AnyToString(data["id"])),
- Pid: utils.StrToInt(utils.AnyToString(data["pid"])),
- Name: utils.AnyToString(data["name"]),
- Level: 1,
- CreateAt: utils.AnyToString(data["create_at"]),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- return err
- }
-
- if data["child"] != nil {
- //todo::有二级类目
- child := reflect.ValueOf(data["child"])
- if child.Len() > 0 {
- //1、查询出所有三级类目
- secondCategoryListData, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
- "key": "pid",
- "value": data["id"],
- })
- var secondCategoryList = map[string]interface{}{}
- for _, secondCategory := range *secondCategoryListData {
- secondCategoryList[utils.AnyToString(secondCategory.Id)] = secondCategory
- }
-
- tempSave := map[string]interface{}{}
- for i := 0; i < child.Len(); i++ {
- tempChild := child.Index(i).Interface().(map[string]interface{})
- tempSave[utils.AnyToString(tempChild["id"])] = tempChild
- m := &model.GoodsCategory{
- Id: utils.StrToInt(utils.AnyToString(tempChild["id"])),
- Pid: utils.StrToInt(utils.AnyToString(data["id"])),
- Name: utils.AnyToString(tempChild["name"]),
- Level: 2,
- CreateAt: utils.AnyToString(tempChild["create_at"]),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- err := db.UpdateOrInsert(engine, m)
- if err != nil {
- return err
- }
- }
-
- for key, _ := range secondCategoryList {
- if tempSave[key] == nil {
- engine.Where("id=?", key).Delete(model.GoodsCategory{})
- }
- }
- } else {
- //删除所有二级类目
- engine.Where("pid=?", data["id"]).Delete(model.GoodsCategory{})
- }
- } else {
- //删除所有二级类目
- engine.Where("pid=?", data["id"]).Delete(model.GoodsCategory{})
- }
- return nil
- }
-
- func GetAllCategory(c *gin.Context) (interface{}, error) {
- engine := db.Db
- var result interface{}
- result, err := MakeCategoryOptionV2(engine)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
-
- // 按照父级子级进行分类
- func MakeCategoryOptionV2(engine *xorm.Engine) ([]md.CateGoryTreeList, error) {
- //1、获取当前主营类目下所有 一级类目
- firstCategoryList, err := db.GetCategoryList(engine, "1")
- if err != nil {
- return nil, err
- }
- var firstCategoryListIds []int
- for _, v := range *firstCategoryList {
- firstCategoryListIds = append(firstCategoryListIds, v.Id)
- }
- //2、获取所有 二级类目
- secondCategoryList, _ := db.CategoryListFindByParams(engine, map[string]interface{}{
- "key": "pid",
- "value": firstCategoryListIds,
- })
-
- var tempResult = map[string][]md.CateGoryTreeList{}
- for _, v := range *secondCategoryList {
- tempResult[utils.AnyToString(v.Pid)] = append(tempResult[utils.AnyToString(v.Pid)], md.CateGoryTreeList{
- Id: v.Id,
- Pid: v.Pid,
- Name: v.Name,
- CreateAt: v.CreateAt,
- UpdateAt: v.CreateAt,
- })
- }
-
- var result []md.CateGoryTreeList
- for _, v := range *firstCategoryList {
- result = append(result, md.CateGoryTreeList{
- Id: v.Id,
- Pid: v.Pid,
- Name: v.Name,
- CreateAt: v.CreateAt,
- UpdateAt: v.CreateAt,
- Children: tempResult[utils.AnyToString(v.Id)],
- })
- }
- return result, nil
- }
|