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

583 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)
  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. CertType: enum.UserIdentityCertTypeForIdCard,
  44. Kind: enum.UserIdentityKindForCommon,
  45. Identity: identity,
  46. EnterpriseId: req.EnterpriseId,
  47. State: enum.UserIdentityStateForNormal,
  48. Memo: "",
  49. CreateAt: now.Format("2006-01-02 15:04:05"),
  50. UpdateAt: now.Format("2006-01-02 15:04:05"),
  51. }
  52. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  53. if err != nil {
  54. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  55. return
  56. }
  57. if insertAffected <= 0 {
  58. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  59. return
  60. }
  61. //3、新增 class_with_user 记录
  62. if !req.IsTeacher {
  63. classWithUserDb := db.ClassWithUserDb{}
  64. classWithUserDb.Set()
  65. _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  66. UserIdentityId: insertAffected,
  67. ClassId: req.ClassId,
  68. CreateAt: now.Format("2006-01-02 15:04:05"),
  69. UpdateAt: now.Format("2006-01-02 15:04:05"),
  70. })
  71. if err != nil {
  72. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  73. return
  74. }
  75. }
  76. } else {
  77. //4、修改身份信息
  78. isHasUserIdentity.Uid = user.Id
  79. isHasUserIdentity.Name = req.Name
  80. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  81. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  82. if err != nil {
  83. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  84. return
  85. }
  86. //5、修改 class_with_user 记录
  87. if !req.IsTeacher {
  88. classWithUserDb := db.ClassWithUserDb{}
  89. classWithUserDb.Set()
  90. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
  91. if err1 != nil {
  92. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  93. return
  94. }
  95. if classWithUser == nil {
  96. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  97. UserIdentityId: isHasUserIdentity.Id,
  98. ClassId: req.ClassId,
  99. CreateAt: now.Format("2006-01-02 15:04:05"),
  100. UpdateAt: now.Format("2006-01-02 15:04:05"),
  101. })
  102. if err2 != nil {
  103. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  104. return
  105. }
  106. } else {
  107. classWithUser.ClassId = req.ClassId
  108. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
  109. if err2 != nil {
  110. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  111. return
  112. }
  113. }
  114. }
  115. }
  116. e.OutSuc(c, "success", nil)
  117. return
  118. }
  119. func SaveSelfSupportForSchoolUserIdentity(c *gin.Context) {
  120. var req md.SaveSelfSupportForSchoolUserIdentityReq
  121. err := c.ShouldBindJSON(&req)
  122. if err != nil {
  123. err = validate.HandleValidateErr(err)
  124. err1 := err.(e.E)
  125. e.OutErr(c, err1.Code, err1.Error())
  126. return
  127. }
  128. user := svc.GetUser(c)
  129. now := time.Now()
  130. var identity = enum.UserIdentityForSelfSupportForStudent
  131. var kind = enum.UserIdentityKindForCommon
  132. if req.Kind == enum.UserIdentityKindForWorker {
  133. kind = enum.UserIdentityKindForWorker
  134. identity = enum.UserIdentityForSelfSupportForWorker
  135. } else {
  136. if req.IsTeacher {
  137. identity = enum.UserIdentityForCentralKitchenForTeacher
  138. }
  139. }
  140. //1、判断当前身份是否已绑定
  141. userIdentityDb := db.UserIdentityDb{}
  142. userIdentityDb.Set(user.Id)
  143. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo, req.CertType)
  144. if err != nil {
  145. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  146. return
  147. }
  148. if isHasUserIdentity == nil {
  149. if identity != enum.UserIdentityForSelfSupportForStudent {
  150. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  151. return
  152. }
  153. //2、新增身份信息
  154. userIdentity := &model.UserIdentity{
  155. Uid: user.Id,
  156. Name: req.Name,
  157. IdNo: req.IdNo,
  158. CertType: req.CertType,
  159. Kind: kind,
  160. Identity: identity,
  161. EnterpriseId: req.EnterpriseId,
  162. State: enum.UserIdentityStateForNormal,
  163. Memo: "",
  164. CreateAt: now.Format("2006-01-02 15:04:05"),
  165. UpdateAt: now.Format("2006-01-02 15:04:05"),
  166. }
  167. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  168. if err != nil {
  169. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  170. return
  171. }
  172. if insertAffected <= 0 {
  173. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  174. return
  175. }
  176. //3、新增 class_with_user 记录
  177. if !req.IsTeacher {
  178. classWithUserDb := db.ClassWithUserDb{}
  179. classWithUserDb.Set()
  180. _, err1 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  181. UserIdentityId: insertAffected,
  182. ClassId: req.ClassId,
  183. CreateAt: now.Format("2006-01-02 15:04:05"),
  184. UpdateAt: now.Format("2006-01-02 15:04:05"),
  185. })
  186. if err1 != nil {
  187. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  188. return
  189. }
  190. }
  191. //4、新增 self_support_for_user_face_info 记录
  192. selfSupportForSchoolInfoDb := db.SelfSupportForSchoolInfoDb{}
  193. selfSupportForSchoolInfoDb.Set(req.EnterpriseId)
  194. selfSupportForSchoolInfo, err := selfSupportForSchoolInfoDb.GetSelfSupportForSchoolInfo()
  195. if err != nil {
  196. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  197. return
  198. }
  199. if selfSupportForSchoolInfo == nil {
  200. e.OutErr(c, e.ERR_NO_DATA, "当前学校暂未完成《一脸通行入驻》")
  201. return
  202. }
  203. selfSupportForUserFaceInfoDb := db.SelfSupportForUserFaceInfoDb{}
  204. selfSupportForUserFaceInfoDb.Set(insertAffected)
  205. _, err2 := selfSupportForUserFaceInfoDb.SelfSupportForUserFaceInfoInsert(&model.SelfSupportForUserFaceInfo{
  206. EnterpriseId: req.EnterpriseId,
  207. UserIdentityId: insertAffected,
  208. CollectFaceType: 1,
  209. SchoolCode: selfSupportForSchoolInfo.SchoolCode,
  210. SchoolStdCode: selfSupportForSchoolInfo.SchoolStdCode,
  211. ParentUserId: "",
  212. ParentLogonId: "",
  213. UserId: "",
  214. SchoolFacePassStatus: "CLOSE",
  215. SchoolFacePaymentStatus: "CLOSE",
  216. ConcentrateSchoolFacePassStatus: 1,
  217. CreateAt: now.Format("2006-01-02 15:04:05"),
  218. UpdateAt: now.Format("2006-01-02 15:04:05"),
  219. })
  220. if err2 != nil {
  221. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  222. return
  223. }
  224. } else {
  225. if isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
  226. e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
  227. return
  228. }
  229. //4、修改身份信息
  230. isHasUserIdentity.Uid = user.Id
  231. isHasUserIdentity.Name = req.Name
  232. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  233. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  234. if err != nil {
  235. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  236. return
  237. }
  238. if identity == enum.UserIdentityForSelfSupportForStudent {
  239. //5、修改 class_with_user 记录
  240. classWithUserDb := db.ClassWithUserDb{}
  241. classWithUserDb.Set()
  242. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
  243. if err1 != nil {
  244. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  245. return
  246. }
  247. if classWithUser == nil {
  248. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  249. UserIdentityId: isHasUserIdentity.Id,
  250. ClassId: req.ClassId,
  251. CreateAt: now.Format("2006-01-02 15:04:05"),
  252. UpdateAt: now.Format("2006-01-02 15:04:05"),
  253. })
  254. if err2 != nil {
  255. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  256. return
  257. }
  258. } else {
  259. classWithUser.ClassId = req.ClassId
  260. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
  261. if err2 != nil {
  262. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  263. return
  264. }
  265. }
  266. }
  267. }
  268. e.OutSuc(c, "success", nil)
  269. return
  270. }
  271. func SaveNursingHomeUserIdentity(c *gin.Context) {
  272. var req md.SaveNursingHomeUserIdentityReq
  273. err := c.ShouldBindJSON(&req)
  274. if err != nil {
  275. err = validate.HandleValidateErr(err)
  276. err1 := err.(e.E)
  277. e.OutErr(c, err1.Code, err1.Error())
  278. return
  279. }
  280. user := svc.GetUser(c)
  281. var identity = enum.UserIdentityForNursingHome
  282. //1、判断当前身份是否已绑定
  283. userIdentityDb := db.UserIdentityDb{}
  284. userIdentityDb.Set(user.Id)
  285. isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo, enum.UserIdentityCertTypeForIdCard)
  286. if err != nil {
  287. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  288. return
  289. }
  290. now := time.Now()
  291. if isHasUserIdentity == nil {
  292. //2、新增身份信息
  293. userIdentity := &model.UserIdentity{
  294. Uid: user.Id,
  295. Name: req.Name,
  296. IdNo: req.IdNo,
  297. Kind: enum.UserIdentityKindForCommon,
  298. Identity: identity,
  299. EnterpriseId: req.EnterpriseId,
  300. State: enum.UserIdentityStateForNormal,
  301. Memo: "",
  302. CreateAt: now.Format("2006-01-02 15:04:05"),
  303. UpdateAt: now.Format("2006-01-02 15:04:05"),
  304. }
  305. insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
  306. if err != nil {
  307. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  308. return
  309. }
  310. if insertAffected <= 0 {
  311. e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
  312. return
  313. }
  314. } else {
  315. //4、修改身份信息
  316. isHasUserIdentity.Uid = user.Id
  317. isHasUserIdentity.Name = req.Name
  318. isHasUserIdentity.State = enum.UserIdentityStateForNormal
  319. _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name", "state")
  320. if err != nil {
  321. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  322. return
  323. }
  324. }
  325. e.OutSuc(c, "success", nil)
  326. return
  327. }
  328. func UpdateCentralKitchenForSchoolUserIdentity(c *gin.Context) {
  329. var req md.UpdateCentralKitchenForSchoolUserIdentityReq
  330. err := c.ShouldBindJSON(&req)
  331. if err != nil {
  332. err = validate.HandleValidateErr(err)
  333. err1 := err.(e.E)
  334. e.OutErr(c, err1.Code, err1.Error())
  335. return
  336. }
  337. user := svc.GetUser(c)
  338. now := time.Now()
  339. //1、查询当前身份是否存在
  340. userIdentityDb := db.UserIdentityDb{}
  341. userIdentityDb.Set(user.Id)
  342. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  343. if err != nil {
  344. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  345. return
  346. }
  347. if userIdentity == nil {
  348. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  349. return
  350. }
  351. //2、修改身份信息
  352. userIdentity.Name = req.Name
  353. userIdentity.IdNo = req.IdNo
  354. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  355. if err != nil {
  356. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  357. return
  358. }
  359. //3、修改 class_with_user 记录
  360. if userIdentity.Identity == enum.UserIdentityForCentralKitchenForStudent {
  361. classWithUserDb := db.ClassWithUserDb{}
  362. classWithUserDb.Set()
  363. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  364. if err1 != nil {
  365. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  366. return
  367. }
  368. if classWithUser == nil {
  369. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  370. UserIdentityId: userIdentity.Id,
  371. ClassId: req.ClassId,
  372. CreateAt: now.Format("2006-01-02 15:04:05"),
  373. UpdateAt: now.Format("2006-01-02 15:04:05"),
  374. })
  375. if err2 != nil {
  376. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  377. return
  378. }
  379. } else {
  380. classWithUser.ClassId = req.ClassId
  381. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  382. if err2 != nil {
  383. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  384. return
  385. }
  386. }
  387. }
  388. e.OutSuc(c, "success", nil)
  389. return
  390. }
  391. func UpdateSelfSupportForSchoolUserIdentity(c *gin.Context) {
  392. var req md.UpdateSelfSupportForSchoolUserIdentityReq
  393. err := c.ShouldBindJSON(&req)
  394. if err != nil {
  395. err = validate.HandleValidateErr(err)
  396. err1 := err.(e.E)
  397. e.OutErr(c, err1.Code, err1.Error())
  398. return
  399. }
  400. user := svc.GetUser(c)
  401. now := time.Now()
  402. //1、查询当前身份是否存在
  403. userIdentityDb := db.UserIdentityDb{}
  404. userIdentityDb.Set(user.Id)
  405. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  406. if err != nil {
  407. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  408. return
  409. }
  410. if userIdentity == nil {
  411. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  412. return
  413. }
  414. //2、修改身份信息
  415. userIdentity.Name = req.Name
  416. userIdentity.IdNo = req.IdNo
  417. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  418. if err != nil {
  419. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  420. return
  421. }
  422. //3、修改 class_with_user 记录
  423. if userIdentity.Identity == enum.UserIdentityForSelfSupportForStudent {
  424. classWithUserDb := db.ClassWithUserDb{}
  425. classWithUserDb.Set()
  426. classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(userIdentity.Id)
  427. if err1 != nil {
  428. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  429. return
  430. }
  431. if classWithUser == nil {
  432. _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
  433. UserIdentityId: userIdentity.Id,
  434. ClassId: req.ClassId,
  435. CreateAt: now.Format("2006-01-02 15:04:05"),
  436. UpdateAt: now.Format("2006-01-02 15:04:05"),
  437. })
  438. if err2 != nil {
  439. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  440. return
  441. }
  442. } else {
  443. classWithUser.ClassId = req.ClassId
  444. _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(userIdentity.Id, classWithUser, "class_id")
  445. if err2 != nil {
  446. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  447. return
  448. }
  449. }
  450. }
  451. e.OutSuc(c, "success", nil)
  452. return
  453. }
  454. func UpdateNursingHomeUserIdentity(c *gin.Context) {
  455. var req md.UpdateNursingHomeUserIdentityReq
  456. err := c.ShouldBindJSON(&req)
  457. if err != nil {
  458. err = validate.HandleValidateErr(err)
  459. err1 := err.(e.E)
  460. e.OutErr(c, err1.Code, err1.Error())
  461. return
  462. }
  463. user := svc.GetUser(c)
  464. //1、查询当前身份是否存在
  465. userIdentityDb := db.UserIdentityDb{}
  466. userIdentityDb.Set(user.Id)
  467. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  468. if err != nil {
  469. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  470. return
  471. }
  472. if userIdentity == nil {
  473. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  474. return
  475. }
  476. //2、修改身份信息
  477. userIdentity.Name = req.Name
  478. userIdentity.IdNo = req.IdNo
  479. _, err = userIdentityDb.UserIdentityUpdate(userIdentity.Id, userIdentity, "id_no", "name")
  480. if err != nil {
  481. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  482. return
  483. }
  484. e.OutSuc(c, "success", nil)
  485. return
  486. }
  487. func DeleteUserIdentity(c *gin.Context) {
  488. userIdentityId := utils.StrToInt(c.Param("id"))
  489. user := svc.GetUser(c)
  490. //1、查询当前身份是否存在
  491. userIdentityDb := db.UserIdentityDb{}
  492. userIdentityDb.Set(user.Id)
  493. userIdentity, err := userIdentityDb.GetUserIdentity(userIdentityId)
  494. if err != nil {
  495. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  496. return
  497. }
  498. if userIdentity == nil {
  499. e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
  500. return
  501. }
  502. switch userIdentity.Identity {
  503. case enum.UserIdentityForCentralKitchenForStudent:
  504. err1 := svc.DeleteUserIdentityForCentralKitchenForStudent(userIdentity.Id)
  505. if err1 != nil {
  506. e.OutErr(c, e.ERR, err1.Error())
  507. return
  508. }
  509. break
  510. case enum.UserIdentityForCentralKitchenForTeacher:
  511. err1 := svc.DeleteUserIdentityForCentralKitchenForTeacher(userIdentity.Id)
  512. if err1 != nil {
  513. e.OutErr(c, e.ERR, err1.Error())
  514. return
  515. }
  516. break
  517. case enum.UserIdentityForCentralKitchenForWorker:
  518. break
  519. case enum.UserIdentityForSelfSupportForStudent:
  520. err1 := svc.DeleteUserIdentityForSelfSupportForStudent(userIdentity.Id)
  521. if err1 != nil {
  522. e.OutErr(c, e.ERR, err1.Error())
  523. return
  524. }
  525. break
  526. case enum.UserIdentityForSelfSupportForTeacher:
  527. err1 := svc.DeleteUserIdentityForSelfSupportForTeacher(userIdentity.Id)
  528. if err1 != nil {
  529. e.OutErr(c, e.ERR, err1.Error())
  530. return
  531. }
  532. break
  533. case enum.UserIdentityForSelfSupportForWorker:
  534. err1 := svc.DeleteUserIdentityForSelfSupportForWorker(userIdentity.Id)
  535. if err1 != nil {
  536. e.OutErr(c, e.ERR, err1.Error())
  537. return
  538. }
  539. break
  540. case enum.UserIdentityForNursingHome:
  541. err1 := svc.DeleteUserIdentityForNursingHome(userIdentity.Id)
  542. if err1 != nil {
  543. e.OutErr(c, e.ERR, err1.Error())
  544. return
  545. }
  546. break
  547. }
  548. e.OutSuc(c, "success", nil)
  549. return
  550. }