item.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package material
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/form"
  7. "zhiyuan/services/material/brand"
  8. "zhiyuan/services/material/item"
  9. )
  10. func ItemList(c *gin.Context) {
  11. page := app.HandlePageNum(c)
  12. where := map[string]interface{}{"_orderby": "id desc", "deleted_at": 0}
  13. if name := c.Query("name"); name != "" {
  14. where["item_name like"] = "%" + name + "%"
  15. }
  16. if brand_id := c.Query("brand_id"); brand_id != "" {
  17. where["brand_id"] = brand_id
  18. }
  19. if type_id := c.Query("type_id"); type_id != "" {
  20. where["type_id"] = type_id
  21. }
  22. type ItemList struct {
  23. ID int `json:"id"`
  24. ItemName string `json:"item_name"`
  25. Level int `json:"level"`
  26. Unit int `json:"unit"`
  27. BrandID int `json:"brand_id"`
  28. BrandName string `json:"brand_name"`
  29. TypeID int `json:"type_id"`
  30. TypeName string `json:"type_name"`
  31. CreatedAt string `json:"created_at"`
  32. }
  33. total, err := item.Count(where)
  34. if err != nil {
  35. app.Error(c, err.Error())
  36. return
  37. }
  38. itemList := make([]ItemList, 0)
  39. if _, err := item.GetList(where, nil, page, &itemList); err != nil {
  40. app.Error(c, err.Error())
  41. return
  42. }
  43. brandIds := make([]int, 0)
  44. for _, v := range itemList {
  45. brandIds = append(brandIds, v.BrandID)
  46. }
  47. brandListMap := make(map[int]string, 0)
  48. if len(brandIds) > 0 {
  49. if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err != nil {
  50. app.Error(c, err.Error())
  51. return
  52. } else {
  53. for _, v := range brandList {
  54. brandListMap[v.ID] = v.BrandName
  55. }
  56. }
  57. }
  58. typeListMap := item.GetTypeMapByCache()
  59. for k, v := range itemList {
  60. v.BrandName = brandListMap[v.BrandID]
  61. v.TypeName = typeListMap[v.TypeID]
  62. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  63. itemList[k] = v
  64. }
  65. data := gin.H{
  66. "list": itemList,
  67. "total": total,
  68. "limit": page.PageSize,
  69. }
  70. app.Success(c, data)
  71. }
  72. func ItemAdd(c *gin.Context) {
  73. var form form.MaterialItemAdd
  74. if app.Bind(c, &form) != nil {
  75. return
  76. }
  77. if id, err := item.Add(form); err != nil {
  78. app.Error(c, err.Error())
  79. return
  80. } else {
  81. app.Success(c, gin.H{"id": id})
  82. }
  83. }
  84. func ItemEdit(c *gin.Context) {
  85. id := utils.StrTo(c.Param("id")).MustInt()
  86. if id <= 0 {
  87. app.Error(c, "id有误")
  88. return
  89. }
  90. var form form.MaterialItemAdd
  91. if app.Bind(c, &form) != nil {
  92. return
  93. }
  94. if _, err := item.Edit(form, id); err != nil {
  95. app.ErrorMsg(c, err.Error(), nil)
  96. return
  97. }
  98. app.Success(c, nil)
  99. }
  100. func ItemInfo(c *gin.Context) {
  101. id := utils.StrTo(c.Param("id")).MustInt()
  102. if id <= 0 {
  103. app.Error(c, "id有误")
  104. return
  105. }
  106. type ItemInfo struct {
  107. ID int `json:"id"`
  108. BrandID int `json:"brand_id"`
  109. ItemName string `json:"item_name"`
  110. TypeID int `json:"type_id"`
  111. Level int `json:"level"`
  112. Price float64 `json:"price"`
  113. Unit int `json:"unit"`
  114. Pic string `json:"pic"`
  115. Content string `json:"content"`
  116. Colors []string `json:"colors"`
  117. }
  118. colors := make([]string, 0)
  119. if optionList, err := item.GetOptionList(map[string]interface{}{"item_id": id}, nil, app.Page{}, nil); err == nil {
  120. for _, v := range optionList {
  121. colors = append(colors, v.OptionName)
  122. }
  123. }
  124. var itemInfo *ItemInfo
  125. if _, err := item.GetOne(map[string]interface{}{"id": id}, nil, &itemInfo); err != nil {
  126. app.ErrorMsg(c, err.Error(), nil)
  127. return
  128. }
  129. itemInfo.Colors = colors
  130. if detail, _ := item.GetDetailOne(map[string]interface{}{"item_id": id}, nil, nil); detail != nil {
  131. itemInfo.Content = detail.Content
  132. }
  133. app.Success(c, itemInfo)
  134. }
  135. func ItemDel(c *gin.Context) {
  136. id := utils.StrTo(c.Param("id")).MustInt()
  137. if id <= 0 {
  138. app.Error(c, "工单 id 有误")
  139. return
  140. }
  141. err := item.Del(id)
  142. if err != nil {
  143. app.Error(c, err.Error())
  144. return
  145. }
  146. app.Success(c, nil)
  147. }
  148. func TypeList(c *gin.Context) {
  149. isTree := utils.ToInt(c.Query("tree"))
  150. if isTree == 1 {
  151. TypeListTree(c)
  152. return
  153. }
  154. isSelect := utils.ToInt(c.Query("select"))
  155. if isSelect == 1 {
  156. TypeListSelect(c)
  157. return
  158. }
  159. page := app.HandlePageNum(c)
  160. where := make(map[string]interface{})
  161. total, err := item.CountType(where)
  162. if err != nil {
  163. app.Error(c, err.Error())
  164. return
  165. }
  166. types, err := item.GetList(where, []string{"id", "type_name"}, page, nil)
  167. if err != nil {
  168. app.Error(c, err.Error())
  169. return
  170. }
  171. data := gin.H{
  172. "list": types,
  173. "total": total,
  174. "limit": page.PageSize,
  175. }
  176. app.Success(c, data)
  177. }
  178. func TypeListSelect(c *gin.Context) {
  179. where := make(map[string]interface{})
  180. types, err := item.GetList(where, []string{"id", "type_name"}, app.Page{}, nil)
  181. if err != nil {
  182. app.Error(c, err.Error())
  183. return
  184. }
  185. app.Success(c, types)
  186. }
  187. func TypeListTree(c *gin.Context) {
  188. where := make(map[string]interface{})
  189. category := utils.ToInt(c.Query("category"))
  190. if category > 0 {
  191. where["category"] = category
  192. }
  193. auths, err := item.GetTypeList(where, []string{"id", "type_name", "pid", "category"}, app.Page{}, nil)
  194. if err != nil {
  195. app.Error(c, err.Error())
  196. return
  197. }
  198. app.Success(c, utils.GenTree(auths, "id", "pid", "children", 0))
  199. }
  200. func TypeAdd(c *gin.Context) {
  201. var form form.MaterialItemTypeAdd
  202. if app.Bind(c, &form) != nil {
  203. return
  204. }
  205. if id, err := item.AddType(form); err != nil {
  206. app.Error(c, err.Error())
  207. return
  208. } else {
  209. app.Success(c, gin.H{"id": id})
  210. }
  211. }
  212. func TypeEdit(c *gin.Context) {
  213. id := utils.StrTo(c.Param("id")).MustInt()
  214. if id <= 0 {
  215. app.Error(c, "类型id有误")
  216. return
  217. }
  218. var form form.MaterialItemTypeAdd
  219. if app.Bind(c, &form) != nil {
  220. return
  221. }
  222. if _, err := item.EditType(form, id); err != nil {
  223. app.ErrorMsg(c, err.Error(), nil)
  224. return
  225. }
  226. app.Success(c, nil)
  227. }