|
- 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 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,
- 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、更新 grade && class 数据
-
- //2、删除 grade && class 数据
- gradeDb := db.GradeDb{}
- gradeDb.Set(req.Id)
- //_, err = gradeDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
- //if err != nil {
- // e.OutErr(c, e.ERR_DB_ORM, err.Error())
- // return
- //}
- classDb := db.ClassDb{}
- classDb.Set(0)
- //_, err = classDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
- //if err != nil {
- // e.OutErr(c, e.ERR_DB_ORM, err.Error())
- // return
- //}
-
- //新增 grade 数据 && class 数据
- for _, v := range req.GradeList {
- var classes []*model.Class
-
- if v.Id == 0 {
- //TODO::新增
- insertId, err1 := gradeDb.GradeInsert(&model.Grade{
- EnterpriseId: req.Id,
- Name: v.Name,
- Memo: "",
- 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.ClassList {
- classes = append(classes, &model.Class{
- Name: v1.Name,
- Memo: "",
- GradeId: insertId,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
-
- } else {
- //TODO::编辑
- grade, err3 := gradeDb.GetGrade(v.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 = v.Name
- _, err4 := gradeDb.GradeUpdate(grade, "name")
- if err4 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err4.Error())
- return
- }
-
- for _, v1 := range v.ClassList {
- if v1.Id == 0 {
- //新增
- classes = append(classes, &model.Class{
- Name: v1.Name,
- Memo: "",
- GradeId: grade.Id,
- EnterpriseId: req.Id,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- } else {
- //编辑
- class, err5 := classDb.GetClass(v1.Id)
- if err5 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err5.Error())
- return
- }
- if class == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相关班级信息")
- return
- }
- class.Name = v1.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
- }
|