package controllers
|
|
import (
|
"context"
|
"errors"
|
"fmt"
|
"github.com/gin-gonic/gin"
|
"gorm.io/gorm"
|
"strconv"
|
"wms/constvar"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/pkg/logx"
|
"wms/proto/init_client"
|
"wms/proto/user"
|
"wms/request"
|
)
|
|
type DictController struct{}
|
|
// AddMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 添加字典信息
|
// @Produce application/json
|
// @Param object body request.AddMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/add [post]
|
func (slf DictController) AddMiniDict(c *gin.Context) {
|
var params request.AddMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
if len(params.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
_, err := models.NewMiniDictSearch().SetType(params.Type).SetName(params.Name).First()
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
util.ResponseFormat(c, code.NameExistedError, "名称已存在")
|
return
|
}
|
|
autoCode, _ := getAutoCode(params.Type)
|
record := models.MiniDict{
|
Type: params.Type,
|
Code: autoCode,
|
Name: params.Name,
|
Value: params.Value,
|
IsDefault: params.IsDefault,
|
}
|
|
if err := models.NewMiniDictSearch().Create(&record); err != nil {
|
logx.Errorf("MiniDict add err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "添加失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// EditMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 编辑字典信息
|
// @Produce application/json
|
// @Param object body request.EditMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/edit [post]
|
func (slf DictController) EditMiniDict(c *gin.Context) {
|
var params request.EditMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if params.ID == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效ID")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
if len(params.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
record := models.MiniDict{
|
Type: params.Type,
|
Code: params.Code,
|
Name: params.Name,
|
Value: params.Value,
|
IsDefault: params.IsDefault,
|
}
|
record.ID = uint(params.ID)
|
|
if err := models.NewMiniDictSearch().SetID(uint(params.ID)).Save(&record); err != nil {
|
logx.Errorf("MiniDict edit err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "编辑失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "编辑成功")
|
}
|
|
// DeleteMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 删除字典信息
|
// @Param id path string true "id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/delete/{id} [delete]
|
func (slf DictController) DeleteMiniDict(c *gin.Context) {
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效ID")
|
return
|
}
|
err := models.NewMiniDictSearch().SetID(uint(id)).Delete()
|
if err != nil {
|
logx.Errorf("MiniDict delete err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "删除失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "删除成功")
|
}
|
|
// SaveMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 批量更新迷你字典(会删除原数据)
|
// @Produce application/json
|
// @Param object body request.SaveMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/save [post]
|
func (slf DictController) SaveMiniDict(c *gin.Context) {
|
var params request.SaveMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
var records []*models.MiniDict
|
for _, v := range params.List {
|
if len(v.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
records = append(records, &models.MiniDict{
|
Type: params.Type,
|
Name: v.Name,
|
IsDefault: v.IsDefault,
|
Value: v.Value,
|
})
|
}
|
|
err := models.WithTransaction(func(tx *gorm.DB) error {
|
err := models.NewMiniDictSearch().SetOrm(tx).SetType(params.Type).Delete()
|
if err != nil {
|
return err
|
}
|
|
err = models.NewMiniDictSearch().SetOrm(tx).CreateBatch(records)
|
if err != nil {
|
return err
|
}
|
return nil
|
})
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "保存失败")
|
return
|
}
|
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// GetMiniDictList
|
//
|
// @Tags 数据字典
|
// @Summary 获取字典信息列表
|
// @Produce application/json
|
// @Param object body request.GetMiniDictList true "参数"
|
// @Success 200 {object} util.ResponseList{data=[]models.MiniDict} "成功"
|
// @Router /api-wms/v1/dict/getDictList [post]
|
func (slf DictController) GetMiniDictList(c *gin.Context) {
|
var params request.GetMiniDictList
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
list, total, err := models.NewMiniDictSearch().SetKeyword(params.Keyword).SetType(params.Type).Find()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
|
util.ResponseFormatList(c, code.Success, list, int(total))
|
}
|
|
// GetUserList
|
// @Tags 数据字典
|
// @Summary 获取用户列表
|
// @Produce application/json
|
// @Param object query user.GetUserRequest true "参数"
|
// @Success 200 {object} util.ResponseList{data=[]user.GetUserRequest} "成功"
|
// @Router /api-wms/v1/dict/getUserList [get]
|
func (slf DictController) GetUserList(c *gin.Context) {
|
if init_client.AdminConn == nil {
|
util.ResponseFormat(c, code.InternalError, "请先配置Admin的grpc服务信息")
|
}
|
query := new(user.GetUserRequest)
|
if c.Query("id") != "" {
|
query.Id = c.Query("id")
|
}
|
if c.Query("userName") != "" {
|
query.UserName = c.Query("userName")
|
}
|
if c.Query("nickName") != "" {
|
query.NickName = c.Query("nickName")
|
}
|
if c.Query("parentId") != "" {
|
query.ParentId = c.Query("parentId")
|
}
|
if userType, err := strconv.Atoi(c.Query("userType")); err == nil && userType > 0 {
|
query.UserType = int32(userType)
|
}
|
|
cli := user.NewUserServiceClient(init_client.AdminConn)
|
list, err := cli.GetUserList(context.Background(), query)
|
if err != nil {
|
util.ResponseFormat(c, code.InternalError, "内部错误")
|
logx.Error("grpc调用失败, GetPersonnelList err : " + err.Error())
|
return
|
}
|
util.ResponseFormat(c, code.Success, list.List)
|
}
|
|
// GetAutoCode 获取字典自动编码
|
func getAutoCode(dictType constvar.MiniDictType) (string, error) {
|
|
var prefix string
|
switch constvar.MiniDictType(dictType) {
|
case constvar.StorageType:
|
prefix = "IN0"
|
case constvar.StockoutType:
|
prefix = "OUT"
|
case constvar.TransferType:
|
prefix = "TF0"
|
case constvar.TakeStockType:
|
prefix = "TS0"
|
case constvar.DisuseType:
|
prefix = "DIS"
|
case constvar.ProductSource:
|
prefix = "PS0"
|
default:
|
return "", errors.New("编码规则不存在")
|
}
|
id, err := models.NewMiniDictSearch().SetType(constvar.MiniDictType(dictType)).MaxAutoIncr()
|
if err != nil {
|
return "", errors.New("获取最大值失败")
|
}
|
|
strMaxAutoIncr := strconv.Itoa(id + 1)
|
count := 5 - len(strMaxAutoIncr)
|
for i := 0; i < count; i++ {
|
strMaxAutoIncr = "0" + strMaxAutoIncr
|
}
|
|
return fmt.Sprintf("%v%v", prefix, strMaxAutoIncr), nil
|
}
|