package new_mobile_api_controllers import ( "XT_New/controllers/mobile_api_controllers" "XT_New/enums" "XT_New/models" "XT_New/service" "XT_New/utils" "github.com/astaxie/beego" "strconv" "time" ) type HomeController struct { NewMobileBaseAPIAuthController } func (this *HomeController) GetHomeData() { adminUserInfo := this.GetMobileAdminUserInfo() if adminUserInfo.Org != nil && adminUserInfo.Org.Id != 0 { //获取该管理员所有机构列表 var orgs []*models.Org adminUser, err := service.GetHomeData(adminUserInfo.AdminUser.Id) if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } for _, item := range adminUser.Org { orgs = append(orgs, item) } for _, item := range adminUser.VMApp_Role { for _, subItem := range item.Org { orgs = append(orgs, subItem) } } orgs = RemoveRepeatedOrgElement(orgs) apps, err := service.GetAllApp(adminUserInfo.Org.Id) if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } banners, err := service.GetSystemBanner() if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } this.ServeSuccessJSON(map[string]interface{}{ "orgs": orgs, "apps": apps, "banners": banners, "isCreateOrg": true, }) } else { apps, err := service.GetAllApp(0) if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } banners, err := service.GetSystemBanner() if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } this.ServeSuccessJSON(map[string]interface{}{ "isCreateOrg": false, "apps": apps, "banners": banners, }) } } func RemoveRepeatedOrgElement(orgs []*models.Org) (newOrgs []*models.Org) { newOrgs = make([]*models.Org, 0) for i := 0; i < len(orgs); i++ { repeat := false for j := i + 1; j < len(orgs); j++ { if orgs[i].Id == orgs[j].Id { repeat = true break } } if !repeat { newOrgs = append(newOrgs, orgs[i]) } } return } func (this *HomeController) ChangeOrg() { org_id, _ := this.GetInt64("org_id") adminUserInfo := this.GetMobileAdminUserInfo() tempOrg, err := service.GetOrgById(org_id) if err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError) return } if tempOrg == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeOrgNoExist) return } mobile := adminUserInfo.AdminUser.Mobile // 只取最近被创建的 admin_role adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile) //账号信息唯一值 if getAdminErr != nil { utils.ErrorLog("获取管理员失败:%v", getAdminErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) this.ServeJSON() return } else if adminUser == nil { utils.ErrorLog("查找不到 mobile = %v 的用户", mobile) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong) this.ServeJSON() return } else { var appRole *models.App_Role var org *models.Org var subscibe *models.ServeSubscibe var app *models.OrgApp //根据登录信息的机构和用户id,去获取对应用户信息和机构信息 tempApp, _ := service.GetOrgApp(tempOrg.Id, 3) tempRole, _ := service.GetAppRole(tempOrg.Id, tempApp.Id, adminUser.Id) tempSubscibe, getSubscibeErr := service.GetOrgServeSubscibe(tempOrg.Id) if getSubscibeErr != nil { utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) this.ServeJSON() return } subscibe = tempSubscibe org = tempOrg appRole = tempRole app = tempApp templateInfo, _ := service.GetOrgInfoTemplate(org.Id) mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{ AdminUser: adminUser, Org: org, App: app, AppRole: appRole, Subscibe: subscibe, TemplateInfo: &templateInfo, } //删除session和cookie this.DelSession("mobile_admin_user_info") this.Ctx.SetCookie("token_cookie", "") //设置new seesion this.SetSession("mobile_admin_user_info", mobileAdminUserInfo) //设置new cookie mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10) token := utils.GenerateLoginToken(mobile) expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second") this.Ctx.SetCookie("token_cookie", token, expiration, "/") var configList interface{} var FiledList []*models.FiledConfig if org.Id > 0 { configList, _ = service.GetConfigList(org.Id) FiledList, _ = service.FindFiledByOrgId(org.Id) } if len(FiledList) == 0 { var err error if org.Id > 0 { err = service.BatchInsertFiledConfig(org.Id) if err == nil { FiledList, _ = service.FindFiledByOrgId(org.Id) } else { utils.ErrorLog("字段批量插入失败:%v", err) } } else { FiledList = make([]*models.FiledConfig, 0) } } this.ServeSuccessJSON(map[string]interface{}{ "admin": adminUser, "user": appRole, "org": org, "template_info": map[string]interface{}{ "id": templateInfo.ID, "org_id": templateInfo.OrgId, "template_id": templateInfo.TemplateId, }, "config_list": configList, "filed_list": FiledList, }) } } func (this *HomeController) CreateOrg() { adminUserInfo := this.GetMobileAdminUserInfo() adminUser := adminUserInfo.AdminUser //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil { // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) // this.ServeJSON() // return //} else if didCreateOrg { // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg) // this.ServeJSON() // return //} name := this.GetString("org_name") shortName := name provinceName := this.GetString("provinces_name") cityName := this.GetString("city_name") districtName := this.GetString("district_name") address := this.GetString("address") org_type := this.GetString("org_type") contactName := this.GetString("contact_name") openXT := true openCDM := false openSCRM := false openMall := false 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 { this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) this.ServeJSON() return } orgPhone := this.GetString("telephone") if len(orgPhone) > 0 { if utils.PhoneRegexp().MatchString(orgPhone) == false { this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) this.ServeJSON() return } } provinceID := 0 cityID := 0 districtID := 0 province, getProvinceErr := service.GetProvinceWithName(provinceName) if getProvinceErr != nil { utils.ErrorLog("查询省名失败:%v", getProvinceErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) this.ServeJSON() return } else if province != nil { provinceID = int(province.ID) city, getCityErr := service.GetCityWithName(province.ID, cityName) if getCityErr != nil { utils.ErrorLog("查询城市名失败:%v", getCityErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) this.ServeJSON() return } else if city != nil { cityID = int(city.ID) district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName) if getDistrictErr != nil { utils.ErrorLog("查询区县名失败:%v", getDistrictErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException) this.ServeJSON() return } else if district != nil { districtID = int(district.ID) } } } orgType := service.GetOrgTypeByName(org_type) org := &models.Org{ Creator: adminUser.Id, OrgName: name, OrgShortName: shortName, Province: int64(provinceID), City: int64(cityID), District: int64(districtID), Address: address, OrgType: orgType.ID, Telephone: orgPhone, ContactName: contactName, Claim: 1, Evaluate: 5, Status: 1, CreateTime: time.Now().Unix(), ModifyTime: time.Now().Unix(), } createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上 if createErr != nil { utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr) this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate) this.ServeJSON() } else { //初始化病人和排班相关数据 InitPatientAndSchedule(org) //初始化透析方案 InitSystemPrescrption(org) //初始化医嘱模版 InitAdviceTemplate(org) //初始化角色和权限 //初始化设备管理 //初始化显示配置 this.ServeSuccessJSON(map[string]interface{}{ "org": org, }) } }