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

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