package controllers import ( "errors" "fmt" "github.com/gin-gonic/gin" "github.com/spf13/cast" "gorm.io/gorm" "strconv" "strings" "wms/constvar" "wms/extend/code" "wms/extend/util" "wms/models" "wms/pkg/logx" "wms/pkg/structx" "wms/request" ) type OperationTypeController struct{} // Add // @Tags 业务类型 // @Summary 添加作业类型 // @Produce application/json // @Param object body request.AddOperationType true "作业类型信息" // @Success 200 {object} util.Response "成功" // @Router /api-wms/v1/operationType/operationType [post] func (slf OperationTypeController) Add(c *gin.Context) { var reqParams request.AddOperationType var params models.OperationType if err := c.BindJSON(&reqParams); err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } if err := structx.AssignTo(reqParams, ¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, "数据转换错误") return } if err := slf.ParamsCheck(params); err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) return } if err := models.NewOperationTypeSearch().Create(¶ms); err != nil { logx.Errorf("OperationType create err: %v", err) util.ResponseFormat(c, code.SaveFail, "插入失败") return } util.ResponseFormat(c, code.Success, "添加成功") } // Update // @Tags 业务类型 // @Summary 编辑作业类型 // @Produce application/json // @Param object body request.UpdateOperationType true "作业类型信息" // @Param id path string true "作业类型id" // @Success 200 {object} util.Response "成功" // @Router /api-wms/v1/operationType/operationType/{id} [put] func (slf OperationTypeController) Update(c *gin.Context) { id := cast.ToUint(c.Param("id")) if id == 0 { util.ResponseFormat(c, code.RequestParamError, "空的记录id") return } var ( reqParams request.UpdateOperationType params models.OperationType ) if err := c.BindJSON(&reqParams); err != nil { util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error())) return } if err := structx.AssignTo(reqParams, ¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("数据转换错误: %v", err.Error())) return } params.ID = id if err := slf.ParamsCheck(params); err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) return } err := models.NewOperationTypeSearch().SetID(params.ID).Update(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "修改失败") return } util.ResponseFormat(c, code.UpdateSuccess, "更新成功") } func (slf OperationTypeController) ParamsCheck(params models.OperationType) (err error) { if params.ID != 0 { _, err = models.NewOperationTypeSearch().SetID(params.ID).First() if err == gorm.ErrRecordNotFound { return errors.New("记录不存在") } } return nil } // List // @Tags 业务类型 // @Summary 查询作业类型列表 // @Produce application/json // @Param object query request.GetOperationTypeList true "查询参数" // @Success 200 {object} util.ResponseList "成功" // @Router /api-wms/v1/operationType/operationType [get] func (slf OperationTypeController) List(c *gin.Context) { var params request.GetOperationTypeList if err := c.ShouldBindQuery(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) return } warehouseIds := make([]int, 0) if "" != params.WarehouseIds { for _, strId := range strings.Split(params.WarehouseIds, ",") { intId, err := strconv.Atoi(strId) if err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) } warehouseIds = append(warehouseIds, intId) } } list, total, err := models.NewOperationTypeSearch(). SetPage(params.Page, params.PageSize). SetKeyword(params.Keyword). SetOrder("id desc"). SetWarehouseIds(warehouseIds). SetPreload(true).Find() if err != nil { util.ResponseFormat(c, code.RequestError, "查找失败") return } var idList []int for _, v := range list { idList = append(idList, v.Id) } statistics, err := models.NewOperationTypeSearch().ListByStatusAndCount(idList) if err != nil { util.ResponseFormat(c, code.RequestError, err.Error()) return } mapStatistics := make(map[string]*models.OperationTypeByStatus, 0) for _, v := range statistics { mapStatistics[strconv.Itoa(v.Id)+string(v.Status)] = v } for k, v := range list { if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Ready)]; ok { list[k].ReadyCount = value.Count } if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Finish)]; ok { list[k].FinishCount = value.Count } if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Cancel)]; ok { list[k].CancelCount = value.Count } } util.ResponseFormatListWithPage(c, code.Success, list, cast.ToInt(total), params.Page, params.PageSize) } // Delete // @Tags 业务类型 // @Summary 删除作业类型 // @Produce application/json // @Param id path string true "作业类型id" // @Success 200 {object} util.Response "成功" // @Router /api-wms/v1/operationType/operationType/{id} [delete] func (slf OperationTypeController) Delete(c *gin.Context) { id := cast.ToUint(c.Param("id")) if id == 0 { util.ResponseFormat(c, code.RequestParamError, "空的记录id") return } err := models.NewOperationTypeSearch().SetID(id).Delete() if err != nil { util.ResponseFormat(c, code.RequestParamError, "删除失败") return } util.ResponseFormat(c, code.UpdateSuccess, "删除成功") } // ListTransfer // @Tags 业务类型 // @Summary 调拨类型列表 // @Produce application/json // @Param object query request.ListTransfer true "查询参数" // @Success 200 {object} util.ResponseList "成功" // @Router /api-wms/v1/operationType/listTransfer [get] func (slf OperationTypeController) ListTransfer(c *gin.Context) { var params request.GetOperationTypeList if err := c.ShouldBindQuery(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, err.Error()) return } list, total, err := models.NewOperationTypeSearch().SetPage(params.Page, params.PageSize).SetBaseOperationType(constvar.BaseOperationTypeInternal).SetKeyword(params.Keyword).SetOrder("id desc").SetPreload(true).Find() if err != nil { util.ResponseFormat(c, code.RequestError, "查找失败") return } var idList []int for _, v := range list { idList = append(idList, v.Id) } statistics, err := models.NewOperationTypeSearch().ListByStatusAndCount(idList) if err != nil { util.ResponseFormat(c, code.RequestError, err.Error()) return } mapStatistics := make(map[string]*models.OperationTypeByStatus, 0) for _, v := range statistics { mapStatistics[strconv.Itoa(v.Id)+string(v.Status)] = v } for k, v := range list { if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Ready)]; ok { list[k].ReadyCount = value.Count } if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Finish)]; ok { list[k].FinishCount = value.Count } if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Cancel)]; ok { list[k].FinishCount = value.Count } } util.ResponseFormatListWithPage(c, code.Success, list, cast.ToInt(total), params.Page, params.PageSize) }