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

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