| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package base
- import (
- "context"
- "fmt"
- "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.M
- if req.DistCode != "" {
- distributorModel = distributorModel.Where(s.Dao.Columns.DistCode+" like ?", "%"+req.DistCode+"%")
- }
- if req.DistName != "" {
- distributorModel = distributorModel.Where(s.Dao.Columns.DistName+" like ?", "%"+req.DistName+"%")
- }
- if req.BelongSale != "" {
- distributorModel = distributorModel.Where(s.Dao.Columns.BelongSale+" like ?", "%"+req.BelongSale+"%")
- }
- if req.ProvinceId > 0 {
- distributorModel = distributorModel.Where(s.Dao.Columns.ProvinceId+" in (?)", req.ProvinceId)
- }
- g.Log().Info("搜索条件", req.ProvinceId)
- total, err = distributorModel.Count()
- if err != nil {
- g.Log().Error(err)
- 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 {
- g.Log().Error(err)
- return 0, err
- }
- return
- }
- //GetEntityById 详情
- func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.DistributorRonp, err error) {
- err = s.Dao.Where(base.BaseProduct.Columns.Id, id).Scan(&distributorInfo)
- if err != nil {
- g.Log().Error(err)
- 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.NewMsgError(nil, "无修改数据")
- 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.Columns.DistCode, s.Dao.Columns.Id,
- s.Dao.Columns.CreatedName, s.Dao.Columns.CreatedBy, s.Dao.Columns.CreatedTime).WherePri(s.Dao.Columns.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.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
- }
|