浏览代码

Merge branch 'eta/1.4.1' into debug

hsun 1 年之前
父节点
当前提交
86b8bfde18

+ 185 - 0
controllers/eta_business/eta_business_menu.go

@@ -0,0 +1,185 @@
+package eta_business
+
+import (
+	"encoding/json"
+	"hongze/hz_crm_api/controllers"
+	"hongze/hz_crm_api/models"
+	"hongze/hz_crm_api/models/eta_business"
+	etaBusinessService "hongze/hz_crm_api/services/eta_business"
+)
+
+// EtaBusinessMenuController ETA商家菜单
+type EtaBusinessMenuController struct {
+	controllers.BaseAuthController
+}
+
+// List
+// @Title 商家菜单列表
+// @Description 商家菜单列表
+// @Param   BusinessId	query	int	false	"商家ID"
+// @Success 200 Ret=200 获取成功
+// @router /menu/list [get]
+func (this *EtaBusinessMenuController) List() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		if br.ErrMsg == "" {
+			br.IsSendEmail = false
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+
+	resp := new(eta_business.EtaBusinessMenuListResp)
+	resp.ChoiceList = make([]int, 0)
+	resp.List = make([]*eta_business.EtaBusinessMenuItem, 0)
+	// 权限校验
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Data = resp
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "获取成功"
+		return
+	}
+
+	// 商家勾选的权限
+	businessId, _ := this.GetInt("EtaBusinessId", 0)
+	if businessId > 0 {
+		relateOb := new(eta_business.EtaBusinessMenuRelate)
+		cond := ` AND eta_business_id = ?`
+		pars := make([]interface{}, 0)
+		pars = append(pars, businessId)
+		relates, e := relateOb.GetItemsByCondition(cond, pars, []string{}, "")
+		if e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取商家菜单关联列表失败, Err: " + e.Error()
+			return
+		}
+		for _, r := range relates {
+			resp.ChoiceList = append(resp.ChoiceList, r.MenuId)
+		}
+	}
+
+	menuOb := new(eta_business.EtaBusinessMenu)
+	order := `sort ASC, create_time DESC, menu_id DESC`
+	list, e := menuOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, order)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取商家菜单列表失败, Err: " + e.Error()
+		return
+	}
+
+	items := make([]*eta_business.EtaBusinessMenuItem, 0)
+	for _, v := range list {
+		t := &eta_business.EtaBusinessMenuItem{
+			MenuId:       v.MenuId,
+			ParentId:     v.ParentId,
+			Name:         v.Name,
+			Sort:         v.Sort,
+			Path:         v.Path,
+			PathName:     v.PathName,
+			IconPath:     v.IconPath,
+			Component:    v.Component,
+			Hidden:       v.Hidden,
+			HiddenLayout: v.HiddenLayout,
+			Level:        v.Level,
+			MenuType:     v.MenuType,
+			ButtonCode:   v.ButtonCode,
+			Children:     make([]*eta_business.EtaBusinessMenuItem, 0),
+		}
+		items = append(items, t)
+	}
+
+	// 递归返回树形结构
+	items = etaBusinessService.GetMenuTreeRecursive(items, 0)
+
+	resp.List = items
+	br.Data = resp
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+}
+
+// SaveRelate
+// @Title 保存商家菜单关联
+// @Description 保存商家菜单关联
+// @Param	request	body eta_business.EtaBusinessAddReq true "type json string"
+// @Success 200 Ret=200 操作成功
+// @router /menu/relate/save [post]
+func (this *EtaBusinessMenuController) SaveRelate() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		if br.ErrMsg == "" {
+			br.IsSendEmail = false
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+
+	// 操作权限校验
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Msg = "无权操作"
+		return
+	}
+
+	// 参数校验
+	var req eta_business.EtaBusinessMenuRelateSaveReq
+	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + e.Error()
+		return
+	}
+	if req.EtaBusinessId <= 0 {
+		br.Msg = "参数有误"
+		return
+	}
+	if len(req.MenuIds) == 0 {
+		br.Msg = "请选择菜单"
+		return
+	}
+
+	ob := new(eta_business.EtaBusinessMenuRelate)
+	items := make([]*eta_business.EtaBusinessMenuRelate, 0)
+	for _, v := range req.MenuIds {
+		t := new(eta_business.EtaBusinessMenuRelate)
+		t.MenuId = v
+		t.EtaBusinessId = req.EtaBusinessId
+		items = append(items, t)
+	}
+	if e := ob.CreateMulti(req.EtaBusinessId, items); e != nil {
+		br.Msg = "保存失败"
+		br.ErrMsg = "保存商家菜单关联失败, Err: " + e.Error()
+		return
+	}
+
+	// TODO:后续需要生成SQL脚本文件
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "操作成功"
+}

