sso

org_service.go 7.1KB

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