func GetUserByName(_name string) (*User, bool, error) {
	var _u = &User{
		Name: _name,
	}
	err := userDB.CacheGet(_u, "name")
	switch err {
	case nil:
		if _u.CreatedAt == 0 || _u.DeletedTs != 0 {
			return nil, false, nil
		}
		return _u, true, nil
	case sql.ErrNoRows:
		return nil, false, nil
	default:
		return nil, false, err
	}
}
GetUserByName query a User data from database by 'name' unique key.
NOTE:
With cache layer;
If @return bool=false error=nil, means the data is not exist.
package model
import (
	"database/sql"
	"unsafe"
	"github.com/henrylee2cn/erpc/v6"
	"github.com/henrylee2cn/goutil/coarsetime"
	"github.com/xiaoenai/tp-micro/v6/model/mysql"
	"github.com/xiaoenai/tp-micro/v6/model/sqlx"
	"github.com/xiaoenai/tp-micro/examples/project/args"
)
type User args.User
func ToUser(_u *args.User) *User {
	return (*User)(unsafe.Pointer(_u))
}
func ToArgsUser(_u *User) *args.User {
	return (*args.User)(unsafe.Pointer(_u))
}
func ToUserSlice(a []*args.User) []*User {
	return *(*[]*User)(unsafe.Pointer(&a))
}
func ToArgsUserSlice(a []*User) []*args.User {
	return *(*[]*args.User)(unsafe.Pointer(&a))
}
func (*User) TableName() string {
	return "user"
}
func (_u *User) isZeroPrimaryKey() bool {
	var _id int64
	if _u.Id != _id {
		return false
	}
	return true
}
var userDB, _ = mysqlHandler.RegCacheableDB(new(User), cacheExpire, args.UserSql)
func GetUserDB() *mysql.CacheableDB {
	return userDB
}
func InsertUser(_u *User, tx ...*sqlx.Tx) (int64, error) {
	_u.UpdatedAt = coarsetime.FloorTimeNow().Unix()
	if _u.CreatedAt == 0 {
		_u.CreatedAt = _u.UpdatedAt
	}
	return _u.Id, userDB.Callback(func(tx sqlx.DbOrTx) error {
		var (
			query            string
			isZeroPrimaryKey = _u.isZeroPrimaryKey()
		)
		if isZeroPrimaryKey {
			query = "INSERT INTO `user` (`name`,`age`,`updated_at`,`created_at`)VALUES(:name,:age,:updated_at,:created_at);"
		} else {
			query = "INSERT INTO `user` (`id`,`name`,`age`,`updated_at`,`created_at`)VALUES(:id,:name,:age,:updated_at,:created_at);"
		}
		r, err := tx.NamedExec(query, _u)
		if isZeroPrimaryKey && err == nil {
			_u.Id, err = r.LastInsertId()
		}
		return err
	}, tx...)
}
func UpsertUser(_u *User, _updateFields []string, tx ...*sqlx.Tx) (int64, error) {
	if _u.UpdatedAt == 0 {
		_u.UpdatedAt = coarsetime.FloorTimeNow().Unix()
	}
	if _u.CreatedAt == 0 {
		_u.CreatedAt = _u.UpdatedAt
	}
	err := userDB.Callback(func(tx sqlx.DbOrTx) error {
		var (
			query            string
			isZeroPrimaryKey = _u.isZeroPrimaryKey()
		)
		if isZeroPrimaryKey {
			query = "INSERT INTO `user` (`name`,`age`,`updated_at`,`created_at`)VALUES(:name,:age,:updated_at,:created_at)"
		} else {
			query = "INSERT INTO `user` (`id`,`name`,`age`,`updated_at`,`created_at`)VALUES(:id,:name,:age,:updated_at,:created_at)"
		}
		query += " ON DUPLICATE KEY UPDATE "
		if len(_updateFields) == 0 {
			query += "`name`=VALUES(`name`),`age`=VALUES(`age`),`updated_at`=VALUES(`updated_at`);"
		} else {
			for _, s := range _updateFields {
				if s == "updated_at" || s == "created_at" || s == "deleted_ts" || s == "id" {
					continue
				}
				query += "`" + s + "`=VALUES(`" + s + "`),"
			}
			if query[len(query)-1] != ',' {
				return nil
			}
			query += "`updated_at`=VALUES(`updated_at`),`deleted_ts`=0;"
		}
		r, err := tx.NamedExec(query, _u)
		if isZeroPrimaryKey && err == nil {
			var rowsAffected int64
			rowsAffected, err = r.RowsAffected()
			if rowsAffected == 1 {
				_u.Id, err = r.LastInsertId()
			}
		}
		return err
	}, tx...)
	if err != nil {
		return _u.Id, err
	}
	err = userDB.DeleteCache(_u)
	if err != nil {
		erpc.Errorf("%s", err.Error())
	}
	return _u.Id, nil
}
func UpdateUserByPrimary(_u *User, _updateFields []string, tx ...*sqlx.Tx) error {
	_u.UpdatedAt = coarsetime.FloorTimeNow().Unix()
	err := userDB.Callback(func(tx sqlx.DbOrTx) error {
		query := "UPDATE `user` SET "
		if len(_updateFields) == 0 {
			query += "`name`=:name,`age`=:age,`updated_at`=:updated_at WHERE `id`=:id AND `deleted_ts`=0 LIMIT 1;"
		} else {
			for _, s := range _updateFields {
				if s == "updated_at" || s == "created_at" || s == "deleted_ts" || s == "id" {
					continue
				}
				query += "`" + s + "`=:" + s + ","
			}
			if query[len(query)-1] != ',' {
				return nil
			}
			query += "`updated_at`=:updated_at WHERE `id`=:id AND `deleted_ts`=0 LIMIT 1;"
		}
		_, err := tx.NamedExec(query, _u)
		return err
	}, tx...)
	if err != nil {
		return err
	}
	err = userDB.DeleteCache(_u)
	if err != nil {
		erpc.Errorf("%s", err.Error())
	}
	return nil
}
func UpdateUserByName(_u *User, _updateFields []string, tx ...*sqlx.Tx) error {
	_u.UpdatedAt = coarsetime.FloorTimeNow().Unix()
	err := userDB.Callback(func(tx sqlx.DbOrTx) error {
		query := "UPDATE `user` SET "
		if len(_updateFields) == 0 {
			query += "`name`=:name,`age`=:age,`updated_at`=:updated_at WHERE `name`=:name AND `deleted_ts`=0 LIMIT 1;"
		} else {
			for _, s := range _updateFields {
				if s == "updated_at" || s == "created_at" || s == "deleted_ts" || s == "name" || s == "id" {
					continue
				}
				query += "`" + s + "`=:" + s + ","
			}
			if query[len(query)-1] != ',' {
				return nil
			}
			query += "`updated_at`=:updated_at WHERE `name`=:name AND `deleted_ts`=0 LIMIT 1;"
		}
		_, err := tx.NamedExec(query, _u)
		return err
	}, tx...)
	if err != nil {
		return err
	}
	err = userDB.DeleteCache(_u, "name")
	if err != nil {
		erpc.Errorf("%s", err.Error())
	}
	return nil
}
func DeleteUserByPrimary(_id int64, deleteHard bool, tx ...*sqlx.Tx) error {
	var err error
	if deleteHard {
		err = userDB.Callback(func(tx sqlx.DbOrTx) error {
			_, err := tx.Exec("DELETE FROM `user` WHERE `id`=? AND `deleted_ts`=0;", _id)
			return err
		}, tx...)
	} else {
		ts := coarsetime.FloorTimeNow().Unix()
		err = userDB.Callback(func(tx sqlx.DbOrTx) error {
			_, err := tx.Exec("UPDATE `user` SET `updated_at`=?, `deleted_ts`=? WHERE `id`=? AND `deleted_ts`=0;", ts, ts, _id)
			return err
		}, tx...)
	}
	if err != nil {
		return err
	}
	err = userDB.DeleteCache(&User{
		Id: _id,
	})
	if err != nil {
		erpc.Errorf("%s", err.Error())
	}
	return nil
}
func DeleteUserByName(_name string, deleteHard bool, tx ...*sqlx.Tx) error {
	var err error
	if deleteHard {
		err = userDB.Callback(func(tx sqlx.DbOrTx) error {
			_, err := tx.Exec("DELETE FROM `user` WHERE `name`=? AND `deleted_ts`=0;", _name)
			return err
		}, tx...)
	} else {
		ts := coarsetime.FloorTimeNow().Unix()
		err = userDB.Callback(func(tx sqlx.DbOrTx) error {
			_, err := tx.Exec("UPDATE `user` SET `updated_at`=?, `deleted_ts`=? WHERE `name`=? AND `deleted_ts`=0;", ts, ts, _name)
			return err
		}, tx...)
	}
	if err != nil {
		return err
	}
	err = userDB.DeleteCache(&User{
		Name: _name,
	}, "name")
	if err != nil {
		erpc.Errorf("%s", err.Error())
	}
	return nil
}
func GetUserByPrimary(_id int64) (*User, bool, error) {
	var _u = &User{
		Id: _id,
	}
	err := userDB.CacheGet(_u)
	switch err {
	case nil:
		if _u.CreatedAt == 0 || _u.DeletedTs != 0 {
			return nil, false, nil
		}
		return _u, true, nil
	case sql.ErrNoRows:
		return nil, false, nil
	default:
		return nil, false, err
	}
}