sso

org_service.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 GetLastName(creator int64) (models.Org, error) {
  81. org := models.Org{}
  82. err := readUserDb.Model(&org).Where("creator = ? and status = 1").Last(&org).Error
  83. return org, err
  84. }
  85. func GetOrgWithAdminUserID(adminUserID int) (*models.Org, error) {
  86. var org models.Org
  87. err := readUserDb.Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
  88. if err == gorm.ErrRecordNotFound {
  89. err = nil
  90. }
  91. if err != nil {
  92. return nil, err
  93. } else {
  94. return &org, nil
  95. }
  96. }
  97. func GetOrgApps(adminUserID int, orgID int) ([]*models.OrgApp, error) {
  98. var apps []*models.OrgApp
  99. err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND status = 1", adminUserID, orgID).Find(&apps).Error
  100. if err == gorm.ErrRecordNotFound {
  101. err = nil
  102. }
  103. if err != nil {
  104. return nil, err
  105. } else {
  106. return apps, nil
  107. }
  108. }
  109. func DidOrgDidCreateAppWithType(orgID int, appType int) (bool, error) {
  110. var count int
  111. err := readUserDb.Model(&models.OrgApp{}).Where("org_id = ? AND app_type = ? AND status = 1", orgID, appType).Count(&count).Error
  112. if err != nil {
  113. return false, err
  114. } else {
  115. return count == 1, nil
  116. }
  117. }
  118. func CreateOrgApp(adminUserID int, orgID int, appType int, open bool) error {
  119. var user models.AdminUser
  120. err := readUserDb.Where("id = ? AND status = 1", adminUserID).Find(&user).Error
  121. if err != nil {
  122. return err
  123. }
  124. openStatus := 0
  125. if open {
  126. openStatus = 1
  127. }
  128. now := time.Now().Unix()
  129. tx := writeUserDb.Begin()
  130. app := models.OrgApp{
  131. Creator: adminUserID,
  132. OrgId: orgID,
  133. AppType: appType,
  134. OpenStatus: openStatus,
  135. Status: 1,
  136. CreateTime: now,
  137. ModifyTime: now,
  138. }
  139. if createAppErr := tx.Create(&app).Error; createAppErr != nil {
  140. tx.Rollback()
  141. return createAppErr
  142. }
  143. var role models.Role
  144. getRoleErr := readUserDb.Where("creator = ? AND org_id = ? AND is_super_admin = 1", adminUserID, orgID).First(&role).Error
  145. if getRoleErr != nil {
  146. tx.Rollback()
  147. return getRoleErr
  148. }
  149. app_role := models.App_Role{
  150. AdminUserId: adminUserID,
  151. OrgId: orgID,
  152. AppId: app.Id,
  153. RoleId: role.Id,
  154. Avatar: "",
  155. UserName: user.Mobile,
  156. Intro: "",
  157. UserType: 1,
  158. UserTitle: 1,
  159. Status: 1,
  160. CreateTime: now,
  161. ModifyTime: now,
  162. }
  163. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  164. tx.Rollback()
  165. return createApp_RoleErr
  166. }
  167. return tx.Commit().Error
  168. }
  169. // 如果出错,没有 rollback,需自行 rollback
  170. func createOrgApp(tx *gorm.DB, superAdminRole *models.Role, userName string, appType int, open bool) error {
  171. openStatus := 0
  172. if open {
  173. openStatus = 1
  174. }
  175. now := time.Now().Unix()
  176. app := models.OrgApp{
  177. Creator: superAdminRole.Creator,
  178. OrgId: superAdminRole.OrgId,
  179. AppType: appType,
  180. OpenStatus: openStatus,
  181. Status: 1,
  182. CreateTime: now,
  183. ModifyTime: now,
  184. }
  185. if createAppErr := tx.Create(&app).Error; createAppErr != nil {
  186. return createAppErr
  187. }
  188. app_role := models.App_Role{
  189. AdminUserId: superAdminRole.Creator,
  190. OrgId: superAdminRole.OrgId,
  191. AppId: app.Id,
  192. RoleId: superAdminRole.Id,
  193. Avatar: "",
  194. UserName: userName,
  195. Intro: "",
  196. UserType: 1,
  197. UserTitle: 1,
  198. Status: 1,
  199. CreateTime: now,
  200. ModifyTime: now,
  201. }
  202. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  203. return createApp_RoleErr
  204. }
  205. return nil
  206. }
  207. func GetIllness() ([]*models.Illness, error) {
  208. var illness []*models.Illness
  209. err := readUserDb.Model(&models.Illness{}).Where("status = 1").Find(&illness).Error
  210. if err != nil {
  211. return nil, err
  212. } else {
  213. return illness, nil
  214. }
  215. }
  216. func GetOrgCategoriesByPid(pid int64) ([]*models.OrgType, error) {
  217. var cats []*models.OrgType
  218. err := readUserDb.Model(&models.OrgType{}).Where("pid = ? AND status = 1", pid).Order("sort_no asc").Find(&cats).Error
  219. if err != nil {
  220. return nil, err
  221. } else {
  222. return cats, nil
  223. }
  224. }
  225. func GetOrgServeSubscibe(orgID int) (*models.ServeSubscibe, error) {
  226. var model models.ServeSubscibe
  227. err := readUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? AND status = 1", orgID).First(&model).Error
  228. if err != nil {
  229. if err == gorm.ErrRecordNotFound {
  230. return nil, nil
  231. } else {
  232. return nil, err
  233. }
  234. }
  235. return &model, nil
  236. }
  237. func CreateOrgServeSubscibe(subscibe *models.ServeSubscibe) error {
  238. err := writeUserDb.Create(subscibe).Error
  239. return err
  240. }
  241. func GetOrgAppWithType(adminUserID int, orgID int, appType int) (*models.OrgApp, error) {
  242. var app models.OrgApp
  243. err := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND org_id = ? AND app_type = ? AND status = 1", adminUserID, orgID, appType).First(&app).Error
  244. if err == gorm.ErrRecordNotFound {
  245. err = nil
  246. }
  247. if err != nil {
  248. return nil, err
  249. } else {
  250. return &app, nil
  251. }
  252. }
  253. func SaveOrgApp(app *models.OrgApp) error {
  254. return writeUserDb.Save(app).Error
  255. }