// Package model -----------------------------
// @file      : user_view.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2023/2/27 13:51
// -------------------------------------------
package model

import (
	"gorm.io/gorm"
	"time"
)

type UserView struct {
	UserId            int64     `json:"userId" gorm:"column:user_id;comment:用户表id"`
	AccId             int64     `json:"accId" gorm:"column:mgmt_acc_id;comment:账号id"`
	ArtistUid         string    `json:"artistUid" gorm:"column:mgmt_artist_uid;comment:账号id"`
	TelNum            string    `json:"telNum" gorm:"column:tel_num;comment:电话号码"`
	InviteCode        string    `json:"inviteCode" gorm:"column:invited_code;comment:邀请码"`
	Account           string    `json:"account" gorm:"column:account;comment:账号"`
	Photo             string    `json:"photo" gorm:"column:photo;comment:个人近照"`
	IsRealName        int64     `json:"isRealName" gorm:"column:is_real_name;comment:是否实名认证"`
	FddState          int64     `json:"fddState" gorm:"column:fdd_state;comment:法大大状态"`
	IsRead            int64     `json:"is_read" gorm:"column:is_read;comment:是否已读 0未读 1已读"`
	IsLock            bool      `json:"isLock" gorm:"column:is_lock;comment:是否锁定"`
	LatestLockTime    string    `json:"latestLockTime" gorm:"column:latest_lock_time;comment:"`
	RealName          string    `json:"realName" gorm:"column:real_name;comment:真实姓名"`
	IdNum             string    `json:"idNum" gorm:"column:id_num;comment:证件号码"`
	Sex               SexType   `json:"sex" gorm:"column:sex;comment:性别"`
	Age               int64     `json:"age" gorm:"column:age;comment:年龄"`
	RealAddress       string    `json:"address" gorm:"column:real_address;comment:真实地址"`
	IdcardBack        string    `json:"idcardBack" gorm:"column:idcard_back;comment:身份证背面"`
	IdcardFront       string    `json:"idcard_front" gorm:"column:idcard_front;comment:身份证反面"`
	InviterInviteCode string    `json:"inviterInviteCode" gorm:"column:inviter_invite_code;comment:邀请人的邀请码"`
	InviterRealName   string    `json:"inviterRealName" gorm:"column:inviter_name;comment:邀请人的真实姓名"`
	CreatedAt         time.Time `json:"createdAt" gorm:"column:created_at;comment:"`
	UpdatedAt         time.Time `json:"updatedAt" gorm:"column:updated_at;comment:"`
	DeletedAt         int64     `json:"deletedAt" gorm:"column:deleted_at;comment:"`
}

func (u UserView) TableName() string {
	return "user_view"
}
func CreateUserView(db *gorm.DB) error {
	var sql = `
CREATE OR REPLACE  VIEW user_view AS
SELECT 
 su.id user_id,
 su.mgmt_acc_id,
 su.mgmt_artist_uid,
 su.tel_num,
 su.invited_code,
 su.account,
 su.photo,
 su.is_real_name,
 su.fdd_state,
 su.is_read,
 su.is_lock,
 rn.name real_name,
 rn.idcard_front,
 rn.idcard_back,
 rn.address real_address,
 rn.id_num,
 rn.sex,
 rn.age,
 su.created_at,
 su.updated_at,
 su.deleted_at,
 inviter.invited_code inviter_invite_code,
 inviter_rn.name inviter_name,
 su.latest_lock_time
FROM sys_user su
LEFT JOIN real_name rn ON rn.id  = su.real_name_id 
-- 邀请者信息
LEFT JOIN invite ON invite.invited_id = su.id
LEFT JOIN sys_user inviter ON inviter.id = invite.user_id 
LEFT JOIN real_name inviter_rn ON inviter_rn.id = inviter.real_name_id
WHERE 
su.deleted_at = 0
`
	db.Exec(sql)
	return nil
}
