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

581 lines
17 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.UserIdentityExistV1(req.EnterpriseId, req.Name, req.ClassId)
  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 SaveNursingHomeUserIdentity(c *gin.Context) {
  270. var req md.SaveNursingHomeUserIdentityReq
  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. var identity = enum.UserIdentityForNursingHome
  280. //1、判断当前身份是否已绑定
  281. userIdentityDb := db.UserIdentityDb{}
  282. userIdentityDb.Set(user.Id)
  283. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
  284. if err != nil {
  285. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  286. return
  287. }
  288. now := time.Now()
  289. if isHasUserIdentity == nil {
  290. //2、新增身份信息
  291. userIdentity := &model.UserIdentity{
  292. Uid: user.Id,
  293. Name: req.Name,
  294. IdNo: req.IdNo,
  295. Kind: enum.UserIdentityKindForCommon,
  296. Identity: identity,
  297. EnterpriseId: req.EnterpriseId,
  298. State: enum.UserIdentityStateForNormal,
  299. Memo: "",
  300. CreateAt: now.Format("2006-01-02 15:04:05"),
  301. UpdateAt: now.Format("2006-01-02 15:04:05"),
  302. }
  303. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  304. if err != nil {
  305. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  306. return
  307. }
  308. if insertAffected <= 0 {
  309. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  310. return
  311. }
  312. } else {
  313. //4、修改身份信息
  314. isHasUserIdentity.Uid = user.Id
  315. isHasUserIdentity.Name = req.Name
  316. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  317. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  318. if err != nil {
  319. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  320. return
  321. }
  322. }
  323. e.OutSuc(c, "success", nil)
  324. return
  325. }
  326. func UpdateCentralKitchenForSchoolUserIdentity(c *gin.Context) {
  327. var req md.UpdateCentralKitchenForSchoolUserIdentityReq
  328. err := c.ShouldBindJSON(&req)
  329. if err != nil {
  330. err = validate.HandleValidateErr(err)
  331. err1 := err.(e.E)
  332. e.OutErr(c, err1.Code, err1.Error())
  333. return
  334. }
  335. user := svc.GetUser(c)
  336. now := time.Now()
  337. //1、查询当前身份是否存在
  338. userIdentityDb := db.UserIdentityDb{}
  339. userIdentityDb.Set(user.Id)
  340. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  341. if err != nil {
  342. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  343. return
  344. }
  345. if userIdentity == nil {
  346. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  347. return
  348. }
  349. //2、修改身份信息
  350. userIdentity.Name = req.Name
  351. userIdentity.IdNo = req.IdNo
  352. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  353. if err != nil {
  354. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  355. return
  356. }
  357. //3、修改 class_with_user 记录
  358. if userIdentity.Identity == enum.UserIdentityForCentralKitchenForStudent {
  359. classWithUserDb := db.ClassWithUserDb{}
  360. classWithUserDb.Set()
  361. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  362. if err1 != nil {
  363. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  364. return
  365. }
  366. if classWithUser == nil {
  367. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  368. UserIdentityId: userIdentity.Id,
  369. ClassId: req.ClassId,
  370. CreateAt: now.Format("2006-01-02 15:04:05"),
  371. UpdateAt: now.Format("2006-01-02 15:04:05"),
  372. })
  373. if err2 != nil {
  374. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  375. return
  376. }
  377. } else {
  378. classWithUser.ClassId = req.ClassId
  379. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  380. if err2 != nil {
  381. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  382. return
  383. }
  384. }
  385. }
  386. e.OutSuc(c, "success", nil)
  387. return
  388. }
  389. func UpdateSelfSupportForSchoolUserIdentity(c *gin.Context) {
  390. var req md.UpdateSelfSupportForSchoolUserIdentityReq
  391. err := c.ShouldBindJSON(&req)
  392. if err != nil {
  393. err = validate.HandleValidateErr(err)
  394. err1 := err.(e.E)
  395. e.OutErr(c, err1.Code, err1.Error())
  396. return
  397. }
  398. user := svc.GetUser(c)
  399. now := time.Now()
  400. //1、查询当前身份是否存在
  401. userIdentityDb := db.UserIdentityDb{}
  402. userIdentityDb.Set(user.Id)
  403. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  404. if err != nil {
  405. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  406. return
  407. }
  408. if userIdentity == nil {
  409. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  410. return
  411. }
  412. //2、修改身份信息
  413. userIdentity.Name = req.Name
  414. userIdentity.IdNo = req.IdNo
  415. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  416. if err != nil {
  417. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  418. return
  419. }
  420. //3、修改 class_with_user 记录
  421. if userIdentity.Identity == enum.UserIdentityForSelfSupportForStudent {
  422. classWithUserDb := db.ClassWithUserDb{}
  423. classWithUserDb.Set()
  424. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  425. if err1 != nil {
  426. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  427. return
  428. }
  429. if classWithUser == nil {
  430. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  431. UserIdentityId: userIdentity.Id,
  432. ClassId: req.ClassId,
  433. CreateAt: now.Format("2006-01-02 15:04:05"),
  434. UpdateAt: now.Format("2006-01-02 15:04:05"),
  435. })
  436. if err2 != nil {
  437. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  438. return
  439. }
  440. } else {
  441. classWithUser.ClassId = req.ClassId
  442. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  443. if err2 != nil {
  444. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  445. return
  446. }
  447. }
  448. }
  449. e.OutSuc(c, "success", nil)
  450. return
  451. }
  452. func UpdateNursingHomeUserIdentity(c *gin.Context) {
  453. var req md.UpdateNursingHomeUserIdentityReq
  454. err := c.ShouldBindJSON(&req)
  455. if err != nil {
  456. err = validate.HandleValidateErr(err)
  457. err1 := err.(e.E)
  458. e.OutErr(c, err1.Code, err1.Error())
  459. return
  460. }
  461. user := svc.GetUser(c)
  462. //1、查询当前身份是否存在
  463. userIdentityDb := db.UserIdentityDb{}
  464. userIdentityDb.Set(user.Id)
  465. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  466. if err != nil {
  467. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  468. return
  469. }
  470. if userIdentity == nil {
  471. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  472. return
  473. }
  474. //2、修改身份信息
  475. userIdentity.Name = req.Name
  476. userIdentity.IdNo = req.IdNo
  477. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  478. if err != nil {
  479. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  480. return
  481. }
  482. e.OutSuc(c, "success", nil)
  483. return
  484. }
  485. func DeleteUserIdentity(c *gin.Context) {
  486. userIdentityId := utils.StrToInt(c.Param("id"))
  487. user := svc.GetUser(c)
  488. //1、查询当前身份是否存在
  489. userIdentityDb := db.UserIdentityDb{}
  490. userIdentityDb.Set(user.Id)
  491. userIdentity, err := userIdentityDb.GetUserIdentity(userIdentityId)
  492. if err != nil {
  493. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  494. return
  495. }
  496. if userIdentity == nil {
  497. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  498. return
  499. }
  500. switch userIdentity.Identity {
  501. case enum.UserIdentityForCentralKitchenForStudent:
  502. err1 := svc.DeleteUserIdentityForCentralKitchenForStudent(userIdentity.Id)
  503. if err1 != nil {
  504. e.OutErr(c, e.ERR, err1.Error())
  505. return
  506. }
  507. break
  508. case enum.UserIdentityForCentralKitchenForTeacher:
  509. err1 := svc.DeleteUserIdentityForCentralKitchenForTeacher(userIdentity.Id)
  510. if err1 != nil {
  511. e.OutErr(c, e.ERR, err1.Error())
  512. return
  513. }
  514. break
  515. case enum.UserIdentityForCentralKitchenForWorker:
  516. break
  517. case enum.UserIdentityForSelfSupportForStudent:
  518. err1 := svc.DeleteUserIdentityForSelfSupportForStudent(userIdentity.Id)
  519. if err1 != nil {
  520. e.OutErr(c, e.ERR, err1.Error())
  521. return
  522. }
  523. break
  524. case enum.UserIdentityForSelfSupportForTeacher:
  525. err1 := svc.DeleteUserIdentityForSelfSupportForTeacher(userIdentity.Id)
  526. if err1 != nil {
  527. e.OutErr(c, e.ERR, err1.Error())
  528. return
  529. }
  530. break
  531. case enum.UserIdentityForSelfSupportForWorker:
  532. err1 := svc.DeleteUserIdentityForSelfSupportForWorker(userIdentity.Id)
  533. if err1 != nil {
  534. e.OutErr(c, e.ERR, err1.Error())
  535. return
  536. }
  537. break
  538. case enum.UserIdentityForNursingHome:
  539. err1 := svc.DeleteUserIdentityForNursingHome(userIdentity.Id)
  540. if err1 != nil {
  541. e.OutErr(c, e.ERR, err1.Error())
  542. return
  543. }
  544. break
  545. }
  546. e.OutSuc(c, "success", nil)
  547. return
  548. }