123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- package service
-
- import (
- "SSO/models"
- "strconv"
- "time"
-
- "github.com/jinzhu/gorm"
- )
-
- func CreateOrg(org *models.Org, user_name string, openXT bool, openCDM bool, openSCRM bool, openMall bool) error {
- now := time.Now()
- tx_admin := writeUserDb.Begin()
- if err := tx_admin.Create(org).Error; err != nil {
- tx_admin.Rollback()
- return err
- }
- role := models.Role{
- RoleName: "超级管理员",
- RoleIntro: "",
- Creator: org.Creator,
- OrgId: org.Id,
- AppId: 0,
- IsSuperAdmin: true,
- Status: 1,
- CreateTime: now.Unix(),
- ModifyTime: now.Unix(),
- }
- if err := tx_admin.Create(&role).Error; err != nil {
- tx_admin.Rollback()
- return err
- }
-
- // app 的创建顺序也决定了登录后前往 app 的优先级
- if err := createOrgApp(tx_admin, &role, user_name, 3, openXT); err != nil {
- tx_admin.Rollback()
- return err
- }
-
- nextMonthDate := now.AddDate(0, 0, 30)
- subscibe := models.ServeSubscibe{
- OrgId: int64(org.Id),
- PeriodStart: now.Unix(),
- PeriodEnd: nextMonthDate.Unix(),
- Status: 1,
- CreatedTime: now.Unix(),
- UpdatedTime: now.Unix(),
- State: 9,
- }
- if err := tx_admin.Create(&subscibe).Error; err != nil {
- tx_admin.Rollback()
- return err
- }
-
- tx_patient := writeDb.Begin()
- if err := tx_patient.Exec("INSERT INTO sgj_patient_articles_menu (name, status, type, user_org_id, ctime, mtime) VALUES(?, ?, ?, ?, ?, ?);", "科普教育", 1, 1, org.Id, now.Unix(), now.Unix()).Error; err != nil {
- tx_patient.Rollback()
- tx_admin.Rollback()
- return err
- }
- if err := tx_patient.Exec("INSERT INTO sgj_patient_good_category (user_org_id, category_name, status, created_time, updated_time) VALUES(?, ?, ?, ?, ?);", org.Id, "普通商品", 1, now.Unix(), now.Unix()).Error; err != nil {
- tx_patient.Rollback()
- tx_admin.Rollback()
- return err
- }
-
- if err := tx_admin.Exec("INSERT INTO sgj_user_membership_card (user_org_id, card_name, card_level, upgrade_integral, card_right, use_notice, background_type, background, service_phone, status, created_time, updated_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", org.Id, "普通会员", 0, 0, "", "", 1, "#CACA41", "", 1, now.Unix(), now.Unix()).Error; err != nil {
- tx_admin.Rollback()
- tx_patient.Rollback()
- return err
- }
-
- tx_admin.Commit()
- tx_patient.Commit()
- return nil
- }
-
- func GetLastName(creator int64) (models.Org, error) {
- org := models.Org{}
- err := readUserDb.Model(&org).Where("creator = ? and status = 1").Last(&org).Error
- return org, err
- }
-
- func GetOrgWithAdminUserID(adminUserID int) (*models.Org, error) {
- var org models.Org
- err := readUserDb.Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
- if err == gorm.ErrRecordNotFound {
- err = nil
- }
- if err != nil {
- return nil, err
- } else {
- return &org, nil
- }
- }
-
- func GetOrgApps(adminUserID int, orgID int) ([]*models.OrgApp, error) {
- var apps []*models.OrgApp
- err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND status = 1", adminUserID, orgID).Find(&apps).Error
- if err == gorm.ErrRecordNotFound {
- err = nil
- }
- if err != nil {
- return nil, err
- } else {
- return apps, nil
- }
- }
-
- func DidOrgDidCreateAppWithType(orgID int, appType int) (bool, error) {
- var count int
- err := readUserDb.Model(&models.OrgApp{}).Where("org_id = ? AND app_type = ? AND status = 1", orgID, appType).Count(&count).Error
- if err != nil {
- return false, err
- } else {
- return count == 1, nil
- }
- }
-
- func CreateOrgApp(adminUserID int, orgID int, appType int, open bool) error {
- var user models.AdminUser
- err := readUserDb.Where("id = ? AND status = 1", adminUserID).Find(&user).Error
- if err != nil {
- return err
- }
-
- openStatus := 0
- if open {
- openStatus = 1
- }
-
- now := time.Now().Unix()
- tx := writeUserDb.Begin()
- app := models.OrgApp{
- Creator: adminUserID,
- OrgId: orgID,
- AppType: appType,
- OpenStatus: openStatus,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- }
- if createAppErr := tx.Create(&app).Error; createAppErr != nil {
- tx.Rollback()
- return createAppErr
- }
- var role models.Role
- getRoleErr := readUserDb.Where("creator = ? AND org_id = ? AND is_super_admin = 1", adminUserID, orgID).First(&role).Error
- if getRoleErr != nil {
- tx.Rollback()
- return getRoleErr
- }
- app_role := models.App_Role{
- AdminUserId: adminUserID,
- OrgId: orgID,
- AppId: app.Id,
- RoleId: role.Id,
- Avatar: "",
- UserName: user.Mobile,
- Intro: "",
- UserType: 1,
- UserTitle: 1,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- }
- if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
- tx.Rollback()
- return createApp_RoleErr
- }
- return tx.Commit().Error
- }
-
- // 如果出错,没有 rollback,需自行 rollback
- func createOrgApp(tx *gorm.DB, superAdminRole *models.Role, userName string, appType int, open bool) error {
- //创建应用
- now := time.Now().Unix()
- var app_id int64
- apps, _ := FindSystemOrgApp()
- for _, item := range apps {
- app := &VMOrgApp{
- AppType: item.AppType,
- Creator: int(superAdminRole.Creator),
- OrgId: int(superAdminRole.OrgId),
- OpenStatus: item.OpenStatus,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- Name: item.Name,
- Url: item.Url,
- Number: item.Number,
- Pid: 0,
- }
- writeUserDb.Create(&app)
- if app.AppType == 3 {
- app_id = app.ID
-
- }
-
- for _, subItem := range item.VMOrgApp {
- app := &VMOrgApp{
- AppType: subItem.AppType,
- Creator: int(superAdminRole.Creator),
- OrgId: int(superAdminRole.OrgId),
- OpenStatus: subItem.OpenStatus,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- Name: subItem.Name,
- Url: subItem.Url,
- Number: subItem.Number,
- Pid: app.ID,
- }
- writeUserDb.Create(&app)
- }
- }
-
- app_role := models.App_Role{
- AdminUserId: superAdminRole.Creator,
- OrgId: superAdminRole.OrgId,
- AppId: int(app_id),
- RoleId: superAdminRole.Id,
- Avatar: "",
- RoleIds: strconv.Itoa(superAdminRole.Id),
- UserName: userName,
- Intro: "",
- UserType: 1,
- UserTitle: 1,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- }
- if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
- return createApp_RoleErr
- }
- return nil
- }
-
- func GetIllness() ([]*models.Illness, error) {
- var illness []*models.Illness
- err := readUserDb.Model(&models.Illness{}).Where("status = 1").Find(&illness).Error
- if err != nil {
- return nil, err
- } else {
- return illness, nil
- }
- }
-
- func GetOrgCategoriesByPid(pid int64) ([]*models.OrgType, error) {
- var cats []*models.OrgType
- err := readUserDb.Model(&models.OrgType{}).Where("pid = ? AND status = 1", pid).Order("sort_no asc").Find(&cats).Error
- if err != nil {
- return nil, err
- } else {
- return cats, nil
- }
- }
-
- func GetOrgServeSubscibe(orgID int) (*models.ServeSubscibe, error) {
- var model models.ServeSubscibe
- err := readUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? AND status = 1", orgID).First(&model).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &model, nil
- }
-
- func CreateOrgServeSubscibe(subscibe *models.ServeSubscibe) error {
- err := writeUserDb.Create(subscibe).Error
- return err
- }
-
- func GetOrgAppWithType(adminUserID int, orgID int, appType int) (*models.OrgApp, error) {
- var app models.OrgApp
- err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND app_type = ? AND status = 1", adminUserID, orgID, appType).First(&app).Error
- if err == gorm.ErrRecordNotFound {
- err = nil
- }
- if err != nil {
- return nil, err
- } else {
- return &app, nil
- }
- }
-
- func SaveOrgApp(app *models.OrgApp) error {
- return writeUserDb.Save(app).Error
- }
-
- type VMOrgApp struct {
- ID int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // ID
- AppType int `gorm:"column:app_type" json:"app_type"`
- Creator int `json:"creator"` // 创建者,即管理员用户的 id
- OrgId int `gorm:"column:org_id" json:"org_id"`
- OpenStatus int `gorm:"column:open_status" json:"open_status"`
- Status int8 `json:"status"` // 状态 0.无效 1.有效 2.禁用
- CreateTime int64 `gorm:"column:ctime" json:"ctime"` // 创建时间
- ModifyTime int64 `gorm:"column:mtime" json:"mtime"` // 修改时间
- Name string `gorm:"column:name" json:"name" form:"name"`
- Url string `gorm:"column:url" json:"url" form:"url"`
- Pid int64 `gorm:"column:pid" json:"pid" form:"pid"`
- Number int64 `gorm:"column:number" json:"order" form:"number"`
- VMOrgApp []*VMOrgApp `gorm:"ForeignKey:Pid;AssociationForeignKey:Id" `
- }
-
- func (VMOrgApp) TableName() string {
- return "sgj_user_org_app"
- }
-
- func FindSystemOrgApp() (app []*VMOrgApp, err error) {
- err = readUserDb.Model(&VMOrgApp{}).Preload("VMOrgApp", "org_id = 0 AND status =1 AND pid > 0").Where("status = 1 AND org_id = 0 AND pid = 0").Find(&app).Error
- return
- }
|