|
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "errors"
- "time"
- )
-
- func AddNursingHomePackage(req md.SaveNursingHomePackageReq) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- now := time.Now()
- //TODO::判断当前月份是否存在
- NursingHomePackageDb := db.NursingHomePackageDb{}
- NursingHomePackageDb.Set()
- isHasPackage, err := NursingHomePackageDb.GetNursingHomePackageByMonth(req.EnterpriseId, req.Year, req.Month)
- if err != nil {
- return err
- }
- if isHasPackage != nil {
- return errors.New("请勿重复添加【" + req.Month + "】套餐")
- }
-
- //1、查询`central_kitchen_for_school_with_spec`
- NursingHomeWithSpec := db.NursingHomeWithSpec{}
- NursingHomeWithSpec.Set(req.EnterpriseId)
- spec, err := NursingHomeWithSpec.GetNursingHomeWithSpec()
- if err != nil {
- _ = session.Rollback()
- return err
- }
- //1、新增`central_kitchen_for_school_package`
- packageId, err := NursingHomePackageDb.NursingHomePackageInsertBySession(session, &model.NursingHomePackage{
- EnterpriseId: req.EnterpriseId,
- Year: req.Year,
- Month: req.Month,
- TotalPrice: "0",
- StartDate: req.StartDate,
- EndDate: req.EndDate,
- State: 1,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return err
- }
-
- //2、批量新增 `central_kitchen_for_school_package_with_day`
- var NursingHomePackageWithDays []*model.NursingHomePackageWithDay
- for _, v := range req.DateList {
- var isOpenBreakfast, isOpenLunch, isOpenDinner, isOpenReplenish = md.CloseBreakfast, md.CloseLunch, md.CloseDinner, md.CloseReplenish
- if spec.IsOpenBreakfast == md.OpenBreakfast && v.IsOpenBreakfast == md.OpenBreakfast {
- isOpenBreakfast = md.OpenBreakfast
- }
- if spec.IsOpenLunch == md.OpenLunch && v.IsOpenLunch == md.OpenLunch {
- isOpenLunch = md.OpenLunch
- }
- if spec.IsOpenDinner == md.OpenDinner && v.IsOpenDinner == md.OpenDinner {
- isOpenDinner = md.OpenDinner
- }
- if v.IsOpenReplenish == md.OpenReplenish {
- isOpenReplenish = md.OpenReplenish
- }
- NursingHomePackageWithDays = append(NursingHomePackageWithDays, &model.NursingHomePackageWithDay{
- Date: v.Date,
- PackageId: packageId,
- IsOpenBreakfast: isOpenBreakfast,
- IsOpenLunch: isOpenLunch,
- IsOpenDinner: isOpenDinner,
- IsOpenReplenish: isOpenReplenish,
- })
- }
- NursingHomePackageWithDayDb := db.NursingHomePackageWithDayDb{}
- NursingHomePackageWithDayDb.Set(packageId)
- _, err = NursingHomePackageWithDayDb.BatchAddNursingHomePackageWithDaysBySession(session, NursingHomePackageWithDays)
- if err != nil {
- _ = session.Rollback()
- return err
- }
- return session.Commit()
- }
-
- func UpdateNursingHomePackage(req md.SaveNursingHomePackageReq) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- now := time.Now()
- //1、查询`central_kitchen_for_school_with_spec`
- NursingHomeWithSpec := db.NursingHomeWithSpec{}
- NursingHomeWithSpec.Set(req.EnterpriseId)
- spec, err := NursingHomeWithSpec.GetNursingHomeWithSpec()
- if err != nil {
- _ = session.Rollback()
- return err
- }
- //2、修改`central_kitchen_for_school_package`
- NursingHomePackageDb := db.NursingHomePackageDb{}
- NursingHomePackageDb.Set()
- NursingHomePackage, err := NursingHomePackageDb.GetNursingHomePackage(req.PackageId)
- if err != nil {
- _ = session.Rollback()
- return err
- }
- if NursingHomePackage == nil {
- _ = session.Rollback()
- return errors.New("未查询到对应套餐记录")
- }
- NursingHomePackage.Year = req.Year
- NursingHomePackage.Month = req.Month
- NursingHomePackage.StartDate = req.StartDate
- NursingHomePackage.EndDate = req.EndDate
- NursingHomePackage.State = req.State
- NursingHomePackage.UpdateAt = now.Format("2006-01-02 15:04:05")
- affected, err := NursingHomePackageDb.NursingHomePackageUpdateBySession(session, NursingHomePackage.Id, NursingHomePackage)
- if err != nil {
- _ = session.Rollback()
- return
- }
- if affected == 0 {
- _ = session.Rollback()
- return errors.New("更新套餐记录失败")
- }
-
- //3、删除 `central_kitchen_for_school_package_with_day` 旧数据
- NursingHomePackageWithDayDb := db.NursingHomePackageWithDayDb{}
- NursingHomePackageWithDayDb.Set(NursingHomePackage.Id)
- _, err = NursingHomePackageWithDayDb.NursingHomePackageWithDayDeleteBySession(session)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //4、批量新增 `central_kitchen_for_school_package_with_day`
- var NursingHomePackageWithDays []*model.NursingHomePackageWithDay
- for _, v := range req.DateList {
- var isOpenBreakfast, isOpenLunch, isOpenDinner, isOpenReplenish = md.CloseBreakfast, md.CloseLunch, md.CloseDinner, md.CloseReplenish
- if spec.IsOpenBreakfast == md.OpenBreakfast && v.IsOpenBreakfast == md.OpenBreakfast {
- isOpenBreakfast = md.OpenBreakfast
- }
- if spec.IsOpenLunch == md.OpenLunch && v.IsOpenLunch == md.OpenLunch {
- isOpenLunch = md.OpenLunch
- }
- if spec.IsOpenDinner == md.OpenDinner && v.IsOpenDinner == md.OpenDinner {
- isOpenDinner = md.OpenDinner
- }
- if v.IsOpenReplenish == md.OpenReplenish {
- isOpenReplenish = md.OpenReplenish
- }
- NursingHomePackageWithDays = append(NursingHomePackageWithDays, &model.NursingHomePackageWithDay{
- Date: v.Date,
- PackageId: NursingHomePackage.Id,
- IsOpenBreakfast: isOpenBreakfast,
- IsOpenLunch: isOpenLunch,
- IsOpenDinner: isOpenDinner,
- IsOpenReplenish: isOpenReplenish,
- })
- }
- _, err = NursingHomePackageWithDayDb.BatchAddNursingHomePackageWithDaysBySession(session, NursingHomePackageWithDays)
- if err != nil {
- _ = session.Rollback()
- return err
- }
- return session.Commit()
- }
-
- func DeleteNursingHomePackage(packageId int) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- //1、删除`central_kitchen_for_school_package`
- NursingHomePackageDb := db.NursingHomePackageDb{}
- NursingHomePackageDb.Set()
- NursingHomePackage, err := NursingHomePackageDb.GetNursingHomePackage(packageId)
- if err != nil {
- _ = session.Rollback()
- return err
- }
- if NursingHomePackage == nil {
- _ = session.Rollback()
- return errors.New("未查询到对应套餐记录")
- }
- NursingHomePackage.IsDelete = 1
- affected, err := NursingHomePackageDb.NursingHomePackageUpdateBySession(session, NursingHomePackage.Id, NursingHomePackage, "is_delete")
- if err != nil {
- _ = session.Rollback()
- return
- }
- if affected == 0 {
- _ = session.Rollback()
- return errors.New("删除套餐记录失败")
- }
- return session.Commit()
- }
|