mobile_regist_controller.go 34KB

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