sso

org_service.go 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package service
  2. import (
  3. "SSO/models"
  4. "strconv"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func CreateOrg(org *models.Org, user_name string, openXT bool, openCDM bool, openSCRM bool, openMall bool) error {
  9. now := time.Now()
  10. tx_admin := writeUserDb.Begin()
  11. if err := tx_admin.Create(org).Error; err != nil {
  12. tx_admin.Rollback()
  13. return err
  14. }
  15. role := models.Role{
  16. RoleName: "超级管理员",
  17. RoleIntro: "",
  18. Creator: org.Creator,
  19. OrgId: org.Id,
  20. AppId: 0,
  21. IsSuperAdmin: true,
  22. Status: 1,
  23. CreateTime: now.Unix(),
  24. ModifyTime: now.Unix(),
  25. }
  26. if err := tx_admin.Create(&role).Error; err != nil {
  27. tx_admin.Rollback()
  28. return err
  29. }
  30. // app 的创建顺序也决定了登录后前往 app 的优先级
  31. if err := createOrgApp(tx_admin, &role, user_name, 3, openXT); err != nil {
  32. tx_admin.Rollback()
  33. return err
  34. }
  35. nextMonthDate := now.AddDate(0, 0, 30)
  36. subscibe := models.ServeSubscibe{
  37. OrgId: int64(org.Id),
  38. PeriodStart: now.Unix(),
  39. PeriodEnd: nextMonthDate.Unix(),
  40. Status: 1,
  41. CreatedTime: now.Unix(),
  42. UpdatedTime: now.Unix(),
  43. State: 9,
  44. }
  45. if err := tx_admin.Create(&subscibe).Error; err != nil {
  46. tx_admin.Rollback()
  47. return err
  48. }
  49. tx_patient := writeDb.Begin()
  50. 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 {
  51. tx_patient.Rollback()
  52. tx_admin.Rollback()
  53. return err
  54. }
  55. 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 {
  56. tx_patient.Rollback()
  57. tx_admin.Rollback()
  58. return err
  59. }
  60. 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 {
  61. tx_admin.Rollback()
  62. tx_patient.Rollback()
  63. return err
  64. }
  65. tx_admin.Commit()
  66. tx_patient.Commit()
  67. return nil
  68. }
  69. func GetLastName(creator int64) (models.Org, error) {
  70. org := models.Org{}
  71. err := readUserDb.Model(&org).Where("creator = ? and status = 1").Last(&org).Error
  72. return org, err
  73. }
  74. func GetOrgWithAdminUserID(adminUserID int) (*models.Org, error) {
  75. var org models.Org
  76. err := readUserDb.Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
  77. if err == gorm.ErrRecordNotFound {
  78. err = nil
  79. }
  80. if err != nil {
  81. return nil, err
  82. } else {
  83. return &org, nil
  84. }
  85. }
  86. func GetOrgApps(adminUserID int, orgID int) ([]*models.OrgApp, error) {
  87. var apps []*models.OrgApp
  88. err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND status = 1", adminUserID, orgID).Find(&apps).Error
  89. if err == gorm.ErrRecordNotFound {
  90. err = nil
  91. }
  92. if err != nil {
  93. return nil, err
  94. } else {
  95. return apps, nil
  96. }
  97. }
  98. func DidOrgDidCreateAppWithType(orgID int, appType int) (bool, error) {
  99. var count int
  100. err := readUserDb.Model(&models.OrgApp{}).Where("org_id = ? AND app_type = ? AND status = 1", orgID, appType).Count(&count).Error
  101. if err != nil {
  102. return false, err
  103. } else {
  104. return count == 1, nil
  105. }
  106. }
  107. func CreateOrgApp(adminUserID int, orgID int, appType int, open bool) error {
  108. var user models.AdminUser
  109. err := readUserDb.Where("id = ? AND status = 1", adminUserID).Find(&user).Error
  110. if err != nil {
  111. return err
  112. }
  113. openStatus := 0
  114. if open {
  115. openStatus = 1
  116. }
  117. now := time.Now().Unix()
  118. tx := writeUserDb.Begin()
  119. app := models.OrgApp{
  120. Creator: adminUserID,
  121. OrgId: orgID,
  122. AppType: appType,
  123. OpenStatus: openStatus,
  124. Status: 1,
  125. CreateTime: now,
  126. ModifyTime: now,
  127. }
  128. if createAppErr := tx.Create(&app).Error; createAppErr != nil {
  129. tx.Rollback()
  130. return createAppErr
  131. }
  132. var role models.Role
  133. getRoleErr := readUserDb.Where("creator = ? AND org_id = ? AND is_super_admin = 1", adminUserID, orgID).First(&role).Error
  134. if getRoleErr != nil {
  135. tx.Rollback()
  136. return getRoleErr
  137. }
  138. app_role := models.App_Role{
  139. AdminUserId: adminUserID,
  140. OrgId: orgID,
  141. AppId: app.Id,
  142. RoleId: role.Id,
  143. Avatar: "",
  144. UserName: user.Mobile,
  145. Intro: "",
  146. UserType: 1,
  147. UserTitle: 1,
  148. Status: 1,
  149. CreateTime: now,
  150. ModifyTime: now,
  151. }
  152. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  153. tx.Rollback()
  154. return createApp_RoleErr
  155. }
  156. return tx.Commit().Error
  157. }
  158. // 如果出错,没有 rollback,需自行 rollback
  159. func createOrgApp(tx *gorm.DB, superAdminRole *models.Role, userName string, appType int, open bool) error {
  160. //创建应用
  161. now := time.Now().Unix()
  162. var app_id int64
  163. apps, _ := FindSystemOrgApp()
  164. for _, item := range apps {
  165. app := &VMOrgApp{
  166. AppType: item.AppType,
  167. Creator: int(superAdminRole.Creator),
  168. OrgId: int(superAdminRole.OrgId),
  169. OpenStatus: item.OpenStatus,
  170. Status: 1,
  171. CreateTime: now,
  172. ModifyTime: now,
  173. Name: item.Name,
  174. Url: item.Url,
  175. Number: item.Number,
  176. Pid: 0,
  177. }
  178. writeUserDb.Create(&app)
  179. if app.AppType == 3 {
  180. app_id = app.ID
  181. }
  182. for _, subItem := range item.VMOrgApp {
  183. app := &VMOrgApp{
  184. AppType: subItem.AppType,
  185. Creator: int(superAdminRole.Creator),
  186. OrgId: int(superAdminRole.OrgId),
  187. OpenStatus: subItem.OpenStatus,
  188. Status: 1,
  189. CreateTime: now,
  190. ModifyTime: now,
  191. Name: subItem.Name,
  192. Url: subItem.Url,
  193. Number: subItem.Number,
  194. Pid: app.ID,
  195. }
  196. writeUserDb.Create(&app)
  197. }
  198. }
  199. app_role := models.App_Role{
  200. AdminUserId: superAdminRole.Creator,
  201. OrgId: superAdminRole.OrgId,
  202. AppId: int(app_id),
  203. RoleId: superAdminRole.Id,
  204. Avatar: "",
  205. RoleIds: strconv.Itoa(superAdminRole.Id),
  206. UserName: userName,
  207. Intro: "",
  208. UserType: 1,
  209. UserTitle: 1,
  210. Status: 1,
  211. CreateTime: now,
  212. ModifyTime: now,
  213. }
  214. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  215. return createApp_RoleErr
  216. }
  217. return nil
  218. }
  219. func GetIllness() ([]*models.Illness, error) {
  220. var illness []*models.Illness
  221. err := readUserDb.Model(&models.Illness{}).Where("status = 1").Find(&illness).Error
  222. if err != nil {
  223. return nil, err
  224. } else {
  225. return illness, nil
  226. }
  227. }
  228. func GetOrgCategoriesByPid(pid int64) ([]*models.OrgType, error) {
  229. var cats []*models.OrgType
  230. err := readUserDb.Model(&models.OrgType{}).Where("pid = ? AND status = 1", pid).Order("sort_no asc").Find(&cats).Error
  231. if err != nil {
  232. return nil, err
  233. } else {
  234. return cats, nil
  235. }
  236. }
  237. func GetOrgServeSubscibe(orgID int) (*models.ServeSubscibe, error) {
  238. var model models.ServeSubscibe
  239. err := readUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? AND status = 1", orgID).First(&model).Error
  240. if err != nil {
  241. if err == gorm.ErrRecordNotFound {
  242. return nil, nil
  243. } else {
  244. return nil, err
  245. }
  246. }
  247. return &model, nil
  248. }
  249. func CreateOrgServeSubscibe(subscibe *models.ServeSubscibe) error {
  250. err := writeUserDb.Create(subscibe).Error
  251. return err
  252. }
  253. func GetOrgAppWithType(adminUserID int, orgID int, appType int) (*models.OrgApp, error) {
  254. var app models.OrgApp
  255. err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND app_type = ? AND status = 1", adminUserID, orgID, appType).First(&app).Error
  256. if err == gorm.ErrRecordNotFound {
  257. err = nil
  258. }
  259. if err != nil {
  260. return nil, err
  261. } else {
  262. return &app, nil
  263. }
  264. }
  265. func SaveOrgApp(app *models.OrgApp) error {
  266. return writeUserDb.Save(app).Error
  267. }
  268. type VMOrgApp struct {
  269. ID int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // ID
  270. AppType int `gorm:"column:app_type" json:"app_type"`
  271. Creator int `json:"creator"` // 创建者,即管理员用户的 id
  272. OrgId int `gorm:"column:org_id" json:"org_id"`
  273. OpenStatus int `gorm:"column:open_status" json:"open_status"`
  274. Status int8 `json:"status"` // 状态 0.无效 1.有效 2.禁用
  275. CreateTime int64 `gorm:"column:ctime" json:"ctime"` // 创建时间
  276. ModifyTime int64 `gorm:"column:mtime" json:"mtime"` // 修改时间
  277. Name string `gorm:"column:name" json:"name" form:"name"`
  278. Url string `gorm:"column:url" json:"url" form:"url"`
  279. Pid int64 `gorm:"column:pid" json:"pid" form:"pid"`
  280. Number int64 `gorm:"column:number" json:"order" form:"number"`
  281. VMOrgApp []*VMOrgApp `gorm:"ForeignKey:Pid;AssociationForeignKey:Id" `
  282. }
  283. func (VMOrgApp) TableName() string {
  284. return "sgj_user_org_app"
  285. }
  286. func FindSystemOrgApp() (app []*VMOrgApp, err error) {
  287. 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
  288. return
  289. }