home_api_controller.go 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package new_mobile_api_controllers
  2. import (
  3. "XT_New/controllers/mobile_api_controllers"
  4. "XT_New/enums"
  5. "XT_New/models"
  6. "XT_New/service"
  7. "XT_New/utils"
  8. "github.com/astaxie/beego"
  9. "strconv"
  10. "time"
  11. )
  12. type HomeController struct {
  13. NewMobileBaseAPIAuthController
  14. }
  15. func (this *HomeController) GetHomeData() {
  16. adminUserInfo := this.GetMobileAdminUserInfo()
  17. if adminUserInfo.Org != nil && adminUserInfo.Org.Id != 0 {
  18. //获取该管理员所有机构列表
  19. var orgs []*models.Org
  20. adminUser, err := service.GetHomeData(adminUserInfo.AdminUser.Id)
  21. if err != nil {
  22. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  23. return
  24. }
  25. for _, item := range adminUser.Org {
  26. orgs = append(orgs, item)
  27. }
  28. for _, item := range adminUser.VMApp_Role {
  29. for _, subItem := range item.Org {
  30. orgs = append(orgs, subItem)
  31. }
  32. }
  33. orgs = RemoveRepeatedOrgElement(orgs)
  34. apps, err := service.GetAllApp(adminUserInfo.Org.Id)
  35. if err != nil {
  36. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  37. return
  38. }
  39. banners, err := service.GetSystemBanner()
  40. if err != nil {
  41. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  42. return
  43. }
  44. this.ServeSuccessJSON(map[string]interface{}{
  45. "orgs": orgs,
  46. "apps": apps,
  47. "banners": banners,
  48. "isCreateOrg": true,
  49. })
  50. } else {
  51. apps, err := service.GetAllApp(0)
  52. if err != nil {
  53. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  54. return
  55. }
  56. banners, err := service.GetSystemBanner()
  57. if err != nil {
  58. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  59. return
  60. }
  61. this.ServeSuccessJSON(map[string]interface{}{
  62. "isCreateOrg": false,
  63. "apps": apps,
  64. "banners": banners,
  65. })
  66. }
  67. }
  68. func RemoveRepeatedOrgElement(orgs []*models.Org) (newOrgs []*models.Org) {
  69. newOrgs = make([]*models.Org, 0)
  70. for i := 0; i < len(orgs); i++ {
  71. repeat := false
  72. for j := i + 1; j < len(orgs); j++ {
  73. if orgs[i].Id == orgs[j].Id {
  74. repeat = true
  75. break
  76. }
  77. }
  78. if !repeat {
  79. newOrgs = append(newOrgs, orgs[i])
  80. }
  81. }
  82. return
  83. }
  84. func (this *HomeController) ChangeOrg() {
  85. org_id, _ := this.GetInt64("org_id")
  86. adminUserInfo := this.GetMobileAdminUserInfo()
  87. tempOrg, err := service.GetOrgById(org_id)
  88. if err != nil {
  89. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  90. return
  91. }
  92. if tempOrg == nil {
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeOrgNoExist)
  94. return
  95. }
  96. mobile := adminUserInfo.AdminUser.Mobile
  97. // 只取最近被创建的 admin_role
  98. adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile) //账号信息唯一值
  99. if getAdminErr != nil {
  100. utils.ErrorLog("获取管理员失败:%v", getAdminErr)
  101. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  102. this.ServeJSON()
  103. return
  104. } else if adminUser == nil {
  105. utils.ErrorLog("查找不到 mobile = %v 的用户", mobile)
  106. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  107. this.ServeJSON()
  108. return
  109. } else {
  110. var appRole *models.App_Role
  111. var org *models.Org
  112. var subscibe *models.ServeSubscibe
  113. var app *models.OrgApp
  114. //根据登录信息的机构和用户id,去获取对应用户信息和机构信息
  115. tempApp, _ := service.GetOrgApp(tempOrg.Id, 3)
  116. tempRole, _ := service.GetAppRole(tempOrg.Id, tempApp.Id, adminUser.Id)
  117. tempSubscibe, getSubscibeErr := service.GetOrgServeSubscibe(tempOrg.Id)
  118. if getSubscibeErr != nil {
  119. utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr)
  120. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  121. this.ServeJSON()
  122. return
  123. }
  124. subscibe = tempSubscibe
  125. org = tempOrg
  126. appRole = tempRole
  127. app = tempApp
  128. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  129. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  130. AdminUser: adminUser,
  131. Org: org,
  132. App: app,
  133. AppRole: appRole,
  134. Subscibe: subscibe,
  135. TemplateInfo: &templateInfo,
  136. }
  137. //删除session和cookie
  138. this.DelSession("mobile_admin_user_info")
  139. this.Ctx.SetCookie("token_cookie", "")
  140. //设置new seesion
  141. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  142. //设置new cookie
  143. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  144. token := utils.GenerateLoginToken(mobile)
  145. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  146. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  147. var configList interface{}
  148. var FiledList []*models.FiledConfig
  149. if org.Id > 0 {
  150. configList, _ = service.GetConfigList(org.Id)
  151. FiledList, _ = service.FindFiledByOrgId(org.Id)
  152. }
  153. if len(FiledList) == 0 {
  154. var err error
  155. if org.Id > 0 {
  156. err = service.BatchInsertFiledConfig(org.Id)
  157. if err == nil {
  158. FiledList, _ = service.FindFiledByOrgId(org.Id)
  159. } else {
  160. utils.ErrorLog("字段批量插入失败:%v", err)
  161. }
  162. } else {
  163. FiledList = make([]*models.FiledConfig, 0)
  164. }
  165. }
  166. this.ServeSuccessJSON(map[string]interface{}{
  167. "admin": adminUser,
  168. "user": appRole,
  169. "org": org,
  170. "template_info": map[string]interface{}{
  171. "id": templateInfo.ID,
  172. "org_id": templateInfo.OrgId,
  173. "template_id": templateInfo.TemplateId,
  174. },
  175. "config_list": configList,
  176. "filed_list": FiledList,
  177. })
  178. }
  179. }
  180. func (this *HomeController) CreateOrg() {
  181. adminUserInfo := this.GetMobileAdminUserInfo()
  182. adminUser := adminUserInfo.AdminUser
  183. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  184. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  185. // this.ServeJSON()
  186. // return
  187. //} else if didCreateOrg {
  188. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  189. // this.ServeJSON()
  190. // return
  191. //}
  192. name := this.GetString("org_name")
  193. shortName := name
  194. provinceName := this.GetString("provinces_name")
  195. cityName := this.GetString("city_name")
  196. districtName := this.GetString("district_name")
  197. address := this.GetString("address")
  198. org_type := this.GetString("org_type")
  199. contactName := this.GetString("contact_name")
  200. openXT := true
  201. openCDM := false
  202. openSCRM := false
  203. openMall := false
  204. if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || len(provinceName) <= 0 || len(cityName) <= 0 || len(districtName) <= 0 || len(org_type) <= 0 {
  205. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. this.ServeJSON()
  207. return
  208. }
  209. orgPhone := this.GetString("telephone")
  210. if len(orgPhone) > 0 {
  211. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  212. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  213. this.ServeJSON()
  214. return
  215. }
  216. }
  217. provinceID := 0
  218. cityID := 0
  219. districtID := 0
  220. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  221. if getProvinceErr != nil {
  222. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  223. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  224. this.ServeJSON()
  225. return
  226. } else if province != nil {
  227. provinceID = int(province.ID)
  228. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  229. if getCityErr != nil {
  230. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  231. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  232. this.ServeJSON()
  233. return
  234. } else if city != nil {
  235. cityID = int(city.ID)
  236. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  237. if getDistrictErr != nil {
  238. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  239. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  240. this.ServeJSON()
  241. return
  242. } else if district != nil {
  243. districtID = int(district.ID)
  244. }
  245. }
  246. }
  247. orgType := service.GetOrgTypeByName(org_type)
  248. org := &models.Org{
  249. Creator: adminUser.Id,
  250. OrgName: name,
  251. OrgShortName: shortName,
  252. Province: int64(provinceID),
  253. City: int64(cityID),
  254. District: int64(districtID),
  255. Address: address,
  256. OrgType: orgType.ID,
  257. Telephone: orgPhone,
  258. ContactName: contactName,
  259. Claim: 1,
  260. Evaluate: 5,
  261. Status: 1,
  262. CreateTime: time.Now().Unix(),
  263. ModifyTime: time.Now().Unix(),
  264. }
  265. createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  266. if createErr != nil {
  267. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  268. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  269. this.ServeJSON()
  270. } else {
  271. //初始化病人和排班相关数据
  272. InitPatientAndSchedule(org)
  273. //初始化透析方案
  274. InitSystemPrescrption(org)
  275. //初始化医嘱模版
  276. InitAdviceTemplate(org)
  277. //初始化角色和权限
  278. //初始化设备管理
  279. //初始化显示配置
  280. this.ServeSuccessJSON(map[string]interface{}{
  281. "org": org,
  282. })
  283. }
  284. }