| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package base
- import (
- "context"
- "fmt"
- "dashoo.cn/opms_libary/myerrors"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/util/gconv"
- "dashoo.cn/micro/app/dao/base"
- model "dashoo.cn/micro/app/model/base"
- "dashoo.cn/micro/app/service"
- )
- type productService struct {
- *service.ContextService
- Dao *base.BaseProductDao
- }
- func NewProductService(ctx context.Context) (svc *productService, err error) {
- svc = new(productService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = base.NewBaseProductDao(svc.Tenant)
- return svc, nil
- }
- // 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+"%")
- }
- if req.ProdClass != "" {
- Dao = Dao.Where("prod_class = ?", req.ProdClass)
- }
- if req.OnlyHardware {
- Dao = Dao.Where("prod_class in (40,50,60,90)")
- }
- if req.OnlySoftware {
- Dao = Dao.Where("prod_class in (10,20,30)")
- }
- if req.StateType == "启用" {
- Dao = Dao.Where("state IS NULL OR state<>'20'")
- } else if req.StateType == "禁用" {
- Dao = Dao.Where("state='20'")
- }
- total, err = Dao.Count()
- if err != nil {
- g.Log().Error(err)
- err = myerrors.DbError("获取总行数失败。")
- return
- }
- if req.PageNum != 0 && req.PageSize != 0 {
- Dao = Dao.Page(req.GetPage())
- }
- err = Dao.Order("id asc").Scan(&productList)
- return
- }
- // 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
- }
- return
- }
- // Create 添加信息
- func (s *productService) Create(req *model.AddProductReq) (lastInsertId int64, err error) {
- product := new(model.BaseProduct)
- if err = gconv.Struct(req, product); err != nil {
- g.Log().Error(err)
- return
- }
- product.State = "10"
- service.SetCreatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
- lastInsertId, err = s.Dao.InsertAndGetId(product)
- if err != nil {
- g.Log().Error(err)
- return
- }
- return
- }
- // UpdateById 修改数据
- func (s *productService) UpdateById(req *model.UpdateProductReq) (err error) {
- product, err := s.Dao.FindOne(req.Id)
- if err != nil {
- g.Log().Error(err)
- return err
- }
- if product == nil {
- return myerrors.TipsError("产品信息不存在或已被删除")
- }
- if err = gconv.Struct(req, product); err != nil {
- return
- }
- service.SetUpdatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
- _, err = s.Dao.WherePri(req.Id).Update(product)
- if err != nil {
- g.Log().Error(err)
- return
- }
- return
- }
- // UpdateState 修改数据
- func (s *productService) UpdateState(req *model.UpdateStateReq) (err error) {
- _, err = s.Dao.WherePri(req.Id).Update(fmt.Sprintf("state='%v'", req.State))
- if err != nil {
- g.Log().Error(err)
- return
- }
- return
- }
- // 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 err
- }
- if count == 0 {
- return myerrors.TipsError("数据不存在或已被删除,请刷新页面")
- }
- _, err = s.Dao.WherePri(s.Dao.C.Id, ids[0]).Delete()
- if err != nil {
- g.Log().Error(err)
- return err
- }
- return err
- } else {
- _, err := s.Dao.WhereIn(s.Dao.C.Id, ids).LockShared().Count()
- if err != nil {
- g.Log().Error(err)
- return err
- }
- result, err := s.Dao.WhereIn(s.Dao.C.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.TipsError(fmt.Sprintf("应更新%d行,实际更新%d行", len(ids), int(rows)))
- }
- }
- }
- return nil
- }
|