package cases

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

	"github.com/astaxie/beego"
)

// GetCaseEquipment 获取体检设备
func (s *CaseServ) GetCaseEquipment(caseids string, page int, pageSize int) (map[string]interface{}, error) {
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	specs, err := s.dao.GetEquipmentList(caseids, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetEquipmentCount(caseids)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

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

// GetCaseEquipmentByID 根据ID获取体检设备
func (s *CaseServ) GetCaseEquipmentByID(equipmentid string) (*model.SysCaseEquipment, error) {
	equipment, err := s.dao.GetCaseEquipmentByID(equipmentid)
	return equipment, err
}

// SaveCaseEquipment 保存体检设备
func (s *CaseServ) SaveCaseEquipment(equipment model.SysCaseEquipment) (*model.SysCaseEquipment, error) {
	var err error
	if equipment.Name == "" {
		return nil, utils.LogError("体检设备名称不能为空！")
	}
	if equipment.EquipmentId == "" {
		_, err = s.dao.AddCaseEquipment(&equipment)
	} else {
		err = s.dao.UpdateCaseEquipment(equipment)
	}
	return &equipment, err
}

// DelCaseEquipment 删除体检设备
func (s *CaseServ) DelCaseEquipment(equipmentid string) error {
	err := s.dao.DelCaseEquipment(equipmentid)
	return err
}
