修改了基础数据-获取地区列表的路由,新增了新增地区、修改地区
This commit is contained in:
parent
adb89dd9a1
commit
cbe044f1ef
@ -1,45 +0,0 @@
|
|||||||
package controller
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"hospital-admin-api/api/dao"
|
|
||||||
"hospital-admin-api/api/dto"
|
|
||||||
"hospital-admin-api/api/requests"
|
|
||||||
"hospital-admin-api/api/responses"
|
|
||||||
"hospital-admin-api/global"
|
|
||||||
"hospital-admin-api/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Area struct{}
|
|
||||||
|
|
||||||
// GetAreaList 获取地区列表
|
|
||||||
func (b *Area) GetAreaList(c *gin.Context) {
|
|
||||||
req := requests.AreaRequest{}
|
|
||||||
if err := c.ShouldBind(&req.GetAreaList); err != nil {
|
|
||||||
responses.FailWithMessage(err.Error(), c)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 参数验证
|
|
||||||
if err := global.Validate.Struct(req.GetAreaList); err != nil {
|
|
||||||
responses.FailWithMessage(utils.Translate(err), c)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理参数
|
|
||||||
if req.ParentId == "" && req.AreaId == "" && req.AreaName == "" {
|
|
||||||
req.ParentId = "1"
|
|
||||||
req.AreaType = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
areaDao := dao.AreaDao{}
|
|
||||||
area, err := areaDao.GetAreaListByStruct(req.GetAreaList)
|
|
||||||
if err != nil {
|
|
||||||
responses.Ok(c)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理返回值
|
|
||||||
r := dto.GetAreaListDto(area)
|
|
||||||
responses.OkWithData(r, c)
|
|
||||||
}
|
|
||||||
@ -17,6 +17,7 @@ type Api struct {
|
|||||||
exportManage // 导出管理
|
exportManage // 导出管理
|
||||||
productManage // 商品管理
|
productManage // 商品管理
|
||||||
couponManage // 优惠卷管理
|
couponManage // 优惠卷管理
|
||||||
|
basicManage // 基础数据管理
|
||||||
}
|
}
|
||||||
|
|
||||||
// SysSetting 系统设置
|
// SysSetting 系统设置
|
||||||
@ -41,7 +42,6 @@ type basic struct {
|
|||||||
Hospital // 医院管理
|
Hospital // 医院管理
|
||||||
DiseaseClassExpertise // 专长管理
|
DiseaseClassExpertise // 专长管理
|
||||||
Bank // 银行管理
|
Bank // 银行管理
|
||||||
Area // 省市区管理
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 订单管理
|
// 订单管理
|
||||||
@ -97,3 +97,8 @@ type productManage struct {
|
|||||||
type couponManage struct {
|
type couponManage struct {
|
||||||
Coupon
|
Coupon
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 基础数据管理
|
||||||
|
type basicManage struct {
|
||||||
|
Basic
|
||||||
|
}
|
||||||
|
|||||||
@ -1 +1,117 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"hospital-admin-api/api/dao"
|
||||||
|
"hospital-admin-api/api/dto"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
|
"hospital-admin-api/api/responses"
|
||||||
|
"hospital-admin-api/api/service"
|
||||||
|
"hospital-admin-api/global"
|
||||||
|
"hospital-admin-api/utils"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Basic struct {
|
||||||
|
Area // 省市区
|
||||||
|
}
|
||||||
|
|
||||||
|
// Area 省市区
|
||||||
|
type Area struct{}
|
||||||
|
|
||||||
|
// GetAreaList 获取地区列表
|
||||||
|
func (b *Area) GetAreaList(c *gin.Context) {
|
||||||
|
basicRequest := requests.BasicRequest{}
|
||||||
|
req := basicRequest.GetAreaList
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(req); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理参数
|
||||||
|
if req.ParentId == "" && req.AreaId == "" && req.AreaName == "" {
|
||||||
|
req.ParentId = "1"
|
||||||
|
req.AreaType = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
areaDao := dao.AreaDao{}
|
||||||
|
area, err := areaDao.GetAreaListByStruct(req)
|
||||||
|
if err != nil {
|
||||||
|
responses.Ok(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理返回值
|
||||||
|
r := dto.GetAreaListDto(area)
|
||||||
|
responses.OkWithData(r, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddArea 新增地区
|
||||||
|
func (r *Area) AddArea(c *gin.Context) {
|
||||||
|
basicRequest := requests.BasicRequest{}
|
||||||
|
req := basicRequest.AddArea
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(req); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
basicService := service.BasicService{}
|
||||||
|
_, err := basicService.AddArea(req)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutArea 修改地区
|
||||||
|
func (r *Area) PutArea(c *gin.Context) {
|
||||||
|
basicRequest := requests.BasicRequest{}
|
||||||
|
req := basicRequest.PutArea
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(req); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := c.Param("area_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
areaId, err := strconv.Atoi(id)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
basicService := service.BasicService{}
|
||||||
|
_, err = basicService.PutArea(areaId, req)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
package requests
|
|
||||||
|
|
||||||
type AreaRequest struct {
|
|
||||||
GetAreaList // 获取地区列表
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAreaList 获取地区列表
|
|
||||||
type GetAreaList struct {
|
|
||||||
AreaId string `json:"area_id" form:"area_id" label:"地区编号"`
|
|
||||||
AreaName string `json:"area_name" form:"area_name" label:"名称"`
|
|
||||||
ParentId string `json:"parent_id" form:"parent_id" label:"上级编号"`
|
|
||||||
AreaType int `json:"area_type" form:"area_type" label:"类型(1:国家,2:省,3:市,4:区县)"`
|
|
||||||
}
|
|
||||||
@ -1,4 +1,29 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
type BasicRequest struct {
|
type BasicRequest struct {
|
||||||
|
GetAreaList // 获取地区列表
|
||||||
|
AddArea // 新增地区
|
||||||
|
PutArea // 修改地区
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAreaList 获取地区列表
|
||||||
|
type GetAreaList struct {
|
||||||
|
AreaId string `json:"area_id" form:"area_id" label:"地区编号"`
|
||||||
|
AreaName string `json:"area_name" form:"area_name" label:"名称"`
|
||||||
|
ParentId string `json:"parent_id" form:"parent_id" label:"上级编号"`
|
||||||
|
AreaType int `json:"area_type" form:"area_type" label:"类型(1:国家,2:省,3:市,4:区县)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddArea 新增地区
|
||||||
|
type AddArea struct {
|
||||||
|
AreaName string `json:"area_name" form:"area_name" label:"名称" validate:"required"`
|
||||||
|
ParentId string `json:"parent_id" form:"parent_id" label:"上级编号" validate:"required"`
|
||||||
|
AreaType *int `json:"area_type" form:"area_type" label:"类型(1:国家,2:省,3:市,4:区县)" validate:"required"`
|
||||||
|
Zip string `json:"zip" form:"zip" label:"邮编"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutArea 修改地区
|
||||||
|
type PutArea struct {
|
||||||
|
AreaName string `json:"area_name" form:"area_name" label:"名称" validate:"required"`
|
||||||
|
Zip string `json:"zip" form:"zip" label:"邮编"`
|
||||||
}
|
}
|
||||||
|
|||||||
@ -165,11 +165,7 @@ func basicRouter(r *gin.Engine, api controller.Api) {
|
|||||||
areaGroup := basicGroup.Group("/area")
|
areaGroup := basicGroup.Group("/area")
|
||||||
{
|
{
|
||||||
// 获取地区列表
|
// 获取地区列表
|
||||||
areaGroup.GET("/list", api.Area.GetAreaList)
|
areaGroup.GET("/list", api.Basic.Area.GetAreaList)
|
||||||
|
|
||||||
// 获取地区列表-分页
|
|
||||||
|
|
||||||
// 地区详情
|
|
||||||
|
|
||||||
// 修改地区
|
// 修改地区
|
||||||
|
|
||||||
@ -752,4 +748,21 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
|||||||
userGroup.POST("/page", api.Coupon.GetUserCouponPage)
|
userGroup.POST("/page", api.Coupon.GetUserCouponPage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 基础数据
|
||||||
|
basicGroup := adminGroup.Group("/basic")
|
||||||
|
{
|
||||||
|
// 地区管理
|
||||||
|
areaGroup := basicGroup.Group("/area")
|
||||||
|
{
|
||||||
|
// 获取地区列表
|
||||||
|
areaGroup.GET("", api.Basic.Area.GetAreaList)
|
||||||
|
|
||||||
|
// 新增地区
|
||||||
|
areaGroup.POST("", api.Basic.Area.AddArea)
|
||||||
|
|
||||||
|
// 修改地区
|
||||||
|
areaGroup.PUT("/:area_id", api.Basic.Area.PutArea)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,113 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hospital-admin-api/api/dao"
|
||||||
|
"hospital-admin-api/api/model"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
|
"hospital-admin-api/global"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
type BasicService struct{}
|
type BasicService struct{}
|
||||||
|
|
||||||
|
// AddArea 新增地区
|
||||||
|
func (r *BasicService) AddArea(req requests.AddArea) (bool, error) {
|
||||||
|
if *req.AreaType == 1 {
|
||||||
|
return false, errors.New("不可添加国家")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测上级是否存在
|
||||||
|
maps := make(map[string]interface{})
|
||||||
|
maps["parent_id"] = req.ParentId
|
||||||
|
if *req.AreaType == 4 {
|
||||||
|
maps["area_type"] = 3
|
||||||
|
}
|
||||||
|
if *req.AreaType == 3 {
|
||||||
|
maps["area_type"] = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if *req.AreaType == 2 {
|
||||||
|
maps["area_type"] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
areaDao := dao.AreaDao{}
|
||||||
|
_, err := areaDao.GetAreaList(maps)
|
||||||
|
if err != nil {
|
||||||
|
return false, errors.New("上级地区错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
parentId, err := strconv.ParseInt(req.ParentId, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("新增失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增优惠卷表
|
||||||
|
area := &model.Area{
|
||||||
|
AreaName: req.AreaName,
|
||||||
|
ParentId: parentId,
|
||||||
|
Zip: req.Zip,
|
||||||
|
AreaType: *req.AreaType,
|
||||||
|
}
|
||||||
|
|
||||||
|
area, err = areaDao.AddArea(tx, area)
|
||||||
|
if err != nil || area == nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutArea 修改地区
|
||||||
|
func (r *BasicService) PutArea(areaId int, req requests.PutArea) (bool, error) {
|
||||||
|
areaDao := dao.AreaDao{}
|
||||||
|
area, err := areaDao.GetAreaById(areaId)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
areaData := make(map[string]interface{})
|
||||||
|
|
||||||
|
if req.AreaName != area.AreaName {
|
||||||
|
areaData["area_name"] = req.AreaName
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Zip != "" {
|
||||||
|
if req.Zip != area.Zip {
|
||||||
|
areaData["zip"] = req.Zip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(areaData) > 0 {
|
||||||
|
err = areaDao.EditAreaById(tx, areaId, areaData)
|
||||||
|
if err != nil || area == nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user