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

354 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. }
  229. e.OutSuc(c, "success", nil)
  230. return
  231. }
  232. func NursingHomeOrderRefundList(c *gin.Context) {
  233. var req md.NursingHomeOrderRefundListReq
  234. err := c.ShouldBindJSON(&req)
  235. if err != nil {
  236. err = validate.HandleValidateErr(err)
  237. err1 := err.(e.E)
  238. e.OutErr(c, err1.Code, err1.Error())
  239. return
  240. }
  241. list, total, err := svc2.NursingHomeOrderRefundList(req)
  242. if err != nil {
  243. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  244. return
  245. }
  246. e.OutSuc(c, map[string]interface{}{
  247. "list": list,
  248. "total": total,
  249. "audit_kind_list": []map[string]interface{}{
  250. {
  251. "name": "同意",
  252. "value": 1,
  253. },
  254. {
  255. "name": "拒绝",
  256. "value": 2,
  257. },
  258. },
  259. "kind_list": []map[string]interface{}{
  260. {
  261. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast),
  262. "value": enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  263. },
  264. {
  265. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch),
  266. "value": enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  267. },
  268. {
  269. "name": enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner),
  270. "value": enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  271. },
  272. },
  273. "state_list": []map[string]interface{}{
  274. {
  275. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing),
  276. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  277. },
  278. {
  279. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass),
  280. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass,
  281. },
  282. {
  283. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject),
  284. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject,
  285. },
  286. {
  287. "name": enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete),
  288. "value": enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete,
  289. },
  290. },
  291. "user_identity_kind_list": []map[string]interface{}{
  292. {
  293. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForStudent),
  294. "value": enum.UserIdentityForCentralKitchenForStudent,
  295. },
  296. {
  297. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForTeacher),
  298. "value": enum.UserIdentityForCentralKitchenForTeacher,
  299. },
  300. {
  301. "name": enum.UserIdentity.String(enum.UserIdentityForCentralKitchenForWorker),
  302. "value": enum.UserIdentityForCentralKitchenForWorker,
  303. },
  304. {
  305. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForStudent),
  306. "value": enum.UserIdentityForSelfSupportForStudent,
  307. },
  308. {
  309. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForTeacher),
  310. "value": enum.UserIdentityForSelfSupportForTeacher,
  311. },
  312. {
  313. "name": enum.UserIdentity.String(enum.UserIdentityForSelfSupportForWorker),
  314. "value": enum.UserIdentityForSelfSupportForWorker,
  315. },
  316. },
  317. }, nil)
  318. return
  319. }
  320. func NursingHomeOrderRefundAudit(c *gin.Context) {
  321. var req md.NursingHomeOrderRefundAuditReq
  322. err := c.ShouldBindJSON(&req)
  323. if err != nil {
  324. err = validate.HandleValidateErr(err)
  325. err1 := err.(e.E)
  326. e.OutErr(c, err1.Code, err1.Error())
  327. return
  328. }
  329. err = svc2.NursingHomeOrderRefundAudit(req)
  330. if err != nil {
  331. e.OutErr(c, e.ERR, err.Error())
  332. return
  333. }
  334. e.OutSuc(c, "success", nil)
  335. return
  336. }