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

import (
	"time"

	"gorm.io/gorm"
)

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:"isRead" 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:"idcardFront" gorm:"column:idcard_front;comment:身份证反面"`
	JoinAssoTime      string    `json:"joinAssoTime" gorm:"column:join_asso_time;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:"`
	CertificateNum    string    `json:"certificateNum" gorm:"column:certificate_num;comment:"`
	CertificateImg    string    `json:"certificateImg" gorm:"column:certificate_img;comment:"`
	BankName          string    `json:"bankName" gorm:"column:bank_name;comment:"`
	BankNum           string    `json:"bankNum" gorm:"column:bank_num;comment:"`
	WxAccount         string    `json:"wxAccount" gorm:"column:wx_account;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,
 su.join_asso_time,
 su.certificate_num,
 su.certificate_img,
 su.wx_account,
 aas.bank_name,
 aas.bank_num

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 BINARY inviter.invited_code = BINARY invite.invite_code 
LEFT JOIN real_name inviter_rn ON inviter_rn.id = inviter.real_name_id
LEFT JOIN artshow_artist_supplement aas ON aas.artist_uid = su.mgmt_artist_uid AND aas.lock_time = (
	select MAX(lock_time) from artshow_artist_supplement WHERE artist_uid = su.mgmt_artist_uid
   )
WHERE 
su.deleted_at = 0
`
	db.Exec(sql)
	return nil
}