package repository import ( "fmt" "time" "xorm.io/xorm" "xps/pkg/system/datamodel" "xps/pkg/system/viewmodel" ) type AccountRepo struct { engine *xorm.Engine } func NewAccountRepo(engine *xorm.Engine) *AccountRepo { return &AccountRepo{ engine: engine, } } func (d *AccountRepo) GetAccountByName(accountName string) (*viewmodel.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 := &viewmodel.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) (*viewmodel.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 := &viewmodel.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) (*viewmodel.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 := &viewmodel.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{}) ([]viewmodel.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([]viewmodel.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 *datamodel.Account) error { _, err := d.engine.Insert(data) return err } func (d *AccountRepo) Update(ocId, accountId int64, data *datamodel.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 := &datamodel.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(datamodel.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(datamodel.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(datamodel.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(datamodel.Account)) if err != nil { return true, err } return count > 0, nil }