@@ -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 | |||
} |
@@ -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" ` | |||
} |
@@ -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" | |||
} |
@@ -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 | |||
} |
@@ -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"` | |||
} |
@@ -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)"` | |||
} |
@@ -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")) | |||
} |