package controllerimport ("encoding/json""fmt""service/components""service/modules""service/utils""strconv""time""github.com/gin-gonic/gin")// AddAttributeController 添加商品的展示属性func AddAttributeController(ctx *gin.Context) {var cc modules.Attributectx.ShouldBind(&cc)count := modules.GetAttributeByNameAndTypeService(cc)if count != 0 {utils.HandleFailResponse(ctx, cc.Name+" 标签已存在", nil)return}fmt.Println("cc: ", cc, count)cc.Uid = 1cc.CreatedAt = time.Now()err := modules.AddAttributeService(&cc)if err != nil {utils.HandleFailResponse(ctx, "增加商品售卖属性失败", nil)return}utils.HandleSuccessResponse(ctx, "添加成功!!", nil)}// GetAllAttributeController 获取商品的所有展示属性func GetAllAttributeController(ctx *gin.Context) {_type := ctx.Query("type")display := ctx.DefaultQuery("display", "1")cc, err := modules.GetAllAttributeService(_type, display)var parent []modules.Attributevar children []modules.Attributeif err != nil {utils.HandleFailResponse(ctx, "查询失败", nil)return}for _, attribute := range cc {if attribute.IsParent == 0 {parent = append(parent, attribute)} else {children = append(children, attribute)}}finally := make([]modules.Attribute, len(parent))for i := 0; i < len(parent); i++ {finally[i] = parent[i]for j := 0; j < len(children); j++ {if parent[i].Id == children[j].IsParent {finally[i].Children = append(finally[i].Children, &children[j])}}}for i := 0; i < len(finally); i++ {for j := 0; j < len(finally[i].Children); j++ {for k := 0; k < len(children); k++ {if finally[i].Children[j].Id == children[k].IsParent {finally[i].Children[j].Children = append(finally[i].Children[j].Children, &children[k])}}}}utils.HandleSuccessResponse(ctx, "查询商品属性成功", finally)}func PatchAttributeController() gin.HandlerFunc {return func(ctx *gin.Context) {raw, _ := ctx.GetRawData()var data map[string]anyerr := json.Unmarshal(raw, &data)if err != nil {utils.HandleFailResponse(ctx, "数据绑定失败", nil)return}if data["value"] == nil {fmt.Println("更新显示和隐藏", data["display"])err = components.DB.Model(modules.Attribute{}).Where("id = ?", data["id"]).Update("display", data["display"]).Error} else {err = components.DB.Model(modules.Attribute{}).Where("id = ?", data["id"]).Update("name", data["value"]).Errorid, _ := strconv.Atoi(fmt.Sprintf("%v", data["id"]))if id >= 99 && id <= 105 {err = components.DB.Model(&modules.File{}).Where("attribute = ?", data["name"]).Updates(map[string]interface{}{"attribute": data["value"],}).Errorif err != nil {utils.HandleFailResponse(ctx, "更新文件类失败", nil)return}}}if err != nil {utils.HandleFailResponse(ctx, "更新失败", err.Error())return}utils.HandleSuccessResponse(ctx, "更新成功", nil)}}// DeleteAttributeController 删除商品的展示属性func DeleteAttributeController(ctx *gin.Context) {c := new(modules.Attribute)ctx.ShouldBind(c)fmt.Println("c: ", c)err := modules.DeleteAttributeService(c)if err != nil {utils.HandleFailResponse(ctx, "属性删除失败!", nil)return}utils.HandleSuccessResponse(ctx, "属性删除成功~", nil)}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。