package system import ( "fmt" "log" "time" "xorm.io/core" "xorm.io/xorm" "xps/datamodels" "xps/viewmodels" ) type AccountRepo struct { engine *xorm.Engine } func NewAccountRepo(engine *xorm.Engine) *AccountRepo { return &AccountRepo{ engine: engine, } } func (d *AccountRepo) GetAccountByName(accountName string) *viewmodels.AccountInfo { data := &viewmodels.AccountInfo{} sqlSelect := ` SELECT A.oc_id, B.oc_type_id, B.oc_name, A.account_id, A.account_name, A.account_real_name, A.password, A.account_type, A.account_staff_no, A.account_phone, A.account_last_ip, A.account_loc, A.status, A.account_avatar, A.account_photo, A.account_real_name, A.account_intro, A.wx_id, A.is_fixed ` sqlFrom := ` FROM account AS A LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) ` sqlWhere := `WHERE A.deleted_flag = 0 ` if accountName != "" { sqlWhere += ` AND A.account_name like ` + "'%" + accountName + "%'" } //err2 := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("role_id DESC").Limit(pageSize, pageStart).Find(&datalist) ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data) if !ok && err != nil { return nil } else { return data } } func (d *AccountRepo) GetAccountByCode(code string) (*viewmodels.AccountInfo, error) { data := &viewmodels.AccountInfo{} sqlSelect := ` SELECT A.oc_id, B.oc_type_id, B.oc_name, A.account_id, A.account_name, A.account_real_name, A.password, A.account_type, A.account_staff_no, A.account_phone, A.account_last_ip, A.account_loc, A.status, A.account_avatar, A.account_photo, A.account_real_name, A.account_intro, A.wx_id, A.is_fixed ` sqlFrom := ` FROM account AS A LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) ` sqlWhere := ` WHERE A.deleted_flag = 0 ` sqlWhere += ` AND (A.account_name = ` + "'" + code + "'" sqlWhere += ` OR A.account_phone = ` + "'" + code + "'" sqlWhere += ` OR A.account_mail = ` + "'" + code + "'" sqlWhere += ` OR A.account_staff_no = ` + "'" + code + "')" ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data) if !ok && err != nil { return nil, err } return data, nil } func (d *AccountRepo) GetById(ocId int64, accountId int64) *viewmodels.AccountInfo { var user = &viewmodels.AccountInfo{} sqlSelect := ` SELECT A.oc_id, B.oc_type_id, B.oc_name, A.account_id, A.account_name, A.account_real_name, A.password, A.account_type, A.account_staff_no, A.account_phone, A.account_last_ip, A.account_loc, A.status, A.account_avatar, A.account_photo, A.account_real_name, A.account_intro, A.wx_id, A.is_fixed ` sqlFrom := ` FROM account AS A LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) ` sqlWhere := ` WHERE A.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId) sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", accountId) has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(user) if !has || err != nil { return nil } else { return user } } func (d *AccountRepo) GetByList(ocId int64) ([]viewmodels.AccountInfo, error) { datalist := make([]viewmodels.AccountInfo, 0) sqlSelect := ` SELECT A.oc_id, B.oc_type_id, B.oc_name, A.account_id, A.account_name, A.account_real_name, A.password, A.account_type, A.account_staff_no, A.account_phone, A.account_last_ip, A.account_loc, A.status, A.account_avatar, A.account_photo, A.account_real_name, A.account_intro, A.wx_id, A.is_fixed ` sqlFrom := ` FROM account AS A LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) ` sqlWhere := ` WHERE A.deleted_flag = 0 ` err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Asc("account_id").Find(&datalist) if err != nil { return nil, err } return datalist, nil } func (d *AccountRepo) Create(data *datamodels.Account) error { _, err := d.engine.Insert(data) return err } func (d *AccountRepo) Update(ocId, accountId int64, data *datamodels.Account) error { _, err := d.engine.ID(core.PK{ocId, accountId}).Update(data) return err } func (d *AccountRepo) Delete(m map[string]interface{}) error { ocId := m["ocId"].(int64) accountId := m["accountId"].(int64) deletedBy := m["deletedBy"].(int64) data := &datamodels.Account{ OcId: ocId, AccountId: accountId, DeletedFlag: 1, DeletedBy: deletedBy, DeletedAt: time.Now(), } _, err := d.engine.ID(core.PK{ocId, accountId}).Update(data) return err } func (d *AccountRepo) IsUserNameExist(ocId int64, accountName string) bool { count, err := d.engine.Where("deleted_flag = 0").Where("account_name = ?", accountName).Count(new(datamodels.Account)) if err != nil { log.Fatal(err) return true } return count > 0 } func (d *AccountRepo) IsUserPhoneExist(ocId int64, accountPhone string) bool { count, err := d.engine.Where("deleted_flag = 0").Where("account_phone = ?", accountPhone).Count(new(datamodels.Account)) if err != nil { log.Fatal(err) return true } return count > 0 }