package controllers
|
|
import (
|
"errors"
|
"github.com/gin-gonic/gin"
|
"strconv"
|
"strings"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/request"
|
)
|
|
type LocationController struct {
|
}
|
|
// AddLocation
|
// @Tags 位置
|
// @Summary 添加位置信息
|
// @Produce application/json
|
// @Param object body models.Location true "位置信息"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/location/addLocation [post]
|
func (slf LocationController) AddLocation(c *gin.Context) {
|
var params models.Location
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
|
return
|
}
|
if err := slf.CheckLocation(params); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err)
|
return
|
}
|
|
if params.WarehouseId == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "仓库ID参数缺失")
|
return
|
}
|
|
if params.ParentId != 0 {
|
//查询上级名称
|
first, err := models.NewLocationSearch().SetID(params.ParentId).First()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查询上级名称失败")
|
return
|
}
|
params.JointName = first.JointName + "/" + params.Name
|
if first.WarehouseId != 0 {
|
params.WarehouseId = first.WarehouseId
|
} else {
|
//根据仓库缩写查询仓库
|
houseCode := strings.Split(first.JointName, "/")[0]
|
warehouse, err := models.NewWarehouseSearch().SetCode(houseCode).First()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err)
|
return
|
}
|
params.WarehouseId = warehouse.Id
|
}
|
|
} else {
|
params.JointName = params.Name
|
}
|
|
err := models.NewLocationSearch().Create(¶ms)
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "创建失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "创建成功")
|
}
|
|
// GetLocationList
|
// @Tags 位置
|
// @Summary 获取位置列表
|
// @Produce application/json
|
// @Param object body request.GetLocationList true "查询参数"
|
// @Success 200 {object} util.ResponseList{data=[]models.Location} "成功"
|
// @Router /api-wms/v1/location/getLocationList [post]
|
func (slf LocationController) GetLocationList(c *gin.Context) {
|
var params request.GetLocationList
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
search := models.NewLocationSearch()
|
if params.PageInfo.Check() {
|
search.SetPage(params.Page, params.PageSize)
|
}
|
list, total, err := search.SetKeyword(params.KeyWord).SetType(params.Type).SetJointName(params.JointName).
|
SetIsScrapLocation(params.IsScrapLocation).SetOrder("created_at desc").
|
SetWarehouseId(params.WarehouseId).
|
Find()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
|
util.ResponseFormatList(c, code.Success, list, int(total))
|
}
|
|
// GetLocationTreeList
|
// @Tags 位置
|
// @Summary 获取位置列表树
|
// @Produce application/json
|
// @Success 200 {object} util.ResponseList{data=[]models.Location} "成功"
|
// @Router /api-wms/v1/location/getLocationTreeList [get]
|
func (slf LocationController) GetLocationTreeList(c *gin.Context) {
|
all, err := models.NewLocationSearch().SetType(3).FindAll()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
var tree []*models.Location
|
m := make(map[int]*models.Location)
|
for _, location := range all {
|
m[location.Id] = location
|
}
|
for _, location := range all {
|
if location.ParentId == 0 {
|
tree = append(tree, location)
|
} else {
|
if _, ok := m[location.ParentId]; !ok {
|
tree = append(tree, location)
|
continue
|
}
|
if m[location.ParentId].Children == nil {
|
m[location.ParentId].Children = make([]*models.Location, 0)
|
}
|
|
m[location.ParentId].Children = append(m[location.ParentId].Children, location)
|
}
|
}
|
|
util.ResponseFormat(c, code.Success, tree)
|
}
|
|
// GetLocationDetails
|
// @Tags 位置
|
// @Summary 获取位置详情
|
// @Produce application/json
|
// @Param id path string true "id" "查询参数"
|
// @Success 200 {object} util.Response{data=models.Location} "成功"
|
// @Router /api-wms/v1/location/getLocationDetails/{id} [get]
|
func (slf LocationController) GetLocationDetails(c *gin.Context) {
|
id, _ := strconv.Atoi(c.Param("id"))
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效id")
|
return
|
}
|
location, err := models.NewLocationSearch().SetID(id).First()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, location)
|
}
|
|
// UpdateLocation
|
// @Tags 位置
|
// @Summary 修改位置
|
// @Produce application/json
|
// @Param object body models.Location true "产品信息"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/location/updateLocation [post]
|
func (slf LocationController) UpdateLocation(c *gin.Context) {
|
var params models.Location
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
if err := slf.CheckLocation(params); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err)
|
return
|
}
|
//index := strings.LastIndex(params.JointName, "/")
|
//if index > 0 {
|
// jn := params.JointName[:index]
|
// oldName := params.JointName[index+1:]
|
// if oldName != params.Name {
|
// params.JointName = jn + "/" + params.Name
|
// }
|
//}
|
if params.ParentId != 0 {
|
//查询上级名称
|
first, err := models.NewLocationSearch().SetID(params.ParentId).First()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查询上级名称失败")
|
return
|
}
|
params.JointName = first.JointName + "/" + params.Name
|
} else {
|
params.JointName = strings.Split(params.JointName, "/")[0] + "/" + params.Name
|
}
|
err := models.NewLocationSearch().Update(¶ms)
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "位置信息更新失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "更新成功")
|
}
|
|
// DeleteLocation
|
// @Tags 位置
|
// @Summary 删除位置
|
// @Produce application/json
|
// @Param id path string true "id" "查询参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/location/deleteLocation/{id} [delete]
|
func (slf LocationController) DeleteLocation(c *gin.Context) {
|
id, _ := strconv.Atoi(c.Param("id"))
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效id")
|
return
|
}
|
err := models.NewLocationSearch().SetID(id).Delete()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "删除失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "删除成功")
|
}
|
|
func (slf LocationController) CheckLocation(params models.Location) error {
|
if params.Name == "" {
|
return errors.New("名称不能为空")
|
}
|
if params.Type == 0 {
|
return errors.New("请选择正确的位置类型")
|
}
|
return nil
|
}
|