|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- package hdl
-
- import (
- enum2 "applet/app/admin/enum"
- "applet/app/admin/lib/validate"
- "applet/app/admin/md"
- "applet/app/admin/svc"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/enum"
- "applet/app/utils"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func EnterpriseList(c *gin.Context) {
- var req md.EnterpriseListReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- if req.Limit == 0 {
- req.Limit = 10
- }
- if req.Page == 0 {
- req.Page = 10
- }
-
- admin := svc.GetUser(c)
- enterprises, total, err := svc.EnterpriseList(*admin, req)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": enterprises,
- "total": total,
- "state": []map[string]interface{}{
- {
- "name": enum.EnterpriseState(enum.EnterpriseStateForNormal).String(),
- "value": enum.EnterpriseStateForNormal,
- },
- {
- "name": enum.EnterpriseState(enum.EnterpriseStateForFreeze).String(),
- "value": enum.EnterpriseStateForFreeze,
- },
- },
- "kind": []map[string]interface{}{
- {
- "name": enum.EnterprisePvd(enum.EnterprisePvdByCentralKitchenForSchool).String(),
- "value": enum.EnterprisePvdByCentralKitchenForSchool,
- },
- {
- "name": enum.EnterprisePvd(enum.EnterprisePvdByCentralKitchenForFactory).String(),
- "value": enum.EnterprisePvdByCentralKitchenForFactory,
- },
- {
- "name": enum.EnterprisePvd(enum.EnterprisePvdBySelfSupportForSchool).String(),
- "value": enum.EnterprisePvdBySelfSupportForSchool,
- },
- {
- "name": enum.EnterprisePvd(enum.EnterprisePvdBySelfSupportForFactory).String(),
- "value": enum.EnterprisePvdBySelfSupportForFactory,
- },
- {
- "name": enum.EnterprisePvd(enum.EnterprisePvdByNursingHome).String(),
- "value": enum.EnterprisePvdByNursingHome,
- },
- },
- }, nil)
- return
- }
-
- func Detail(c *gin.Context) {
- enterpriseId := c.DefaultQuery("id", "")
- detail, err := svc.EnterpriseDetail(utils.StrToInt(enterpriseId))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, detail, nil)
- return
- }
-
- func SchoolBelowGrade(c *gin.Context) {
- enterpriseId := c.DefaultQuery("enterprise_id", "")
- gradeDb := db.GradeDb{}
- gradeDb.Set(utils.StrToInt(enterpriseId))
- gradeList, err := gradeDb.FindGrade()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": gradeList,
- }, nil)
- return
- }
-
- func SchoolBelowPeriod(c *gin.Context) {
- enterpriseId := c.DefaultQuery("enterprise_id", "")
- periodDb := db.PeriodDb{}
- periodDb.Set(utils.StrToInt(enterpriseId))
- periodList, err := periodDb.FindPeriod()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": periodList,
- }, nil)
- return
- }
-
- func SchoolGradeBelowClass(c *gin.Context) {
- gradeId := c.DefaultQuery("grade_id", "")
- enterpriseId := c.DefaultQuery("enterprise_id", "")
- classDb := db.ClassDb{}
-
- if gradeId != "" {
- classDb.Set(utils.StrToInt(gradeId))
- classList, err := classDb.FindClass()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": classList,
- }, nil)
- } else {
- classDb.Set(0)
- classList, err := classDb.FindClassByEnterprise(enterpriseId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": classList,
- }, nil)
- }
- return
- }
-
- func EnterpriseAdd(c *gin.Context) {
- var req md.EnterpriseAddReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- now := time.Now()
- var pvd = 2
- if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory {
- pvd = 1
- }
- enterprise := model.Enterprise{
- Name: req.Name,
- Pvd: int32(pvd),
- Kind: req.Kind,
- CompanyId: req.CompanyId,
- State: enum2.CompanyStateForNormal,
- Memo: req.Memo,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- enterpriseId, err := enterpriseDb.EnterpriseInsert(&enterprise)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool {
- //新增 `central_kitchen_for_school_set` && `central_kitchen_for_school_with_spec`
- centralKitchenForSchoolSetDb := db.CentralKitchenForSchoolSetDb{}
- centralKitchenForSchoolSetDb.Set(enterpriseId)
- centralKitchenForSchoolSetDb.CentralKitchenForSchoolSetInsert(&model.CentralKitchenForSchoolSet{
- EnterpriseId: enterpriseId,
- IsOpenTeacherReportMeal: 1,
- IsOpenReportMealForDay: 1,
- IsOpenReportMealForMonth: 1,
- IsOpenReportMealForSemester: 1,
- IsOpenTeacherReportMealForDay: 1,
- IsOpenTeacherReportMealForMonth: 1,
- IsOpenTeacherReportMealForSemester: 1,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
-
- centralKitchenForSchoolWithSpec := db.CentralKitchenForSchoolWithSpec{}
- centralKitchenForSchoolWithSpec.Set(enterpriseId)
- centralKitchenForSchoolWithSpec.CentralKitchenForSchoolWithSpecInsert(&model.CentralKitchenForSchoolWithSpec{
- EnterpriseId: enterpriseId,
- IsOpenBreakfast: 1,
- IsOpenLunch: 1,
- IsOpenDinner: 1,
- BreakfastUnitPrice: "0.00",
- LunchUnitPrice: "0.00",
- DinnerUnitPrice: "0.00",
- BreakfastUnitPriceForTeacher: "0.00",
- LunchUnitPriceForTeacher: "0.00",
- DinnerUnitPriceForTeacher: "0.00",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
- if req.Kind == enum.EnterprisePvdByNursingHome {
- //新增 `nursing_home_set` && `nursing_home_with_spec`
- nursingHomeSetDb := db.NursingHomeSetDb{}
- nursingHomeSetDb.Set(enterpriseId)
- nursingHomeSetDb.NursingHomeSetInsert(&model.NursingHomeSet{
- EnterpriseId: enterpriseId,
- IsOpenReportMealForDay: 1,
- IsOpenReportMealForMonth: 1,
- IsOpenReportMealForYear: 1,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
-
- nursingHomeWithSpec := db.NursingHomeWithSpec{}
- nursingHomeWithSpec.Set(enterpriseId)
- nursingHomeWithSpec.NursingHomeWithSpecInsert(&model.NursingHomeWithSpec{
- EnterpriseId: enterpriseId,
- IsOpenBreakfast: 1,
- IsOpenLunch: 1,
- IsOpenDinner: 1,
- BreakfastUnitPrice: "0.00",
- LunchUnitPrice: "0.00",
- DinnerUnitPrice: "0.00",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func EnterpriseDelete(c *gin.Context) {
- var req md.EnterpriseDeleteReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- err = svc.EnterpriseDelete(req.EnterpriseIds)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func EnterpriseUpdate(c *gin.Context) {
- var req md.EnterpriseUpdateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
-
- //1、更新 enterprise
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if enterprise == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- var pvd = 2
- if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory || req.Kind == enum.EnterprisePvdByNursingHome {
- pvd = 1
- }
-
- now := time.Now()
- enterprise.Name = req.Name
- enterprise.Memo = req.Memo
- enterprise.Pvd = int32(pvd)
- enterprise.Kind = req.Kind
- enterprise.CompanyId = req.CompanyId
- enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "pvd", "kind", "company_id", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- //2、更新 period && grade && class 数据
- periodDb := db.PeriodDb{}
- periodDb.Set(req.Id)
- gradeDb := db.GradeDb{}
- gradeDb.Set(req.Id)
- classDb := db.ClassDb{}
- classDb.Set(0)
-
- //新增 grade 数据 && class 数据
- var classes []*model.Class
- for _, v := range req.PeriodList {
- if v.Id == 0 {
- //TODO::新增
- insertId, err1 := periodDb.PeriodInsert(&model.Period{
- EnterpriseId: req.Id,
- Name: v.Name,
- BreakfastUnitPrice: "0.00",
- LunchUnitPrice: "0.00",
- DinnerUnitPrice: "0.00",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
-
- for _, v1 := range v.GradeList {
- insertId1, err2 := gradeDb.GradeInsert(&model.Grade{
- Name: v1.Name,
- Memo: "",
- PeriodId: insertId,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
-
- for _, v2 := range v1.ClassList {
- classes = append(classes, &model.Class{
- Name: v2.Name,
- Memo: "",
- GradeId: insertId1,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
- }
- } else {
- //TODO::编辑
- period, err1 := periodDb.GetPeriod(v.Id)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if period == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相关学段信息")
- return
- }
- period.Name = v.Name
- _, err2 := periodDb.PeriodUpdate(period, "name")
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- for _, v1 := range v.GradeList {
- var gradeId int
- if v1.Id == 0 {
- gradeId, err2 = gradeDb.GradeInsert(&model.Grade{
- Name: v1.Name,
- Memo: "",
- PeriodId: period.Id,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- } else {
- grade, err3 := gradeDb.GetGrade(v1.Id)
- if err3 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err3.Error())
- return
- }
- if grade == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相关年级信息")
- return
- }
- grade.Name = v1.Name
- _, err4 := gradeDb.GradeUpdate(grade, "name")
- if err4 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err4.Error())
- return
- }
- gradeId = grade.Id
- }
-
- for _, v2 := range v1.ClassList {
- if v2.Id == 0 {
- //新增
- classes = append(classes, &model.Class{
- Name: v2.Name,
- Memo: "",
- GradeId: gradeId,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- } else {
- //编辑
- class, err4 := classDb.GetClass(v2.Id)
- if err4 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err4.Error())
- return
- }
- if class == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相关班级信息")
- return
- }
- class.Name = v2.Name
- _, err6 := classDb.ClassUpdate(class, "name")
- if err6 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err6.Error())
- return
- }
- }
- }
- }
- }
- }
-
- if len(classes) > 0 {
- _, err2 := classDb.BatchAddClass(classes)
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func EnterpriseAddGrade(c *gin.Context) {
- var req md.EnterpriseUpdateStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if enterprise == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- now := time.Now()
- enterprise.State = req.State
- enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func EnterpriseUpdateState(c *gin.Context) {
- var req md.EnterpriseUpdateStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if enterprise == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- now := time.Now()
- enterprise.State = req.State
- enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func ListCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
- var req md.ListCentralKitchenForSchoolPackageForSystemReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- centralKitchenForSchoolPackageForSystemDb := db.CentralKitchenForSchoolPackageForSystemDb{}
- centralKitchenForSchoolPackageForSystemDb.Set()
- list, total, err := centralKitchenForSchoolPackageForSystemDb.CentralKitchenForSchoolPackageForSystemList(req)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": list,
- "total": total,
- }, nil)
- return
- }
-
- func SetCentralKitchenForSchoolWithSpecForSystem(c *gin.Context) {
- var req md.SetCentralKitchenForSchoolWithSpecForSystemReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
-
- centralKitchenForSchoolWithSpecForSystemDb := db.CentralKitchenForSchoolWithSpecForSystem{}
- centralKitchenForSchoolWithSpecForSystemDb.Set()
- system, err := centralKitchenForSchoolWithSpecForSystemDb.GetCentralKitchenForSchoolWithSpecForSystem()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- system.BreakfastUnitPrice = req.BreakfastUnitPrice
- system.LunchUnitPrice = req.LunchUnitPrice
- system.DinnerUnitPrice = req.DinnerUnitPrice
- system.BreakfastUnitPriceForTeacher = req.BreakfastUnitPriceForTeacher
- system.LunchUnitPriceForTeacher = req.LunchUnitPriceForTeacher
- system.DinnerUnitPriceForTeacher = req.DinnerUnitPriceForTeacher
- system.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
- updateAffected, err := centralKitchenForSchoolWithSpecForSystemDb.CentralKitchenForSchoolWithSpecForSystemUpdate(system.Id, system)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if updateAffected <= 0 {
- e.OutErr(c, e.ERR, "更新数据失败")
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func GetCentralKitchenForSchoolWithSpecForSystem(c *gin.Context) {
- centralKitchenForSchoolWithSpecForSystemDb := db.CentralKitchenForSchoolWithSpecForSystem{}
- centralKitchenForSchoolWithSpecForSystemDb.Set()
- system, err := centralKitchenForSchoolWithSpecForSystemDb.GetCentralKitchenForSchoolWithSpecForSystem()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "data": system,
- }, nil)
- return
- }
-
- func SaveCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
- var req md.SaveCentralKitchenForSchoolPackageForSystemReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- //判断是新增 / 编辑
- if req.Id > 0 {
- err = svc.UpdateCentralKitchenForSchoolPackageForSystem(req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- } else {
- err = svc.AddCentralKitchenForSchoolPackageForSystem(req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func SyncCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
- var req md.SyncCentralKitchenForSchoolPackageForSystemReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
-
- err = svc.SyncCentralKitchenForSchoolPackageForSystem(req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func DeleteCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
- id := c.Param("id")
- err := svc.DeleteCentralKitchenForSchoolPackageForSystem(utils.StrToInt(id))
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
|