+ 2 - 0
models/db.go

@@ -651,6 +651,8 @@ func initEtaBusiness() {
 		new(eta_business.EtaBusiness),                // ETA商家表
 		new(eta_business.EtaBusinessContract),        // ETA合同表
 		new(eta_business.EtaBusinessOperationRecord), // ETA操作记录表
+		new(eta_business.EtaBusinessMenu),            // ETA商家菜单表
+		new(eta_business.EtaBusinessMenuRelate),      // ETA商家菜单关联表
 	)
 }
 

+ 146 - 0
models/eta_business/eta_business_menu.go

@@ -0,0 +1,146 @@
+package eta_business
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"strings"
+	"time"
+)
+
+// EtaBusinessMenu ETA商家基础菜单表
+type EtaBusinessMenu struct {
+	MenuId       int       `orm:"column(menu_id);pk"`
+	ParentId     int       `description:"父级菜单ID"`
+	Name         string    `description:"菜单名称或者按钮名称"`
+	RootId       int       `description:"顶层菜单ID"`
+	Sort         string    `description:"排序"`
+	Path         string    `description:"路由地址"`
+	PathName     string    `description:"路由名称"`
+	IconPath     string    `description:"菜单图标地址"`
+	Component    int       `description:"组件路径"`
+	Hidden       int       `description:"是否隐藏:1-隐藏 0-显示"`
+	HiddenLayout int       `description:"是否隐藏layout:1-隐藏 0-显示"`
+	Level        int       `description:"菜单等级:1-2-3"`
+	IsLevel      int       `description:"是否为多级菜单:1,只有一级;2,有多级"`
+	LevelPath    string    `description:"兼容以前menu表的字段"`
+	MenuType     int       `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
+	ButtonCode   string    `description:"按钮/菜单唯一标识"`
+	CreateTime   time.Time `description:"创建时间"`
+	ModifyTime   time.Time `description:"更新时间"`
+}
+
+func (m *EtaBusinessMenu) TableName() string {
+	return "eta_business_menu"
+}
+
+func (m *EtaBusinessMenu) PrimaryId() string {
+	return "menu_id"
+}
+
+func (m *EtaBusinessMenu) Create() (err error) {
+	o := orm.NewOrm()
+	id, err := o.Insert(m)
+	if err != nil {
+		return
+	}
+	m.MenuId = int(id)
+	return
+}
+
+func (m *EtaBusinessMenu) CreateMulti(items []*EtaBusinessMenu) (err error) {
+	if len(items) == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(len(items), items)
+	return
+}
+
+func (m *EtaBusinessMenu) Update(cols []string) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Update(m, cols...)
+	return
+}
+
+func (m *EtaBusinessMenu) Del() (err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	_, err = o.Raw(sql, m.MenuId).Exec()
+	return
+}
+
+func (m *EtaBusinessMenu) GetItemById(id int) (item *EtaBusinessMenu, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
+func (m *EtaBusinessMenu) GetItemByCondition(condition string, pars []interface{}) (item *EtaBusinessMenu, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&item)
+	return
+}
+
+func (m *EtaBusinessMenu) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
+func (m *EtaBusinessMenu) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusinessMenu, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := `ORDER BY create_time DESC`
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}
+
+func (m *EtaBusinessMenu) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaBusinessMenu, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := `ORDER BY create_time DESC`
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+// EtaBusinessMenuListResp ETA商家菜单列表响应体
+type EtaBusinessMenuListResp struct {
+	ChoiceList []int                  `description:"已选菜单"`
+	List       []*EtaBusinessMenuItem `description:"菜单列表"`
+}
+
+// EtaBusinessMenuItem ETA商家菜单
+type EtaBusinessMenuItem struct {
+	MenuId       int
+	ParentId     int                    `description:"父级菜单ID"`
+	Name         string                 `description:"菜单名称或者按钮名称"`
+	RootId       int                    `description:"顶层菜单ID"`
+	Sort         string                 `description:"排序"`
+	Path         string                 `description:"路由地址"`
+	PathName     string                 `description:"路由名称"`
+	IconPath     string                 `description:"菜单图标地址"`
+	Component    int                    `description:"组件路径"`
+	Hidden       int                    `description:"是否隐藏:1-隐藏 0-显示"`
+	HiddenLayout int                    `description:"是否隐藏layout:1-隐藏 0-显示"`
+	Level        int                    `description:"菜单等级:1-2-3"`
+	MenuType     int                    `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
+	ButtonCode   string                 `description:"按钮/菜单唯一标识"`
+	Children     []*EtaBusinessMenuItem `description:"子菜单"`
+}

