sso

org_service.go 7.1KB

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