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

403 lines
13 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/lib/validate"
  4. "applet/app/admin/md"
  5. svc "applet/app/admin/svc/enterprise_manage"
  6. svc2 "applet/app/admin/svc/order"
  7. "applet/app/db"
  8. "applet/app/db/model"
  9. "applet/app/e"
  10. "applet/app/enum"
  11. "applet/app/utils"
  12. "applet/app/utils/cache"
  13. "applet/app/utils/logx"
  14. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  15. "errors"
  16. "github.com/gin-gonic/gin"
  17. "time"
  18. )
  19. func CentralKitchenForSchoolOrderRefundList(c *gin.Context) {
  20. var req md.CentralKitchenForSchoolOrderRefundListReq
  21. err := c.ShouldBindJSON(&req)
  22. if err != nil {
  23. err = validate.HandleValidateErr(err)
  24. err1 := err.(e.E)
  25. e.OutErr(c, err1.Code, err1.Error())
  26. return
  27. }
  28. list, total, err := svc2.CentralKitchenForSchoolOrderRefundList(c, req)
  29. if err != nil {
  30. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  31. return
  32. }
  33. e.OutSuc(c, map[string]interface{}{
  34. "list": list,
  35. "total": total,
  36. "audit_kind_list": []map[string]interface{}{
  37. {
  38. "name": "同意",
  39. "value": 1,
  40. },
  41. {
  42. "name": "拒绝",
  43. "value": 2,
  44. },
  45. },
  46. "order_by_name_list": []map[string]interface{}{
  47. {
  48. "name": "按姓名升序",
  49. "value": 1,
  50. },
  51. {
  52. "name": "按姓名降序",
  53. "value": 2,
  54. },
  55. },
  56. "kind_list": []map[string]interface{}{
  57. {
  58. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast),
  59. "value": enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  60. },
  61. {
  62. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch),
  63. "value": enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  64. },
  65. {
  66. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner),
  67. "value": enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  68. },
  69. },
  70. "state_list": []map[string]interface{}{
  71. {
  72. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing),
  73. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  74. },
  75. {
  76. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass),
  77. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass,
  78. },
  79. {
  80. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject),
  81. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject,
  82. },
  83. {
  84. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete),
  85. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete,
  86. },
  87. {
  88. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng),
  89. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng,
  90. },
  91. {
  92. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr),
  93. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr,
  94. },
  95. },
  96. "user_identity_kind_list": []map[string]interface{}{
  97. {
  98. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForStudent),
  99. "value": enum.UserIdentityForCentralKitchenForStudent,
  100. },
  101. {
  102. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForTeacher),
  103. "value": enum.UserIdentityForCentralKitchenForTeacher,
  104. },
  105. {
  106. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForWorker),
  107. "value": enum.UserIdentityForCentralKitchenForWorker,
  108. },
  109. {
  110. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForStudent),
  111. "value": enum.UserIdentityForSelfSupportForStudent,
  112. },
  113. {
  114. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForTeacher),
  115. "value": enum.UserIdentityForSelfSupportForTeacher,
  116. },
  117. {
  118. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForWorker),
  119. "value": enum.UserIdentityForSelfSupportForWorker,
  120. },
  121. },
  122. }, nil)
  123. return
  124. }
  125. func CentralKitchenForSchoolOrderRefundAudit(c *gin.Context) {
  126. var req md.CentralKitchenForSchoolOrderRefundAuditReq
  127. err := c.ShouldBindJSON(&req)
  128. if err != nil {
  129. err = validate.HandleValidateErr(err)
  130. err1 := err.(e.E)
  131. e.OutErr(c, err1.Code, err1.Error())
  132. return
  133. }
  134. //限制500条
  135. if len(req.Ids) > 500 {
  136. e.OutErr(c, e.ERR, errors.New("受理数据过长,请分批次处理"))
  137. return
  138. }
  139. rabbitMqCh, err := rabbit.Cfg.Pool.GetChannel()
  140. if err != nil {
  141. e.OutErr(c, e.ERR, err.Error())
  142. return
  143. }
  144. defer rabbitMqCh.Release()
  145. //TODO:: 加锁 Redis 锁,防并发
  146. getString, _ := cache.GetString(md.AuditLockKeyForCentralKitchenLockKey)
  147. if getString != "" {
  148. e.OutErr(c, e.ERR, "请稍等,存在其他退款处理中!")
  149. return
  150. }
  151. cache.Set(md.AuditLockKeyForCentralKitchenLockKey, "running")
  152. utils.FilePutContents("DealCentralKitchenForSchoolOrderRefundAudit", "退款处理,开始时间:::"+time.Now().Format("2006-01-02 15:04:05"))
  153. //先将所有申请单改成 审核通过 状态
  154. session := db.Db.NewSession()
  155. defer session.Close()
  156. session.Begin()
  157. centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{}
  158. centralKitchenForSchoolUserRefundDayDb.Set(0)
  159. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  160. centralKitchenForSchoolUserWithDayDb.Set(0)
  161. var recordsId = map[int]bool{}
  162. for _, v := range req.Ids {
  163. returnDay, err1 := centralKitchenForSchoolUserRefundDayDb.GetCentralKitchenForSchoolUserRefundDay(utils.StrToInt(v))
  164. if returnDay == nil {
  165. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  166. e.OutErr(c, e.ERR_DB_ORM, "记录id:【"+v+"】不存在")
  167. return
  168. }
  169. if err1 != nil {
  170. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  171. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  172. return
  173. }
  174. if returnDay.State != enum.CentralKitchenForSchoolUserRefundDayStateForAuditing && returnDay.State != enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr {
  175. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  176. _ = session.Rollback()
  177. e.OutErr(c, e.ERR, "请勿重复审核申请单!")
  178. return
  179. }
  180. if recordsId[returnDay.RecordsId] {
  181. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  182. _ = session.Rollback()
  183. userIdentityDb := db.UserIdentityDb{}
  184. userIdentityDb.Set(0)
  185. identity, err11 := userIdentityDb.GetUserIdentity(returnDay.IdentityId)
  186. if err11 != nil {
  187. e.OutErr(c, e.ERR_DB_ORM, err11.Error())
  188. return
  189. }
  190. e.OutErr(c, e.ERR, "当前含有重复退订餐记录【身份名称:"+identity.Name+"】;"+"记录id:【"+v+"】!")
  191. return
  192. }
  193. recordsId[returnDay.RecordsId] = true
  194. //1、更新状态
  195. if req.State == 1 {
  196. returnDay.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass
  197. } else {
  198. userWithDay, err3 := centralKitchenForSchoolUserWithDayDb.GetCentralKitchenForSchoolUserWithDay(returnDay.RecordsId)
  199. if err3 != nil {
  200. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  201. _ = session.Rollback()
  202. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  203. return
  204. }
  205. returnDay.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject
  206. userWithDay.State = enum.CentralKitchenForSchoolUserWithDayStateForWait
  207. updateAck1, err4 := centralKitchenForSchoolUserWithDayDb.CentralKitchenForSchoolUserWithDayUpdateBySess(session, userWithDay.Id, userWithDay, "state")
  208. if err4 != nil {
  209. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  210. _ = session.Rollback()
  211. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  212. return
  213. }
  214. if updateAck1 <= 0 {
  215. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  216. _ = session.Rollback()
  217. e.OutErr(c, e.ERR_DB_ORM, errors.New("更新退款就餐记录状态失败2"))
  218. return
  219. }
  220. //2、处理订单状态
  221. err5 := svc.JudgePackageOrdOrdState(returnDay.OutTradeNo, rabbitMqCh)
  222. if err5 != nil {
  223. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  224. _ = session.Rollback()
  225. e.OutErr(c, e.ERR, err5.Error())
  226. return
  227. }
  228. }
  229. returnDay.Memo = req.Memo
  230. updateAck, err2 := centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayUpdateBySess(session, returnDay.Id, returnDay, "state", "memo")
  231. if err2 != nil {
  232. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  233. _ = session.Rollback()
  234. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  235. return
  236. }
  237. if updateAck <= 0 {
  238. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  239. _ = session.Rollback()
  240. e.OutErr(c, e.ERR_DB_ORM, errors.New("更新退款订单记录状态失败1"))
  241. return
  242. }
  243. }
  244. err = session.Commit()
  245. if err != nil {
  246. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  247. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  248. return
  249. }
  250. if req.State == 1 {
  251. //TODO::审核通过,进行退款处理
  252. go func() {
  253. err1 := svc2.CentralKitchenForSchoolOrderRefundAudit(c, req)
  254. if err1 != nil {
  255. logx.Error(err1)
  256. println("<<<<CentralKitchenForSchoolOrderRefundAudit>>>>>Error:::", err1.Error())
  257. var m model.CentralKitchenForSchoolUserRefundDay
  258. m.Memo = "退款处理失败:(" + err1.Error() + ")"
  259. m.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditing
  260. db.Db.In("id", req.Ids).Cols("memo", "state").Update(&m)
  261. }
  262. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  263. utils.FilePutContents("DealCentralKitchenForSchoolOrderRefundAudit", "退款处理,结束时间:::"+time.Now().Format("2006-01-02 15:04:05"))
  264. }()
  265. } else {
  266. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  267. }
  268. e.OutSuc(c, "success", nil)
  269. return
  270. }
  271. func NursingHomeOrderRefundList(c *gin.Context) {
  272. var req md.NursingHomeOrderRefundListReq
  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. list, total, err := svc2.NursingHomeOrderRefundList(req)
  281. if err != nil {
  282. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  283. return
  284. }
  285. e.OutSuc(c, map[string]interface{}{
  286. "list": list,
  287. "total": total,
  288. "audit_kind_list": []map[string]interface{}{
  289. {
  290. "name": "同意",
  291. "value": 1,
  292. },
  293. {
  294. "name": "拒绝",
  295. "value": 2,
  296. },
  297. },
  298. "kind_list": []map[string]interface{}{
  299. {
  300. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast),
  301. "value": enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  302. },
  303. {
  304. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch),
  305. "value": enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  306. },
  307. {
  308. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner),
  309. "value": enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  310. },
  311. },
  312. "state_list": []map[string]interface{}{
  313. {
  314. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing),
  315. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  316. },
  317. {
  318. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass),
  319. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass,
  320. },
  321. {
  322. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject),
  323. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject,
  324. },
  325. {
  326. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete),
  327. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete,
  328. },
  329. {
  330. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng),
  331. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng,
  332. },
  333. {
  334. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr),
  335. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr,
  336. },
  337. },
  338. "user_identity_kind_list": []map[string]interface{}{
  339. {
  340. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForStudent),
  341. "value": enum.UserIdentityForCentralKitchenForStudent,
  342. },
  343. {
  344. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForTeacher),
  345. "value": enum.UserIdentityForCentralKitchenForTeacher,
  346. },
  347. {
  348. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForWorker),
  349. "value": enum.UserIdentityForCentralKitchenForWorker,
  350. },
  351. {
  352. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForStudent),
  353. "value": enum.UserIdentityForSelfSupportForStudent,
  354. },
  355. {
  356. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForTeacher),
  357. "value": enum.UserIdentityForSelfSupportForTeacher,
  358. },
  359. {
  360. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForWorker),
  361. "value": enum.UserIdentityForSelfSupportForWorker,
  362. },
  363. },
  364. }, nil)
  365. return
  366. }
  367. func NursingHomeOrderRefundAudit(c *gin.Context) {
  368. var req md.NursingHomeOrderRefundAuditReq
  369. err := c.ShouldBindJSON(&req)
  370. if err != nil {
  371. err = validate.HandleValidateErr(err)
  372. err1 := err.(e.E)
  373. e.OutErr(c, err1.Code, err1.Error())
  374. return
  375. }
  376. err = svc2.NursingHomeOrderRefundAudit(req)
  377. if err != nil {
  378. e.OutErr(c, e.ERR, err.Error())
  379. return
  380. }
  381. e.OutSuc(c, "success", nil)
  382. return
  383. }