智慧食堂
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.

229 regels
6.2 KiB

  1. package hdl
  2. import (
  3. "applet/app/customer/lib/validate"
  4. "applet/app/customer/md"
  5. "applet/app/customer/svc"
  6. svc2 "applet/app/customer/svc/order"
  7. "applet/app/db"
  8. "applet/app/db/model"
  9. "applet/app/e"
  10. "applet/app/enum"
  11. "applet/app/utils"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. func SaveCentralKitchenForSchoolUserIdentity(c *gin.Context) {
  16. var req md.SaveCentralKitchenForSchoolUserIdentityReq
  17. err := c.ShouldBindJSON(&req)
  18. if err != nil {
  19. err = validate.HandleValidateErr(err)
  20. err1 := err.(e.E)
  21. e.OutErr(c, err1.Code, err1.Error())
  22. return
  23. }
  24. user := svc.GetUser(c)
  25. var identity = enum.UserIdentityForCentralKitchenForStudent
  26. if req.IsTeacher {
  27. identity = enum.UserIdentityForCentralKitchenForTeacher
  28. }
  29. //1、判断当前身份是否已绑定
  30. userIdentityDb := db.UserIdentityDb{}
  31. userIdentityDb.Set(user.Id)
  32. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
  33. if err != nil {
  34. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  35. return
  36. }
  37. if isHasUserIdentity != nil && isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
  38. e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
  39. return
  40. }
  41. //2、新增身份信息
  42. now := time.Time{}
  43. userIdentity := &model.UserIdentity{
  44. Uid: user.Id,
  45. Name: req.Name,
  46. IdNo: req.IdNo,
  47. Kind: enum.UserIdentityKindForCommon,
  48. Identity: identity,
  49. EnterpriseId: req.EnterpriseId,
  50. State: enum.UserIdentityStateForNormal,
  51. Memo: "",
  52. CreateAt: now,
  53. UpdateAt: now,
  54. }
  55. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  56. if err != nil {
  57. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  58. return
  59. }
  60. if insertAffected <= 0 {
  61. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  62. return
  63. }
  64. //3、新增 class_with_user 记录
  65. if !req.IsTeacher {
  66. classWithUserDb := db.ClassWithUserDb{}
  67. classWithUserDb.Set()
  68. _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  69. UserIdentityId: insertAffected,
  70. ClassId: req.ClassId,
  71. CreateAt: now,
  72. UpdateAt: now,
  73. })
  74. if err != nil {
  75. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  76. return
  77. }
  78. }
  79. e.OutSuc(c, "success", nil)
  80. return
  81. }
  82. func SaveSelfSupportForSchoolUserIdentity(c *gin.Context) {
  83. var req md.SaveSelfSupportForSchoolUserIdentityReq
  84. err := c.ShouldBindJSON(&req)
  85. if err != nil {
  86. err = validate.HandleValidateErr(err)
  87. err1 := err.(e.E)
  88. e.OutErr(c, err1.Code, err1.Error())
  89. return
  90. }
  91. user := svc.GetUser(c)
  92. now := time.Time{}
  93. var identity = enum.UserIdentityForSelfSupportForStudent
  94. var kind = enum.UserIdentityKindForCommon
  95. if req.Kind == enum.UserIdentityKindForWorker {
  96. kind = enum.UserIdentityKindForWorker
  97. identity = enum.UserIdentityForSelfSupportForWorker
  98. } else {
  99. if req.IsTeacher {
  100. identity = enum.UserIdentityForCentralKitchenForTeacher
  101. }
  102. }
  103. //1、判断当前身份是否已绑定
  104. userIdentityDb := db.UserIdentityDb{}
  105. userIdentityDb.Set(user.Id)
  106. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
  107. if err != nil {
  108. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  109. return
  110. }
  111. if isHasUserIdentity == nil {
  112. if identity != enum.UserIdentityForSelfSupportForStudent {
  113. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  114. return
  115. }
  116. //2、新增身份信息
  117. userIdentity := &model.UserIdentity{
  118. Uid: user.Id,
  119. Name: req.Name,
  120. IdNo: req.IdNo,
  121. Kind: kind,
  122. Identity: identity,
  123. EnterpriseId: req.EnterpriseId,
  124. State: enum.UserIdentityStateForNormal,
  125. Memo: "",
  126. CreateAt: now,
  127. UpdateAt: now,
  128. }
  129. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  130. if err != nil {
  131. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  132. return
  133. }
  134. if insertAffected <= 0 {
  135. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  136. return
  137. }
  138. //3、新增 class_with_user 记录
  139. if !req.IsTeacher {
  140. classWithUserDb := db.ClassWithUserDb{}
  141. classWithUserDb.Set()
  142. _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  143. UserIdentityId: insertAffected,
  144. ClassId: req.ClassId,
  145. CreateAt: now,
  146. UpdateAt: now,
  147. })
  148. if err != nil {
  149. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  150. return
  151. }
  152. }
  153. } else {
  154. if isHasUserIdentity != nil && isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
  155. e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
  156. return
  157. }
  158. //4、修改身份信息
  159. isHasUserIdentity.Uid = user.Id
  160. isHasUserIdentity.Name = req.Name
  161. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name")
  162. if err != nil {
  163. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  164. return
  165. }
  166. if identity == enum.UserIdentityForSelfSupportForStudent {
  167. //5、修改 class_with_user 记录
  168. classWithUserDb := db.ClassWithUserDb{}
  169. classWithUserDb.Set()
  170. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
  171. if err1 != nil {
  172. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  173. return
  174. }
  175. if classWithUser == nil {
  176. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  177. UserIdentityId: isHasUserIdentity.Id,
  178. ClassId: req.ClassId,
  179. CreateAt: now,
  180. UpdateAt: now,
  181. })
  182. if err2 != nil {
  183. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  184. return
  185. }
  186. } else {
  187. classWithUser.ClassId = req.ClassId
  188. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
  189. if err2 != nil {
  190. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  191. return
  192. }
  193. }
  194. }
  195. }
  196. e.OutSuc(c, "success", nil)
  197. return
  198. }
  199. func CentralKitchenForSchoolMyReserve(c *gin.Context) {
  200. date := c.DefaultQuery("date", "")
  201. userIdentityId := c.DefaultQuery("user_identity_id", "")
  202. if date == "" {
  203. e.OutErr(c, e.ERR_INVALID_ARGS, "日期不能为空")
  204. return
  205. }
  206. startDate := date + "-01"
  207. startDateTime, _ := time.Parse("2006-01-02", startDate)
  208. endDate := startDateTime.AddDate(0, 1, -1).Format("2006-01-02")
  209. err, resp := svc2.CentralKitchenForSchoolMyReserve(utils.StrToInt(userIdentityId), date, startDate, endDate)
  210. if err != nil {
  211. e.OutErr(c, e.ERR, err.Error())
  212. return
  213. }
  214. e.OutSuc(c, map[string]interface{}{
  215. "info": resp,
  216. }, nil)
  217. return
  218. }