蛋蛋星球 后台端
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.

hdl_level__management.go 12 KiB

3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package member_center
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. md "applet/app/md/institutional_management/member_center"
  6. "applet/app/utils"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  10. "errors"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. // GetLevelList
  15. // @Summary 制度中心-会员中心-等级管理(获取)
  16. // @Tags 会员中心
  17. // @Description 标签管理(获取)
  18. // @Accept json
  19. // @Produce json
  20. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  21. // @Param limit query int true "每页大小"
  22. // @Param page query int true "页数"
  23. // @Success 200 {object} md.GetLevelListResp "具体数据"
  24. // @Failure 400 {object} md.Response "具体错误"
  25. // @Router /api/memberCenter/levelManagement/getLevelList [get]
  26. func GetLevelList(c *gin.Context) {
  27. page := c.DefaultQuery("page", "1")
  28. limit := c.DefaultQuery("limit", "10")
  29. levelDb := implement.NewUserLevelDb(db.Db)
  30. levels, total, err := levelDb.UserLevelAllByAscByPage(utils.StrToInt(page), utils.StrToInt(limit))
  31. if err != nil {
  32. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  33. return
  34. }
  35. sql := "SElECT count(distinct id) as total, level FROM `user` GROUP BY level"
  36. results, err := db.QueryNativeString(db.Db, sql)
  37. if err != nil {
  38. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  39. return
  40. }
  41. var countMap = map[int]string{}
  42. for _, res := range results {
  43. countMap[utils.StrToInt(res["level"])] = res["total"]
  44. }
  45. list := make([]md.LevelListNode, 0)
  46. for i, level := range levels {
  47. list[i].LevelWeight = level.LevelWeight
  48. list[i].LevelID = level.Id
  49. list[i].LevelName = level.LevelName
  50. list[i].Count = countMap[level.Id]
  51. list[i].CreateAt = level.CreateAt
  52. list[i].IsUse = level.IsUse
  53. list[i].Memo = level.Memo
  54. }
  55. resp := md.GetLevelListResp{
  56. List: list,
  57. Paginate: md.Paginate{
  58. Limit: utils.StrToInt(limit),
  59. Page: utils.StrToInt(page),
  60. Total: total,
  61. },
  62. }
  63. e.OutSuc(c, resp, nil)
  64. }
  65. // GetOneLevel
  66. // @Summary 制度中心-会员中心-等级管理-等级明细(获取)
  67. // @Tags 会员中心
  68. // @Description 等级明细(获取)
  69. // @Accept json
  70. // @Produce json
  71. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  72. // @Param level_id query int true "等级 ID"
  73. // @Success 200 {object} md.GetOneLevelResp "具体数据"
  74. // @Failure 400 {object} md.Response "具体错误"
  75. // @Router /api/memberCenter/levelManagement/getOneLevel [get]
  76. func GetOneLevel(c *gin.Context) {
  77. id := c.Query("level_id")
  78. if id == "" {
  79. e.OutErr(c, e.ERR_INVALID_ARGS, errors.New("必须传入等级ID"))
  80. return
  81. }
  82. taskType := []map[string]interface{}{
  83. {
  84. "name": enum.UserLevelTaskType.String(enum.DirectPushMembersNum),
  85. "value": enum.DirectPushMembersNum,
  86. },
  87. {
  88. "name": enum.UserLevelTaskType.String(enum.DirectPushActiveDayNum),
  89. "value": enum.DirectPushMembersNum,
  90. },
  91. {
  92. "name": enum.UserLevelTaskType.String(enum.DirectPushLevel),
  93. "value": enum.DirectPushLevel,
  94. },
  95. }
  96. levelDb := implement.NewUserLevelDb(db.Db)
  97. level, err := levelDb.UserLevelByID(id)
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. userLevelTaskDb := implement.NewUserLevelTaskDb(db.Db)
  103. tasks, err := userLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{
  104. "key": "level_id",
  105. "value": id,
  106. })
  107. if err != nil {
  108. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  109. return
  110. }
  111. resp := md.GetOneLevelResp{
  112. TaskType: taskType,
  113. LevelID: level.Id,
  114. IsUse: level.IsUse,
  115. LevelName: level.LevelName,
  116. Memo: level.Memo,
  117. AutoUpdate: level.AutoUpdate,
  118. ChoosableNum: level.ChoosableNum,
  119. LevelWeight: level.LevelWeight,
  120. UserLevelTasks: tasks,
  121. }
  122. e.OutSuc(c, resp, nil)
  123. }
  124. // AddLevelTask
  125. // @Summary 制度中心-会员中心-等级明细-等级任务(新增)
  126. // @Tags 会员中心
  127. // @Description 等级任务(新增)
  128. // @Accept json
  129. // @Produce json
  130. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  131. // @Param req body md.AddLevelTaskReq true "需要新增的任务信息"
  132. // @Success 200 {int} "新增的任务ID"
  133. // @Failure 400 {object} md.Response "具体错误"
  134. // @Router /api/memberCenter/levelManagement/addLevelTask [post]
  135. func AddLevelTask(c *gin.Context) {
  136. var req *md.AddLevelTaskReq
  137. if err := c.ShouldBindJSON(&req); err != nil {
  138. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  139. return
  140. }
  141. task := model.UserLevelTask{
  142. Id: 0,
  143. LevelId: utils.StrToInt(req.LevelID),
  144. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  145. }
  146. if req.IsMustTask != "" {
  147. task.IsMustTask = utils.StrToInt(req.IsMustTask)
  148. }
  149. if req.TaskType != "" {
  150. task.TaskType = utils.StrToInt(req.TaskType)
  151. }
  152. if req.WithinDays != "" {
  153. task.WithinDays = utils.StrToInt(req.WithinDays)
  154. }
  155. if req.FinishCount != "" {
  156. task.FinishCount = utils.StrToInt(req.FinishCount)
  157. }
  158. if req.TaskTypeLevelId != "" {
  159. task.TaskTypeLevelId = utils.StrToInt(req.TaskTypeLevelId)
  160. }
  161. taskDb := implement.NewUserLevelTaskDb(db.Db)
  162. taskID, err := taskDb.UserLevelTaskInsert(&task)
  163. if err != nil {
  164. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  165. return
  166. }
  167. e.OutSuc(c, taskID, nil)
  168. }
  169. // UpdateLevelTask
  170. // @Summary 制度中心-会员中心-等级明细-等级任务(更新)
  171. // @Tags 会员中心
  172. // @Description 等级任务(更新)
  173. // @Accept json
  174. // @Produce json
  175. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  176. // @Param req body md.UpdateLevelTaskReq true "需要修改的等级任务信息"
  177. // @Success 200 {int} "成功修改数据条数"
  178. // @Failure 400 {object} md.Response "具体错误"
  179. // @Router /api/memberCenter/levelManagement/updateLevelTask [post]
  180. func UpdateLevelTask(c *gin.Context) {
  181. var req *md.UpdateLevelTaskReq
  182. if err := c.ShouldBindJSON(&req); err != nil {
  183. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  184. return
  185. }
  186. task := &model.UserLevelTask{
  187. Id: utils.StrToInt(req.TaskID),
  188. }
  189. if req.IsMustTask != "" {
  190. task.IsMustTask = utils.StrToInt(req.IsMustTask)
  191. }
  192. if req.TaskType != "" {
  193. task.TaskType = utils.StrToInt(req.TaskType)
  194. }
  195. if req.WithinDays != "" {
  196. task.WithinDays = utils.StrToInt(req.WithinDays)
  197. }
  198. if req.FinishCount != "" {
  199. task.FinishCount = utils.StrToInt(req.FinishCount)
  200. }
  201. if req.TaskTypeLevelId != "" {
  202. task.TaskTypeLevelId = utils.StrToInt(req.TaskTypeLevelId)
  203. }
  204. taskDb := implement.NewUserLevelTaskDb(db.Db)
  205. affected, err := taskDb.UserLevelTaskUpdate(task)
  206. if err != nil {
  207. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  208. return
  209. }
  210. e.OutSuc(c, affected, nil)
  211. }
  212. // UpdateLevel
  213. // @Summary 制度中心-会员中心-等级管理(更新)
  214. // @Tags 会员中心
  215. // @Description 等级管理(更新)
  216. // @Accept json
  217. // @Produce json
  218. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  219. // @Param req body md.UpdateLevelReq true "需要修改的等级信息"
  220. // @Success 200 {int} "成功修改数据条数"
  221. // @Failure 400 {object} md.Response "具体错误"
  222. // @Router /api/memberCenter/levelManagement/updateLevel [post]
  223. func UpdateLevel(c *gin.Context) {
  224. var req *md.UpdateLevelReq
  225. if err := c.ShouldBindJSON(&req); err != nil {
  226. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  227. return
  228. }
  229. level := &model.UserLevel{
  230. Id: utils.StrToInt(req.LevelID),
  231. LevelName: req.LevelName,
  232. Memo: req.Memo,
  233. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  234. }
  235. if req.LevelWeight != "" {
  236. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  237. }
  238. if req.IsUse == "0" || req.IsUse == "1" {
  239. level.LevelWeight = utils.StrToInt(req.IsUse)
  240. }
  241. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  242. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  243. }
  244. forceColumns := []string{"level_weight", "is_use"}
  245. levelDb := implement.NewUserLevelDb(db.Db)
  246. affected, err := levelDb.UserLevelUpdate(req.LevelID, level, forceColumns...)
  247. if err != nil {
  248. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  249. return
  250. }
  251. e.OutSuc(c, affected, nil)
  252. }
  253. // AddLevel
  254. // @Summary 制度中心-会员中心-等级管理(新增)
  255. // @Tags 会员中心
  256. // @Description 等级管理(新增)
  257. // @Accept json
  258. // @Produce json
  259. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  260. // @Param req body md.AddLevelReq true "需要新增的等级信息"
  261. // @Success 200 {int} "新增等级ID"
  262. // @Failure 400 {object} md.Response "具体错误"
  263. // @Router /api/memberCenter/levelManagement/addLevel [post]
  264. func AddLevel(c *gin.Context) {
  265. var req *md.AddLevelReq
  266. if err := c.ShouldBindJSON(&req); err != nil {
  267. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  268. return
  269. }
  270. level := &model.UserLevel{
  271. LevelName: req.LevelName,
  272. Memo: req.Memo,
  273. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  274. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  275. }
  276. if req.LevelWeight != "" {
  277. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  278. }
  279. if req.IsUse == "0" || req.IsUse == "1" {
  280. level.LevelWeight = utils.StrToInt(req.IsUse)
  281. }
  282. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  283. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  284. }
  285. levelDb := implement.NewUserLevelDb(db.Db)
  286. id, err := levelDb.UserLevelInsert(level)
  287. if err != nil {
  288. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  289. return
  290. }
  291. e.OutSuc(c, id, nil)
  292. }
  293. // DeleteLevelTask
  294. // @Summary 制度中心-会员中心-等级管理-等级任务(删除)
  295. // @Tags 会员中心
  296. // @Description 等级任务(删除)
  297. // @Accept json
  298. // @Produce json
  299. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  300. // @Param req body md.DeleteTaskReq true "需要删除的等级任务ID"
  301. // @Success 200 {int} "成功删除数据数量"
  302. // @Failure 400 {object} md.Response "具体错误"
  303. // @Router /api/memberCenter/levelManagement/deleteLevelTask [delete]
  304. func DeleteLevelTask(c *gin.Context) {
  305. var req *md.DeleteTaskReq
  306. if err := c.ShouldBindJSON(&req); err != nil {
  307. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  308. return
  309. }
  310. taskDb := implement.NewUserLevelTaskDb(db.Db)
  311. affected, err := taskDb.UserLevelTaskDelete(req.TaskID)
  312. if err != nil {
  313. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  314. return
  315. }
  316. e.OutSuc(c, affected, nil)
  317. }
  318. // DeleteLevel
  319. // @Summary 制度中心-会员中心-等级管理(删除)
  320. // @Tags 会员中心
  321. // @Description 等级管理(删除)
  322. // @Accept json
  323. // @Produce json
  324. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  325. // @Param req body md.DeleteLevelReq true "需要删除的等级 ID"
  326. // @Success 200 {int} "成功删除数据数量"
  327. // @Failure 400 {object} md.Response "具体错误"
  328. // @Router /api/memberCenter/levelManagement/deleteLevel [delete]
  329. func DeleteLevel(c *gin.Context) {
  330. var req *md.DeleteLevelReq
  331. if err := c.ShouldBindJSON(&req); err != nil {
  332. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  333. return
  334. }
  335. session := db.Db.NewSession()
  336. defer session.Close()
  337. session.Begin()
  338. levelDb := implement.NewUserLevelDb(db.Db)
  339. affected, err := levelDb.UserLevelDeleteBySession(session, req.LevelID)
  340. if err != nil {
  341. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  342. return
  343. }
  344. sql := "UPDATE `user` SET level = 0 WHERE level = ?"
  345. _, err = session.QueryString(sql, req.LevelID)
  346. if err != nil {
  347. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  348. return
  349. }
  350. session.Commit()
  351. e.OutSuc(c, affected, nil)
  352. }