base_distributor_contact.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package base
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. dao "dashoo.cn/micro/app/dao/base"
  7. model "dashoo.cn/micro/app/model/base"
  8. "dashoo.cn/opms_libary/micro_srv"
  9. "dashoo.cn/opms_libary/myerrors"
  10. "dashoo.cn/opms_libary/request"
  11. "github.com/gogf/gf/database/gdb"
  12. "github.com/gogf/gf/os/glog"
  13. "github.com/gogf/gf/os/gtime"
  14. "github.com/gogf/gf/util/gvalid"
  15. )
  16. type BaseDistributorContactService struct {
  17. Dao *dao.BaseDistributorContactDao
  18. DistDao *dao.BaseDistributorDao
  19. distSrv *distributorService
  20. Tenant string
  21. userInfo request.UserInfo
  22. }
  23. func NewBaseDistributorContactService(ctx context.Context) (*BaseDistributorContactService, error) {
  24. tenant, err := micro_srv.GetTenant(ctx)
  25. if err != nil {
  26. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  27. }
  28. // 获取用户信息
  29. userInfo, err := micro_srv.GetUserInfo(ctx)
  30. if err != nil {
  31. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  32. }
  33. distSrv, err := NewDistributorService(ctx)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &BaseDistributorContactService{
  38. Dao: dao.NewBaseDistributorContactDao(tenant),
  39. DistDao: dao.NewBaseDistributorDao(tenant),
  40. distSrv: distSrv,
  41. Tenant: tenant,
  42. userInfo: userInfo,
  43. }, nil
  44. }
  45. func (s BaseDistributorContactService) List(ctx context.Context, req *model.BaseDistributorContactListReq) (int, []*model.BaseDistributorContact, error) {
  46. dao := &s.Dao.BaseDistributorContactDao
  47. if req.Name != "" {
  48. likestr := fmt.Sprintf("%%%s%%", req.Name)
  49. dao = dao.Where("name LIKE ?", likestr)
  50. }
  51. if req.DistId != 0 {
  52. dao = dao.Where("dist_id = ?", req.DistId)
  53. }
  54. total, err := dao.Count()
  55. if err != nil {
  56. return 0, nil, err
  57. }
  58. if req.PageNum != 0 {
  59. dao = dao.Page(req.GetPage())
  60. }
  61. orderby := "created_time desc"
  62. if req.OrderBy != "" {
  63. orderby = req.OrderBy
  64. }
  65. dao = dao.Order(orderby)
  66. ents := []*model.BaseDistributorContact{}
  67. err = dao.Structs(&ents)
  68. if err != nil && err != sql.ErrNoRows {
  69. return 0, nil, err
  70. }
  71. return total, ents, nil
  72. }
  73. func (s BaseDistributorContactService) Add(ctx context.Context, req *model.BaseDistributorContactAddReq) (int, error) {
  74. validErr := gvalid.CheckStruct(ctx, req, nil)
  75. if validErr != nil {
  76. return 0, myerrors.TipsError(validErr.Current().Error())
  77. }
  78. t, err := s.DistDao.Where("id = ?", req.DistId).One()
  79. if err != nil {
  80. return 0, err
  81. }
  82. if t == nil {
  83. return 0, myerrors.TipsError("所属经销商不存在")
  84. }
  85. id, err := s.Dao.InsertAndGetId(model.BaseDistributorContact{
  86. DistId: req.DistId,
  87. Name: req.Name,
  88. Post: req.Post,
  89. Phone: req.Phone,
  90. Wechat: req.Wechat,
  91. Mail: req.Mail,
  92. Honorific: req.Honorific,
  93. Territory: req.Territory,
  94. Remark: req.Remark,
  95. CreatedBy: int(s.userInfo.Id),
  96. CreatedName: s.userInfo.NickName,
  97. CreatedTime: gtime.Now(),
  98. UpdatedBy: int(s.userInfo.Id),
  99. UpdatedName: s.userInfo.NickName,
  100. UpdatedTime: gtime.Now(),
  101. })
  102. if err != nil {
  103. return 0, err
  104. }
  105. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  106. err = s.distSrv.AddDynamicsByCurrentUser(tx, req.DistId, "创建联系人", map[string]interface{}{"id": id})
  107. return err
  108. })
  109. return int(id), err
  110. }
  111. func (s BaseDistributorContactService) Update(ctx context.Context, req *model.BaseDistributorContactUpdateReq) error {
  112. validErr := gvalid.CheckStruct(ctx, req, nil)
  113. if validErr != nil {
  114. return myerrors.TipsError(validErr.Current().Error())
  115. }
  116. ent, err := s.Dao.Where("id = ?", req.Id).One()
  117. if err != nil {
  118. return err
  119. }
  120. if ent == nil {
  121. return myerrors.TipsError(fmt.Sprintf("联系人不存在: %d", req.Id))
  122. }
  123. dao := &s.Dao.BaseDistributorContactDao
  124. toupdate := map[string]interface{}{}
  125. if req.Name != "" {
  126. toupdate["name"] = req.Name
  127. }
  128. if req.Post != "" {
  129. toupdate["post"] = req.Post
  130. }
  131. if req.Phone != "" {
  132. toupdate["phone"] = req.Phone
  133. }
  134. if req.Wechat != nil {
  135. toupdate["wechat"] = *req.Wechat
  136. }
  137. if req.Mail != nil {
  138. toupdate["mail"] = *req.Mail
  139. }
  140. if req.Honorific != nil {
  141. toupdate["honorific"] = *req.Honorific
  142. }
  143. if req.Territory != nil {
  144. toupdate["territory"] = *req.Territory
  145. }
  146. if req.Remark != nil {
  147. toupdate["remark"] = *req.Remark
  148. }
  149. if len(toupdate) != 0 {
  150. toupdate["updated_by"] = int(s.userInfo.Id)
  151. toupdate["updated_name"] = s.userInfo.NickName
  152. toupdate["updated_time"] = gtime.Now()
  153. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  154. if err != nil {
  155. return err
  156. }
  157. }
  158. return nil
  159. }
  160. func (s BaseDistributorContactService) Delete(ctx context.Context, id []int) error {
  161. if len(id) == 0 {
  162. return nil
  163. }
  164. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  165. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  166. for _, i := range id {
  167. ent, err := s.Dao.Where("id = ?", i).Unscoped().One()
  168. if err != nil {
  169. glog.Error(err)
  170. continue
  171. }
  172. err = s.distSrv.AddDynamicsByCurrentUser(tx, ent.DistId, "删除联系人", map[string]interface{}{"id": i})
  173. if err != nil {
  174. glog.Error(err)
  175. continue
  176. }
  177. }
  178. return nil
  179. })
  180. return err
  181. }