/*
 * @description: 视图层 用户
 * @Author: CP
 * @Date: 2020-09-08 16:27:54
 * @FilePath: \construction_management\web\viewmodels\project_account.go
 */
package viewmodels

import (
	"regexp"

	validation "github.com/go-ozzo/ozzo-validation/v3"
)

type ProjectAccount struct {
	Id                 string `form:"id" json:"id"`
	ProjectId          string `form:"projectId" json:"projectId"`
	BidsectionId       string `form:"bidsectionId" json:"bidsectionId"`
	Account            string `form:"account" json:"account"`
	Password           string `form:"password" json:"password"`
	Name               string `form:"name" json:"name"`
	Company            string `form:"company" json:"company"`
	Position           string `form:"position" json:"position"`
	Role               string `form:"role" json:"role"`
	Mobile             string `form:"mobile" json:"mobile"`
	Telephone          string `form:"telephone" json:"telephone"`
	IsAdmin            int    `form:"isAdmin" json:"isAdmin"`
	AccountGroup       int    `form:"accountGroup" json:"accountGroup"`
	Enable             int    `form:"enable" json:"enable"`
	ContractPermission string `form:"contractPermission" json:"contractPermission"`
	QualityPermission  string `form:"qualityPermission" json:"qualityPermission"`
	SafePermission     string `form:"safePermission" json:"safePermission"`
}

type AccountPassword struct {
	Password        string `form:"password" json:"password"`
	NewPassword     string `form:"newPassword" json:"newPassword"`
	ConfirmPassword string `form:"confirmPassword" json:"confirmPassword"`
}

func (l ProjectAccount) Validate() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Account, validation.Required.Error("账号不能为空"), validation.Match(regexp.MustCompile("^[A-Za-z0-9]+$")).Error("只支持英文数字组合")),
		// validation.Match(regexp.MustCompile("^[a-zA-Z~!@#$%^&*]\\w{5,17}$")).Error("密码支持英文数字及符号,6~18之间")
		validation.Field(&l.Password, validation.Required.Error("密码不能为空"), validation.Length(6, 18).Error("密码位数6~18之间")),
		validation.Field(&l.AccountGroup, validation.Required.Error("账号组不能为空"), validation.In(1, 2, 3, 4).Error("未找到相关账号组")),
		validation.Field(&l.Name, validation.Required.Error("姓名不能为空")),
		validation.Field(&l.Company, validation.Required.Error("单位不能为空")),
		validation.Field(&l.Position, validation.Required.Error("职位不能为空")),
		validation.Field(&l.Mobile, validation.Required.Error("手机不能为空"), validation.Match(regexp.MustCompile("^([1][3,4,5,6,7,8,9])\\d{9}$")).Error("手机号码错误")),
		validation.Field(&l.AccountGroup, validation.Required.Error("账号组不能为空")),
	)
}

func (l ProjectAccount) ValidateAddBs() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.ProjectId, validation.Required.Error("项目ID不能为空")),
		validation.Field(&l.Account, validation.Required.Error("账号不能为空"), validation.Match(regexp.MustCompile("^[A-Za-z0-9]+$")).Error("只支持英文数字组合")),
		validation.Field(&l.Password, validation.Required.Error("密码不能为空"), validation.Length(6, 18).Error("密码位数6~18之间")),
		validation.Field(&l.Name, validation.Required.Error("姓名不能为空")),
		validation.Field(&l.Company, validation.Required.Error("单位不能为空")),
	)
}

func (l ProjectAccount) ValidateSaveBs() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Id, validation.Required.Error("ID不能为空")),
		validation.Field(&l.ProjectId, validation.Required.Error("项目ID不能为空")),
		validation.Field(&l.Name, validation.Required.Error("姓名不能为空")),
		validation.Field(&l.Company, validation.Required.Error("单位不能为空")),
	)
}

