order.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package pick
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "strings"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/param/material"
  7. pickParam "zhiyuan/pkg/param/material/pick"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/form"
  10. "zhiyuan/services/material/pick"
  11. "zhiyuan/services/material/pkg"
  12. "zhiyuan/services/structs"
  13. )
  14. func OrderAdd(c *gin.Context) {
  15. var form form.MaterialPickOrderAdd
  16. if app.Bind(c, &form) != nil {
  17. return
  18. }
  19. form.AdminID = c.GetInt("adminID")
  20. id, err := pick.AddOrder(form)
  21. if err != nil {
  22. app.ErrorMsg(c, err.Error(), nil)
  23. return
  24. }
  25. app.Success(c, gin.H{"id": id})
  26. }
  27. func OrderEdit(c *gin.Context) {
  28. orderID := utils.StrTo(c.Param("id")).MustInt()
  29. if orderID <= 0 {
  30. app.Error(c, "订单 id 有误")
  31. return
  32. }
  33. var form form.MaterialPickOrderAdd
  34. if app.Bind(c, &form) != nil {
  35. return
  36. }
  37. form.AdminID = c.GetInt("adminID")
  38. id, err := pick.EditOrder(form, orderID)
  39. if err != nil {
  40. app.ErrorMsg(c, err.Error(), nil)
  41. return
  42. }
  43. app.Success(c, gin.H{"id": id})
  44. }
  45. func OrderList(c *gin.Context) {
  46. page := app.HandlePageNum(c)
  47. where := map[string]interface{}{"_orderby": "id desc", "admin_id": c.GetInt("adminID")}
  48. if keyword := c.Query("keyword"); keyword != "" {
  49. where["_or"] = []map[string]interface{}{
  50. {"customer_name like": "%" + keyword + "%"},
  51. {"customer_phone like": "%" + keyword + "%"},
  52. }
  53. }
  54. type Auth struct {
  55. Edit bool `json:"edit"`
  56. Export bool `json:"export"`
  57. }
  58. type OrderList struct {
  59. ID int `json:"id"`
  60. CustomerName string `json:"customer_name"`
  61. CustomerPhone string `json:"customer_phone"`
  62. PkgID int `json:"pkg_id"`
  63. PkgName string `json:"pkg_name"`
  64. HouseAddress string `json:"house_address"`
  65. State int `json:"state"`
  66. StateName string `json:"state_name"`
  67. StateColor string `json:"state_color"`
  68. Auth Auth `json:"auth"`
  69. CreatedAt string `json:"created_at"`
  70. }
  71. pkgMap := make(map[int]string, 0)
  72. if pkgList, err := pkg.GetPkgs(map[string]interface{}{"state": 1}, nil, app.Page{}, nil); err == nil {
  73. for _, v := range pkgList {
  74. pkgMap[v.ID] = v.PkgName
  75. }
  76. }
  77. orderList := make([]*OrderList, 0)
  78. if _, err := pick.GetOrders(where, nil, page, &orderList); err != nil {
  79. app.Error(c, err.Error())
  80. return
  81. }
  82. orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id")
  83. for _, v := range orderList {
  84. v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["name"])
  85. v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"])
  86. v.PkgName = pkgMap[v.PkgID]
  87. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  88. v.Auth = Auth{
  89. Edit: utils.IsContain(pickParam.OrderAllow.Edit, v.State),
  90. Export: true,
  91. }
  92. }
  93. app.Success(c, orderList)
  94. }
  95. func OrderInfo(c *gin.Context) {
  96. orderID := utils.StrTo(c.Param("id")).MustInt()
  97. if orderID <= 0 {
  98. app.Error(c, "订单 id 有误")
  99. return
  100. }
  101. type Item struct {
  102. ID int `json:"id"`
  103. Num int `json:"num"`
  104. Level int `json:"level"`
  105. Remarks string `json:"remarks"`
  106. BrandName string `json:"brand_name"`
  107. ItemName string `json:"item_name"`
  108. UnitName string `json:"unit_name"`
  109. Color string `json:"color"`
  110. }
  111. type Room struct {
  112. Num int `json:"num"`
  113. Type int `json:"type"`
  114. }
  115. type OrderInfo struct {
  116. ID int `json:"id"`
  117. CustomerName string `json:"customer_name"`
  118. CustomerPhone string `json:"customer_phone"`
  119. HouseAddress string `json:"house_address"`
  120. HouseArea float64 `json:"house_area"`
  121. Content string `json:"content"`
  122. Item []map[int]Item `json:"item"`
  123. HouseStyle string `json:"house_style"`
  124. Room []structs.HouseStyle `json:"room"`
  125. RoomText string `json:"room_text"`
  126. PkgID int `json:"pkg_id"`
  127. PkgName string `json:"pkg_name"`
  128. State int `json:"state"`
  129. StateName string `json:"state_name"`
  130. StateColor string `json:"state_color"`
  131. CreatedAt string `json:"created_at"`
  132. }
  133. var orderInfo *OrderInfo
  134. if _, err := pick.GetOrder(map[string]interface{}{"id": orderID}, nil, &orderInfo); err != nil {
  135. app.Error(c, err.Error())
  136. return
  137. }
  138. contentData := make([]structs.MaterialPickOrderContent, 0)
  139. utils.JsonDecode(orderInfo.Content).To(&contentData)
  140. item := make([]map[int]Item, 0)
  141. for _, v := range contentData {
  142. itemMap := make(map[int]Item)
  143. for _, sub := range v.Picks {
  144. itemMap[sub.PickID] = Item{
  145. ID: sub.ItemID,
  146. Num: sub.Num,
  147. Remarks: sub.Remarks,
  148. BrandName: sub.BrandName,
  149. ItemName: sub.ItemName,
  150. Level: sub.Level,
  151. UnitName: sub.UnitName,
  152. Color: sub.Color,
  153. }
  154. }
  155. item = append(item, itemMap)
  156. }
  157. orderInfo.Item = item
  158. utils.JsonDecode(orderInfo.HouseStyle).To(&orderInfo.Room)
  159. roomTypes := utils.ParseSliceMap(material.Params.RoomType, "id")
  160. for _, v := range orderInfo.Room {
  161. orderInfo.RoomText = orderInfo.RoomText + utils.ToStr(v.Num) + utils.ToStr(roomTypes[utils.ToStr(v.Type)]["short_name"]) + " "
  162. }
  163. strings.TrimRight(orderInfo.RoomText, " ")
  164. orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id")
  165. orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"])
  166. orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"])
  167. orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD HH:mm")
  168. if pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": orderInfo.PkgID}, []string{"id, pkg_name"}, nil); err == nil {
  169. orderInfo.PkgName = pkgInfo.PkgName
  170. }
  171. app.Success(c, orderInfo)
  172. }