智慧食堂
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

476 řádky
14 KiB

  1. package hdl
  2. import (
  3. "applet/app/customer/lib/validate"
  4. "applet/app/customer/md"
  5. "applet/app/customer/svc"
  6. "applet/app/db"
  7. "applet/app/db/model"
  8. "applet/app/e"
  9. "applet/app/enum"
  10. "applet/app/utils"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. func SaveCentralKitchenForSchoolUserIdentity(c *gin.Context) {
  15. var req md.SaveCentralKitchenForSchoolUserIdentityReq
  16. err := c.ShouldBindJSON(&req)
  17. if err != nil {
  18. err = validate.HandleValidateErr(err)
  19. err1 := err.(e.E)
  20. e.OutErr(c, err1.Code, err1.Error())
  21. return
  22. }
  23. user := svc.GetUser(c)
  24. var identity = enum.UserIdentityForCentralKitchenForStudent
  25. if req.IsTeacher {
  26. identity = enum.UserIdentityForCentralKitchenForTeacher
  27. }
  28. //1、判断当前身份是否已绑定
  29. userIdentityDb := db.UserIdentityDb{}
  30. userIdentityDb.Set(user.Id)
  31. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
  32. if err != nil {
  33. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  34. return
  35. }
  36. now := time.Now()
  37. if isHasUserIdentity == nil {
  38. //2、新增身份信息
  39. userIdentity := &model.UserIdentity{
  40. Uid: user.Id,
  41. Name: req.Name,
  42. IdNo: req.IdNo,
  43. Kind: enum.UserIdentityKindForCommon,
  44. Identity: identity,
  45. EnterpriseId: req.EnterpriseId,
  46. State: enum.UserIdentityStateForNormal,
  47. Memo: "",
  48. CreateAt: now.Format("2006-01-02 15:04:05"),
  49. UpdateAt: now.Format("2006-01-02 15:04:05"),
  50. }
  51. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  52. if err != nil {
  53. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  54. return
  55. }
  56. if insertAffected <= 0 {
  57. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  58. return
  59. }
  60. //3、新增 class_with_user 记录
  61. if !req.IsTeacher {
  62. classWithUserDb := db.ClassWithUserDb{}
  63. classWithUserDb.Set()
  64. _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  65. UserIdentityId: insertAffected,
  66. ClassId: req.ClassId,
  67. CreateAt: now.Format("2006-01-02 15:04:05"),
  68. UpdateAt: now.Format("2006-01-02 15:04:05"),
  69. })
  70. if err != nil {
  71. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  72. return
  73. }
  74. }
  75. } else {
  76. //4、修改身份信息
  77. isHasUserIdentity.Uid = user.Id
  78. isHasUserIdentity.Name = req.Name
  79. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  80. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  81. if err != nil {
  82. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  83. return
  84. }
  85. //5、修改 class_with_user 记录
  86. if !req.IsTeacher {
  87. classWithUserDb := db.ClassWithUserDb{}
  88. classWithUserDb.Set()
  89. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
  90. if err1 != nil {
  91. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  92. return
  93. }
  94. if classWithUser == nil {
  95. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  96. UserIdentityId: isHasUserIdentity.Id,
  97. ClassId: req.ClassId,
  98. CreateAt: now.Format("2006-01-02 15:04:05"),
  99. UpdateAt: now.Format("2006-01-02 15:04:05"),
  100. })
  101. if err2 != nil {
  102. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  103. return
  104. }
  105. } else {
  106. classWithUser.ClassId = req.ClassId
  107. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
  108. if err2 != nil {
  109. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  110. return
  111. }
  112. }
  113. }
  114. }
  115. e.OutSuc(c, "success", nil)
  116. return
  117. }
  118. func SaveSelfSupportForSchoolUserIdentity(c *gin.Context) {
  119. var req md.SaveSelfSupportForSchoolUserIdentityReq
  120. err := c.ShouldBindJSON(&req)
  121. if err != nil {
  122. err = validate.HandleValidateErr(err)
  123. err1 := err.(e.E)
  124. e.OutErr(c, err1.Code, err1.Error())
  125. return
  126. }
  127. user := svc.GetUser(c)
  128. now := time.Now()
  129. var identity = enum.UserIdentityForSelfSupportForStudent
  130. var kind = enum.UserIdentityKindForCommon
  131. if req.Kind == enum.UserIdentityKindForWorker {
  132. kind = enum.UserIdentityKindForWorker
  133. identity = enum.UserIdentityForSelfSupportForWorker
  134. } else {
  135. if req.IsTeacher {
  136. identity = enum.UserIdentityForCentralKitchenForTeacher
  137. }
  138. }
  139. //1、判断当前身份是否已绑定
  140. userIdentityDb := db.UserIdentityDb{}
  141. userIdentityDb.Set(user.Id)
  142. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
  143. if err != nil {
  144. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  145. return
  146. }
  147. if isHasUserIdentity == nil {
  148. if identity != enum.UserIdentityForSelfSupportForStudent {
  149. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  150. return
  151. }
  152. //2、新增身份信息
  153. userIdentity := &model.UserIdentity{
  154. Uid: user.Id,
  155. Name: req.Name,
  156. IdNo: req.IdNo,
  157. Kind: kind,
  158. Identity: identity,
  159. EnterpriseId: req.EnterpriseId,
  160. State: enum.UserIdentityStateForNormal,
  161. Memo: "",
  162. CreateAt: now.Format("2006-01-02 15:04:05"),
  163. UpdateAt: now.Format("2006-01-02 15:04:05"),
  164. }
  165. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  166. if err != nil {
  167. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  168. return
  169. }
  170. if insertAffected <= 0 {
  171. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  172. return
  173. }
  174. //3、新增 class_with_user 记录
  175. if !req.IsTeacher {
  176. classWithUserDb := db.ClassWithUserDb{}
  177. classWithUserDb.Set()
  178. _, err1 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  179. UserIdentityId: insertAffected,
  180. ClassId: req.ClassId,
  181. CreateAt: now.Format("2006-01-02 15:04:05"),
  182. UpdateAt: now.Format("2006-01-02 15:04:05"),
  183. })
  184. if err1 != nil {
  185. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  186. return
  187. }
  188. }
  189. //4、新增 self_support_for_user_face_info 记录
  190. selfSupportForSchoolInfoDb := db.SelfSupportForSchoolInfoDb{}
  191. selfSupportForSchoolInfoDb.Set(req.EnterpriseId)
  192. selfSupportForSchoolInfo, err := selfSupportForSchoolInfoDb.GetSelfSupportForSchoolInfo()
  193. if err != nil {
  194. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  195. return
  196. }
  197. if selfSupportForSchoolInfo == nil {
  198. e.OutErr(c, e.ERR_NO_DATA, "当前学校暂未完成《一脸通行入驻》")
  199. return
  200. }
  201. selfSupportForUserFaceInfoDb := db.SelfSupportForUserFaceInfoDb{}
  202. selfSupportForUserFaceInfoDb.Set(insertAffected)
  203. _, err2 := selfSupportForUserFaceInfoDb.SelfSupportForUserFaceInfoInsert(&model.SelfSupportForUserFaceInfo{
  204. EnterpriseId: req.EnterpriseId,
  205. UserIdentityId: insertAffected,
  206. CollectFaceType: 1,
  207. SchoolCode: selfSupportForSchoolInfo.SchoolCode,
  208. SchoolStdCode: selfSupportForSchoolInfo.SchoolStdCode,
  209. ParentUserId: "",
  210. ParentLogonId: "",
  211. UserId: "",
  212. SchoolFacePassStatus: "CLOSE",
  213. SchoolFacePaymentStatus: "CLOSE",
  214. ConcentrateSchoolFacePassStatus: 1,
  215. CreateAt: now.Format("2006-01-02 15:04:05"),
  216. UpdateAt: now.Format("2006-01-02 15:04:05"),
  217. })
  218. if err2 != nil {
  219. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  220. return
  221. }
  222. } else {
  223. if isHasUserIdentity != nil && isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
  224. e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
  225. return
  226. }
  227. //4、修改身份信息
  228. isHasUserIdentity.Uid = user.Id
  229. isHasUserIdentity.Name = req.Name
  230. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  231. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  232. if err != nil {
  233. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  234. return
  235. }
  236. if identity == enum.UserIdentityForSelfSupportForStudent {
  237. //5、修改 class_with_user 记录
  238. classWithUserDb := db.ClassWithUserDb{}
  239. classWithUserDb.Set()
  240. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
  241. if err1 != nil {
  242. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  243. return
  244. }
  245. if classWithUser == nil {
  246. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  247. UserIdentityId: isHasUserIdentity.Id,
  248. ClassId: req.ClassId,
  249. CreateAt: now.Format("2006-01-02 15:04:05"),
  250. UpdateAt: now.Format("2006-01-02 15:04:05"),
  251. })
  252. if err2 != nil {
  253. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  254. return
  255. }
  256. } else {
  257. classWithUser.ClassId = req.ClassId
  258. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
  259. if err2 != nil {
  260. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  261. return
  262. }
  263. }
  264. }
  265. }
  266. e.OutSuc(c, "success", nil)
  267. return
  268. }
  269. func UpdateCentralKitchenForSchoolUserIdentity(c *gin.Context) {
  270. var req md.UpdateCentralKitchenForSchoolUserIdentityReq
  271. err := c.ShouldBindJSON(&req)
  272. if err != nil {
  273. err = validate.HandleValidateErr(err)
  274. err1 := err.(e.E)
  275. e.OutErr(c, err1.Code, err1.Error())
  276. return
  277. }
  278. user := svc.GetUser(c)
  279. now := time.Now()
  280. //1、查询当前身份是否存在
  281. userIdentityDb := db.UserIdentityDb{}
  282. userIdentityDb.Set(user.Id)
  283. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  284. if err != nil {
  285. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  286. return
  287. }
  288. if userIdentity == nil {
  289. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  290. return
  291. }
  292. //2、修改身份信息
  293. userIdentity.Name = req.Name
  294. userIdentity.IdNo = req.IdNo
  295. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  296. if err != nil {
  297. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  298. return
  299. }
  300. //3、修改 class_with_user 记录
  301. if userIdentity.Identity == enum.UserIdentityForCentralKitchenForStudent {
  302. classWithUserDb := db.ClassWithUserDb{}
  303. classWithUserDb.Set()
  304. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  305. if err1 != nil {
  306. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  307. return
  308. }
  309. if classWithUser == nil {
  310. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  311. UserIdentityId: userIdentity.Id,
  312. ClassId: req.ClassId,
  313. CreateAt: now.Format("2006-01-02 15:04:05"),
  314. UpdateAt: now.Format("2006-01-02 15:04:05"),
  315. })
  316. if err2 != nil {
  317. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  318. return
  319. }
  320. } else {
  321. classWithUser.ClassId = req.ClassId
  322. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  323. if err2 != nil {
  324. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  325. return
  326. }
  327. }
  328. }
  329. e.OutSuc(c, "success", nil)
  330. return
  331. }
  332. func UpdateSelfSupportForSchoolUserIdentity(c *gin.Context) {
  333. var req md.UpdateSelfSupportForSchoolUserIdentityReq
  334. err := c.ShouldBindJSON(&req)
  335. if err != nil {
  336. err = validate.HandleValidateErr(err)
  337. err1 := err.(e.E)
  338. e.OutErr(c, err1.Code, err1.Error())
  339. return
  340. }
  341. user := svc.GetUser(c)
  342. now := time.Now()
  343. //1、查询当前身份是否存在
  344. userIdentityDb := db.UserIdentityDb{}
  345. userIdentityDb.Set(user.Id)
  346. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  347. if err != nil {
  348. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  349. return
  350. }
  351. if userIdentity == nil {
  352. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  353. return
  354. }
  355. //2、修改身份信息
  356. userIdentity.Name = req.Name
  357. userIdentity.IdNo = req.IdNo
  358. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  359. if err != nil {
  360. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  361. return
  362. }
  363. //3、修改 class_with_user 记录
  364. if userIdentity.Identity == enum.UserIdentityForSelfSupportForStudent {
  365. classWithUserDb := db.ClassWithUserDb{}
  366. classWithUserDb.Set()
  367. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  368. if err1 != nil {
  369. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  370. return
  371. }
  372. if classWithUser == nil {
  373. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  374. UserIdentityId: userIdentity.Id,
  375. ClassId: req.ClassId,
  376. CreateAt: now.Format("2006-01-02 15:04:05"),
  377. UpdateAt: now.Format("2006-01-02 15:04:05"),
  378. })
  379. if err2 != nil {
  380. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  381. return
  382. }
  383. } else {
  384. classWithUser.ClassId = req.ClassId
  385. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  386. if err2 != nil {
  387. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  388. return
  389. }
  390. }
  391. }
  392. e.OutSuc(c, "success", nil)
  393. return
  394. }
  395. func DeleteUserIdentity(c *gin.Context) {
  396. userIdentityId := utils.StrToInt(c.Param("id"))
  397. user := svc.GetUser(c)
  398. //1、查询当前身份是否存在
  399. userIdentityDb := db.UserIdentityDb{}
  400. userIdentityDb.Set(user.Id)
  401. userIdentity, err := userIdentityDb.GetUserIdentity(userIdentityId)
  402. if err != nil {
  403. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  404. return
  405. }
  406. if userIdentity == nil {
  407. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  408. return
  409. }
  410. switch userIdentity.Identity {
  411. case enum.UserIdentityForCentralKitchenForStudent:
  412. err1 := svc.DeleteUserIdentityForCentralKitchenForStudent(userIdentity.Id)
  413. if err1 != nil {
  414. e.OutErr(c, e.ERR, err1.Error())
  415. return
  416. }
  417. break
  418. case enum.UserIdentityForCentralKitchenForTeacher:
  419. err1 := svc.DeleteUserIdentityForCentralKitchenForTeacher(userIdentity.Id)
  420. if err1 != nil {
  421. e.OutErr(c, e.ERR, err1.Error())
  422. return
  423. }
  424. break
  425. case enum.UserIdentityForCentralKitchenForWorker:
  426. break
  427. case enum.UserIdentityForSelfSupportForStudent:
  428. err1 := svc.DeleteUserIdentityForSelfSupportForStudent(userIdentity.Id)
  429. if err1 != nil {
  430. e.OutErr(c, e.ERR, err1.Error())
  431. return
  432. }
  433. break
  434. case enum.UserIdentityForSelfSupportForTeacher:
  435. err1 := svc.DeleteUserIdentityForSelfSupportForTeacher(userIdentity.Id)
  436. if err1 != nil {
  437. e.OutErr(c, e.ERR, err1.Error())
  438. return
  439. }
  440. break
  441. case enum.UserIdentityForSelfSupportForWorker:
  442. err1 := svc.DeleteUserIdentityForSelfSupportForWorker(userIdentity.Id)
  443. if err1 != nil {
  444. e.OutErr(c, e.ERR, err1.Error())
  445. return
  446. }
  447. break
  448. }
  449. e.OutSuc(c, "success", nil)
  450. return
  451. }