You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.7 KiB
Go

package logic
import (
"errors"
"exhibition-register/internal/dao"
"exhibition-register/internal/model"
"exhibition-register/pb/exhibition"
"exhibition-register/pkg/utils"
"fmt"
)
type IRegister interface {
CheckByPhone(in *exhibition.RegisterInfo) (out *exhibition.CheckPhoneResp, err error)
CheckIdCard(in *exhibition.RegisterInfo) (out *exhibition.CheckIdCardResp, err error)
SaveRegisterRecord(in *exhibition.RegisterInfo) (out *exhibition.SaveRegisterRecordResp, err error)
RegisterRecordList(in *exhibition.RecordListReq) (out *exhibition.RecordListResp, err error)
FindAllRecord() (out *exhibition.ExportRecordResp, err error)
}
func NewRegister() IRegister {
return &Register{}
}
type Register struct {
}
func (r *Register) CheckIdCard(in *exhibition.RegisterInfo) (out *exhibition.CheckIdCardResp, err error) {
out = &exhibition.CheckIdCardResp{}
check, err1 := dao.CheckBy("id_card", in.IdCard, "")
if err1 != nil {
return nil, err1
}
if check != nil {
return nil, errors.New("该身份证号已被" + check.PhoneNum + "注册!")
}
return out, nil
}
func (r *Register) FindAllRecord() (out *exhibition.ExportRecordResp, err error) {
out = &exhibition.ExportRecordResp{}
allRecord, err := dao.AllRecord()
if err != nil {
return nil, err
}
if len(allRecord) > 0 {
for _, record := range allRecord {
var address string
address = fmt.Sprintf("%s%s", record.Address, record.Address1)
tmp := &exhibition.ExportInfo{
ArtistName: record.ArtistName,
Gender: record.Gender,
PhoneNum: record.PhoneNum,
IdCard: record.IdCard,
Address: address,
IdCardPhoto: record.IdCardPhoto,
IdCardBackPhoto: record.IdCardBackPhoto,
ArtistPhoto: record.ArtistPhoto,
CreatedAt: record.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: record.UpdatedAt.Format("2006-01-02 15:04:05"),
}
out.Data = append(out.Data, tmp)
}
}
return
}
func (r *Register) RegisterRecordList(in *exhibition.RecordListReq) (out *exhibition.RecordListResp, err error) {
out = &exhibition.RecordListResp{}
recordList, total, err := dao.RecordList(in)
if err != nil {
return nil, err
}
if total > 0 {
for _, item := range recordList {
tmp := &exhibition.RegisterInfo{
Id: int32(item.ID),
Uuid: item.UUID,
ArtistName: item.ArtistName,
Gender: item.Gender,
PhoneNum: item.PhoneNum,
IdCard: item.IdCard,
Address: item.Address,
Address1: item.Address1,
IdCardPhoto: item.IdCardPhoto,
IdCardBackPhoto: item.IdCardBackPhoto,
ArtistPhoto: item.ArtistPhoto,
ArtworkFile: item.ArtworkFile,
CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: item.UpdatedAt.Format("2006-01-02 15:04:05"),
}
out.Data = append(out.Data, tmp)
}
out.Count = int32(total)
}
return out, err
}
func (r *Register) SaveRegisterRecord(in *exhibition.RegisterInfo) (out *exhibition.SaveRegisterRecordResp, err error) {
out = &exhibition.SaveRegisterRecordResp{}
record := &model.RegisterRecord{
ArtistName: in.ArtistName,
Gender: in.Gender,
PhoneNum: in.PhoneNum,
IdCard: in.IdCard,
Address: in.Address,
Address1: in.Address1,
IdCardPhoto: in.IdCardPhoto,
IdCardBackPhoto: in.IdCardBackPhoto,
ArtistPhoto: in.ArtistPhoto,
ArtworkFile: in.ArtworkFile,
}
tmpRecord := &model.RegisterRecord{}
if in.Uuid != "" {
check, err := dao.CheckBy("phone_num", record.PhoneNum, in.Uuid)
if err != nil {
return nil, err
}
if check != nil {
return nil, errors.New("该手机号已经被注册!")
}
record.UUID = in.Uuid
registerRecord, err1 := dao.UpdateRegisterRecord(record)
if err1 != nil {
return nil, err1
}
tmpRecord = registerRecord
} else {
record.UUID = utils.GetUUID()
registerRecord, err := dao.CreateRegisterRecord(record)
if err != nil {
return nil, err
}
tmpRecord = registerRecord
}
data := &exhibition.RegisterInfo{
Id: int32(tmpRecord.ID),
Uuid: tmpRecord.UUID,
ArtistName: tmpRecord.ArtistName,
Gender: tmpRecord.Gender,
PhoneNum: tmpRecord.PhoneNum,
IdCard: tmpRecord.IdCard,
Address: tmpRecord.Address,
Address1: tmpRecord.Address1,
IdCardPhoto: tmpRecord.IdCardPhoto,
IdCardBackPhoto: tmpRecord.IdCardBackPhoto,
ArtistPhoto: tmpRecord.ArtistPhoto,
ArtworkFile: tmpRecord.ArtworkFile,
CreatedAt: tmpRecord.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: tmpRecord.UpdatedAt.Format("2006-01-02 15:04:05"),
}
out.Data = data
return out, nil
}
// CheckByPhone 通过手机号检索 存在的就返回数据
func (r *Register) CheckByPhone(in *exhibition.RegisterInfo) (out *exhibition.CheckPhoneResp, err error) {
out = &exhibition.CheckPhoneResp{}
record, err := dao.CheckByPhone(in.PhoneNum)
if err != nil {
return nil, err
}
if record == nil {
out.IsExist = false
return out, nil
}
data := &exhibition.RegisterInfo{
Id: int32(record.ID),
Uuid: record.UUID,
ArtistName: record.ArtistName,
Gender: record.Gender,
PhoneNum: record.PhoneNum,
IdCard: record.IdCard,
Address: record.Address,
Address1: record.Address1,
IdCardPhoto: record.IdCardPhoto,
IdCardBackPhoto: record.IdCardBackPhoto,
ArtistPhoto: record.ArtistPhoto,
ArtworkFile: record.ArtworkFile,
CreatedAt: record.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: record.UpdatedAt.Format("2006-01-02 15:04:05"),
}
out.Data = data
out.IsExist = true
return out, nil
}