| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package base
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/container/garray"
- "strconv"
- "dashoo.cn/opms_libary/myerrors"
- "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"
- model "dashoo.cn/micro/app/model/base"
- "dashoo.cn/micro/app/service"
- )
- type distributorService struct {
- *service.ContextService
- Dao *base.BaseDistributorDao
- }
- func NewDistributorService(ctx context.Context) (svc *distributorService, err error) {
- svc = new(distributorService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = base.NewBaseDistributorDao(svc.Tenant)
- return svc, nil
- }
- // GetList 经销商信息列表
- func (s *distributorService) GetList(req *model.BaseDistributorSearchReq) (total int, distributorList []*model.DistributorRonp, err error) {
- distributorModel := s.Dao.FieldsEx(s.Dao.C.DeletedTime)
- // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据
- if len(s.CxtUser.Roles) == 1 && garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
- distributorModel = distributorModel.DataScope(s.Ctx, "belong_sale_id")
- }
- if req.DistCode != "" {
- distributorModel = distributorModel.WhereLike(s.Dao.C.DistCode, "%"+req.DistCode+"%")
- }
- if req.DistName != "" {
- distributorModel = distributorModel.WhereLike(s.Dao.C.DistName, "%"+req.DistName+"%")
- }
- if req.BelongSale != "" {
- distributorModel = distributorModel.WhereLike(s.Dao.C.BelongSale, "%"+req.BelongSale+"%")
- }
- if len(req.ProvinceId) > 0 {
- distributorModel = distributorModel.WhereIn(s.Dao.C.ProvinceId, req.ProvinceId)
- }
- total, err = distributorModel.Count()
- if err != nil {
- err = myerrors.DbError("获取总行数失败。")
- return
- }
- err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
- return
- }
- // Create 经销商创建
- func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) {
- DistributorData := new(model.BaseDistributor)
- if err = gconv.Struct(req, DistributorData); err != nil {
- return
- }
- service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
- DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5)
- lastId, err = s.Dao.InsertAndGetId(DistributorData)
- if err != nil {
- return 0, err
- }
- return
- }
- // GetEntityById 详情
- func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.DistributorRonp, err error) {
- err = s.Dao.Where(base.BaseProduct.C.Id, id).Scan(&distributorInfo)
- if err != nil {
- return
- }
- return
- }
- // UpdateById 修改数据
- func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
- count, err := s.Dao.Where("id = ", req.Id).Count()
- if err != nil {
- g.Log().Error(err)
- return
- }
- if count == 0 {
- err = myerrors.TipsError("无修改数据")
- return
- }
- distData := new(model.BaseDistributor)
- if err = gconv.Struct(req, distData); err != nil {
- return
- }
- service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
- _, err = s.Dao.FieldsEx(s.Dao.C.DistCode, s.Dao.C.Id,
- s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime).WherePri(s.Dao.C.Id, req.Id).Update(distData)
- if err != nil {
- g.Log().Error(err)
- return
- }
- return
- }
- // DeleteByIds 删除
- func (s *distributorService) 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("数据不存在或已被删除,请刷新页面")
- }
- } 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("应更新%s行,实际更新%s行", len(ids), int(rows)))
- }
- }
- }
- return
- }
|