123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package material
- import (
- "github.com/gin-gonic/gin"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/material/brand"
- )
- func BrandList(c *gin.Context) {
- isSelect := utils.ToInt(c.Query("is_select"))
- if isSelect == 1 {
- BrandListSelect(c)
- return
- }
- page := app.HandlePageNum(c)
- where := map[string]interface{}{
- "deleted_at": 0,
- }
- total, err := brand.Count(where)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- type BrandList struct {
- ID int `json:"id"`
- BrandName string `json:"brand_name"`
- CreatedAt string `json:"created_at"`
- }
- brandList := make([]BrandList, 0)
- if _, err := brand.GetList(where, nil, page, &brandList); err != nil {
- app.Error(c, err.Error())
- return
- }
- for k, v := range brandList {
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
- brandList[k] = v
- }
- data := gin.H{
- "list": brandList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func BrandListSelect(c *gin.Context) {
- type BrandList struct {
- ID int `json:"id"`
- BrandName string `json:"brand_name"`
- }
- where := map[string]interface{}{
- "_orderby": "CONVERT(brand_name USING gbk) COLLATE gbk_chinese_ci ASC",
- }
- if name := c.Query("name"); name != "" {
- where["brand_name like"] = "%" + name + "%"
- }
- if c.Query("show_all") == "" {
- where["deleted_at"] = 0
- }
- if id := c.Query("id"); id != "" {
- where["id"] = id
- }
- page := app.Page{}
- if c.Query("limit") != "" {
- page = app.HandlePageNum(c)
- }
- brandList := make([]BrandList, 0)
- _, err := brand.GetList(where, []string{"id", "brand_name"}, page, &brandList)
- if err != nil {
- app.Error(c, err.Error())
- }
- app.Success(c, brandList)
- }
- func BrandAdd(c *gin.Context) {
- var form form.MaterialBrandAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if id, err := brand.Add(form); err != nil {
- app.Error(c, err.Error())
- return
- } else {
- app.Success(c, gin.H{"id": id})
- }
- }
- func BrandEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "套餐id有误")
- return
- }
- var form form.MaterialBrandAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if _, err := brand.Edit(form, id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
|