|
|
@@ -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
|
|
|
}
|