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

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