package controllers import ( "XT_New/enums" "XT_New/models" "XT_New/service" "XT_New/utils" "encoding/json" "fmt" "github.com/astaxie/beego" "github.com/jinzhu/gorm" "strings" "time" ) type SecondaryOrderApiController struct { BaseAuthAPIController } func SecondaryOrderApiRegistRouters() { beego.Router("/api/secondary/getcode", &SecondaryOrderApiController{}, "get:GetStoreCode") //获取仓库编码 beego.Router("/api/secondary/updatestatus", &SecondaryOrderApiController{}, "get:UpdateStatus") //修改仓库状态 beego.Router("/api/secondary/deletestorehouse", &SecondaryOrderApiController{}, "get:DeleteStorehouse") //删除仓库 beego.Router("/api/secondary/isstorehousename", &SecondaryOrderApiController{}, "get:IsStorehouseName") //仓库名称是否重复 beego.Router("/api/secondary/isstorehouseaddress", &SecondaryOrderApiController{}, "get:IsStorehouseAddress") //仓库地址是否重复 beego.Router("/api/secondary/storehouselist", &SecondaryOrderApiController{}, "get:StorehouseList") //分页 beego.Router("/api/secondary/addstorehouse", &SecondaryOrderApiController{}, "post:AddStorehouse") //新增仓库 beego.Router("/api/secondary/updatestorehouse", &SecondaryOrderApiController{}, "post:UpdateStorehouse") //修改 beego.Router("/api/secondary/getonestorehouse", &SecondaryOrderApiController{}, "get:GetOneStorehouse") //查一条仓库的信息 beego.Router("/api/secondary/getallstorehousename", &SecondaryOrderApiController{}, "get:GetAllStorehouseName") //获取当前机构的所有可用仓库名称 beego.Router("/api/secondary/findstorehouseconfig", &SecondaryOrderApiController{}, "get:FindStorehouseConfig") //查询该机构的仓库配置 beego.Router("/api/secondary/updateinfo", &SecondaryOrderApiController{}, "get:UpdateInfo") //更改耗材自动入库仓库 beego.Router("/api/secondary/updateoutinfo", &SecondaryOrderApiController{}, "get:UpdateOutInfo") //更改耗材自动出库仓库 beego.Router("/api/secondary/updatedruginfo", &SecondaryOrderApiController{}, "get:UpdateDrugInfo") //更改药品自动入库仓库 beego.Router("/api/secondary/updatedrugout", &SecondaryOrderApiController{}, "get:UpdateDrugOut") //更改药品自动出库仓库 beego.Router("/api/secondary/getusername", &SecondaryOrderApiController{}, "get:GetuserName") //获取仓库管理员信息 beego.Router("/api/secondary/byliinit", &SecondaryOrderApiController{}, "get:Byliinit") //初始化旧数据 beego.Router("/api/secondary/getcreaterid", &SecondaryOrderApiController{}, "get:GetCreaterId") //获取当前登录的人的id } //获取仓库编码 func (this *SecondaryOrderApiController) GetStoreCode() { orgId := this.GetAdminUserInfo().CurrentOrgId var code string for a := true; a == true; { code = service.CreateCode() tmp := service.FindStorehouseCode(orgId, code) //如果没有重复的编码结束循环 if tmp == false { a = false } } this.ServeSuccessJSON(map[string]interface{}{ "list": code, }) return } //修改仓库状态 func (this *SecondaryOrderApiController) UpdateStatus() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") //判断该仓库的库存是否为零 boolean := service.IsStorehouseNil(id, orgId) if boolean == false { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库库存不为0,不支持该操作") return } //判断该仓库是否在仓库配置表中 boolean = service.IsInConfig(orgId, id) if boolean == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前仓库是自动出入库仓库,请先取消自动出入库之后才能进行此操作") return } //修改仓库状态 err = service.UpdateStorehouseStatus(id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "修改成功", }) return } //删除仓库 func (this *SecondaryOrderApiController) DeleteStorehouse() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") //判断该仓库的库存是否为零 boolean := service.IsStorehouseNil(id, orgId) if boolean == false { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库库存不为0,不支持该操作") return } //判断该仓库是否在仓库配置表中 boolean = service.IsInConfig(orgId, id) if boolean == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前仓库是自动出入库仓库,请先取消自动出入库之后才能进行此操作") return } err = service.DeleteStorehouse(id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "删除成功", }) return } //仓库名称是否重复 func (this *SecondaryOrderApiController) IsStorehouseName() { orgId := this.GetAdminUserInfo().CurrentOrgId storehouse_name := this.GetString("storehouse_name") check := map[string][]string{ "storehouse_name": {"must", "string", "storehouse_name"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } var bo bool bo, err = service.IsStorehouseName(orgId, storehouse_name) if bo == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库已存在,请重新输入") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "ok", }) return } //仓库地址是否重复 func (this *SecondaryOrderApiController) IsStorehouseAddress() { orgId := this.GetAdminUserInfo().CurrentOrgId storehouse_address := this.GetString("storehouse_address") check := map[string][]string{ "storehouse_address": {"must", "string", "storehouse_address"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } var bo bool bo, err = service.IsStorehouseAddress(orgId, storehouse_address) if bo == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该地址已存在,请重新输入") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "ok", }) return } //分页 func (this *SecondaryOrderApiController) StorehouseList() { adminUserInfo := this.GetAdminUserInfo() page, _ := this.GetInt64("page") //页码 limit, _ := this.GetInt64("limit") //每一页查出来的条数 check := map[string][]string{ "page": {"must", "string", "page"}, "limit": {"must", "string", "limit"}, } _, err := checkParams(this, &check) keyword := this.GetString("keyword") orgId := this.GetAdminUserInfo().CurrentOrgId if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) } namemap := make(map[int64]string) //根据管理员id获取管理员 viewModels, _, _ := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100) for _, v := range viewModels { namemap[int64(v.AdminUserId)] = v.UserName } slicekey := make([]int64, 0) if len(keyword) > 0 { for k, v := range namemap { res := strings.Contains(v, keyword) if res == true { slicekey = append(slicekey, k) } } } //获取分页的数据 list, total, err := service.StorehouseList(page, limit, orgId, keyword, slicekey) //分页 type Storehouselist struct { ID int64 StorehouseCode string //仓库编号 StorehouseName string //仓库名称 StorehouseAddress string //仓库地址 StorehouseStatus int64 //仓库状态 Status int64 //数据状态 StorehouseAdminId int64 //仓库管理员id StorehouseAdminName string //仓库管理员名字 UserOrgId int64 } //初始化该结构体 tmplist := []*Storehouselist{} for i := 0; i < len(list); i++ { tlist := &Storehouselist{ list[i].ID, list[i].StorehouseCode, list[i].StorehouseName, list[i].StorehouseAddress, list[i].StorehouseStatus, list[i].Status, list[i].StorehouseAdminId, "", list[i].UserOrgId, } tmplist = append(tmplist, tlist) } for _, v := range tmplist { if k, ok := namemap[v.StorehouseAdminId]; ok { v.StorehouseAdminName = k } else { v.StorehouseAdminName = "超级管理员" } } this.ServeSuccessJSON(map[string]interface{}{ "list": tmplist, "total": total, }) } //新增仓库 func (this *SecondaryOrderApiController) AddStorehouse() { orgId := this.GetAdminUserInfo().CurrentOrgId dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } var storehouse_status, admin_id int64 tmpstatus := dataBody["storehouse_status"] tmpid := dataBody["storehouse_admin_id"] //管理员id if tmpstatus == nil { storehouse_status = 1 } else { storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态 } if tmpid == nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "管理员id不能为空") return } else { admin_id = int64(dataBody["storehouse_admin_id"].(float64)) //管理员id } switch { case dataBody["storehouse_code"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库编号不能为空") return case dataBody["storehouse_name"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空") return case dataBody["storehouse_address"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空") return } code := dataBody["storehouse_code"].(string) //仓库编号 name := dataBody["storehouse_name"].(string) //仓库名称 address := dataBody["storehouse_address"].(string) //地址 switch { case name == "": this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空") return case address == "": this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空") return } //判断仓库名称是否重复 var bo bool bo, err = service.IsStorehouseName(orgId, name) if bo == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库已存在,请重新输入") return } //判断仓库地址是否重复 bo, err = service.IsStorehouseAddress(orgId, address) if bo == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该地址已存在,请重新输入") return } storehouse := models.Storehouse{ StorehouseCode: code, StorehouseName: name, StorehouseAddress: address, StorehouseStatus: storehouse_status, UserOrgId: orgId, Status: 1, StorehouseAdminId: admin_id, Ctime: time.Now().Unix(), } err = service.AddStroehouse(storehouse) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "保存失败") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "保存成功", }) return } //修改仓库 func (this *SecondaryOrderApiController) UpdateStorehouse() { orgId := this.GetAdminUserInfo().CurrentOrgId dataBody := make(map[string]interface{}, 0) //orgId := this.GetAdminUserInfo().CurrentOrgId err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } var storehouse_status, admin_id int64 tmpstatus := dataBody["storehouse_status"] tmpid := dataBody["storehouse_admin_id"] if tmpstatus == nil { storehouse_status = 1 } else { storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态 } if tmpid == nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "管理员id不能为空") return } else { admin_id = int64(dataBody["storehouse_admin_id"].(float64)) //管理员id } switch { case dataBody["id"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库id不能为空") return case dataBody["storehouse_name"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空") return case dataBody["storehouse_address"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空") return } id := int64(dataBody["id"].(float64)) name := dataBody["storehouse_name"].(string) //仓库名称 address := dataBody["storehouse_address"].(string) //地址 //查询当前仓库状态,根据当前状态判断是否需要更改 list, errs := service.GetOneStorehouse(id, orgId) if errs != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } if storehouse_status != list.StorehouseStatus && storehouse_status == 0 { //判断该仓库的库存是否为零 boolean := service.IsStorehouseNil(id, orgId) if boolean == false { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库库存不为0,不支持该操作") return } //判断该仓库是否在仓库配置表中 boolean = service.IsInConfig(orgId, id) if boolean == true { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前仓库是自动出入库仓库,请先取消自动出入库之后才能进行此操作") return } } storehouse := models.Storehouse{ ID: id, StorehouseName: name, StorehouseAddress: address, StorehouseStatus: storehouse_status, StorehouseAdminId: admin_id, Mtime: time.Now().Unix(), } err = service.UpdateStroehouse(storehouse) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "保存失败") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "保存成功", }) return } //查询一条仓库信息 func (this *SecondaryOrderApiController) GetOneStorehouse() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") var list models.Storehouse list, err = service.GetOneStorehouse(id, orgId) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": list, }) return } //获取当前机构所有可用仓库的名字 func (this *SecondaryOrderApiController) GetAllStorehouseName() { orgId := this.GetAdminUserInfo().CurrentOrgId list, err := service.GetAllStorehouseName(orgId) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": list, }) } //根据机构id查询仓库配置 func (this *SecondaryOrderApiController) FindStorehouseConfig() { orgId := this.GetAdminUserInfo().CurrentOrgId storehouse, err := service.FindStorehouseConfig(orgId) //如果没有仓库配置信息就新建一个 if err == gorm.ErrRecordNotFound { err := service.GetDefaultStorehouse(orgId) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } storehouse, err = service.FindStorehouseConfig(orgId) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } } if err != nil && err.Error() != "record not found" { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } var storehouse_info, storehouse_out_info, drug_storehouse_info, drug_storehouse_out models.Storehouse storehouse_info, err = service.FindStorehouseName(storehouse.StorehouseInfo) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } storehouse_out_info, err = service.FindStorehouseName(storehouse.StorehouseOutInfo) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } drug_storehouse_info, err = service.FindStorehouseName(storehouse.DrugStorehouseInfo) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } drug_storehouse_out, err = service.FindStorehouseName(storehouse.DrugStorehouseOut) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "storehouse_info": storehouse_info.StorehouseName, "storehouse_out_info": storehouse_out_info.StorehouseName, "drug_storehouse_info": drug_storehouse_info.StorehouseName, "drug_storehouse_out": drug_storehouse_out.StorehouseName, }) return } //更改耗材自动入库仓库 func (this *SecondaryOrderApiController) UpdateInfo() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") err = service.UpdateInfo(orgId, id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "修改成功", }) return } //更改耗材自动出库仓库 func (this *SecondaryOrderApiController) UpdateOutInfo() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") err = service.UpdateOutInfo(orgId, id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "修改成功", }) return } //更改药品自动入库仓库 func (this *SecondaryOrderApiController) UpdateDrugInfo() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") err = service.UpdateDrugInfo2(orgId, id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "修改成功", }) return } //更改药品自动出库仓库 func (this *SecondaryOrderApiController) UpdateDrugOut() { orgId := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, } _, err := checkParams(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") err = service.UpdateDrugOut(orgId, id) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "修改成功", }) return } //判断前端参数是否为空 func checkParams(this *SecondaryOrderApiController, m *map[string][]string) (map[string]string, error) { tmp := make(map[string]string) for k, v := range *m { t := this.GetString(k) if v[0] == "must" && t == "" { return nil, fmt.Errorf(v[2] + "不能为空") } tmp[k] = t } return tmp, nil } //兼容旧数据 func (this *SecondaryOrderApiController) Byliinit() { err := service.Byliinit() if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "初始化成功", }) return } //查询机构所属管理员 func (this *SecondaryOrderApiController) GetuserName() { adminUserInfo := this.GetAdminUserInfo() viewModels, _, _ := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100) c, _ := service.Getcreateid(adminUserInfo.CurrentOrgId) //c.Creator admin := []*service.AdminUserManageViewModel{} //记录当前管理员的信息 //去除禁用的角色 tmp := []*service.AdminUserManageViewModel{} for i := 0; i < len(viewModels); i++ { if viewModels[i].Status == 1 { tmp = append(tmp, viewModels[i]) } if int64(viewModels[i].AdminUserId) == c.Creator { admin = append(admin, viewModels[i]) } } roles := service.FindRoles(adminUserInfo.CurrentOrgId) //去除没有权限的角色 tmplist := []*service.AdminUserManageViewModel{} if roles == nil || len(roles) == 0 { this.ServeSuccessJSON(map[string]interface{}{ "list": tmplist, }) return } for i := 0; i < len(tmp); i++ { boolean := false //获取并解析当前用户的角色 tmproles := strings.Split(tmp[i].RoleIds, ",") for j := 0; j < len(tmproles); j++ { //判断这些角色是否有权限 if _, ok := roles[tmproles[j]]; ok { boolean = true } } if boolean { tmplist = append(tmplist, tmp[i]) } } isappend := true //判断结果中是否添加机构创建者,true添加,false不添加 if len(tmplist) > 0 { for i := 0; i < len(tmplist); i++ { if int64(tmplist[i].AdminUserId) == c.Creator { isappend = false } } } if isappend { tmplist = append(tmplist, admin...) } this.ServeSuccessJSON(map[string]interface{}{ "list": tmplist, }) return } func (this *SecondaryOrderApiController) GetCreaterId() { creater := this.GetAdminUserInfo().AdminUser.Id this.ServeSuccessJSON(map[string]interface{}{ "list": creater, }) }