package pick import ( "github.com/gin-gonic/gin" "strings" "zhiyuan/pkg/app" "zhiyuan/pkg/param/material" pickParam "zhiyuan/pkg/param/material/pick" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/material/pick" "zhiyuan/services/material/pkg" "zhiyuan/services/structs" ) func OrderAdd(c *gin.Context) { var form form.MaterialPickOrderAdd if app.Bind(c, &form) != nil { return } form.AdminID = c.GetInt("adminID") id, err := pick.AddOrder(form) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{"id": id}) } func OrderEdit(c *gin.Context) { orderID := utils.StrTo(c.Param("id")).MustInt() if orderID <= 0 { app.Error(c, "订单 id 有误") return } var form form.MaterialPickOrderAdd if app.Bind(c, &form) != nil { return } form.AdminID = c.GetInt("adminID") id, err := pick.EditOrder(form, orderID) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{"id": id}) } func OrderList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]interface{}{"_orderby": "id desc", "admin_id": c.GetInt("adminID")} if keyword := c.Query("keyword"); keyword != "" { where["_or"] = []map[string]interface{}{ {"customer_name like": "%" + keyword + "%"}, {"customer_phone like": "%" + keyword + "%"}, } } type Auth struct { Edit bool `json:"edit"` Export bool `json:"export"` } type OrderList struct { ID int `json:"id"` CustomerName string `json:"customer_name"` CustomerPhone string `json:"customer_phone"` PkgID int `json:"pkg_id"` PkgName string `json:"pkg_name"` HouseAddress string `json:"house_address"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` Auth Auth `json:"auth"` CreatedAt string `json:"created_at"` } pkgMap := make(map[int]string, 0) if pkgList, err := pkg.GetPkgs(map[string]interface{}{"state": 1}, nil, app.Page{}, nil); err == nil { for _, v := range pkgList { pkgMap[v.ID] = v.PkgName } } orderList := make([]*OrderList, 0) if _, err := pick.GetOrders(where, nil, page, &orderList); err != nil { app.Error(c, err.Error()) return } orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id") for _, v := range orderList { v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["name"]) v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"]) v.PkgName = pkgMap[v.PkgID] v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.Auth = Auth{ Edit: utils.IsContain(pickParam.OrderAllow.Edit, v.State), Export: true, } } app.Success(c, orderList) } func OrderInfo(c *gin.Context) { orderID := utils.StrTo(c.Param("id")).MustInt() if orderID <= 0 { app.Error(c, "订单 id 有误") return } type Item struct { ID int `json:"id"` Num int `json:"num"` Level int `json:"level"` Remarks string `json:"remarks"` BrandName string `json:"brand_name"` ItemName string `json:"item_name"` UnitName string `json:"unit_name"` Color string `json:"color"` } type Room struct { Num int `json:"num"` Type int `json:"type"` } type OrderInfo struct { ID int `json:"id"` CustomerName string `json:"customer_name"` CustomerPhone string `json:"customer_phone"` HouseAddress string `json:"house_address"` HouseArea float64 `json:"house_area"` Content string `json:"content"` Item []map[int]Item `json:"item"` HouseStyle string `json:"house_style"` Room []structs.HouseStyle `json:"room"` RoomText string `json:"room_text"` PkgID int `json:"pkg_id"` PkgName string `json:"pkg_name"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` CreatedAt string `json:"created_at"` } var orderInfo *OrderInfo if _, err := pick.GetOrder(map[string]interface{}{"id": orderID}, nil, &orderInfo); err != nil { app.Error(c, err.Error()) return } contentData := make([]structs.MaterialPickOrderContent, 0) utils.JsonDecode(orderInfo.Content).To(&contentData) item := make([]map[int]Item, 0) for _, v := range contentData { itemMap := make(map[int]Item) for _, sub := range v.Picks { itemMap[sub.PickID] = Item{ ID: sub.ItemID, Num: sub.Num, Remarks: sub.Remarks, BrandName: sub.BrandName, ItemName: sub.ItemName, Level: sub.Level, UnitName: sub.UnitName, Color: sub.Color, } } item = append(item, itemMap) } orderInfo.Item = item utils.JsonDecode(orderInfo.HouseStyle).To(&orderInfo.Room) roomTypes := utils.ParseSliceMap(material.Params.RoomType, "id") for _, v := range orderInfo.Room { orderInfo.RoomText = orderInfo.RoomText + utils.ToStr(v.Num) + utils.ToStr(roomTypes[utils.ToStr(v.Type)]["short_name"]) + " " } strings.TrimRight(orderInfo.RoomText, " ") orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id") orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"]) orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"]) orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD HH:mm") if pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": orderInfo.PkgID}, []string{"id, pkg_name"}, nil); err == nil { orderInfo.PkgName = pkgInfo.PkgName } app.Success(c, orderInfo) }