From e8123200e6fafec0ce97940ab5dcb626ca36b135 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 6 Mar 2024 15:46:33 +0800 Subject: [PATCH] update --- app/admin/hdl/hdl_merchant.go | 148 +++++++++++++++++++++++++++ app/admin/md/md_merchant.go | 19 ++++ app/db/db_merchant.go | 107 +++++++++++++++++++ app/db/db_merchant_with_device.go | 68 ++++++++++++ app/db/model/merchant.go | 9 ++ app/db/model/merchant_with_device.go | 7 ++ app/router/admin_router.go | 8 ++ 7 files changed, 366 insertions(+) create mode 100644 app/admin/hdl/hdl_merchant.go create mode 100644 app/admin/md/md_merchant.go create mode 100644 app/db/db_merchant.go create mode 100644 app/db/db_merchant_with_device.go create mode 100644 app/db/model/merchant.go create mode 100644 app/db/model/merchant_with_device.go diff --git a/app/admin/hdl/hdl_merchant.go b/app/admin/hdl/hdl_merchant.go new file mode 100644 index 0000000..b30e9f8 --- /dev/null +++ b/app/admin/hdl/hdl_merchant.go @@ -0,0 +1,148 @@ +package hdl + +import ( + "applet/app/admin/lib/validate" + "applet/app/admin/md" + "applet/app/db" + "applet/app/db/model" + "applet/app/e" + "applet/app/utils" + "github.com/gin-gonic/gin" + "time" +) + +func MerchantList(c *gin.Context) { + var req md.MerchantListReq + 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 = 1 + } + var m []*db.MerchantWithEnterpriseInfo + engine := db.Db.Where("1=1") + if req.EnterpriseId != 0 { + engine = engine.And("merchant.enterprise_id =?", req.EnterpriseId) + } + if req.Name != "" { + engine = engine.And("merchant.name like =?", "%"+req.Name+"%") + } + total, err := engine.Join("LEFT", "enterprise", "enterprise.id = merchant.enterprise_id"). + Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + var resp []interface{} + for _, v := range m { + resp = append(resp, map[string]interface{}{ + "id": v.Merchant.Id, + "name": v.Merchant.Name, + "enterprise_id": v.Merchant.EnterpriseId, + "create_at": v.Merchant.CreateAt, + "update_at": v.Merchant.UpdateAt, + "enterprise_name": v.Enterprise.Name, + }) + } + e.OutSuc(c, map[string]interface{}{ + "list": resp, + "total": total, + }, nil) + return +} + +func MerchantAdd(c *gin.Context) { + var req md.MerchantAddReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + merchantDb := db.MerchantDb{} + merchantDb.Set() + now := time.Now() + insertAffected, err := merchantDb.MerchantInsert(&model.Merchant{ + EnterpriseId: req.EnterpriseId, + Name: req.Name, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + if insertAffected <= 0 { + e.OutErr(c, e.ERR_DB_ORM, "新增数据失败") + return + } + + e.OutSuc(c, "success", nil) + return +} + +func MerchantUpdate(c *gin.Context) { + var req md.MerchantUpdateReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + merchantDb := db.MerchantDb{} + merchantDb.Set() + merchant, err := merchantDb.GetMerchant(req.Id) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + if merchant == nil { + e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") + return + } + now := time.Now() + merchant.EnterpriseId = req.EnterpriseId + merchant.Name = req.Name + merchant.UpdateAt = now.Format("2006-01-02 15:04:05") + _, err = merchantDb.MerchantUpdate(merchant, "name", "enterprise_id", "update_at") + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + e.OutSuc(c, "success", nil) + return +} + +func MerchantDelete(c *gin.Context) { + id := c.Param("merchant_id") + merchantDb := db.MerchantDb{} + merchantDb.Set() + merchant, err := merchantDb.GetMerchant(utils.StrToInt(id)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + if merchant == nil { + e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") + return + } + + _, err = merchantDb.MerchantDelete(id) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + e.OutSuc(c, "success", nil) + return +} diff --git a/app/admin/md/md_merchant.go b/app/admin/md/md_merchant.go new file mode 100644 index 0000000..5ac765f --- /dev/null +++ b/app/admin/md/md_merchant.go @@ -0,0 +1,19 @@ +package md + +type MerchantAddReq struct { + Name string `json:"name" binding:"required" label:"名称"` + EnterpriseId int `json:"enterprise_id" binding:"required" label:"校企id"` +} + +type MerchantUpdateReq struct { + Id int `json:"id" binding:"required" label:"id"` + Name string `json:"name" binding:"required" label:"名称"` + EnterpriseId int `json:"enterprise_id" binding:"required" label:"校企id"` +} + +type MerchantListReq struct { + Name string `json:"name" label:"名称"` + EnterpriseId int `json:"enterprise_id" label:"校企id"` + Limit int `json:"limit"` + Page int `json:"page" ` +} diff --git a/app/db/db_merchant.go b/app/db/db_merchant.go new file mode 100644 index 0000000..a22671f --- /dev/null +++ b/app/db/db_merchant.go @@ -0,0 +1,107 @@ +package db + +import ( + "applet/app/db/model" + "applet/app/utils/logx" + "reflect" + "xorm.io/xorm" +) + +type MerchantDb struct { + Db *xorm.Engine `json:"db"` +} + +func (merchantDb *MerchantDb) Set() { // set方法 + merchantDb.Db = Db +} + +func (merchantDb *MerchantDb) GetMerchant(id int) (m *model.Merchant, err error) { + m = new(model.Merchant) + has, err := merchantDb.Db.Where("id =?", id).Get(m) + if err != nil { + return nil, logx.Error(err) + } + if has == false { + return nil, nil + } + return m, nil +} + +func (merchantDb *MerchantDb) GetPopToCentralKitchen() (m *model.Merchant, err error) { + m = new(model.Merchant) + has, err := merchantDb.Db.Where("is_pop_to_central_kitchen =?", 1).Get(m) + if err != nil { + return nil, logx.Error(err) + } + if has == false { + return nil, nil + } + return m, nil +} + +func (merchantDb *MerchantDb) FindMerchantById(ids interface{}) (*[]model.Merchant, error) { + var m []model.Merchant + if err := merchantDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil { + return nil, logx.Error(err) + } + return &m, nil +} + +func (merchantDb *MerchantDb) FindMerchant(limit, start int) (*[]model.Merchant, error) { + var m []model.Merchant + if limit == 0 || start == 0 { + if err := merchantDb.Db.Asc("sort").Find(&m); err != nil { + return nil, logx.Error(err) + } + } else { + if err := merchantDb.Db.Asc("sort").Limit(limit, start).Find(m); err != nil { + return nil, logx.Error(err) + } + } + return &m, nil +} + +func (merchantDb *MerchantDb) GetMerchantByName(name string) (m *model.Merchant, err error) { + m = new(model.Merchant) + has, err := merchantDb.Db.Where("name =?", name).Get(m) + if err != nil { + return nil, logx.Error(err) + } + if has == false { + return nil, nil + } + return m, nil +} + +func (merchantDb *MerchantDb) MerchantInsert(m *model.Merchant) (int, error) { + _, err := merchantDb.Db.InsertOne(m) + if err != nil { + return 0, err + } + return m.Id, nil +} + +func (merchantDb *MerchantDb) MerchantUpdate(m *model.Merchant, columns ...string) (int64, error) { + affected, err := merchantDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) + if err != nil { + return 0, err + } + return affected, nil +} + +func (merchantDb *MerchantDb) MerchantDelete(id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.Merchant{}) + } else { + return Db.Where("id = ?", id).Delete(model.Merchant{}) + } +} + +type MerchantWithEnterpriseInfo struct { + model.Merchant `xorm:"extends"` + model.Enterprise `xorm:"extends"` +} + +func (MerchantWithEnterpriseInfo) TableName() string { + return "merchant" +} diff --git a/app/db/db_merchant_with_device.go b/app/db/db_merchant_with_device.go new file mode 100644 index 0000000..d678788 --- /dev/null +++ b/app/db/db_merchant_with_device.go @@ -0,0 +1,68 @@ +package db + +import ( + "applet/app/db/model" + "applet/app/utils/logx" + "reflect" + "xorm.io/xorm" +) + +type MerchantWithDeviceDb struct { + Db *xorm.Engine `json:"db"` +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) Set() { // set方法 + merchantWithDeviceDb.Db = Db +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) FindMerchantWithDevice(id int) (*[]model.MerchantWithDevice, error) { + var m []model.MerchantWithDevice + if err := merchantWithDeviceDb.Db.Where("adm_id =?", id).Find(&m); err != nil { + return nil, logx.Error(err) + } + return &m, nil +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error) { + if reflect.TypeOf(admId).Kind() == reflect.Slice { + return session.In("adm_id", admId).Delete(model.MerchantWithDevice{}) + } else { + return session.Where("adm_id = ?", admId).Delete(model.MerchantWithDevice{}) + } +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) GetMerchantWithDeviceByWithEnterprise(id int) (m *model.MerchantWithDevice, err error) { + m = new(model.MerchantWithDevice) + has, err := merchantWithDeviceDb.Db.Where("role_id =?", id).Get(m) + if err != nil { + return nil, logx.Error(err) + } + if has == false { + return nil, nil + } + return m, nil +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) MerchantWithDeviceDeleteForWithEnterpriseBySession(session *xorm.Session, roleId interface{}) (int64, error) { + if reflect.TypeOf(roleId).Kind() == reflect.Slice { + return session.In("role_id", roleId).Delete(model.MerchantWithDevice{}) + } else { + return session.Where("role_id = ?", roleId).Delete(model.MerchantWithDevice{}) + } +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) MerchantWithDeviceDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return session.In("id", id).Delete(model.MerchantWithDevice{}) + } else { + return session.Where("adm_id = ?", id).Delete(model.MerchantWithDevice{}) + } +} + +func (merchantWithDeviceDb *MerchantWithDeviceDb) BatchAddMerchantWithDeviceBySession(session *xorm.Session, mm []*model.MerchantWithDevice) (int64, error) { + affected, err := session.Insert(mm) + if err != nil { + return 0, err + } + return affected, nil +} diff --git a/app/db/model/merchant.go b/app/db/model/merchant.go new file mode 100644 index 0000000..95ac8ef --- /dev/null +++ b/app/db/model/merchant.go @@ -0,0 +1,9 @@ +package model + +type Merchant struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Name string `json:"name" xorm:"not null default '' comment('商家名称') VARCHAR(255)"` + EnterpriseId int `json:"enterprise_id" xorm:"not null default 0 comment('校企id') INT(11)"` + CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` + UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` +} diff --git a/app/db/model/merchant_with_device.go b/app/db/model/merchant_with_device.go new file mode 100644 index 0000000..5a5af50 --- /dev/null +++ b/app/db/model/merchant_with_device.go @@ -0,0 +1,7 @@ +package model + +type MerchantWithDevice struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + MerchantId int `json:"merchant_id" xorm:"not null default 0 comment('商家id') INT(11)"` + DeviceId int `json:"device_id" xorm:"not null default 0 comment('设备id') INT(11)"` +} diff --git a/app/router/admin_router.go b/app/router/admin_router.go index e9ec471..559f653 100644 --- a/app/router/admin_router.go +++ b/app/router/admin_router.go @@ -116,6 +116,13 @@ func rDeviceManage(r *gin.RouterGroup) { r.DELETE("/delete/:device_sn", hdl2.DeviceDelete) //设备管理-删除 } +func rMerchant(r *gin.RouterGroup) { + r.POST("/add", hdl2.MerchantAdd) //商家管理-新增 + r.POST("/update", hdl2.MerchantUpdate) //商家管理-编辑 + r.POST("/list", hdl2.MerchantList) //商家管理-列表 + r.DELETE("/delete/:merchant_id", hdl2.MerchantDelete) //商家管理-删除 +} + func rDataStatistics(r *gin.RouterGroup) { r.POST("/centralKitchenForSchool/export", hdl2.CentralKitchenForSchoolDataStatisticsExport) //数据统计-(央厨-学校)-导出 r.POST("/centralKitchenForSchool/contrast", hdl2.CentralKitchenForSchoolDataStatisticsContrast) //数据统计-(央厨-学校)-数据对比 @@ -311,4 +318,5 @@ func AdminRoute(r *gin.RouterGroup) { rFinanceManage(r.Group("/financeManage")) //财务管理 rDeviceManage(r.Group("/deviceManage")) //设备管理 rDataStatistics(r.Group("/dataStatistics")) //数据统计 + rMerchant(r.Group("/merchant")) }