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" "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/byliinit", &SecondaryOrderApiController{}, "get:Byliinit") //初始化旧数据 } //获取仓库编码 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() { 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()) } //获取分页的数据 list, total, err := service.StorehouseList(page, limit, orgId, keyword) this.ServeSuccessJSON(map[string]interface{}{ "list": list, "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 var admin_name string tmpstatus := dataBody["storehouse_status"] tmpid := dataBody["storehouse_admin_id"] tmpname := dataBody["storehouse_admin_name"] if tmpstatus == nil { storehouse_status = 1 } else { storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态 } if tmpid == nil { admin_id = 0 } else { admin_id = int64(dataBody["storehouse_status"].(float64)) //管理员id } if tmpname == nil { admin_name = "admin" } else { admin_name = dataBody["storehouse_status"].(string) //管理员名称 } 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_admin := int64(dataBody["storehouse_admin"].(float64))//仓库管理员,暂时不用管 storehouse := models.Storehouse{ StorehouseCode: code, StorehouseName: name, StorehouseAddress: address, StorehouseStatus: storehouse_status, UserOrgId: orgId, Status: 1, StorehouseAdminId: admin_id, StorehouseAdminName: admin_name, 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() { 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 int64 tmpstatus := dataBody["storehouse_status"] if tmpstatus == nil { storehouse_status = 1 } else { storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态 } 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) //地址 //storehouse_admin := int64(dataBody["storehouse_admin"].(float64))//仓库管理员,暂时不用管 storehouse := models.Storehouse{ ID: id, StorehouseName: name, StorehouseAddress: address, StorehouseStatus: storehouse_status, 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 }