Преглед изворни кода

fix:规范产品管理代码编写

Cheng Jian пре 3 година
родитељ
комит
5d9c7a6b78

+ 1 - 0
opms_libary/utils/convert_test.go

@@ -0,0 +1 @@
+package utils

+ 22 - 25
opms_parent/app/handler/base/product.go

@@ -16,15 +16,13 @@ import (
 type ProductHandler struct{}
 
 // GetList 获取列表
-func (p *ProductHandler) GetList(ctx context.Context, req *model.BaseProductSearchReq, rsp *comm_def.CommonMsg) error {
-
-	productServer, err := server.NewProductService(ctx) //service.NewBaseProductService(ctx)
+func (h *ProductHandler) GetList(ctx context.Context, req *model.ProductSearchReq, rsp *comm_def.CommonMsg) error {
+	s, err := server.NewProductService(ctx)
 	if err != nil {
 		g.Log().Error(err)
 		return err
 	}
-	g.Log().Info("搜索值", req)
-	total, list, err := productServer.GetList(req)
+	total, list, err := s.GetList(req)
 	_, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
 	if err != nil {
 		g.Log().Error(err)
@@ -35,17 +33,17 @@ func (p *ProductHandler) GetList(ctx context.Context, req *model.BaseProductSear
 }
 
 // Create 添加产品信息
-func (p *ProductHandler) Create(ctx context.Context, req *model.AddBaseProductReq, rsp *comm_def.CommonMsg) error {
+func (h *ProductHandler) Create(ctx context.Context, req *model.AddProductReq, rsp *comm_def.CommonMsg) error {
 	// 参数校验
 	if err := gvalid.CheckStruct(ctx, req, nil); err != nil {
 		return err
 	}
-	productServer, err := server.NewProductService(ctx)
+	s, err := server.NewProductService(ctx)
 	if err != nil {
 		g.Log().Error(err)
 		return err
 	}
-	err = productServer.Create(req)
+	_, err = s.Create(req)
 	_, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
 	if err != nil {
 		g.Log().Error(err)
@@ -54,18 +52,18 @@ func (p *ProductHandler) Create(ctx context.Context, req *model.AddBaseProductRe
 	return nil
 }
 
-//批量删除数据
-func (p *ProductHandler) DeleteByIds(ctx context.Context, req *model.DeIds, rsp *comm_def.CommonMsg) error {
+//DeleteByIds 批量删除数据
+func (h *ProductHandler) DeleteByIds(ctx context.Context, req *model.DeIds, rsp *comm_def.CommonMsg) error {
 	// 参数校验
 	if len(req.Ids) == 0 {
 		return gerror.New("参数为空,操作失败")
 	}
-	productServer, err := server.NewProductService(ctx)
+	s, err := server.NewProductService(ctx)
 	if err != nil {
 		g.Log().Error(err)
-		return gerror.New("系统异常,请重新尝试")
+		return err
 	}
-	err = productServer.DeleteByIds(req.Ids)
+	err = s.DeleteByIds(req.Ids)
 	_, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
 	if err != nil {
 		g.Log().Error(err)
@@ -74,36 +72,35 @@ func (p *ProductHandler) DeleteByIds(ctx context.Context, req *model.DeIds, rsp
 	return nil
 }
 
-//编辑
-func (p *ProductHandler) GetEntityById(ctx context.Context, req *comm_def.IdReq, rsp *comm_def.CommonMsg) error {
+// GetEntityById 根据ID查询详细信息
+func (h *ProductHandler) GetEntityById(ctx context.Context, req *comm_def.IdReq, rsp *comm_def.CommonMsg) error {
 	// 参数校验
 	if req.Id == 0 {
 		return gerror.New("参数有误!")
 	}
-	productServer, err := server.NewProductService(ctx)
+	s, err := server.NewProductService(ctx)
 	if err != nil {
 		g.Log().Error(err)
-		return gerror.New("系统异常,请重新尝试")
+		return err
 	}
-	list, err := productServer.GetEntityById(req.Id)
-
+	product, err := s.GetEntityById(req.Id)
 	_, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
 	if err != nil {
 		g.Log().Error(err)
 		return err
 	}
-	rsp.Data = g.Map{"list": list}
+	rsp.Data = product
 	return nil
 }
 
-//更新信息
-func (p *ProductHandler) UpdateById(ctx context.Context, req *model.UpdateBaseProductReq, rsp *comm_def.CommonMsg) error {
-	productServer, err := server.NewProductService(ctx)
+//UpdateById 更新信息
+func (h *ProductHandler) UpdateById(ctx context.Context, req *model.UpdateProductReq, rsp *comm_def.CommonMsg) error {
+	s, err := server.NewProductService(ctx)
 	if err != nil {
 		g.Log().Error(err)
-		return gerror.New("系统异常,请重新尝试")
+		return err
 	}
-	err = productServer.UpdateById(req)
+	err = s.UpdateById(req)
 	_, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
 	if err != nil {
 		g.Log().Error(err)

+ 16 - 14
opms_parent/app/model/base/base_product.go

@@ -15,29 +15,31 @@ type BaseProduct internal.BaseProduct
 
 // Fill with you ideas below.
 
-type BaseProductSearchReq struct {
-	KeyWords string `json:"prodCode"`
+type ProductSearchReq struct {
+	ProdCode string `json:"prodCode"`
+	ProdName string `json:"prodName"`
 	request.PageReq
 }
 
-//添加数据
-type AddBaseProductReq struct {
-	ProdName    string `p:"prodName"    json:"prodName"   v:"required#产品名称不能为空"`
-	ProdClass   string `p:"prodClass"   json:"prodClass"  v:"required#产品分类不能为空"`
-	GuidPrice   string `p:"guidPrice"   json:"guidPrice"      v:"required#建议成交价"`
-	DistPrice   string `p:"distPrice"   json:"distPrice"      v:"required#经销商价"`
-	AgentPrice  string `p:"agentPrice"  json:"agentPrice"    v:"required#签约代理价"`
-	MarketPrice string `p:"marketPrice" json:"marketPrice"   v:"required#市场报价"`
+//AddProductReq 添加数据
+type AddProductReq struct {
+	ProdCode    string `p:"prodCode"    json:"prodCode"   v:"required#产品编码不允许为空"`
+	ProdName    string `p:"prodName"    json:"prodName"   v:"required#产品名称不允许为空"`
+	ProdClass   string `p:"prodClass"   json:"prodClass"  v:"required#产品分类不允许为空"`
+	GuidPrice   string `p:"guidPrice"   json:"guidPrice"      v:"required#建议成交价不允许为空"`
+	DistPrice   string `p:"distPrice"   json:"distPrice"      v:"required#经销商价不允许为空"`
+	AgentPrice  string `p:"agentPrice"  json:"agentPrice"    v:"required#签约代理价不允许为空"`
+	MarketPrice string `p:"marketPrice" json:"marketPrice"   v:"required#市场报价不允许为空"`
 	Remark      string
 }
 
-//修改数据
-type UpdateBaseProductReq struct {
-	*AddBaseProductReq
+//UpdateProductReq 修改数据
+type UpdateProductReq struct {
+	*AddProductReq
 	Id int `p:"id" json:"id" v:"required# id不能为空"`
 }
 
-//批量删除
+//DeIds 批量删除
 type DeIds struct {
 	Ids []int64 `json:"ids,omitempty,string"`
 }

+ 63 - 64
opms_parent/app/service/base/base_product.go

@@ -2,13 +2,9 @@ package base
 
 import (
 	"context"
+	"dashoo.cn/opms_libary/myerrors"
 	"fmt"
-	"strconv"
-
-	"github.com/gogf/gf/errors/gerror"
 	"github.com/gogf/gf/frame/g"
-	"github.com/gogf/gf/os/gtime"
-	"github.com/gogf/gf/text/gstr"
 	"github.com/gogf/gf/util/gconv"
 
 	"dashoo.cn/micro/app/dao/base"
@@ -18,7 +14,6 @@ import (
 
 type productService struct {
 	*service.ContextService
-
 	Dao *base.BaseProductDao
 }
 
@@ -31,97 +26,101 @@ func NewProductService(ctx context.Context) (svc *productService, err error) {
 	return svc, nil
 }
 
-//产品信息列表
-func (p *productService) GetList(req *model.BaseProductSearchReq) (total int, productList []*model.BaseProduct, err error) {
-
-	productModel := p.Dao.M
-	if req.KeyWords != "" {
-		productModel = productModel.Where("prod_code", req.KeyWords)
-
+//GetList 产品信息列表
+func (s *productService) GetList(req *model.ProductSearchReq) (total int, productList []*model.BaseProduct, err error) {
+	Dao := &s.Dao.BaseProductDao
+	if req.ProdCode != "" {
+		Dao = Dao.Where("prod_code = ?", req.ProdCode)
+	}
+	if req.ProdName != "" {
+		Dao = Dao.Where("prod_name like ?", "%"+req.ProdName+"%")
 	}
-	total, err = productModel.Count()
+
+	total, err = Dao.Count()
 	if err != nil {
 		g.Log().Error(err)
-		err = gerror.New("获取总行数失败")
+		err = myerrors.New("获取总行数失败", err)
 		return
 	}
 	if req.PageNum == 0 {
 		req.PageNum = 1
 	}
 
-	err = productModel.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&productList)
+	err = Dao.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&productList)
 	return
 }
 
-//编辑查询单条查询
-func (p *productService) GetEntityById(id int64) (product *model.BaseProduct, err error) {
-	err = p.Dao.Where(base.BaseProduct.Columns.Id, id).Scan(&product)
+//GetEntityById 根据ID查询详细信息
+func (s *productService) GetEntityById(id int64) (product *model.BaseProduct, err error) {
+	product, err = s.Dao.FindOne(id)
 	if err != nil {
 		g.Log().Error(err)
-		return nil, gerror.New("获取用户数据失败")
+		return nil, err
 	}
 	return
 }
 
-//添加信息
-func (p *productService) Create(req *model.AddBaseProductReq) (err error) {
-	productData := new(model.BaseProduct)
-	if err = gconv.Struct(req, productData); err != nil {
+//Create 添加信息
+func (s *productService) Create(req *model.AddProductReq) (lastInsertId int64, err error) {
+	product := new(model.BaseProduct)
+	if err = gconv.Struct(req, product); err != nil {
 		return
 	}
-	service.SetCreatedInfo(productData, p.GetCxtUserId(), p.GetCxtUserName())
-	Model := p.Dao.M
-	productData.ProdCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6)), 0, -5)
-	res, err := Model.Insert(productData)
-	if err != nil {
-		return
-	}
-	InsertId, _ := res.LastInsertId()
-	fmt.Println(InsertId)
+	service.SetCreatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
+	lastInsertId, err = s.Dao.InsertAndGetId(product)
 	return
 }
 
-//修改数据
-func (p *productService) UpdateById(req *model.UpdateBaseProductReq) (err error) {
-	//uptime := gtime.New(time.Now())
-	db := p.Dao.M
-	record, err := db.FindOne("Id", req.Id)
-	if err != nil || record.IsEmpty() {
-		err = gerror.New("该数据不存在")
+//UpdateById 修改数据
+func (s *productService) UpdateById(req *model.UpdateProductReq) (err error) {
+	product, err := s.Dao.FindOne(req.Id)
+	if err != nil {
 		return err
 	}
-	proInfo := record.Map()
-	fmt.Println(proInfo["created_time"])
-	productData := new(model.BaseProduct)
-	if err = gconv.Struct(req, productData); err != nil {
+	if product == nil {
+		return myerrors.NewMsgError(nil, "产品信息不存在或已被删除")
+	}
+	if err = gconv.Struct(req, product); err != nil {
 		return
 	}
-	service.SetUpdatedInfo(productData, p.GetCxtUserId(), p.GetCxtUserName())
-	_, err = db.FieldsEx(base.BaseProduct.Columns.CreatedTime, base.BaseProduct.Columns.CreatedBy, base.BaseProduct.Columns.CreatedName).
-		WherePri(base.BaseProduct.Columns.Id, req.Id).Update(productData)
-
+	service.SetUpdatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
+	_, err = s.Dao.WherePri(req.Id).Update()
 	if err != nil {
 		g.Log().Error(err)
-		err = gerror.New("修改用户信息失败")
 		return
 	}
 	return
 }
 
-//删掉数据
-func (p *productService) DeleteByIds(dicIds []int64) (err error) {
-	_, err = base.BaseProduct.Fields(base.BaseProduct.Columns.CreatedTime).
-		Where(base.BaseProduct.Columns.Id+" in (?) ", dicIds).All()
-	if err != nil {
-		g.Log().Error(err)
-		err = gerror.New("没有要删除的数据")
-		return
-	}
-	_, err = p.Dao.Delete(base.BaseProduct.Columns.Id+" in (?) ", dicIds)
-	if err != nil {
-		g.Log().Error(err)
-		err = gerror.New("删除数据失败")
-		return err
+//DeleteByIds 删掉数据
+func (s *productService) DeleteByIds(ids []int64) (err error) {
+
+	if len(ids) == 1 {
+		count, err := s.Dao.WherePri(ids[0]).Count()
+		if err != nil {
+			g.Log().Error(err)
+			return
+		}
+		if count == 0 {
+			return myerrors.NewMsgError(nil, "数据不存在或已被删除,请刷新页面")
+		}
+	} else {
+		_, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).LockShared().Count()
+		if err != nil {
+			g.Log().Error(err)
+			return err
+		}
+		result, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Delete()
+		if err != nil {
+			g.Log().Error(err)
+			return err
+		}
+		rows, err := result.RowsAffected()
+		if err == nil {
+			if len(ids) != int(rows) {
+				return myerrors.NewMsgError(nil, fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
+			}
+		}
 	}
-	return
+	return nil
 }