package cases

import (
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/service"
	"spaceofcheng/services/utils"

	"github.com/astaxie/beego"
)

// GetCaseTable 获取案场桌位
func (s *CaseServ) GetCaseTable(caseids string, page int, pageSize int) (map[string]interface{}, error) {
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	areas, err := s.dao.GetCaseTable(caseids, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetCaseTableCount(caseids)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

	return map[string]interface{}{
		"list":     areas,
		"pagesize": pageSize,
		"pagenum":  total,
		"page":     page,
	}, err
}

// GetCaseTableByID 根据桌位ID获取桌位
func (s *CaseServ) GetCaseTableByID(tableid string) (*model.SysCaseTable, error) {
	areas, err := s.dao.GetCaseTableByID(tableid)
	return areas, err
}

// SaveCaseTable 保存桌位
func (s *CaseServ) SaveCaseTable(table model.SysCaseTable) (*model.SysCaseTable, error) {
	var err error
	if table.TableNo == "" {
		return nil, utils.LogError("桌位名称不能为空")
	}
	if table.TableId == "" {
		_, err = s.dao.AddCaseTable(&table)
	} else {
		err = s.dao.UpdateCaseTable(table)
	}
	return &table, err
}

// DelCaseTable 删除桌位
func (s *CaseServ) DelCaseTable(tableid string) error {
	err := s.dao.DelCaseTable(tableid)
	return err
}
