package eta_business

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
	"strings"
	"time"
)

// EtaBusinessMenu ETA商家基础菜单表
type EtaBusinessMenu struct {
	MenuId     int       `orm:"column(menu_id);pk"`
	ParentId   int       `description:"父级菜单ID"`
	Name       string    `description:"菜单名称或者按钮名称"`
	Sort       int       `description:"排序"`
	Path       string    `description:"路由地址"`
	IconPath   string    `description:"菜单图标地址"`
	Component  string    `description:"组件路径"`
	Hidden     int       `description:"是否隐藏：1-隐藏 0-显示"`
	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) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	_, err = o.Raw(sql, menuIds).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
}

// GetMenuItemsByKeyword 关键词获取菜单
func GetMenuItemsByKeyword(keyword string, fieldArr []string, orderRule string) (items []*EtaBusinessMenu, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				%s
			FROM
				(
					SELECT * FROM eta_business_menu WHERE parent_id = 0 AND name LIKE ?
					UNION
					SELECT * FROM eta_business_menu WHERE menu_id IN (
						SELECT
							parent_id
						FROM
							eta_business_menu
						WHERE
							parent_id > 0 AND name LIKE ?
					)
				) AS t %s`
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql = fmt.Sprintf(sql, fields, order)
	_, err = o.Raw(sql, keyword, keyword).QueryRows(&items)
	return
}

// EtaBusinessMenuListResp ETA商家菜单列表响应体
type EtaBusinessMenuListResp struct {
	ChoiceList     []int                  `description:"已选菜单"`
	HalfChoiceList []int                  `description:"半选菜单-方便前端回显用的"`
	List           []*EtaBusinessMenuItem `description:"菜单列表"`
}

// EtaBusinessMenuItem ETA商家菜单
type EtaBusinessMenuItem struct {
	MenuId       int
	ParentId     int                    `description:"父级菜单ID"`
	Name         string                 `description:"菜单名称或者按钮名称"`
	RootId       int                    `description:"顶层菜单ID"`
	Sort         int                    `description:"排序"`
	Path         string                 `description:"路由地址"`
	PathName     string                 `description:"路由名称"`
	IconPath     string                 `description:"菜单图标地址"`
	Component    string                 `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:"按钮/菜单唯一标识"`
	CreateTime   string                 `description:"创建时间"`
	Children     []*EtaBusinessMenuItem `description:"子菜单"`
}

// EtaBusinessMenuAddReq 新增菜单请求体
type EtaBusinessMenuAddReq struct {
	ParentId   int    `description:"父级菜单ID"`
	Name       string `description:"菜单/按钮名称"`
	Sort       int    `description:"排序"`
	Path       string `description:"路由地址"`
	IconPath   string `description:"图标地址"`
	Component  string `description:"组件路径"`
	Hidden     int    `description:"是否隐藏：1-隐藏 0-显示"`
	MenuType   int    `description:"菜单类型：0-菜单; 1-按钮; 2-字段"`
	ButtonCode string `description:"按钮唯一标识"`
}

// EtaBusinessMenuEditReq 编辑菜单请求体
type EtaBusinessMenuEditReq struct {
	MenuId int
	EtaBusinessMenuAddReq
}

// EtaBusinessMenuRemoveReq 删除菜单请求体
type EtaBusinessMenuRemoveReq struct {
	MenuId int
}

// BusinessConf 商户配置表
type BusinessConf struct {
	ConfKey string `description:"配置Key"`
	ConfVal string `description:"配置值"`
}

// SetBusinessConfReq 设置用户的配置
type SetBusinessConfReq struct {
	EtaBusinessId int `description:"商家ID"`
	List          []BusinessConfReq
}

type BusinessConfReq struct {
	ConfigCode  string `description:"配置编码"`
	ConfigValue string `description:"配置值"`
}
