| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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
- }
|