init_db.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package garden
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. "property-garden/errors"
  12. "property-garden/model"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. )
  16. func initTable(db *gorm.DB, dbname string) error {
  17. err := model.NewBuilding(dbname).CreateTable(db)
  18. if err != nil {
  19. return errors.DataBaseError
  20. }
  21. err = model.NewUnit(dbname).CreateTable(db)
  22. if err != nil {
  23. return errors.DataBaseError
  24. }
  25. err = model.NewHouse(dbname).CreateTable(db)
  26. if err != nil {
  27. return errors.DataBaseError
  28. }
  29. err = model.NewChargeConf(dbname).CreateTable(db)
  30. if err != nil {
  31. return errors.DataBaseError
  32. }
  33. err = model.NewChargeBind(dbname).CreateTable(db)
  34. if err != nil {
  35. return errors.DataBaseError
  36. }
  37. err = model.NewChargeBill(dbname).CreateTable(db)
  38. if err != nil {
  39. return errors.DataBaseError
  40. }
  41. err = model.NewChargeBillDel(dbname).CreateTable(db)
  42. if err != nil {
  43. return errors.DataBaseError
  44. }
  45. err = model.NewChargeBillPayed(dbname).CreateTable(db)
  46. if err != nil {
  47. return errors.DataBaseError
  48. }
  49. err = model.NewChargePower(dbname).CreateTable(db)
  50. if err != nil {
  51. return errors.DataBaseError
  52. }
  53. err = model.NewChargeUrge(dbname).CreateTable(db)
  54. if err != nil {
  55. return errors.DataBaseError
  56. }
  57. err = model.NewChargeOrder(dbname).CreateTable(db)
  58. if err != nil {
  59. return errors.DataBaseError
  60. }
  61. err = model.NewPark(dbname).CreateTable(db)
  62. if err != nil {
  63. return errors.DataBaseError
  64. }
  65. err = model.NewMsg(dbname).CreateTable(db)
  66. if err != nil {
  67. return errors.DataBaseError
  68. }
  69. err = model.NewParkSpace(dbname).CreateTable(db)
  70. if err != nil {
  71. return errors.DataBaseError
  72. }
  73. err = model.NewVehicle(dbname).CreateTable(db)
  74. if err != nil {
  75. return errors.DataBaseError
  76. }
  77. err = model.NewHouseApprovedGarden(dbname).CreateTable(db)
  78. if err != nil {
  79. return errors.DataBaseError
  80. }
  81. err = model.NewPropertyPackage(dbname).CreateTable(db)
  82. if err != nil {
  83. return errors.DataBaseError
  84. }
  85. err = model.NewRepairOrder(dbname).CreateTable(db)
  86. if err != nil {
  87. return errors.DataBaseError
  88. }
  89. err = model.NewSuggestionOrder(dbname).CreateTable(db)
  90. if err != nil {
  91. return errors.DataBaseError
  92. }
  93. err = model.NewRepairClass(dbname).CreateTable(db)
  94. if err != nil {
  95. return errors.DataBaseError
  96. }
  97. err = model.NewAnnouncement(dbname).CreateTable(db)
  98. if err != nil {
  99. return errors.DataBaseError
  100. }
  101. err = model.NewHouseRentManager(dbname).CreateTable(db)
  102. if err != nil {
  103. return errors.DataBaseError
  104. }
  105. err = model.NewHouseRent(dbname).CreateTable(db)
  106. if err != nil {
  107. return errors.DataBaseError
  108. }
  109. err = model.NewHouseholdUser(dbname).CreateTable(db)
  110. if err != nil {
  111. return errors.DataBaseError
  112. }
  113. err = model.NewEvent(dbname).CreateTable(db)
  114. if err != nil {
  115. return errors.DataBaseError
  116. }
  117. err = model.NewEventSign(dbname).CreateTable(db)
  118. if err != nil {
  119. return errors.DataBaseError
  120. }
  121. err = model.NewServicePhone(dbname).CreateTable(db)
  122. if err != nil {
  123. return errors.DataBaseError
  124. }
  125. err = model.NewServicePhoneClass(dbname).CreateTable(db)
  126. if err != nil {
  127. return errors.DataBaseError
  128. }
  129. err = model.NewStatisticObj(dbname).CreateTable(db)
  130. if err != nil {
  131. return errors.DataBaseError
  132. }
  133. err = model.NewStatisticRepair(dbname).CreateTable(db)
  134. if err != nil {
  135. return errors.DataBaseError
  136. }
  137. err = model.NewVote(dbname).CreateTable(db)
  138. if err != nil {
  139. return errors.DataBaseError
  140. }
  141. err = model.NewNeighborClass(dbname).CreateTable(db)
  142. if err != nil {
  143. return errors.DataBaseError
  144. }
  145. err = model.NewNeighborArticle(dbname).CreateTable(db)
  146. if err != nil {
  147. return errors.DataBaseError
  148. }
  149. err = model.NewNeighborComment(dbname).CreateTable(db)
  150. if err != nil {
  151. return errors.DataBaseError
  152. }
  153. err = model.NewNeighborLike(dbname).CreateTable(db)
  154. if err != nil {
  155. return errors.DataBaseError
  156. }
  157. err = model.NewBuildingManager(dbname).CreateTable(db)
  158. if err != nil {
  159. return errors.DataBaseError
  160. }
  161. return nil
  162. }
  163. func initView(db *gorm.DB, dbname string) error {
  164. err := model.NewBuildingUnit(dbname).CreateView(db, dbname)
  165. if err != nil {
  166. return errors.DataBaseError
  167. }
  168. err = model.NewHouseUnitBuilding(dbname).CreateView(db, dbname)
  169. if err != nil {
  170. return errors.DataBaseError
  171. }
  172. err = model.NewSpacePark(dbname).CreateView(db, dbname)
  173. if err != nil {
  174. return errors.DataBaseError
  175. }
  176. err = model.NewServicePhoneView(dbname).CreateView(db, dbname)
  177. if err != nil {
  178. return errors.DataBaseError
  179. }
  180. return nil
  181. }
  182. func initDb(gardenId int64) (err error) {
  183. dbname := utils.GetGardenDbName(gardenId)
  184. db := database.DB()
  185. // 创建库
  186. createDbSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';", dbname)
  187. err = db.Exec(createDbSql).Error
  188. if err != nil {
  189. return errors.DataBaseError
  190. }
  191. defer func() {
  192. if err != nil {
  193. fmt.Printf("drop database\n")
  194. sql := fmt.Sprintf("drop database %s", dbname)
  195. db.Exec(sql)
  196. }
  197. }()
  198. // 创建表
  199. err = initTable(db, dbname)
  200. if err != nil {
  201. return errors.DataBaseError
  202. }
  203. // 创建视图
  204. err = initView(db, dbname)
  205. if err != nil {
  206. return errors.DataBaseError
  207. }
  208. return nil
  209. }
  210. func GardenInitDb(ctx context.Context, req *pb_v1.GardenInitDbRequest) (reply *pb_v1.GardenInitDbReply, err error) {
  211. reply = &pb_v1.GardenInitDbReply{}
  212. // 捕获各个task中的异常并返回给调用者
  213. defer func() {
  214. if r := recover(); r != nil {
  215. err = fmt.Errorf("%+v", r)
  216. e := &status.Status{}
  217. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  218. logger.Error("err",
  219. zap.String("system_err", err.Error()),
  220. zap.Stack("stacktrace"))
  221. }
  222. }
  223. }()
  224. err = initDb(req.GardenId)
  225. if err != nil {
  226. return nil, err
  227. }
  228. return reply, nil
  229. }