| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package base
- import (
- "context"
- "dashoo.cn/opms_libary/myerrors"
- "github.com/gogf/gf/container/garray"
- "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 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 garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
- distributorModel = distributorModel.WhereIn("belong_sale_id", s.DataScope["userIds"])
- }
- 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
- }
- // 获取经销商编号
- func (s *distributorService) getDistributorCode(distCode string) (string, error) {
- sequence, err := service.Sequence(s.Dao.DB, "distributor_code")
- if err != nil {
- return "", err
- }
- return distCode + sequence, nil
- }
- // 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
- }
- DistributorData.DistCode, err = s.getDistributorCode("JXS")
- if err != nil {
- return 0, err
- }
- service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
- 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) {
- _, err = s.Dao.WhereIn(s.Dao.C.Id, ids).Delete()
- if err != nil {
- return err
- }
- return
- }
|