|
- package hdl
-
- import (
- "applet/app/customer/lib/validate"
- "applet/app/customer/md"
- "applet/app/customer/svc"
- svc2 "applet/app/customer/svc/order"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/enum"
- "applet/app/utils"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func SaveCentralKitchenForSchoolUserIdentity(c *gin.Context) {
- var req md.SaveCentralKitchenForSchoolUserIdentityReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- var identity = enum.UserIdentityForCentralKitchenForStudent
- if req.IsTeacher {
- identity = enum.UserIdentityForCentralKitchenForTeacher
- }
-
- //1、判断当前身份是否已绑定
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(user.Id)
- isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if isHasUserIdentity != nil && isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
- e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
- return
- }
-
- //2、新增身份信息
- now := time.Time{}
- userIdentity := &model.UserIdentity{
- Uid: user.Id,
- Name: req.Name,
- IdNo: req.IdNo,
- Kind: enum.UserIdentityKindForCommon,
- Identity: identity,
- EnterpriseId: req.EnterpriseId,
- State: enum.UserIdentityStateForNormal,
- Memo: "",
- CreateAt: now,
- UpdateAt: now,
- }
- insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
- return
- }
-
- //3、新增 class_with_user 记录
- if !req.IsTeacher {
- classWithUserDb := db.ClassWithUserDb{}
- classWithUserDb.Set()
- _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
- UserIdentityId: insertAffected,
- ClassId: req.ClassId,
- CreateAt: now,
- UpdateAt: now,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func SaveSelfSupportForSchoolUserIdentity(c *gin.Context) {
- var req md.SaveSelfSupportForSchoolUserIdentityReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- now := time.Time{}
- var identity = enum.UserIdentityForSelfSupportForStudent
- var kind = enum.UserIdentityKindForCommon
- if req.Kind == enum.UserIdentityKindForWorker {
- kind = enum.UserIdentityKindForWorker
- identity = enum.UserIdentityForSelfSupportForWorker
- } else {
- if req.IsTeacher {
- identity = enum.UserIdentityForCentralKitchenForTeacher
- }
- }
-
- //1、判断当前身份是否已绑定
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(user.Id)
- isHasUserIdentity, err := userIdentityDb.UserIdentityExist(req.EnterpriseId, req.IdNo)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- if isHasUserIdentity == nil {
- if identity != enum.UserIdentityForSelfSupportForStudent {
- e.OutErr(c, e.ERR_NO_DATA, "当前身份信息不存在")
- return
- }
- //2、新增身份信息
- userIdentity := &model.UserIdentity{
- Uid: user.Id,
- Name: req.Name,
- IdNo: req.IdNo,
- Kind: kind,
- Identity: identity,
- EnterpriseId: req.EnterpriseId,
- State: enum.UserIdentityStateForNormal,
- Memo: "",
- CreateAt: now,
- UpdateAt: now,
- }
- insertAffected, err := userIdentityDb.UserIdentityInsert(userIdentity)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if insertAffected <= 0 {
- e.OutErr(c, e.ERR_DB_ORM, "新增身份数据失败")
- return
- }
- //3、新增 class_with_user 记录
- if !req.IsTeacher {
- classWithUserDb := db.ClassWithUserDb{}
- classWithUserDb.Set()
- _, err = classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
- UserIdentityId: insertAffected,
- ClassId: req.ClassId,
- CreateAt: now,
- UpdateAt: now,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- }
- } else {
- if isHasUserIdentity != nil && isHasUserIdentity.Uid != 0 && isHasUserIdentity.Uid != user.Id {
- e.OutErr(c, e.ERR, "当前身份信息已被绑定使用")
- return
- }
- //4、修改身份信息
- isHasUserIdentity.Uid = user.Id
- isHasUserIdentity.Name = req.Name
- _, err = userIdentityDb.UserIdentityUpdate(isHasUserIdentity.Id, isHasUserIdentity, "uid", "name")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- if identity == enum.UserIdentityForSelfSupportForStudent {
- //5、修改 class_with_user 记录
- classWithUserDb := db.ClassWithUserDb{}
- classWithUserDb.Set()
- classWithUser, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(isHasUserIdentity.Id)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if classWithUser == nil {
- _, err2 := classWithUserDb.ClassWithUserInsert(&model.ClassWithUser{
- UserIdentityId: isHasUserIdentity.Id,
- ClassId: req.ClassId,
- CreateAt: now,
- UpdateAt: now,
- })
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- } else {
- classWithUser.ClassId = req.ClassId
- _, err2 := classWithUserDb.ClassWithUserUpdateByUserIdentity(isHasUserIdentity.Id, classWithUser, "class_id")
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- }
- }
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func CentralKitchenForSchoolMyReserve(c *gin.Context) {
- date := c.DefaultQuery("date", "")
- userIdentityId := c.DefaultQuery("user_identity_id", "")
- if date == "" {
- e.OutErr(c, e.ERR_INVALID_ARGS, "日期不能为空")
- return
- }
- startDate := date + "-01"
- startDateTime, _ := time.Parse("2006-01-02", startDate)
- endDate := startDateTime.AddDate(0, 1, -1).Format("2006-01-02")
- err, resp := svc2.CentralKitchenForSchoolMyReserve(utils.StrToInt(userIdentityId), date, startDate, endDate)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "info": resp,
- }, nil)
- return
- }
|