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

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