orginfo_service.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strconv"
  6. "time"
  7. )
  8. func GetOrgTypes() (ots []*models.OrgType, err error) {
  9. err = readUserDb.Model(&models.OrgType{}).Where("status=1").Order("sort_no").Find(&ots).Error
  10. return
  11. }
  12. func UpdateOrgInfo(org *models.Org) (err error) {
  13. err = writeUserDb.Model(&models.Org{}).Where("id=?", org.Id).Update(org).Error
  14. return
  15. }
  16. func GetIllnessList() (ills []*models.Illness, err error) {
  17. err = readUserDb.Where("status=1").Find(&ills).Error
  18. return
  19. }
  20. func CreateOrgGalleryItem(item *models.OrgGallery) (err error) {
  21. err = writeUserDb.Create(item).Error
  22. return
  23. }
  24. func DeleteOrgGalleryItem(id int64) (err error) {
  25. err = writeUserDb.Model(&models.OrgGallery{}).Where("id=?", id).Update(map[string]interface{}{"Status": 0, "Mtime": time.Now().Unix()}).Error
  26. return
  27. }
  28. func CreateOrg(org *models.Org, name string, openXT bool, openCDM bool, openSCRM bool, openMall bool) error {
  29. now := time.Now()
  30. tx_admin := writeUserDb.Begin()
  31. if err := tx_admin.Create(org).Error; err != nil {
  32. tx_admin.Rollback()
  33. return err
  34. }
  35. role := models.Role{
  36. RoleName: "超级管理员",
  37. RoleIntro: "",
  38. Creator: org.Creator,
  39. OrgId: org.Id,
  40. AppId: 0,
  41. IsSuperAdmin: true,
  42. Status: 1,
  43. CreateTime: now.Unix(),
  44. ModifyTime: now.Unix(),
  45. }
  46. if err := tx_admin.Create(&role).Error; err != nil {
  47. tx_admin.Rollback()
  48. return err
  49. }
  50. // app 的创建顺序也决定了登录后前往 app 的优先级
  51. if err := createOrgApp(tx_admin, &role, name, 3, openXT); err != nil {
  52. tx_admin.Rollback()
  53. return err
  54. }
  55. nextMonthDate := now.AddDate(0, 0, 30)
  56. subscibe := models.ServeSubscibe{
  57. OrgId: int64(org.Id),
  58. PeriodStart: now.Unix(),
  59. PeriodEnd: nextMonthDate.Unix(),
  60. Status: 1,
  61. CreatedTime: now.Unix(),
  62. UpdatedTime: now.Unix(),
  63. State: 9,
  64. }
  65. if err := tx_admin.Create(&subscibe).Error; err != nil {
  66. tx_admin.Rollback()
  67. return err
  68. }
  69. tx_patient := writeSgjPatientDb.Begin()
  70. 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 {
  71. tx_patient.Rollback()
  72. tx_admin.Rollback()
  73. return err
  74. }
  75. 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 {
  76. tx_patient.Rollback()
  77. tx_admin.Rollback()
  78. return err
  79. }
  80. 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 {
  81. tx_admin.Rollback()
  82. tx_patient.Rollback()
  83. return err
  84. }
  85. tx_admin.Commit()
  86. tx_patient.Commit()
  87. return nil
  88. }
  89. func createOrgApp(tx *gorm.DB, superAdminRole *models.Role, userName string, appType int, open bool) error {
  90. //创建应用
  91. now := time.Now().Unix()
  92. var app_id int64
  93. apps, _ := FindSystemOrgApp()
  94. for _, item := range apps {
  95. app := &VMOrgApp{
  96. AppType: item.AppType,
  97. Creator: int(superAdminRole.Creator),
  98. OrgId: int(superAdminRole.OrgId),
  99. OpenStatus: item.OpenStatus,
  100. Status: 1,
  101. CreateTime: now,
  102. ModifyTime: now,
  103. Name: item.Name,
  104. Url: item.Url,
  105. Number: item.Number,
  106. Pid: 0,
  107. }
  108. writeUserDb.Create(&app)
  109. if app.AppType == 3 {
  110. app_id = app.ID
  111. }
  112. for _, subItem := range item.VMOrgApp {
  113. app := &VMOrgApp{
  114. AppType: subItem.AppType,
  115. Creator: int(superAdminRole.Creator),
  116. OrgId: int(superAdminRole.OrgId),
  117. OpenStatus: subItem.OpenStatus,
  118. Status: 1,
  119. CreateTime: now,
  120. ModifyTime: now,
  121. Name: subItem.Name,
  122. Url: subItem.Url,
  123. Number: subItem.Number,
  124. Pid: app.ID,
  125. }
  126. writeUserDb.Create(&app)
  127. }
  128. }
  129. //string := strconv.FormatInt(int64,10 /)
  130. app_role := models.App_Role{
  131. AdminUserId: superAdminRole.Creator,
  132. OrgId: superAdminRole.OrgId,
  133. AppId: app_id,
  134. RoleId: superAdminRole.Id,
  135. RoleIds: strconv.FormatInt(superAdminRole.Id, 10),
  136. Avatar: "",
  137. UserName: userName,
  138. Intro: "",
  139. UserType: 1,
  140. UserTitle: 0,
  141. Status: 1,
  142. CreateTime: now,
  143. ModifyTime: now,
  144. }
  145. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  146. return createApp_RoleErr
  147. }
  148. return nil
  149. }
  150. func GetOrgTypeByName(org_type_name string) (org_type models.OrgType) {
  151. readUserDb.Model(&models.OrgType{}).Where("status = 1 AND name = ?", org_type_name).First(&org_type)
  152. return
  153. }
  154. type VMOrgApp struct {
  155. ID int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // ID
  156. AppType int `gorm:"column:app_type" json:"app_type"`
  157. Creator int `json:"creator"` // 创建者,即管理员用户的 id
  158. OrgId int `gorm:"column:org_id" json:"org_id"`
  159. OpenStatus int `gorm:"column:open_status" json:"open_status"`
  160. Status int8 `json:"status"` // 状态 0.无效 1.有效 2.禁用
  161. CreateTime int64 `gorm:"column:ctime" json:"ctime"` // 创建时间
  162. ModifyTime int64 `gorm:"column:mtime" json:"mtime"` // 修改时间
  163. Name string `gorm:"column:name" json:"name" form:"name"`
  164. Url string `gorm:"column:url" json:"url" form:"url"`
  165. Pid int64 `gorm:"column:pid" json:"pid" form:"pid"`
  166. Number int64 `gorm:"column:number" json:"order" form:"number"`
  167. VMOrgApp []*VMOrgApp `gorm:"ForeignKey:Pid;AssociationForeignKey:Id" `
  168. }
  169. func (VMOrgApp) TableName() string {
  170. return "sgj_user_org_app"
  171. }
  172. func FindSystemOrgApp() (app []*VMOrgApp, err error) {
  173. 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
  174. return
  175. }
  176. func GetAllOrgByAdminUserId() {
  177. }