func (l ProjectAccount) ValidateEnableBs() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Id, validation.Required.Error("ID不能为空")),
		validation.Field(&l.ProjectId, validation.Required.Error("项目ID不能为空")),
		validation.Field(&l.Enable, validation.In(0, 1).Error("启用或停用值不正确")),
	)
}

func (l ProjectAccount) ValidatePasswordBs() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Id, validation.Required.Error("ID不能为空")),
		validation.Field(&l.ProjectId, validation.Required.Error("项目ID不能为空")),
		validation.Field(&l.Password, validation.Required.Error("密码不能为空"), validation.Length(6, 18).Error("密码位数6~18之间")),
	)
}

func (l ProjectAccount) ValidateUpdate() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Name, validation.Required.Error("姓名不能为空")),
		validation.Field(&l.AccountGroup, validation.Required.Error("账号组不能为空"), validation.In(1, 2, 3, 4).Error("未找到相关账号组")),
		validation.Field(&l.Company, validation.Required.Error("单位不能为空")),
		validation.Field(&l.Position, validation.Required.Error("职位不能为空")),
		//validation.Field(&l.Mobile, validation.Required.Error("手机不能为空"), validation.Match(regexp.MustCompile("^([1][3,4,5,6,7,8,9])\\d{9}$")).Error("只支持英文数字组合")),
		validation.Field(&l.AccountGroup, validation.Required.Error("账号组不能为空")),
	)
}

func (l ProjectAccount) ValidatePassword() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Account, validation.Required.Error("账号不能为空"), validation.Match(regexp.MustCompile("^[A-Za-z0-9]+$")).Error("只支持英文数字组合")),
		//"^[a-zA-Z0-9]\\w{5,17}$"
		// , validation.Match(regexp.MustCompile(`/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{6,18}$/`)).Error("密码支持英文数字及符号,6~18之间")
		validation.Field(&l.Password, validation.Required.Error("密码不能为空")),
	)
}

func (l ProjectAccount) ValidateAccount() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Name, validation.Required.Error("姓名不能为空")),
		validation.Field(&l.Company, validation.Required.Error("单位不能为空")),
		validation.Field(&l.Position, validation.Required.Error("职位不能为空")),
	)
}

func (l ProjectAccount) ValidateProjectId() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.ProjectId, validation.Required.Error("项目ID不能为空")),
	)
}

func (l AccountPassword) ValidateChangePassword() error {
	return validation.ValidateStruct(&l,
		validation.Field(&l.Password, validation.Required.Error("密码不能为空")),
		validation.Field(&l.NewPassword, validation.Required.Error("新密码不能为空")),
		validation.Field(&l.ConfirmPassword, validation.Required.Error("确认新密码不能为空")),
	)
}

//MoveId string `form:"moveId"`
// LastLogin    int       `xorm:"comment('最后登录时间') INT(11)"`

// EnterpriseId     int       `xorm:"comment('企业id') INT(11)"`
// CreateTime   int       `xorm:"comment('创建时间') INT(11)"`

// AuthMobile   string    `xorm:"comment('认证手机') VARCHAR(32)"`
// Permission   string    `xorm:"comment('权限') TEXT"`
// Cooperation  int       `xorm:"comment('协作') TINYINT(4)"`
// LastNotice   time.Time `xorm:"comment('待办事项，通知') DATETIME"`
// SignPath     string    `xorm:"comment('电子签名图片地址') VARCHAR(512)"`
// SessionToken string    `xorm:"comment('token信息验证') VARCHAR(128)"`
// SmsType      string    `xorm:"comment('短信通知类型') TEXT"`
// WxType       string    `xorm:"comment('微信通知类型') TEXT"`
// Bind         int       `xorm:"comment('是否已绑定定制项目') TINYINT(1)"`
// WxOpenid     string    `xorm:"comment('微信绑定openid') VARCHAR(64)"`
// WxName       string    `xorm:"comment('微信昵称') VARCHAR(255)"`
