| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- package repositories
- import (
- "fmt"
- "time"
- "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, error) {
- 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 + "%'"
- }
- data := &viewmodels.AccountInfo{}
- ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
- if !ok && err != nil {
- return nil, err
- } else {
- return data, nil
- }
- }
- func (d *AccountRepo) GetAccountByCode(code string) (*viewmodels.AccountInfo, error) {
- 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 + "')"
-
- data := &viewmodels.AccountInfo{}
- 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, error){
- 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)
-
- user := &viewmodels.AccountInfo{}
- has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(user)
- if !has || err != nil {
- return nil, err
- } else {
- return user, nil
- }
- }
- func (d *AccountRepo) GetList(m map[string]interface{}) ([]viewmodels.AccountInfo, error) {
- 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 ocId, ok := m["ocId"]; ok {
- sqlWhere += " AND A.oc_id = " + fmt.Sprintf("%d", ocId)
- }
-
- datalist := make([]viewmodels.AccountInfo, 0)
- err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Asc("account_id").Find(&datalist)
- if err != nil {
- return nil, err
- } else {
- 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.
- Where("oc_id = ?", ocId).
- Where("account_id = ?", 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.
- Where("oc_id = ?", ocId).
- Where("account_id = ?", accountId).
- Update(data)
- return err
- }
- func (d *AccountRepo) IsUserNameExist(ocId int64, accountName string) (bool, error) {
- count, err := d.engine.
- Where("oc_id = ?", ocId).
- Where("deleted_flag = 0").
- Where("account_name = ?", accountName).
- Count(new(datamodels.Account))
- if err != nil {
- return false, err
- }
- return count > 0, nil
- }
- func (d *AccountRepo) IsUserPhoneExist(ocId int64, accountPhone string)(bool, error){
- count, err := d.engine.
- Where("oc_id = ?", ocId).
- Where("deleted_flag = 0").
- Where("account_phone = ?", accountPhone).
- Count(new(datamodels.Account))
- if err != nil {
- return true, err
- }
- return count > 0, nil
- }
- func (d *AccountRepo) IsStaffNoExist(ocId int64, staffNo string)(bool, error){
- count, err := d.engine.
- Where("oc_id = ?", ocId).
- Where("deleted_flag = 0").
- Where("account_staff_no = ?", staffNo).
- Count(new(datamodels.Account))
- if err != nil {
- return true, err
- }
- return count > 0, nil
- }
- func (d *AccountRepo) IsEmailExist(ocId int64, email string)(bool, error) {
- count, err := d.engine.
- Where("oc_id = ?", ocId).
- Where("deleted_flag = 0").
- Where("account_email = ?", email).
- Count(new(datamodels.Account))
- if err != nil {
- return true, err
- }
- return count > 0, nil
- }
|