mobile_regist_controller.go 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/astaxie/beego"
  12. "io/ioutil"
  13. "log"
  14. "net/http"
  15. "net/url"
  16. "os"
  17. "path"
  18. "regexp"
  19. "runtime"
  20. "strconv"
  21. "time"
  22. )
  23. type MobileRegistController struct {
  24. mobile_api_controllers.MobileBaseAPIController
  25. }
  26. // /mobile/regist [get]
  27. // /mobile/regist/submit [post]
  28. // @param mobile:string
  29. // @param password:string
  30. // @param code:string
  31. func (this *MobileRegistController) RegistSubmit() {
  32. mobile := this.GetString("mobile")
  33. pwd := this.GetString("password")
  34. code := this.GetString("code")
  35. // 判断手机号是否存在
  36. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  37. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  38. return
  39. }
  40. if len(pwd) == 0 {
  41. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  42. return
  43. }
  44. if len(code) == 0 {
  45. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  46. return
  47. }
  48. if service.IsMobileRegister(mobile) == true {
  49. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  50. return
  51. }
  52. if code == "13535547901" {
  53. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  54. if err != nil {
  55. this.ServeFailJSONWithSGJErrorCode(err.Code)
  56. return
  57. } else {
  58. this.Ctx.SetCookie("mobile", mobile)
  59. this.SetSession("mobile_admin_user", admin)
  60. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  61. "result": true,
  62. "id": admin.Id,
  63. })
  64. this.ServeJSON()
  65. }
  66. } else {
  67. redisClient := service.RedisClient()
  68. defer redisClient.Close()
  69. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  70. if cache_code != code {
  71. //this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  72. //this.ServeJSON()
  73. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  74. return
  75. }
  76. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  77. if err != nil {
  78. this.ServeFailJSONWithSGJErrorCode(err.Code)
  79. return
  80. } else {
  81. this.Ctx.SetCookie("mobile", mobile)
  82. this.SetSession("mobile_admin_user", admin)
  83. // 注册成功后验证码就要使其失效
  84. redisClient.Del("code_msg_" + mobile)
  85. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  86. "result": true,
  87. "id": admin.Id,
  88. })
  89. this.ServeJSON()
  90. }
  91. }
  92. }
  93. // /mobile/org/create/submit [post]
  94. // @param name:string
  95. // @param province:string 省名
  96. // @param city:string 市名
  97. // @param district:string 区县
  98. // @param address:string
  99. // @param category:int
  100. // @param contact_name:string
  101. // @param org_phone?:string
  102. // @param open_xt?:bool 是否开启血透系统
  103. // @param open_cdm?:bool 是否开启慢病系统
  104. // @param open_scrm?:bool 是否开启SCRM
  105. // @param open_mall?:bool 是否开启Mall
  106. func (this *MobileRegistController) CreateOrg() {
  107. adminUserObj := this.GetSession("mobile_admin_user")
  108. if adminUserObj == nil {
  109. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  110. this.ServeJSON()
  111. return
  112. }
  113. adminUser := adminUserObj.(*models.AdminUser)
  114. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  115. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  116. // this.ServeJSON()
  117. // return
  118. //} else if didCreateOrg {
  119. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  120. // this.ServeJSON()
  121. // return
  122. //}
  123. name := this.GetString("org_name")
  124. shortName := name
  125. provinceName := this.GetString("provinces_name")
  126. cityName := this.GetString("city_name")
  127. districtName := this.GetString("district_name")
  128. address := this.GetString("address")
  129. org_type := this.GetString("org_type")
  130. contactName := this.GetString("contact_name")
  131. //openXT, _ := this.GetBool("open_xt")
  132. //openCDM, _ := this.GetBool("open_cdm")
  133. //openSCRM, _ := this.GetBool("open_scrm")
  134. //openMall, _ := this.GetBool("open_mall")
  135. openXT := true
  136. openCDM := false
  137. openSCRM := false
  138. openMall := false
  139. 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 {
  140. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  141. this.ServeJSON()
  142. return
  143. }
  144. orgPhone := this.GetString("telephone")
  145. if len(orgPhone) > 0 {
  146. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  147. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  148. this.ServeJSON()
  149. return
  150. }
  151. }
  152. provinceID := 0
  153. cityID := 0
  154. districtID := 0
  155. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  156. if getProvinceErr != nil {
  157. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  158. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  159. this.ServeJSON()
  160. return
  161. } else if province != nil {
  162. provinceID = int(province.ID)
  163. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  164. if getCityErr != nil {
  165. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  166. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  167. this.ServeJSON()
  168. return
  169. } else if city != nil {
  170. cityID = int(city.ID)
  171. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  172. if getDistrictErr != nil {
  173. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  174. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  175. this.ServeJSON()
  176. return
  177. } else if district != nil {
  178. districtID = int(district.ID)
  179. }
  180. }
  181. }
  182. orgType := service.GetOrgTypeByName(org_type)
  183. org := &models.Org{
  184. Creator: adminUser.Id,
  185. OrgName: name,
  186. OrgShortName: shortName,
  187. Province: int64(provinceID),
  188. City: int64(cityID),
  189. District: int64(districtID),
  190. Address: address,
  191. OrgType: orgType.ID,
  192. Telephone: orgPhone,
  193. ContactName: contactName,
  194. Claim: 1,
  195. Evaluate: 5,
  196. Status: 1,
  197. CreateTime: time.Now().Unix(),
  198. ModifyTime: time.Now().Unix(),
  199. }
  200. createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  201. if createErr != nil {
  202. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  203. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  204. this.ServeJSON()
  205. } else {
  206. //初始化病人和排班相关数据
  207. InitPatientAndSchedule(org)
  208. //初始化透析方案
  209. InitSystemPrescrption(org)
  210. //初始化医嘱模版
  211. //InitAdviceTemplate(org)
  212. //初始化角色和权限
  213. InitRoleAndPurviews(org)
  214. //初始化设备管理org
  215. InitEquitMentInformation(org)
  216. //初始化显示配置
  217. //创建完机构后进行登录验证操作
  218. ip := utils.GetIP(this.Ctx.Request)
  219. ssoDomain := beego.AppConfig.String("sso_domain")
  220. api := ssoDomain + "/m/login/pwd"
  221. values := make(url.Values)
  222. values.Set("mobile", adminUser.Mobile)
  223. values.Set("password", adminUser.Password)
  224. values.Set("app_type", "3")
  225. values.Set("ip", ip)
  226. resp, requestErr := http.PostForm(api, values)
  227. if requestErr != nil {
  228. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  229. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  230. return
  231. }
  232. defer resp.Body.Close()
  233. body, ioErr := ioutil.ReadAll(resp.Body)
  234. if ioErr != nil {
  235. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  236. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  237. return
  238. }
  239. var respJSON map[string]interface{}
  240. utils.InfoLog(string(body))
  241. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  242. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  243. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  244. return
  245. }
  246. if respJSON["state"].(float64) != 1 {
  247. msg := respJSON["msg"].(string)
  248. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  249. if int(respJSON["code"].(float64)) == 609 {
  250. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  251. return
  252. }
  253. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  254. return
  255. } else {
  256. utils.SuccessLog("SSO登录成功")
  257. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  258. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  259. userJSONBytes, _ := json.Marshal(userJSON)
  260. var adminUser models.AdminUser
  261. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  262. utils.ErrorLog("解析管理员失败:%v", err)
  263. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  264. return
  265. }
  266. var org models.Org
  267. if respJSON["data"].(map[string]interface{})["org"] != nil {
  268. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  269. orgJSONBytes, _ := json.Marshal(orgJSON)
  270. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  271. utils.ErrorLog("解析机构失败:%v", err)
  272. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  273. return
  274. }
  275. }
  276. var app models.OrgApp
  277. if respJSON["data"].(map[string]interface{})["app"] != nil {
  278. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  279. appJSONBytes, _ := json.Marshal(appJSON)
  280. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  281. utils.ErrorLog("解析应用失败:%v", err)
  282. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  283. return
  284. }
  285. }
  286. var appRole models.App_Role
  287. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  288. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  289. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  290. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  291. utils.ErrorLog("解析AppRole失败:%v", err)
  292. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  293. return
  294. }
  295. }
  296. var subscibe models.ServeSubscibe
  297. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  298. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  299. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  300. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  301. utils.ErrorLog("解析Subscibe失败:%v", err)
  302. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  303. return
  304. }
  305. }
  306. //service.GetOrgSubscibeState(&subscibe)
  307. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  308. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  309. AdminUser: &adminUser,
  310. Org: &org,
  311. App: &app,
  312. AppRole: &appRole,
  313. Subscibe: &subscibe,
  314. TemplateInfo: &templateInfo,
  315. }
  316. //设置seesion
  317. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  318. //设置cookie
  319. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  320. token := utils.GenerateLoginToken(mobile)
  321. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  322. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  323. var configList interface{}
  324. var FiledList []*models.FiledConfig
  325. if org.Id > 0 {
  326. configList, _ = service.GetConfigList(org.Id)
  327. FiledList, _ = service.FindFiledByOrgId(org.Id)
  328. }
  329. if len(FiledList) == 0 {
  330. var err error
  331. if org.Id > 0 {
  332. err = service.BatchInsertFiledConfig(org.Id)
  333. if err == nil {
  334. FiledList, _ = service.FindFiledByOrgId(org.Id)
  335. } else {
  336. utils.ErrorLog("字段批量插入失败:%v", err)
  337. }
  338. } else {
  339. FiledList = make([]*models.FiledConfig, 0)
  340. }
  341. }
  342. this.ServeSuccessJSON(map[string]interface{}{
  343. "admin": adminUser,
  344. "user": appRole,
  345. "org": org,
  346. "template_info": map[string]interface{}{
  347. "id": templateInfo.ID,
  348. "org_id": templateInfo.OrgId,
  349. "template_id": templateInfo.TemplateId,
  350. },
  351. "config_list": configList,
  352. "filed_list": FiledList,
  353. })
  354. }
  355. }
  356. }
  357. func InitAdviceTemplate(org *models.Org) {
  358. //初始化医嘱模版
  359. adviceInit := &models.AdviceInit{
  360. UserOrgId: org.Id,
  361. CreateTime: time.Now().Unix(),
  362. UpdateTime: time.Now().Unix(),
  363. Status: 1,
  364. IsInit: 1,
  365. }
  366. adviceParentTemplate := LoadConfig("./advice_template.json").Parent_template
  367. for _, item := range adviceParentTemplate {
  368. parentTemplate := &models.DoctorAdviceParentTemplate{
  369. OrgId: org.Id,
  370. Name: item.Name,
  371. Status: 1,
  372. CreatedTime: time.Now().Unix(),
  373. UpdatedTime: time.Now().Unix(),
  374. AdviceType: item.AdviceType,
  375. }
  376. createErr := service.CreateDoctorParentTemplate(parentTemplate)
  377. fmt.Println(parentTemplate.ID)
  378. if createErr == nil {
  379. for _, adviceTemplateItem := range item.DoctorAdviceTemplate {
  380. adviceTeplate := &models.DoctorAdviceTemplate{
  381. OrgId: org.Id,
  382. AdviceName: adviceTemplateItem.AdviceName,
  383. AdviceDesc: adviceTemplateItem.AdviceDesc,
  384. SingleDose: adviceTemplateItem.SingleDose,
  385. SingleDoseUnit: adviceTemplateItem.SingleDoseUnit,
  386. PrescribingNumber: adviceTemplateItem.PrescribingNumber,
  387. PrescribingNumberUnit: adviceTemplateItem.PrescribingNumberUnit,
  388. DeliveryWay: adviceTemplateItem.DeliveryWay,
  389. ExecutionFrequency: adviceTemplateItem.ExecutionFrequency,
  390. AdviceDoctor: adviceTemplateItem.AdviceDoctor,
  391. Status: 1,
  392. CreatedTime: time.Now().Unix(),
  393. UpdatedTime: time.Now().Unix(),
  394. TemplateId: parentTemplate.ID,
  395. DrugSpec: adviceTemplateItem.DrugSpec,
  396. DrugSpecUnit: adviceTemplateItem.DrugSpecUnit,
  397. ParentId: adviceTemplateItem.ParentId,
  398. AdviceType: adviceTemplateItem.AdviceType,
  399. DayCount: adviceTemplateItem.DayCount,
  400. WeekDays: adviceTemplateItem.WeekDays,
  401. FrequencyType: adviceTemplateItem.FrequencyType,
  402. }
  403. createErr := service.CreateDoctorTemplate(adviceTeplate)
  404. if createErr == nil {
  405. for _, childItem := range adviceTemplateItem.SubDoctorAdviceTemplate {
  406. adviceTeplate := &models.DoctorAdviceTemplate{
  407. OrgId: org.Id,
  408. AdviceName: childItem.AdviceName,
  409. AdviceDesc: childItem.AdviceDesc,
  410. SingleDose: childItem.SingleDose,
  411. SingleDoseUnit: childItem.SingleDoseUnit,
  412. PrescribingNumber: childItem.PrescribingNumber,
  413. PrescribingNumberUnit: childItem.PrescribingNumberUnit,
  414. DeliveryWay: childItem.DeliveryWay,
  415. ExecutionFrequency: childItem.ExecutionFrequency,
  416. AdviceDoctor: childItem.AdviceDoctor,
  417. Status: 1,
  418. CreatedTime: time.Now().Unix(),
  419. UpdatedTime: time.Now().Unix(),
  420. TemplateId: parentTemplate.ID,
  421. DrugSpec: childItem.DrugSpec,
  422. DrugSpecUnit: childItem.DrugSpecUnit,
  423. ParentId: adviceTeplate.ID,
  424. AdviceType: childItem.AdviceType,
  425. DayCount: childItem.DayCount,
  426. WeekDays: childItem.WeekDays,
  427. FrequencyType: childItem.FrequencyType,
  428. }
  429. service.CreateDoctorTemplate(adviceTeplate)
  430. }
  431. }
  432. }
  433. }
  434. }
  435. service.CreateAdviceInitConfig(adviceInit)
  436. }
  437. func InitSystemPrescrption(org *models.Org) {
  438. //初始化透析方案
  439. prescriptions := LoadPrescriptionConfig("./system_dialysis_prescription.json").Prescription
  440. for _, item := range prescriptions {
  441. item.UserOrgId = org.Id
  442. item.CreatedTime = time.Now().Unix()
  443. item.UpdatedTime = time.Now().Unix()
  444. service.CreateVMPrescription(item)
  445. }
  446. }
  447. func InitPatientAndSchedule(org *models.Org) {
  448. var ids []int64
  449. //数据初始化
  450. //创建两个虚拟病人
  451. patients := LoadPatientConfig("./patient.json").Patients
  452. for _, item := range patients {
  453. item.UserOrgId = org.Id
  454. item.CreatedTime = time.Now().Unix()
  455. item.UpdatedTime = time.Now().Unix()
  456. item.Status = 1
  457. service.CreateVMOrgPatient(item)
  458. ids = append(ids, item.ID)
  459. }
  460. //创建两个血透的虚拟病人到新表
  461. fmt.Print("patients", patients)
  462. for _, it := range patients {
  463. patientsNew := models.XtPatientsNew{
  464. UserOrgId: org.Id,
  465. CreatedTime: time.Now().Unix(),
  466. UpdatedTime: time.Now().Unix(),
  467. Status: 1,
  468. BloodId: it.ID,
  469. BloodPatients: 1,
  470. Name: it.Name,
  471. Avatar: it.Avatar,
  472. PatientType: it.PatientType,
  473. Source: it.Source,
  474. Lapseto: it.Lapseto,
  475. Gender: it.Gender,
  476. Nation: it.Nation,
  477. NativePlace: it.NativePlace,
  478. MaritalStatus: it.MaritalStatus,
  479. IdCardNo: it.IdCardNo,
  480. Birthday: it.Birthday,
  481. ReimbursementWayId: it.ReimbursementWayId,
  482. HealthCareNo: it.HealthCareNo,
  483. Phone: it.Phone,
  484. HomeAddress: it.HomeAddress,
  485. WorkUnit: it.WorkUnit,
  486. UnitAddress: it.UnitAddress,
  487. Children: it.Children,
  488. IsHospitalFirstDialysis: it.IsHospitalFirstDialysis,
  489. FirstDialysisDate: it.FirstDialysisDate,
  490. BindingState: it.BindingState,
  491. PatientComplains: it.PatientComplains,
  492. PresentHistory: it.PresentHistory,
  493. PastHistory: it.PastHistory,
  494. Temperature: it.Temperature,
  495. Pulse: it.Pulse,
  496. Respiratory: it.Respiratory,
  497. Age: it.Age,
  498. IsOpenRemind: it.IsOpenRemind,
  499. TellPhone: it.TellPhone,
  500. FirstTreatmentDate: it.FirstTreatmentDate,
  501. ContactName: it.ContactName,
  502. }
  503. service.CreateVMOrgNewPatient(&patientsNew)
  504. }
  505. //创建1个分组
  506. vmGroup := &models.VMDeviceGroup{
  507. OrgId: org.Id,
  508. Name: "护理一组",
  509. Status: 1,
  510. Ctime: time.Now().Unix(),
  511. Mtime: time.Now().Unix(),
  512. }
  513. service.CreateVMGroup(vmGroup)
  514. //创建两个虚拟分区
  515. vmZone1 := &models.VMDeviceZone{
  516. OrgId: org.Id,
  517. Name: "A区",
  518. Type: 1,
  519. Status: 1,
  520. Ctime: time.Now().Unix(),
  521. Mtime: time.Now().Unix(),
  522. }
  523. service.CreateVMZone(vmZone1)
  524. vmZone2 := &models.VMDeviceZone{
  525. OrgId: org.Id,
  526. Name: "B区",
  527. Type: 2,
  528. Status: 1,
  529. Ctime: time.Now().Unix(),
  530. Mtime: time.Now().Unix(),
  531. }
  532. service.CreateVMZone(vmZone2)
  533. //创建2个虚拟床位
  534. vmDeviceNumber1 := &models.VMDeviceNumber{
  535. OrgId: org.Id,
  536. Number: "1号床",
  537. GroupId: vmGroup.ID,
  538. ZoneId: vmZone1.ID,
  539. Status: 1,
  540. Ctime: time.Now().Unix(),
  541. Mtime: time.Now().Unix(),
  542. }
  543. service.CreateVMDeviceNumber(vmDeviceNumber1)
  544. vmDeviceNumber2 := &models.VMDeviceNumber{
  545. OrgId: org.Id,
  546. Number: "2号床",
  547. GroupId: vmGroup.ID,
  548. ZoneId: vmZone2.ID,
  549. Status: 1,
  550. Ctime: time.Now().Unix(),
  551. Mtime: time.Now().Unix(),
  552. }
  553. service.CreateVMDeviceNumber(vmDeviceNumber2)
  554. var dates []int64
  555. mondayDate := GetDateOfWeek(1)
  556. tuesdayDate := GetDateOfWeek(2)
  557. wednesdayDate := GetDateOfWeek(3)
  558. thursdayDate := GetDateOfWeek(4)
  559. fridayDate := GetDateOfWeek(5)
  560. saturdayDate := GetDateOfWeek(6)
  561. sundayDate := GetDateOfWeek(7)
  562. dates = append(dates, mondayDate)
  563. dates = append(dates, tuesdayDate)
  564. dates = append(dates, wednesdayDate)
  565. dates = append(dates, thursdayDate)
  566. dates = append(dates, fridayDate)
  567. dates = append(dates, saturdayDate)
  568. dates = append(dates, sundayDate)
  569. //创建两个虚拟病人本周的排班
  570. //stime, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
  571. for week_type, date := range dates {
  572. for index, id := range ids {
  573. if index == 0 {
  574. sch := &models.VMSchedule{
  575. UserOrgId: org.Id,
  576. PartitionId: vmZone1.ID,
  577. BedId: vmDeviceNumber1.ID,
  578. PatientId: id,
  579. ScheduleDate: date,
  580. ScheduleType: 1,
  581. ScheduleWeek: int64(week_type + 1),
  582. ModeId: 1,
  583. Status: 1,
  584. CreatedTime: time.Now().Unix(),
  585. UpdatedTime: time.Now().Unix(),
  586. }
  587. service.CreateVMSch(sch)
  588. }
  589. if index == 1 {
  590. sch := &models.VMSchedule{
  591. UserOrgId: org.Id,
  592. PartitionId: vmZone2.ID,
  593. BedId: vmDeviceNumber2.ID,
  594. PatientId: id,
  595. ScheduleDate: date,
  596. ScheduleType: 2,
  597. ScheduleWeek: int64(week_type + 1),
  598. ModeId: 1,
  599. Status: 1,
  600. CreatedTime: time.Now().Unix(),
  601. UpdatedTime: time.Now().Unix(),
  602. }
  603. service.CreateVMSch(sch)
  604. }
  605. }
  606. }
  607. //创建单周模版
  608. //1.创建模版模式
  609. mode := &models.VMPatientScheduleTemplateMode{
  610. OrgId: org.Id,
  611. Mode: 1,
  612. ExecuteTimes: 0,
  613. Status: 1,
  614. Ctime: time.Now().Unix(),
  615. Mtime: time.Now().Unix(),
  616. }
  617. service.CreateVMSchMode(mode)
  618. //2.创建模版id
  619. templaeIdOne := &models.VMPatientScheduleTemplateId{
  620. OrgId: org.Id,
  621. Status: 1,
  622. Ctime: time.Now().Unix(),
  623. Mtime: time.Now().Unix(),
  624. }
  625. service.CreateVMSchTemplateId(templaeIdOne)
  626. templaeIdTwo := &models.VMPatientScheduleTemplateId{
  627. OrgId: org.Id,
  628. Status: 1,
  629. Ctime: time.Now().Unix(),
  630. Mtime: time.Now().Unix(),
  631. }
  632. service.CreateVMSchTemplateId(templaeIdTwo)
  633. //3.创建单周排班模版
  634. items := LoadSchTemplateConfig("./schedule_template.json").Item
  635. for _, item := range items {
  636. if item.TimeType == 1 {
  637. item.OrgId = org.Id
  638. item.TemplateId = templaeIdOne.ID
  639. item.DeviceNumberId = vmDeviceNumber1.ID
  640. item.PatientId = ids[0]
  641. item.Status = 1
  642. item.Ctime = time.Now().Unix()
  643. item.Mtime = time.Now().Unix()
  644. } else if item.TimeType == 2 {
  645. item.OrgId = org.Id
  646. item.TemplateId = templaeIdOne.ID
  647. item.DeviceNumberId = vmDeviceNumber2.ID
  648. item.PatientId = ids[1]
  649. item.Status = 1
  650. item.Ctime = time.Now().Unix()
  651. item.Mtime = time.Now().Unix()
  652. }
  653. service.CreateVMSchTemplate(item)
  654. }
  655. }
  656. func InitRoleAndPurviews(org *models.Org) {
  657. roles := LoadRoleConfig("./role.json").Roles
  658. app, _ := service.GetOrgApp(org.Id, 3)
  659. for _, item := range roles {
  660. role := &models.Role{
  661. RoleName: item.RoleName,
  662. RoleIntro: item.RoleIntroduction,
  663. Creator: 0,
  664. OrgId: org.Id,
  665. AppId: app.Id,
  666. IsSuperAdmin: false,
  667. Status: 1,
  668. CreateTime: time.Now().Unix(),
  669. ModifyTime: time.Now().Unix(),
  670. Number: item.Number,
  671. IsSystem: item.IsSystem,
  672. }
  673. err := service.CreateOrgRole(role)
  674. if err == nil {
  675. purview := &models.RolePurview{
  676. RoleId: role.Id,
  677. OrgId: org.Id,
  678. AppId: role.AppId,
  679. PurviewIds: item.PurviewIds,
  680. Status: 1,
  681. CreateTime: time.Now().Unix(),
  682. ModifyTime: time.Now().Unix(),
  683. }
  684. func_purview := &models.SgjUserRoleFuncPurview{
  685. RoleId: role.Id,
  686. OrgId: org.Id,
  687. AppId: role.AppId,
  688. PurviewIds: item.FuncIds,
  689. Status: 1,
  690. Ctime: time.Now().Unix(),
  691. Mtime: time.Now().Unix(),
  692. }
  693. service.CreateRolePurview(purview)
  694. service.CreateFuncRolePurview(func_purview)
  695. }
  696. }
  697. }
  698. func (this *MobileRegistController) ModifyName() {
  699. name := this.GetString("name")
  700. adminUserObj := this.GetSession("mobile_admin_user")
  701. adminUser := adminUserObj.(*models.AdminUser)
  702. err := service.ModifyAdminUserName(name, adminUser.Id)
  703. if err != nil {
  704. utils.ErrorLog("修改管理员名字失败:%v", err)
  705. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  706. this.ServeJSON()
  707. } else {
  708. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  709. this.ServeJSON()
  710. }
  711. }
  712. func (this *MobileRegistController) Login() {
  713. mobile := this.Ctx.GetCookie("mobile")
  714. adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile)
  715. if err != nil {
  716. utils.ErrorLog("获取管理信息失败:%v", err)
  717. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  718. this.ServeJSON()
  719. } else {
  720. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  721. AdminUser: adminUser,
  722. Org: nil,
  723. App: nil,
  724. AppRole: nil,
  725. Subscibe: nil,
  726. TemplateInfo: nil,
  727. }
  728. var org models.Org
  729. var user models.App_Role
  730. //设置seesion
  731. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  732. //设置cookie
  733. mobile = mobile + "-" + "0" + "-" + "0"
  734. token := utils.GenerateLoginToken(mobile)
  735. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  736. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  737. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  738. "admin": adminUser,
  739. "user": user,
  740. "org": org,
  741. "template_info": map[string]interface{}{
  742. "id": 0,
  743. "org_id": 0,
  744. "template_id": 0,
  745. },
  746. "config_list": nil,
  747. "filed_list": nil,
  748. })
  749. this.ServeJSON()
  750. }
  751. }
  752. type RoleConfig struct {
  753. Roles []*models.VMUserRoleAndPurview "json:roles"
  754. }
  755. type PatientConfig struct {
  756. Patients []*models.VMOrgPatients "json:patients"
  757. }
  758. type SchTemplateConfig struct {
  759. Item []*models.VMPatientScheduleTemplateItem "json:item"
  760. }
  761. type PrescriptionConfig struct {
  762. Prescription []*models.SystemPrescription "json:prescription"
  763. }
  764. func LoadRoleConfig(dataFile string) *RoleConfig {
  765. var config RoleConfig
  766. _, filename, _, _ := runtime.Caller(1)
  767. datapath := path.Join(path.Dir(filename), dataFile)
  768. config_file, err := os.Open(datapath)
  769. if err != nil {
  770. emit("Failed to open config file '%s': %s\n", datapath, err)
  771. return &config
  772. }
  773. fi, _ := config_file.Stat()
  774. buffer := make([]byte, fi.Size())
  775. _, err = config_file.Read(buffer)
  776. buffer, err = StripComments(buffer) //去掉注释
  777. if err != nil {
  778. emit("Failed to strip comments from json: %s\n", err)
  779. return &config
  780. }
  781. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  782. err = json.Unmarshal(buffer, &config) //解析json格式数据
  783. if err != nil {
  784. emit("Failed unmarshalling json: %s\n", err)
  785. return &config
  786. }
  787. return &config
  788. }
  789. func LoadPatientConfig(dataFile string) *PatientConfig {
  790. var config PatientConfig
  791. _, filename, _, _ := runtime.Caller(1)
  792. datapath := path.Join(path.Dir(filename), dataFile)
  793. config_file, err := os.Open(datapath)
  794. if err != nil {
  795. emit("Failed to open config file '%s': %s\n", datapath, err)
  796. return &config
  797. }
  798. fi, _ := config_file.Stat()
  799. buffer := make([]byte, fi.Size())
  800. _, err = config_file.Read(buffer)
  801. buffer, err = StripComments(buffer) //去掉注释
  802. if err != nil {
  803. emit("Failed to strip comments from json: %s\n", err)
  804. return &config
  805. }
  806. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  807. err = json.Unmarshal(buffer, &config) //解析json格式数据
  808. if err != nil {
  809. emit("Failed unmarshalling json: %s\n", err)
  810. return &config
  811. }
  812. return &config
  813. }
  814. func LoadSchTemplateConfig(dataFile string) *SchTemplateConfig {
  815. var config SchTemplateConfig
  816. _, filename, _, _ := runtime.Caller(1)
  817. datapath := path.Join(path.Dir(filename), dataFile)
  818. config_file, err := os.Open(datapath)
  819. if err != nil {
  820. emit("Failed to open config file '%s': %s\n", datapath, err)
  821. return &config
  822. }
  823. fi, _ := config_file.Stat()
  824. buffer := make([]byte, fi.Size())
  825. _, err = config_file.Read(buffer)
  826. buffer, err = StripComments(buffer) //去掉注释
  827. if err != nil {
  828. emit("Failed to strip comments from json: %s\n", err)
  829. return &config
  830. }
  831. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  832. err = json.Unmarshal(buffer, &config) //解析json格式数据
  833. if err != nil {
  834. emit("Failed unmarshalling json: %s\n", err)
  835. return &config
  836. }
  837. return &config
  838. }
  839. func LoadPrescriptionConfig(dataFile string) *PrescriptionConfig {
  840. var config PrescriptionConfig
  841. _, filename, _, _ := runtime.Caller(1)
  842. datapath := path.Join(path.Dir(filename), dataFile)
  843. config_file, err := os.Open(datapath)
  844. if err != nil {
  845. emit("Failed to open config file '%s': %s\n", datapath, err)
  846. return &config
  847. }
  848. fi, _ := config_file.Stat()
  849. buffer := make([]byte, fi.Size())
  850. _, err = config_file.Read(buffer)
  851. buffer, err = StripComments(buffer) //去掉注释
  852. if err != nil {
  853. emit("Failed to strip comments from json: %s\n", err)
  854. return &config
  855. }
  856. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  857. err = json.Unmarshal(buffer, &config) //解析json格式数据
  858. if err != nil {
  859. emit("Failed unmarshalling json: %s\n", err)
  860. return &config
  861. }
  862. return &config
  863. }
  864. func emit(msgfmt string, args ...interface{}) {
  865. log.Printf(msgfmt, args...)
  866. }
  867. func StripComments(data []byte) ([]byte, error) {
  868. data = bytes.Replace(data, []byte("\r"), []byte(""), 0) // Windows
  869. lines := bytes.Split(data, []byte("\n")) //split to muli lines
  870. filtered := make([][]byte, 0)
  871. for _, line := range lines {
  872. match, err := regexp.Match(`^\s*#`, line)
  873. if err != nil {
  874. return nil, err
  875. }
  876. if !match {
  877. filtered = append(filtered, line)
  878. }
  879. }
  880. return bytes.Join(filtered, []byte("\n")), nil
  881. }
  882. func GetDateOfWeek(week_type int) (weekMonday int64) {
  883. thisTime := time.Now()
  884. weekDay := int(thisTime.Weekday())
  885. if weekDay == 0 {
  886. weekDay = 7
  887. }
  888. weekEnd := 7 - weekDay
  889. weekStart := weekEnd - 6
  890. weekTitle := make([]string, 0)
  891. days := make([]string, 0)
  892. for index := weekStart; index <= weekEnd; index++ {
  893. theDay := thisTime.AddDate(0, 0, index)
  894. indexYear, indexMonthTime, indexDay := theDay.Date()
  895. indexMonth := int(indexMonthTime)
  896. indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay)
  897. weekTitle = append(weekTitle, indexWeek)
  898. days = append(days, theDay.Format("2006-01-02"))
  899. }
  900. var targetDayStr string
  901. switch week_type {
  902. case 1:
  903. targetDayStr = days[0]
  904. break
  905. case 2:
  906. targetDayStr = days[1]
  907. break
  908. case 3:
  909. targetDayStr = days[2]
  910. break
  911. case 4:
  912. targetDayStr = days[3]
  913. break
  914. case 5:
  915. targetDayStr = days[4]
  916. break
  917. case 6:
  918. targetDayStr = days[5]
  919. break
  920. case 7:
  921. targetDayStr = days[6]
  922. break
  923. }
  924. targetDay, _ := utils.ParseTimeStringToTime("2006-01-02", targetDayStr)
  925. return targetDay.Unix()
  926. }
  927. type Config struct {
  928. Parent_template []*models.VMDoctorAdviceParentTemplate "json:parent_template"
  929. }
  930. func LoadConfig(dataFile string) *Config {
  931. var config Config
  932. _, filename, _, _ := runtime.Caller(1)
  933. datapath := path.Join(path.Dir(filename), dataFile)
  934. config_file, err := os.Open(datapath)
  935. if err != nil {
  936. emit("Failed to open config file '%s': %s\n", datapath, err)
  937. return &config
  938. }
  939. fi, _ := config_file.Stat()
  940. buffer := make([]byte, fi.Size())
  941. _, err = config_file.Read(buffer)
  942. buffer, err = StripComments(buffer) //去掉注释
  943. if err != nil {
  944. emit("Failed to strip comments from json: %s\n", err)
  945. return &config
  946. }
  947. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  948. err = json.Unmarshal(buffer, &config) //解析json格式数据
  949. if err != nil {
  950. emit("Failed unmarshalling json: %s\n", err)
  951. return &config
  952. }
  953. return &config
  954. }
  955. func InitEquitMentInformation(org *models.Org) {
  956. //添加设备型号
  957. mode := models.VMDeviceMode{
  958. DeviceMode: "5008S",
  959. Status: 1,
  960. UserOrgId: org.Id,
  961. Ctime: time.Now().Unix(),
  962. Mtime: time.Now().Unix(),
  963. }
  964. service.CreatedDeviceMode(&mode)
  965. deviceMode := models.VMDeviceMode{
  966. DeviceMode: "4008S",
  967. Status: 1,
  968. UserOrgId: org.Id,
  969. Ctime: time.Now().Unix(),
  970. Mtime: time.Now().Unix(),
  971. }
  972. service.CreatedDeviceMode(&deviceMode)
  973. firstDeviceMode, _ := service.GetFirstDeviceMode(org.Id)
  974. //获取机构下对应的第一数据
  975. number, _ := service.GetFirstBedNumber(org.Id)
  976. //添加设备
  977. deviceAddmacher := models.VmDeviceAddmacher{
  978. SerialNumber: "8VSAHE13",
  979. DeviceType: 1,
  980. BedId: number.ID,
  981. DeviceName: "费森尤斯",
  982. UnitType: firstDeviceMode.ID,
  983. MachineStatus: 1,
  984. Status: 1,
  985. UserOrgId: org.Id,
  986. Ctime: time.Now().Unix(),
  987. }
  988. service.CreateDeviceAddMacher(&deviceAddmacher)
  989. macher, _ := service.GetLastMacher(org.Id)
  990. fmt.Print(macher.ID)
  991. treatmentmode := models.VmDeviceTreatmentmode{
  992. MachineId: macher.ID,
  993. Status: 1,
  994. Ctime: time.Now().Unix(),
  995. Mtime: time.Now().Unix(),
  996. UserOrgId: org.Id,
  997. TreateMode: 1,
  998. }
  999. service.CreatedTreateMode(&treatmentmode)
  1000. treatmentmodeone := models.VmDeviceTreatmentmode{
  1001. MachineId: macher.ID,
  1002. Status: 1,
  1003. Ctime: time.Now().Unix(),
  1004. Mtime: time.Now().Unix(),
  1005. UserOrgId: org.Id,
  1006. TreateMode: 2,
  1007. }
  1008. service.CreatedTreateMode(&treatmentmodeone)
  1009. treatmentmodetwo := models.VmDeviceTreatmentmode{
  1010. MachineId: macher.ID,
  1011. Status: 1,
  1012. Ctime: time.Now().Unix(),
  1013. Mtime: time.Now().Unix(),
  1014. UserOrgId: org.Id,
  1015. TreateMode: 3,
  1016. }
  1017. service.CreatedTreateMode(&treatmentmodetwo)
  1018. treatmentmodethree := models.VmDeviceTreatmentmode{
  1019. MachineId: macher.ID,
  1020. Status: 1,
  1021. Ctime: time.Now().Unix(),
  1022. Mtime: time.Now().Unix(),
  1023. UserOrgId: org.Id,
  1024. TreateMode: 4,
  1025. }
  1026. service.CreatedTreateMode(&treatmentmodethree)
  1027. treatmentmodefour := models.VmDeviceTreatmentmode{
  1028. MachineId: macher.ID,
  1029. Status: 1,
  1030. Ctime: time.Now().Unix(),
  1031. Mtime: time.Now().Unix(),
  1032. UserOrgId: org.Id,
  1033. TreateMode: 5,
  1034. }
  1035. service.CreatedTreateMode(&treatmentmodefour)
  1036. treatmentmodefive := models.VmDeviceTreatmentmode{
  1037. MachineId: macher.ID,
  1038. Status: 1,
  1039. Ctime: time.Now().Unix(),
  1040. Mtime: time.Now().Unix(),
  1041. UserOrgId: org.Id,
  1042. TreateMode: 12,
  1043. }
  1044. service.CreatedTreateMode(&treatmentmodefive)
  1045. }