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

387 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. "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. "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. e.OutErr(c, e.ERR, "当前含有重复退订餐记录【身份id:"+utils.IntToStr(returnDay.IdentityId)+"】;"+"记录id:【"+v+"】!")
  177. return
  178. }
  179. recordsId[returnDay.RecordsId] = true
  180. //1、更新状态
  181. if req.State == 1 {
  182. returnDay.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass
  183. } else {
  184. userWithDay, err3 := centralKitchenForSchoolUserWithDayDb.GetCentralKitchenForSchoolUserWithDay(returnDay.RecordsId)
  185. if err3 != nil {
  186. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  187. _ = session.Rollback()
  188. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  189. return
  190. }
  191. returnDay.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject
  192. userWithDay.State = enum.CentralKitchenForSchoolUserWithDayStateForWait
  193. updateAck1, err4 := centralKitchenForSchoolUserWithDayDb.CentralKitchenForSchoolUserWithDayUpdateBySess(session, userWithDay.Id, userWithDay, "state")
  194. if err4 != nil {
  195. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  196. _ = session.Rollback()
  197. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  198. return
  199. }
  200. if updateAck1 <= 0 {
  201. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  202. _ = session.Rollback()
  203. e.OutErr(c, e.ERR_DB_ORM, errors.New("更新退款就餐记录状态失败2"))
  204. return
  205. }
  206. //2、处理订单状态
  207. err5 := svc.JudgePackageOrdOrdState(returnDay.OutTradeNo)
  208. if err5 != nil {
  209. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  210. _ = session.Rollback()
  211. e.OutErr(c, e.ERR, err5.Error())
  212. return
  213. }
  214. }
  215. returnDay.Memo = req.Memo
  216. updateAck, err2 := centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayUpdateBySess(session, returnDay.Id, returnDay, "state", "memo")
  217. if err2 != nil {
  218. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  219. _ = session.Rollback()
  220. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  221. return
  222. }
  223. if updateAck <= 0 {
  224. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  225. _ = session.Rollback()
  226. e.OutErr(c, e.ERR_DB_ORM, errors.New("更新退款订单记录状态失败1"))
  227. return
  228. }
  229. }
  230. err = session.Commit()
  231. if err != nil {
  232. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  233. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  234. return
  235. }
  236. if req.State == 1 {
  237. //TODO::审核通过,进行退款处理
  238. go func() {
  239. err1 := svc2.CentralKitchenForSchoolOrderRefundAudit(c, req)
  240. if err1 != nil {
  241. logx.Error(err1)
  242. println("<<<<CentralKitchenForSchoolOrderRefundAudit>>>>>Error:::", err1.Error())
  243. var m model.CentralKitchenForSchoolUserRefundDay
  244. m.Memo = "退款处理失败:(" + err1.Error() + ")"
  245. m.State = enum.CentralKitchenForSchoolUserRefundDayStateForAuditing
  246. db.Db.In("id", req.Ids).Cols("memo", "state").Update(&m)
  247. }
  248. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  249. utils.FilePutContents("DealCentralKitchenForSchoolOrderRefundAudit", "退款处理,结束时间:::"+time.Now().Format("2006-01-02 15:04:05"))
  250. }()
  251. } else {
  252. cache.Del(md.AuditLockKeyForCentralKitchenLockKey)
  253. }
  254. e.OutSuc(c, "success", nil)
  255. return
  256. }
  257. func NursingHomeOrderRefundList(c *gin.Context) {
  258. var req md.NursingHomeOrderRefundListReq
  259. err := c.ShouldBindJSON(&req)
  260. if err != nil {
  261. err = validate.HandleValidateErr(err)
  262. err1 := err.(e.E)
  263. e.OutErr(c, err1.Code, err1.Error())
  264. return
  265. }
  266. list, total, err := svc2.NursingHomeOrderRefundList(req)
  267. if err != nil {
  268. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  269. return
  270. }
  271. e.OutSuc(c, map[string]interface{}{
  272. "list": list,
  273. "total": total,
  274. "audit_kind_list": []map[string]interface{}{
  275. {
  276. "name": "同意",
  277. "value": 1,
  278. },
  279. {
  280. "name": "拒绝",
  281. "value": 2,
  282. },
  283. },
  284. "kind_list": []map[string]interface{}{
  285. {
  286. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast),
  287. "value": enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  288. },
  289. {
  290. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch),
  291. "value": enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  292. },
  293. {
  294. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner),
  295. "value": enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  296. },
  297. },
  298. "state_list": []map[string]interface{}{
  299. {
  300. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing),
  301. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  302. },
  303. {
  304. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass),
  305. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass,
  306. },
  307. {
  308. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject),
  309. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject,
  310. },
  311. {
  312. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete),
  313. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete,
  314. },
  315. {
  316. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng),
  317. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundIng,
  318. },
  319. {
  320. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr),
  321. "value": enum.CentralKitchenForSchoolUserRefundDayStateForRefundErr,
  322. },
  323. },
  324. "user_identity_kind_list": []map[string]interface{}{
  325. {
  326. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForStudent),
  327. "value": enum.UserIdentityForCentralKitchenForStudent,
  328. },
  329. {
  330. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForTeacher),
  331. "value": enum.UserIdentityForCentralKitchenForTeacher,
  332. },
  333. {
  334. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForWorker),
  335. "value": enum.UserIdentityForCentralKitchenForWorker,
  336. },
  337. {
  338. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForStudent),
  339. "value": enum.UserIdentityForSelfSupportForStudent,
  340. },
  341. {
  342. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForTeacher),
  343. "value": enum.UserIdentityForSelfSupportForTeacher,
  344. },
  345. {
  346. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForWorker),
  347. "value": enum.UserIdentityForSelfSupportForWorker,
  348. },
  349. },
  350. }, nil)
  351. return
  352. }
  353. func NursingHomeOrderRefundAudit(c *gin.Context) {
  354. var req md.NursingHomeOrderRefundAuditReq
  355. err := c.ShouldBindJSON(&req)
  356. if err != nil {
  357. err = validate.HandleValidateErr(err)
  358. err1 := err.(e.E)
  359. e.OutErr(c, err1.Code, err1.Error())
  360. return
  361. }
  362. err = svc2.NursingHomeOrderRefundAudit(req)
  363. if err != nil {
  364. e.OutErr(c, e.ERR, err.Error())
  365. return
  366. }
  367. e.OutSuc(c, "success", nil)
  368. return
  369. }