+ 131 - 0
models/eta_business/eta_business_menu_relate.go

@@ -0,0 +1,131 @@
+package eta_business
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"strings"
+)
+
+// EtaBusinessMenuRelate ETA商家菜单关联表
+type EtaBusinessMenuRelate struct {
+	Id            int `orm:"column(id);pk"`
+	EtaBusinessId int `description:"商家ID"`
+	MenuId        int `description:"菜单ID"`
+}
+
+func (m *EtaBusinessMenuRelate) TableName() string {
+	return "eta_business_menu_relate"
+}
+
+func (m *EtaBusinessMenuRelate) PrimaryId() string {
+	return "id"
+}
+
+func (m *EtaBusinessMenuRelate) Create() (err error) {
+	o := orm.NewOrm()
+	id, err := o.Insert(m)
+	if err != nil {
+		return
+	}
+	m.MenuId = int(id)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) CreateMulti(businessId int, items []*EtaBusinessMenuRelate) (err error) {
+	if businessId == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	tx, err := o.Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = tx.Rollback()
+			return
+		}
+		_ = tx.Commit()
+	}()
+
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE eta_business_id = ?`, m.TableName())
+	_, err = tx.Raw(sql, businessId).Exec()
+	if err != nil {
+		return
+	}
+
+	if len(items) > 0 {
+		_, err = tx.InsertMulti(len(items), items)
+	}
+	return
+}
+
+func (m *EtaBusinessMenuRelate) Update(cols []string) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Update(m, cols...)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) Del() (err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	_, err = o.Raw(sql, m.MenuId).Exec()
+	return
+}
+
+func (m *EtaBusinessMenuRelate) GetItemById(id int) (item *EtaBusinessMenuRelate, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) GetItemByCondition(condition string, pars []interface{}) (item *EtaBusinessMenuRelate, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&item)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusinessMenuRelate, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := ``
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}
+
+func (m *EtaBusinessMenuRelate) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaBusinessMenuRelate, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := ``
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+// EtaBusinessMenuRelateSaveReq 保存商家菜单关联
+type EtaBusinessMenuRelateSaveReq struct {
+	EtaBusinessId int   `description:"商家ID"`
+	MenuIds       []int `description:"菜单IDs"`
+}

+ 18 - 0
routers/commentsRouter.go

@@ -6550,6 +6550,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessMenuController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessMenuController"],
+        beego.ControllerComments{
+            Method: "List",
+            Router: `/menu/list`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessMenuController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessMenuController"],
+        beego.ControllerComments{
+            Method: "SaveRelate",
+            Router: `/menu/relate/save`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/help_doc:HelpDocClassifyController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/help_doc:HelpDocClassifyController"],
         beego.ControllerComments{
             Method: "AddClassify",

+ 1 - 0
routers/router.go

@@ -304,6 +304,7 @@ func init() {
 		web.NSNamespace("/eta_business",
 			web.NSInclude(
 				&eta_business.EtaBusinessController{},
+				&eta_business.EtaBusinessMenuController{},
 			),
 		),
 		web.NSNamespace("/eta_version_update_log",

+ 15 - 0
services/eta_business/eta_business_menu.go

@@ -0,0 +1,15 @@
+package eta_business
+
+import "hongze/hz_crm_api/models/eta_business"
+
+// GetMenuTreeRecursive 递归菜单树
+func GetMenuTreeRecursive(list []*eta_business.EtaBusinessMenuItem, parentId int) []*eta_business.EtaBusinessMenuItem {
+	res := make([]*eta_business.EtaBusinessMenuItem, 0)
+	for _, v := range list {
+		if v.ParentId == parentId {
+			v.Children = GetMenuTreeRecursive(list, v.MenuId)
+			res = append(res, v)
+		}
+	}
+	return res
+}