智慧食堂
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

363 рядки
11 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. enum2 "applet/app/enum"
  7. "applet/app/utils"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. func NursingHomeUserUpdate(req md.NursingHomeUserUpdateReq) (err error) {
  14. //1、查找当前用户&&当前单位下的所有身份
  15. var userIdentities []model.UserIdentity
  16. err = db.Db.Where("uid =? AND enterprise_id =?", req.Uid, req.EnterpriseId).Find(&userIdentities)
  17. if err != nil {
  18. return
  19. }
  20. var userIdentityIds []int
  21. for _, v := range userIdentities {
  22. userIdentityIds = append(userIdentityIds, v.Id)
  23. }
  24. _, err = db.Db.In("user_identity_id", userIdentityIds).Delete(model.ClassWithUser{})
  25. if err != nil {
  26. return
  27. }
  28. session := db.Db.NewSession()
  29. defer session.Close()
  30. session.Begin()
  31. //2、新增 / 编辑 数据
  32. now := time.Now()
  33. userIdentityDb := db.UserIdentityDb{}
  34. userIdentityDb.Set(req.Uid)
  35. var newUserIdentityIds []string
  36. for _, v := range req.BindUserIdentities {
  37. if v.UserIdentityId == 0 {
  38. // 新增
  39. insertId, err1 := userIdentityDb.UserIdentityInsertBySession(session, &model.UserIdentity{
  40. Uid: req.Uid,
  41. Name: v.Name,
  42. IdNo: v.IdNo,
  43. Kind: enum2.UserIdentityKindForCommon,
  44. Identity: enum2.UserIdentityForNursingHome,
  45. EnterpriseId: req.EnterpriseId,
  46. State: enum2.UserIdentityStateForNormal,
  47. Memo: "",
  48. CreateAt: now.Format("2006-01-02 15:04:05"),
  49. UpdateAt: now.Format("2006-01-02 15:04:05"),
  50. })
  51. if err1 != nil {
  52. _ = session.Rollback()
  53. return err1
  54. }
  55. newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(insertId))
  56. } else {
  57. //编辑
  58. identity, err2 := userIdentityDb.GetUserIdentity(v.UserIdentityId)
  59. if err != nil {
  60. _ = session.Rollback()
  61. return err2
  62. }
  63. if identity == nil {
  64. _ = session.Rollback()
  65. return errors.New("未查询到对应的身份信息记录")
  66. }
  67. identity.IdNo = v.IdNo
  68. identity.Name = v.Name
  69. _, err3 := userIdentityDb.UserIdentityUpdateBySession(session, v.UserIdentityId, identity, "id_no", "name")
  70. if err3 != nil {
  71. _ = session.Rollback()
  72. return err3
  73. }
  74. newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(v.UserIdentityId))
  75. }
  76. }
  77. //3、删除需要删除的身份信息
  78. for _, v := range userIdentityIds {
  79. if !utils.InArr(utils.IntToStr(v), newUserIdentityIds) {
  80. _, err = session.Where("id = ?", v).Delete(model.UserIdentity{})
  81. if err != nil {
  82. _ = session.Rollback()
  83. return
  84. }
  85. }
  86. }
  87. return session.Commit()
  88. }
  89. func NursingHomeUserDelete(req md.NursingHomeUserDeleteReq) (err error) {
  90. //1、删除 user_identity
  91. _, err = db.Db.Where("enterprise_id =?", req.EnterpriseId).In("uid", req.Uids).Delete(model.UserIdentity{})
  92. if err != nil {
  93. return
  94. }
  95. return
  96. }
  97. func NursingHomeOrdList(req md.NursingHomeOrdListReq) (resp []md.NursingHomeOrdListResp, count int64, err error) {
  98. var classWithUserIdentityIdsOne []int
  99. var classWithUserIdentityIdsTwo []int
  100. classWithUserDb := db.ClassWithUserDb{}
  101. classWithUserDb.Set()
  102. sess := db.Db.Desc("nursing_home_package_ord.id")
  103. if req.EnterpriseId != 0 {
  104. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  105. }
  106. if req.StartDate != "" {
  107. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  108. }
  109. if req.EndDate != "" {
  110. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  111. }
  112. if req.Kind != 0 {
  113. sess.And("nursing_home_package_ord.kind = ", req.Kind)
  114. }
  115. if req.Sate != 0 {
  116. sess.And("nursing_home_package_ord.state = ", req.Sate)
  117. }
  118. if req.OrdSate != 0 {
  119. sess.And("nursing_home_package_ord.ord_state = ", req.OrdSate)
  120. }
  121. if req.OutTradeNo != "" {
  122. sess.And("nursing_home_package_ord.out_trade_no like ?", "%"+req.OutTradeNo+"%")
  123. }
  124. if req.Name != "" {
  125. sess.And("user_identity.name like ?", "%"+req.Name+"%")
  126. }
  127. if req.Phone != "" {
  128. sess.And("user.phone like ?", "%"+req.Phone+"%")
  129. }
  130. if len(classWithUserIdentityIdsOne) > 0 {
  131. sess.In("user_identity.id", classWithUserIdentityIdsOne)
  132. }
  133. if len(classWithUserIdentityIdsTwo) > 0 {
  134. sess.In("user_identity.id", classWithUserIdentityIdsTwo)
  135. }
  136. var m []*db.NursingHomePackageOrdWithUserIdentity
  137. count, err = sess.
  138. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  139. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  140. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  141. Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  142. if err != nil {
  143. return nil, 0, err
  144. }
  145. for _, v := range m {
  146. resp = append(resp, md.NursingHomeOrdListResp{
  147. EnterpriseId: v.UserIdentity.EnterpriseId,
  148. EnterpriseName: v.Enterprise.Name,
  149. Uid: v.UserIdentity.Uid,
  150. UserIdentityId: v.UserIdentity.Id,
  151. UserIdentityKind: v.UserIdentity.Kind,
  152. UserIdentityName: v.NursingHomePackageOrd.UserIdentityName,
  153. TotalPrice: v.TotalPrice,
  154. Kind: v.NursingHomePackageOrd.Kind,
  155. OutTradeNo: v.NursingHomePackageOrd.OutTradeNo,
  156. TradeNo: v.NursingHomePackageOrd.TradeNo,
  157. State: v.NursingHomePackageOrd.State,
  158. OrdState: v.NursingHomePackageOrd.OrdState,
  159. CreateAt: v.NursingHomePackageOrd.CreateAt,
  160. Phone: v.User.Phone,
  161. Name: v.UserIdentity.Name,
  162. })
  163. }
  164. return
  165. }
  166. func NursingHomeReserveList(req md.NursingHomeReserveListReq) (resp []md.NursingHomeReserveListResp, count int64, err error) {
  167. var classWithUserIdentityIdsOne []int
  168. var classWithUserIdentityIdsTwo []int
  169. classWithUserDb := db.ClassWithUserDb{}
  170. classWithUserDb.Set()
  171. sess := db.Db.Where("1=1").Desc("nursing_home_package_ord_for_reserve.id")
  172. if req.EnterpriseId != 0 {
  173. sess.And("nursing_home_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  174. }
  175. if req.StartDate != "" {
  176. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  177. }
  178. if req.EndDate != "" {
  179. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  180. }
  181. if req.Kind != 0 {
  182. sess.And("nursing_home_package_ord_for_reserve.kind = ?", req.Kind)
  183. }
  184. if req.OutTradeNo != "" {
  185. sess.And("nursing_home_package_ord_for_reserve.out_trade_no like ?", "%"+req.OutTradeNo+"%")
  186. }
  187. if req.Name != "" {
  188. sess.And("user_identity.name like ?", "%"+req.Name+"%")
  189. }
  190. if req.Phone != "" {
  191. sess.And("user.phone like ?", "%"+req.Phone+"%")
  192. }
  193. if len(classWithUserIdentityIdsOne) > 0 {
  194. sess.In("user_identity.id", classWithUserIdentityIdsOne)
  195. }
  196. if len(classWithUserIdentityIdsTwo) > 0 {
  197. sess.In("user_identity.id", classWithUserIdentityIdsTwo)
  198. }
  199. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  200. count, err = sess.
  201. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  202. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  203. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  204. Join("LEFT", "user", "user.id = user_identity.uid").
  205. Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  206. if err != nil {
  207. return nil, 0, err
  208. }
  209. for _, v := range m {
  210. resp = append(resp, md.NursingHomeReserveListResp{
  211. Id: v.NursingHomePackageOrdForReserve.Id,
  212. EnterpriseId: v.UserIdentity.EnterpriseId,
  213. EnterpriseName: v.Enterprise.Name,
  214. Uid: v.UserIdentity.Uid,
  215. UserIdentityId: v.UserIdentity.Id,
  216. UserIdentityName: v.NursingHomePackageOrd.UserIdentityName,
  217. Kind: v.NursingHomePackageOrdForReserve.Kind,
  218. ReserveMonth: v.NursingHomePackageOrdForReserve.ReserveMonth,
  219. CreateAt: v.NursingHomePackageOrdForReserve.CreateAt,
  220. OutTradeNo: v.NursingHomePackageOrdForReserve.OutTradeNo,
  221. Phone: v.User.Phone,
  222. })
  223. }
  224. return
  225. }
  226. func NursingHomeOrdRefund(req md.NursingHomeOrdRefundReq) (err error) {
  227. //1、查询出所有 `nursing_home_user_with_day` 记录
  228. var m []model.NursingHomeUserWithDay
  229. nursingHomeUserWithDayDb := db.NursingHomeUserWithDayDb{}
  230. nursingHomeUserWithDayDb.Set(0)
  231. err = nursingHomeUserWithDayDb.Db.In("id", req.Ids).Find(&m)
  232. if err != nil {
  233. return err
  234. }
  235. if len(m) > 0 {
  236. //2、更改 `nursing_home_user_with_day` 的 state 为 退款中
  237. sql := "update nursing_home_user_with_day set state = %d where id In (%s)"
  238. idsStr := strings.Join(req.Ids, ",")
  239. sql = fmt.Sprintf(sql, enum2.NursingHomeUserWithDayStateForCanceling, idsStr)
  240. fmt.Println(sql)
  241. _, err = db.ExecuteOriginalSql(db.Db, sql)
  242. if err != nil {
  243. return err
  244. }
  245. //3、循环处理数据
  246. var dealOutTradeNo = map[string]string{}
  247. var nursingHomeUserRefundDays []*model.NursingHomeUserRefundDay
  248. now := time.Now()
  249. for _, v := range m {
  250. dealOutTradeNo[v.OrdNo] = v.OrdNo
  251. outRequestNo := utils.OrderUUID(v.Uid)
  252. nursingHomeUserRefundDays = append(nursingHomeUserRefundDays, &model.NursingHomeUserRefundDay{
  253. OutTradeNo: v.OrdNo,
  254. OutRequestNo: outRequestNo,
  255. Uid: v.Uid,
  256. IdentityId: v.IdentityId,
  257. RecordsId: v.Id,
  258. State: enum2.NursingHomeUserRefundDayStateForAuditing,
  259. Amount: v.Amount,
  260. Memo: "",
  261. CreateAt: now.Format("2006-01-02 15:04:05"),
  262. UpdateAt: now.Format("2006-01-02 15:04:05"),
  263. })
  264. }
  265. //4、处理 `nursing_home_package_ord` 的 订单状态(ord_state)
  266. for _, v := range dealOutTradeNo {
  267. err1 := JudgeNursingHomePackageOrdOrdState(v)
  268. if err1 != nil {
  269. return err1
  270. }
  271. }
  272. //5、新增 `nursing_home_user_refund_day` 数据
  273. nursingHomeUserRefundDayDb := db.NursingHomeUserRefundDayDb{}
  274. nursingHomeUserRefundDayDb.Set(0)
  275. _, err = nursingHomeUserRefundDayDb.BatchAddNursingHomeUserRefundDays(nursingHomeUserRefundDays)
  276. }
  277. return
  278. }
  279. // JudgeNursingHomePackageOrdOrdState 判断订单状态
  280. func JudgeNursingHomePackageOrdOrdState(outTradeNo string) (err error) {
  281. nursingHomePackageOrd := db.NursingHomePackageOrd{}
  282. nursingHomePackageOrd.Set(outTradeNo)
  283. ord, err := nursingHomePackageOrd.GetNursingHomePackageOrd()
  284. if err != nil {
  285. return
  286. }
  287. var ordState, oldOrdState int
  288. oldOrdState = ord.OrdState
  289. ordState = oldOrdState
  290. var m model.NursingHomeUserWithDay
  291. total, err := db.Db.Where("ord_no =?", outTradeNo).Count(&m)
  292. if err != nil {
  293. return
  294. }
  295. //1、判断是否有 `待就餐`
  296. count1, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForWait).Count(&m)
  297. if err != nil {
  298. return
  299. }
  300. if count1 == 0 {
  301. ordState = enum2.NursingHomePackageOrdOrdStateForComplete
  302. } else {
  303. ordState = enum2.NursingHomePackageOrdOrdStateForSuccess
  304. }
  305. //2、判断是否有 `已退款` / `部分退款`
  306. count2, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForCancel).Count(&m)
  307. if err != nil {
  308. return
  309. }
  310. //if count2 > 0 && count2 < total && count1 > 0 {
  311. if count2 > 0 && count2 < total {
  312. ordState = enum2.NursingHomePackageOrdOrdStateForPartRefunded
  313. }
  314. if count2 > 0 && count2 == total {
  315. ordState = enum2.NursingHomePackageOrdOrdStateForRefunded
  316. }
  317. //3、判断是否有 `退款中`
  318. count3, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForCanceling).Count(&m)
  319. if err != nil {
  320. return
  321. }
  322. if count3 > 0 {
  323. ordState = enum2.NursingHomePackageOrdOrdStateForRefunding
  324. }
  325. if ordState != oldOrdState {
  326. ord.OrdState = ordState
  327. _, err2 := nursingHomePackageOrd.NursingHomePackageOrdUpdate(ord, "ord_state")
  328. if err2 != nil {
  329. return err2
  330. }
  331. }
  332. return
  333. }