面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

275 lines
6.0 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/admin/svc"
  7. svc2 "applet/app/admin/svc/category"
  8. "applet/app/admin/svc/goods"
  9. "applet/app/db"
  10. "applet/app/db/model"
  11. "applet/app/e"
  12. "applet/app/utils"
  13. "github.com/gin-gonic/gin"
  14. "time"
  15. )
  16. func SchemeList(c *gin.Context) {
  17. schemeDb := db.SchemeDb{}
  18. schemeDb.Set()
  19. schemeList, err := schemeDb.FindScheme()
  20. if err != nil {
  21. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  22. return
  23. }
  24. schemeEnterpriseDb := db.SchemeEnterpriseDb{}
  25. schemeEnterpriseDb.Set()
  26. enterpriseDb := db.EnterpriseDb{}
  27. enterpriseDb.Set()
  28. var result []*md.SchemeListResp
  29. for _, v := range *schemeList {
  30. var temp md.SchemeListResp
  31. temp.Data = v
  32. schemeWithEnterprises, err1 := schemeEnterpriseDb.FindSchemeEnterprise(v.Id)
  33. if err1 != nil {
  34. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  35. return
  36. }
  37. for _, schemeWithEnterprise := range *schemeWithEnterprises {
  38. enterprise, err2 := enterpriseDb.GetEnterprise(schemeWithEnterprise.EnterpriseId)
  39. if err2 != nil {
  40. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  41. return
  42. }
  43. temp.EnterpriseList = append(temp.EnterpriseList, struct {
  44. Name string `json:"name"`
  45. }{
  46. Name: enterprise.Name,
  47. })
  48. }
  49. result = append(result, &temp)
  50. }
  51. e.OutSuc(c, map[string]interface{}{
  52. "list": result,
  53. "state": []map[string]interface{}{
  54. {
  55. "name": enum.SchemeState(enum.SchemeStateForNormal).String(),
  56. "value": enum.SchemeStateForNormal,
  57. },
  58. {
  59. "name": enum.SchemeState(enum.SchemeStateForFreeze).String(),
  60. "value": enum.SchemeStateForFreeze,
  61. },
  62. },
  63. }, nil)
  64. return
  65. }
  66. func AddScheme(c *gin.Context) {
  67. var req md.AddSchemeReq
  68. err := c.ShouldBindJSON(&req)
  69. if err != nil {
  70. err = validate.HandleValidateErr(err)
  71. err1 := err.(e.E)
  72. e.OutErr(c, err1.Code, err1.Error())
  73. return
  74. }
  75. schemeDb := db.SchemeDb{}
  76. schemeDb.Set()
  77. now := time.Now()
  78. _, err = schemeDb.SchemeInsert(&model.Scheme{
  79. Name: req.Name,
  80. State: enum.SchemeStateForNormal,
  81. Memo: req.Memo,
  82. CreateAt: now.Format("2006-01-02 15:04:05"),
  83. UpdateAt: now.Format("2006-01-02 15:04:05"),
  84. })
  85. if err != nil {
  86. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  87. return
  88. }
  89. e.OutSuc(c, "success", nil)
  90. return
  91. }
  92. func UpdateScheme(c *gin.Context) {
  93. var req md.UpdateSchemeReq
  94. err := c.ShouldBindJSON(&req)
  95. if err != nil {
  96. err = validate.HandleValidateErr(err)
  97. err1 := err.(e.E)
  98. e.OutErr(c, err1.Code, err1.Error())
  99. return
  100. }
  101. schemeDb := db.SchemeDb{}
  102. schemeDb.Set()
  103. scheme, err := schemeDb.GetScheme(req.Id)
  104. if err != nil {
  105. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  106. return
  107. }
  108. if scheme == nil {
  109. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  110. return
  111. }
  112. scheme.Name = req.Name
  113. scheme.Memo = req.Memo
  114. _, err = schemeDb.UpdateScheme(scheme, "name", "memo")
  115. if err != nil {
  116. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  117. return
  118. }
  119. e.OutSuc(c, "success", nil)
  120. return
  121. }
  122. func SchemeBindEnterprise(c *gin.Context) {
  123. var req md.SchemeBindEnterpriseReq
  124. err := c.ShouldBindJSON(&req)
  125. if err != nil {
  126. err = validate.HandleValidateErr(err)
  127. err1 := err.(e.E)
  128. e.OutErr(c, err1.Code, err1.Error())
  129. return
  130. }
  131. err = svc.SchemeBindEnterprise(req)
  132. if err != nil {
  133. e.OutErr(c, e.ERR, err.Error())
  134. return
  135. }
  136. e.OutSuc(c, "success", nil)
  137. return
  138. }
  139. func UpdateSchemeState(c *gin.Context) {
  140. var req md.UpdateSchemeStateReq
  141. err := c.ShouldBindJSON(&req)
  142. if err != nil {
  143. err = validate.HandleValidateErr(err)
  144. err1 := err.(e.E)
  145. e.OutErr(c, err1.Code, err1.Error())
  146. return
  147. }
  148. schemeDb := db.SchemeDb{}
  149. schemeDb.Set()
  150. scheme, err := schemeDb.GetScheme(req.Id)
  151. if err != nil {
  152. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  153. return
  154. }
  155. if scheme == nil {
  156. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  157. return
  158. }
  159. scheme.State = req.State
  160. _, err = schemeDb.UpdateScheme(scheme, "state")
  161. if err != nil {
  162. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  163. return
  164. }
  165. e.OutSuc(c, "success", nil)
  166. return
  167. }
  168. func DeleteScheme(c *gin.Context) {
  169. id := c.Param("id")
  170. schemeDb := db.SchemeDb{}
  171. schemeDb.Set()
  172. scheme, err := schemeDb.GetScheme(utils.StrToInt(id))
  173. if err != nil {
  174. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  175. return
  176. }
  177. if scheme == nil {
  178. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  179. return
  180. }
  181. err = svc.DeleteScheme(utils.StrToInt(id))
  182. if err != nil {
  183. e.OutErr(c, e.ERR, err.Error())
  184. return
  185. }
  186. e.OutSuc(c, "success", nil)
  187. return
  188. }
  189. func SchemeGoodsDetail(c *gin.Context) {
  190. goodsId := c.Query("goods_id")
  191. if goodsId == "" {
  192. goodsId = "0"
  193. }
  194. schemeId := c.Query("scheme_id")
  195. if schemeId == "" {
  196. schemeId = "0"
  197. }
  198. resp, err := svc.MallGoodsDetailByScheme(c, utils.StrToInt64(goodsId), utils.StrToInt(schemeId))
  199. if err != nil {
  200. e.OutErr(c, 400, err.Error())
  201. return
  202. }
  203. e.OutSuc(c, resp, nil)
  204. return
  205. }
  206. func SaveSchemeGoods(c *gin.Context) {
  207. var req md.SaveSchemeGoodsReq
  208. err := c.ShouldBindJSON(&req)
  209. if err != nil {
  210. err = validate.HandleValidateErr(err)
  211. err1 := err.(e.E)
  212. e.OutErr(c, err1.Code, err1.Error())
  213. return
  214. }
  215. err = svc.SaveSchemeGoods(req)
  216. if err != nil {
  217. e.OutErr(c, e.ERR, err.Error())
  218. return
  219. }
  220. e.OutSuc(c, "success", nil)
  221. return
  222. }
  223. func GoodsList(c *gin.Context) {
  224. var args md.MallGoodsListReq
  225. args.Title = c.Query("title")
  226. args.CategoryId = c.Query("category_id")
  227. args.SaleState = c.Query("sale_state")
  228. args.PageId = c.Query("page")
  229. args.PageSize = c.Query("limit")
  230. args.SkuCode = c.Query("sku_code")
  231. tree, err := svc2.GetAllCategory(c)
  232. data, total, err := goods.GetMallGoodsList(c, &args)
  233. if err != nil {
  234. e.OutErr(c, e.ERR_NO_DATA, err)
  235. return
  236. }
  237. var returnData = map[string]interface{}{
  238. "total": total,
  239. "cateList": tree,
  240. // "dataList": data,
  241. //-1审核拒绝 0审核中 1销售中 2已售罄 3放入仓库 4下架
  242. "stateList": []map[string]interface{}{
  243. {"name": "全部", "value": 99},
  244. {"name": "销售中", "value": 1},
  245. {"name": "已下架", "value": 2},
  246. },
  247. }
  248. if data == nil {
  249. returnData["dataList"] = []string{}
  250. } else {
  251. returnData["dataList"] = data
  252. }
  253. e.OutSuc(c, returnData, nil)
  254. return
  255. }