Files
easygoadmin/app/controller/city.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

191 lines
3.5 KiB
Go

/**
* 城市管理-控制器
* @author
* @since 2021/11/13
* @File : city
*/
package controller
import (
"easygoadmin/app/dto"
"easygoadmin/app/model"
"easygoadmin/app/service"
"easygoadmin/utils"
"easygoadmin/utils/common"
"easygoadmin/utils/gconv"
"easygoadmin/utils/response"
"github.com/gin-gonic/gin"
"net/http"
)
var City = new(cityCtl)
type cityCtl struct{}
func (c *cityCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "city_index.html").WriteTpl()
}
func (c *cityCtl) List(ctx *gin.Context) {
//// 参数
//var req *dto.CityQueryReq
//if err := ctx.ShouldBind(&req); err != nil {
// ctx.JSON(http.StatusOK, common.JsonResult{
// Code: -1,
// Msg: err.Error(),
// })
// return
//}
// 调用获取列表方法
list := service.City.GetList(nil)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}
func (c *cityCtl) Edit(ctx *gin.Context) {
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 编辑
info := &model.City{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 渲染模板
response.BuildTpl(ctx, "city_edit.html").WriteTpl(gin.H{
"info": info,
"levelList": common.CITY_LEVEL,
})
} else {
// 添加
// 渲染模板
response.BuildTpl(ctx, "city_edit.html").WriteTpl()
}
}
func (c *cityCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.CityAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.City.Add(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "添加成功",
})
}
func (c *cityCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.CityUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.City.Update(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新成功",
})
}
func (c *cityCtl) Delete(ctx *gin.Context) {
// 参数
ids := ctx.Param("ids")
if ids == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "记录ID不能为空",
})
return
}
// 调用删除方法
rows, err := service.City.Delete(ids)
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "删除成功",
})
}
func (c *cityCtl) GetChilds(ctx *gin.Context) {
// 参数验证
var req *dto.CityChildReq
if err := ctx.ShouldBind(&req); err != nil {
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用获取子级城市
list, err := service.City.GetChilds(req.CityCode)
if err != nil {
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}