package cases

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

	"github.com/astaxie/beego"
)

// GetCaseUserByCase 根据案场id获取案场相关人员信息
func (s *CaseServ) GetCaseUserByCase(caseid string, page int, pageSize int) (map[string]interface{}, error) {
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	caseusers, err := s.dao.GetCaseUserByCase(caseid, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetCaseUserCount(caseid)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

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

// GetCaseUserByType 根据类型获取人员
func (s *CaseServ) GetCaseUserByType(caseid, usertype string) ([]model.SysCaseUser, error) {
	caseusers, err := s.dao.GetCaseUserByUserType(caseid, usertype)
	return caseusers, err
}

// GetCaseUserByID 根据ID获取人员
func (s *CaseServ) GetCaseUserByID(userid string) (*model.SysCaseUser, error) {
	caseuser, err := s.dao.GetCaseUserByID(userid)
	return caseuser, err
}

// SaveCaseUser 保存人员信息
func (s *CaseServ) SaveCaseUser(caseuser model.SysCaseUser) (*model.SysCaseUser, error) {
	var newInfo *model.SysCaseUser
	var err error
	if caseuser.UserName == "" {
		return nil, utils.LogError("人员名称不能为空！")
	}
	if caseuser.UserType == "" {
		return nil, utils.LogError("人员类型不能为空！")
	}
	if caseuser.Tel == "" {
		return nil, utils.LogError("人员电话不能为空！")
	}
	if caseuser.CaseUserId == "" {
		newInfo, err = s.dao.AddCaseUser(caseuser)
	} else {
		err = s.dao.UpdateCaseUser(caseuser)
		newInfo = &caseuser
	}
	return newInfo, err
}

// DelCaseUser 删除人员信息
func (s *CaseServ) DelCaseUser(userid string) error {
	err := s.dao.DelCaseUser(userid)
	return err
}
