liujiandao
2023-11-13 1dd416deaf053a23a56b42a1ab18fe9f621fefb0
controllers/warehouse.go
@@ -7,6 +7,7 @@
   "github.com/spf13/cast"
   "gorm.io/gorm"
   "strconv"
   "strings"
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
@@ -41,12 +42,24 @@
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   //创建视图
   view := &models.Location{
      Name:      params.Code,
      JointName: params.Code,
      Type:      constvar.LocationTypeView,
   }
   id, err := models.NewLocationSearch().CreateReturnId(view)
   if err != nil {
      util.ResponseFormat(c, code.SaveFail, "视图创建失败")
      return
   }
   //创建默认位置
   location := &models.Location{
      Name:              "默认位置",
      JointName:         params.Code,
      JointName:         params.Code + "/默认位置",
      Type:              constvar.LocationTypeInternal,
      ReplenishLocation: true,
      ParentId:          id,
   }
   locationId, err := models.NewLocationSearch().CreateReturnId(location)
   if err != nil {
@@ -129,9 +142,27 @@
   }
   //更新位置信息
   if oldRecord != nil && params.Code != oldRecord.Code {
      m := make(map[string]interface{})
      m["joint_name"] = params.Code
      err := models.NewLocationSearch().SetID(oldRecord.LocationId).UpdateByMap(m)
      locations, err := models.NewLocationSearch().SetJointName(oldRecord.Code).FindNotTotal()
      if err != nil {
         return errors.New("查询位置信息失败")
      }
      err = models.WithTransaction(func(db *gorm.DB) error {
         for _, location := range locations {
            m := make(map[string]interface{})
            index := strings.Index(location.JointName, "/")
            if index > 0 {
               m["joint_name"] = params.Code + location.JointName[index:]
            } else {
               m["joint_name"] = params.Code
            }
            err = models.NewLocationSearch().SetOrm(db).SetID(location.Id).UpdateByMap(m)
            if err != nil {
               return err
            }
         }
         return nil
      })
      if err != nil {
         return errors.New("更新位置信息失败")
      }
@@ -168,11 +199,11 @@
      return
   }
   //获取位置信息
   codes := make([]string, 0)
   ids := make([]int, 0)
   for _, warehouse := range list {
      codes = append(codes, warehouse.Code)
      ids = append(ids, warehouse.LocationId)
   }
   locations, err := models.NewLocationSearch().SetJointNames(codes).FindNotTotal()
   locations, err := models.NewLocationSearch().SetIds(ids).FindNotTotal()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "位置信息查找失败")
      return