|
- 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
- }
- enterprises, total, err := svc.EnterpriseList(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,
- },
- },
- }, 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"),
- }
- _, err = enterpriseDb.EnterpriseInsert(&enterprise)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- 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 {
- 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
- }
|