智慧食堂
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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