| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package base
- import (
- "context"
- "database/sql"
- "fmt"
- dao "dashoo.cn/micro/app/dao/base"
- model "dashoo.cn/micro/app/model/base"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/glog"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type BaseDistributorContactService struct {
- Dao *dao.BaseDistributorContactDao
- DistDao *dao.BaseDistributorDao
- distSrv *distributorService
- Tenant string
- userInfo request.UserInfo
- }
- func NewBaseDistributorContactService(ctx context.Context) (*BaseDistributorContactService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- distSrv, err := NewDistributorService(ctx)
- if err != nil {
- return nil, err
- }
- return &BaseDistributorContactService{
- Dao: dao.NewBaseDistributorContactDao(tenant),
- DistDao: dao.NewBaseDistributorDao(tenant),
- distSrv: distSrv,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s BaseDistributorContactService) List(ctx context.Context, req *model.BaseDistributorContactListReq) (int, []*model.BaseDistributorContact, error) {
- dao := &s.Dao.BaseDistributorContactDao
- if req.Name != "" {
- likestr := fmt.Sprintf("%%%s%%", req.Name)
- dao = dao.Where("name LIKE ?", likestr)
- }
- if req.DistId != 0 {
- dao = dao.Where("dist_id = ?", req.DistId)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.BaseDistributorContact{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, nil
- }
- // GetEntityById 详情
- func (s BaseDistributorContactService) GetEntityById(id int64) (entityInfo *model.BaseDistributorContact, err error) {
- entityInfo, err = s.Dao.WherePri(id).One()
- if err != nil {
- return
- }
- if entityInfo == nil {
- return nil, myerrors.TipsError("联系人不存在")
- }
- return
- }
- func (s BaseDistributorContactService) Add(ctx context.Context, req *model.BaseDistributorContactAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- t, err := s.DistDao.Where("id = ?", req.DistId).One()
- if err != nil {
- return 0, err
- }
- if t == nil {
- return 0, myerrors.TipsError("所属经销商不存在")
- }
- id, err := s.Dao.InsertAndGetId(model.BaseDistributorContact{
- DistId: req.DistId,
- Name: req.Name,
- Post: req.Post,
- Phone: req.Phone,
- Wechat: req.Wechat,
- Mail: req.Mail,
- Honorific: req.Honorific,
- Territory: req.Territory,
- Remark: req.Remark,
- CreatedBy: int(s.userInfo.Id),
- CreatedName: s.userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: int(s.userInfo.Id),
- UpdatedName: s.userInfo.NickName,
- UpdatedTime: gtime.Now(),
- })
- if err != nil {
- return 0, err
- }
- err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- err = s.distSrv.AddDynamicsByCurrentUser(tx, req.DistId, "创建联系人", map[string]interface{}{"id": id})
- return err
- })
- return int(id), err
- }
- func (s BaseDistributorContactService) Update(ctx context.Context, req *model.BaseDistributorContactUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("联系人不存在: %d", req.Id))
- }
- dao := &s.Dao.BaseDistributorContactDao
- toupdate := map[string]interface{}{}
- if req.Name != "" {
- toupdate["name"] = req.Name
- }
- if req.Post != "" {
- toupdate["post"] = req.Post
- }
- if req.Phone != "" {
- toupdate["phone"] = req.Phone
- }
- if req.Wechat != nil {
- toupdate["wechat"] = *req.Wechat
- }
- if req.Mail != nil {
- toupdate["mail"] = *req.Mail
- }
- if req.Honorific != nil {
- toupdate["honorific"] = *req.Honorific
- }
- if req.Territory != nil {
- toupdate["territory"] = *req.Territory
- }
- if req.Remark != nil {
- toupdate["remark"] = *req.Remark
- }
- if len(toupdate) != 0 {
- toupdate["updated_by"] = int(s.userInfo.Id)
- toupdate["updated_name"] = s.userInfo.NickName
- toupdate["updated_time"] = gtime.Now()
- _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s BaseDistributorContactService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- for _, i := range id {
- ent, err := s.Dao.Where("id = ?", i).Unscoped().One()
- if err != nil {
- glog.Error(err)
- continue
- }
- err = s.distSrv.AddDynamicsByCurrentUser(tx, ent.DistId, "删除联系人", map[string]interface{}{"id": i})
- if err != nil {
- glog.Error(err)
- continue
- }
- }
- return nil
- })
- return err
- }
|