orginfo_service.go 6.1KB

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