package typeManage

import (
	"errors"
	"log-server/global"
)

type ResponsiblePerson struct {
	Id                int64     `json:"id"`
	Img               string    `json:"img"`
	Name              string    `json:"name"`
	Nickname          string    `json:"nickname"`
	DepartmentName    string    `json:"department_name"`
	MobilePhoneNumber string    `json:"mobile_phone_number"`
	GameCounts        int       `json:"game_counts"`
	Custodians        string    `json:"custodians"`
	Remark            string    `json:"remark"`
	StartTime         int       `json:"start_time"`
	Url               string    `json:"url"`
	State             int8      `json:"state"`
	PushStatus        int8      `json:"push_status"`
	CreateTime        LocalTime `gorm:"column:create_time;" json:"createTime"`
	UpdateTime        LocalTime `gorm:"column:update_time;" json:"updateTime"`
}

type ResponsiblePersonMobile struct {
	Id                int64  `json:"id"`
	Name              string `json:"name"`
	MobilePhoneNumber string `json:"mobile_phone_number"`
}

func (m *ResponsiblePerson) GetUserInfo() (mps map[string]string, err error) {
	var person []*ResponsiblePerson
	err = global.GVA_DB.Table("responsible_person").Where("state = ?", 1).Find(&person).Error
	if err != nil {
		return
	}
	if len(person) == 0 {
		return
	}
	var mp = map[string]string{}
	for _, pc := range person {
		mp[pc.Name] = pc.MobilePhoneNumber
	}
	return mp, err
}

func (m *ResponsiblePerson) GetUserInfoData() (mps map[string]ResponsiblePerson, err error) {
	var person []ResponsiblePerson
	err = global.GVA_DB.Table("responsible_person").Where("state = ?", 1).Find(&person).Error
	if err != nil {
		return
	}
	if len(person) == 0 {
		return
	}
	var mp = map[string]ResponsiblePerson{}
	for _, pc := range person {
		mp[pc.Name] = pc
	}
	return mp, err
}

func (m *ResponsiblePerson) GetUserInfoByName(name string) (person ResponsiblePerson, err error) {
	err = global.GVA_DB.Table("responsible_person").Where("name = ?", name).Where("state = ?", 1).Find(&person).Error
	if err != nil {
		return
	}
	if person.Id == 0 {
		return person, errors.New("数据错误")
	}
	return
}
