123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package material
- import (
- "github.com/gin-gonic/gin"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/material/brand"
- "zhiyuan/services/material/item"
- )
- func ItemList(c *gin.Context) {
- page := app.HandlePageNum(c)
- where := map[string]interface{}{"_orderby": "id desc", "deleted_at": 0}
- if name := c.Query("name"); name != "" {
- where["item_name like"] = "%" + name + "%"
- }
- if brand_id := c.Query("brand_id"); brand_id != "" {
- where["brand_id"] = brand_id
- }
- if type_id := c.Query("type_id"); type_id != "" {
- where["type_id"] = type_id
- }
- type ItemList struct {
- ID int `json:"id"`
- ItemName string `json:"item_name"`
- Level int `json:"level"`
- Unit int `json:"unit"`
- BrandID int `json:"brand_id"`
- BrandName string `json:"brand_name"`
- TypeID int `json:"type_id"`
- TypeName string `json:"type_name"`
- CreatedAt string `json:"created_at"`
- }
- total, err := item.Count(where)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- itemList := make([]ItemList, 0)
- if _, err := item.GetList(where, nil, page, &itemList); err != nil {
- app.Error(c, err.Error())
- return
- }
- brandIds := make([]int, 0)
- for _, v := range itemList {
- brandIds = append(brandIds, v.BrandID)
- }
- brandListMap := make(map[int]string, 0)
- if len(brandIds) > 0 {
- if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err != nil {
- app.Error(c, err.Error())
- return
- } else {
- for _, v := range brandList {
- brandListMap[v.ID] = v.BrandName
- }
- }
- }
- typeListMap := item.GetTypeMapByCache()
- for k, v := range itemList {
- v.BrandName = brandListMap[v.BrandID]
- v.TypeName = typeListMap[v.TypeID]
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
- itemList[k] = v
- }
- data := gin.H{
- "list": itemList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func ItemAdd(c *gin.Context) {
- var form form.MaterialItemAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if id, err := item.Add(form); err != nil {
- app.Error(c, err.Error())
- return
- } else {
- app.Success(c, gin.H{"id": id})
- }
- }
- func ItemEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "id有误")
- return
- }
- var form form.MaterialItemAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if _, err := item.Edit(form, id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func ItemInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "id有误")
- return
- }
- type ItemInfo struct {
- ID int `json:"id"`
- BrandID int `json:"brand_id"`
- ItemName string `json:"item_name"`
- TypeID int `json:"type_id"`
- Level int `json:"level"`
- Price float64 `json:"price"`
- Unit int `json:"unit"`
- Pic string `json:"pic"`
- Content string `json:"content"`
- Colors []string `json:"colors"`
- }
- colors := make([]string, 0)
- if optionList, err := item.GetOptionList(map[string]interface{}{"item_id": id}, nil, app.Page{}, nil); err == nil {
- for _, v := range optionList {
- colors = append(colors, v.OptionName)
- }
- }
- var itemInfo *ItemInfo
- if _, err := item.GetOne(map[string]interface{}{"id": id}, nil, &itemInfo); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- itemInfo.Colors = colors
- if detail, _ := item.GetDetailOne(map[string]interface{}{"item_id": id}, nil, nil); detail != nil {
- itemInfo.Content = detail.Content
- }
- app.Success(c, itemInfo)
- }
- func ItemDel(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- err := item.Del(id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func TypeList(c *gin.Context) {
- isTree := utils.ToInt(c.Query("tree"))
- if isTree == 1 {
- TypeListTree(c)
- return
- }
- isSelect := utils.ToInt(c.Query("select"))
- if isSelect == 1 {
- TypeListSelect(c)
- return
- }
- page := app.HandlePageNum(c)
- where := make(map[string]interface{})
- total, err := item.CountType(where)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- types, err := item.GetList(where, []string{"id", "type_name"}, page, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- data := gin.H{
- "list": types,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func TypeListSelect(c *gin.Context) {
- where := make(map[string]interface{})
- types, err := item.GetList(where, []string{"id", "type_name"}, app.Page{}, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, types)
- }
- func TypeListTree(c *gin.Context) {
- where := make(map[string]interface{})
- category := utils.ToInt(c.Query("category"))
- if category > 0 {
- where["category"] = category
- }
- auths, err := item.GetTypeList(where, []string{"id", "type_name", "pid", "category"}, app.Page{}, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, utils.GenTree(auths, "id", "pid", "children", 0))
- }
- func TypeAdd(c *gin.Context) {
- var form form.MaterialItemTypeAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if id, err := item.AddType(form); err != nil {
- app.Error(c, err.Error())
- return
- } else {
- app.Success(c, gin.H{"id": id})
- }
- }
- func TypeEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "类型id有误")
- return
- }
- var form form.MaterialItemTypeAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if _, err := item.EditType(form, id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
|