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

198 lines
6.4 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "errors"
  7. "time"
  8. )
  9. func AddNursingHomePackage(req md.SaveNursingHomePackageReq) (err error) {
  10. session := db.Db.NewSession()
  11. defer session.Close()
  12. session.Begin()
  13. now := time.Now()
  14. //TODO::判断当前月份是否存在
  15. NursingHomePackageDb := db.NursingHomePackageDb{}
  16. NursingHomePackageDb.Set()
  17. isHasPackage, err := NursingHomePackageDb.GetNursingHomePackageByMonth(req.EnterpriseId, req.Year, req.Month)
  18. if err != nil {
  19. return err
  20. }
  21. if isHasPackage != nil {
  22. return errors.New("请勿重复添加【" + req.Month + "】套餐")
  23. }
  24. //1、查询`central_kitchen_for_school_with_spec`
  25. NursingHomeWithSpec := db.NursingHomeWithSpec{}
  26. NursingHomeWithSpec.Set(req.EnterpriseId)
  27. spec, err := NursingHomeWithSpec.GetNursingHomeWithSpec()
  28. if err != nil {
  29. _ = session.Rollback()
  30. return err
  31. }
  32. //1、新增`central_kitchen_for_school_package`
  33. packageId, err := NursingHomePackageDb.NursingHomePackageInsertBySession(session, &model.NursingHomePackage{
  34. EnterpriseId: req.EnterpriseId,
  35. Year: req.Year,
  36. Month: req.Month,
  37. TotalPrice: "0",
  38. StartDate: req.StartDate,
  39. EndDate: req.EndDate,
  40. State: 1,
  41. CreateAt: now.Format("2006-01-02 15:04:05"),
  42. UpdateAt: now.Format("2006-01-02 15:04:05"),
  43. })
  44. if err != nil {
  45. _ = session.Rollback()
  46. return err
  47. }
  48. //2、批量新增 `central_kitchen_for_school_package_with_day`
  49. var NursingHomePackageWithDays []*model.NursingHomePackageWithDay
  50. for _, v := range req.DateList {
  51. var isOpenBreakfast, isOpenLunch, isOpenDinner, isOpenReplenish = md.CloseBreakfast, md.CloseLunch, md.CloseDinner, md.CloseReplenish
  52. if spec.IsOpenBreakfast == md.OpenBreakfast && v.IsOpenBreakfast == md.OpenBreakfast {
  53. isOpenBreakfast = md.OpenBreakfast
  54. }
  55. if spec.IsOpenLunch == md.OpenLunch && v.IsOpenLunch == md.OpenLunch {
  56. isOpenLunch = md.OpenLunch
  57. }
  58. if spec.IsOpenDinner == md.OpenDinner && v.IsOpenDinner == md.OpenDinner {
  59. isOpenDinner = md.OpenDinner
  60. }
  61. if v.IsOpenReplenish == md.OpenReplenish {
  62. isOpenReplenish = md.OpenReplenish
  63. }
  64. NursingHomePackageWithDays = append(NursingHomePackageWithDays, &model.NursingHomePackageWithDay{
  65. Date: v.Date,
  66. PackageId: packageId,
  67. IsOpenBreakfast: isOpenBreakfast,
  68. IsOpenLunch: isOpenLunch,
  69. IsOpenDinner: isOpenDinner,
  70. IsOpenReplenish: isOpenReplenish,
  71. })
  72. }
  73. NursingHomePackageWithDayDb := db.NursingHomePackageWithDayDb{}
  74. NursingHomePackageWithDayDb.Set(packageId)
  75. _, err = NursingHomePackageWithDayDb.BatchAddNursingHomePackageWithDaysBySession(session, NursingHomePackageWithDays)
  76. if err != nil {
  77. _ = session.Rollback()
  78. return err
  79. }
  80. return session.Commit()
  81. }
  82. func UpdateNursingHomePackage(req md.SaveNursingHomePackageReq) (err error) {
  83. session := db.Db.NewSession()
  84. defer session.Close()
  85. session.Begin()
  86. now := time.Now()
  87. //1、查询`central_kitchen_for_school_with_spec`
  88. NursingHomeWithSpec := db.NursingHomeWithSpec{}
  89. NursingHomeWithSpec.Set(req.EnterpriseId)
  90. spec, err := NursingHomeWithSpec.GetNursingHomeWithSpec()
  91. if err != nil {
  92. _ = session.Rollback()
  93. return err
  94. }
  95. //2、修改`central_kitchen_for_school_package`
  96. NursingHomePackageDb := db.NursingHomePackageDb{}
  97. NursingHomePackageDb.Set()
  98. NursingHomePackage, err := NursingHomePackageDb.GetNursingHomePackage(req.PackageId)
  99. if err != nil {
  100. _ = session.Rollback()
  101. return err
  102. }
  103. if NursingHomePackage == nil {
  104. _ = session.Rollback()
  105. return errors.New("未查询到对应套餐记录")
  106. }
  107. NursingHomePackage.Year = req.Year
  108. NursingHomePackage.Month = req.Month
  109. NursingHomePackage.StartDate = req.StartDate
  110. NursingHomePackage.EndDate = req.EndDate
  111. NursingHomePackage.State = req.State
  112. NursingHomePackage.UpdateAt = now.Format("2006-01-02 15:04:05")
  113. affected, err := NursingHomePackageDb.NursingHomePackageUpdateBySession(session, NursingHomePackage.Id, NursingHomePackage)
  114. if err != nil {
  115. _ = session.Rollback()
  116. return
  117. }
  118. if affected == 0 {
  119. _ = session.Rollback()
  120. return errors.New("更新套餐记录失败")
  121. }
  122. //3、删除 `central_kitchen_for_school_package_with_day` 旧数据
  123. NursingHomePackageWithDayDb := db.NursingHomePackageWithDayDb{}
  124. NursingHomePackageWithDayDb.Set(NursingHomePackage.Id)
  125. _, err = NursingHomePackageWithDayDb.NursingHomePackageWithDayDeleteBySession(session)
  126. if err != nil {
  127. _ = session.Rollback()
  128. return
  129. }
  130. //4、批量新增 `central_kitchen_for_school_package_with_day`
  131. var NursingHomePackageWithDays []*model.NursingHomePackageWithDay
  132. for _, v := range req.DateList {
  133. var isOpenBreakfast, isOpenLunch, isOpenDinner, isOpenReplenish = md.CloseBreakfast, md.CloseLunch, md.CloseDinner, md.CloseReplenish
  134. if spec.IsOpenBreakfast == md.OpenBreakfast && v.IsOpenBreakfast == md.OpenBreakfast {
  135. isOpenBreakfast = md.OpenBreakfast
  136. }
  137. if spec.IsOpenLunch == md.OpenLunch && v.IsOpenLunch == md.OpenLunch {
  138. isOpenLunch = md.OpenLunch
  139. }
  140. if spec.IsOpenDinner == md.OpenDinner && v.IsOpenDinner == md.OpenDinner {
  141. isOpenDinner = md.OpenDinner
  142. }
  143. if v.IsOpenReplenish == md.OpenReplenish {
  144. isOpenReplenish = md.OpenReplenish
  145. }
  146. NursingHomePackageWithDays = append(NursingHomePackageWithDays, &model.NursingHomePackageWithDay{
  147. Date: v.Date,
  148. PackageId: NursingHomePackage.Id,
  149. IsOpenBreakfast: isOpenBreakfast,
  150. IsOpenLunch: isOpenLunch,
  151. IsOpenDinner: isOpenDinner,
  152. IsOpenReplenish: isOpenReplenish,
  153. })
  154. }
  155. _, err = NursingHomePackageWithDayDb.BatchAddNursingHomePackageWithDaysBySession(session, NursingHomePackageWithDays)
  156. if err != nil {
  157. _ = session.Rollback()
  158. return err
  159. }
  160. return session.Commit()
  161. }
  162. func DeleteNursingHomePackage(packageId int) (err error) {
  163. session := db.Db.NewSession()
  164. defer session.Close()
  165. session.Begin()
  166. //1、删除`central_kitchen_for_school_package`
  167. NursingHomePackageDb := db.NursingHomePackageDb{}
  168. NursingHomePackageDb.Set()
  169. NursingHomePackage, err := NursingHomePackageDb.GetNursingHomePackage(packageId)
  170. if err != nil {
  171. _ = session.Rollback()
  172. return err
  173. }
  174. if NursingHomePackage == nil {
  175. _ = session.Rollback()
  176. return errors.New("未查询到对应套餐记录")
  177. }
  178. NursingHomePackage.IsDelete = 1
  179. affected, err := NursingHomePackageDb.NursingHomePackageUpdateBySession(session, NursingHomePackage.Id, NursingHomePackage, "is_delete")
  180. if err != nil {
  181. _ = session.Rollback()
  182. return
  183. }
  184. if affected == 0 {
  185. _ = session.Rollback()
  186. return errors.New("删除套餐记录失败")
  187. }
  188. return session.Commit()
  189. }