base_distributor_contact.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // GetEntityById 详情
  74. func (s BaseDistributorContactService) GetEntityById(id int64) (entityInfo *model.BaseDistributorContact, err error) {
  75. entityInfo, err = s.Dao.WherePri(id).One()
  76. if err != nil {
  77. return
  78. }
  79. if entityInfo == nil {
  80. return nil, myerrors.TipsError("联系人不存在")
  81. }
  82. return
  83. }
  84. func (s BaseDistributorContactService) Add(ctx context.Context, req *model.BaseDistributorContactAddReq) (int, error) {
  85. validErr := gvalid.CheckStruct(ctx, req, nil)
  86. if validErr != nil {
  87. return 0, myerrors.TipsError(validErr.Current().Error())
  88. }
  89. t, err := s.DistDao.Where("id = ?", req.DistId).One()
  90. if err != nil {
  91. return 0, err
  92. }
  93. if t == nil {
  94. return 0, myerrors.TipsError("所属经销商不存在")
  95. }
  96. id, err := s.Dao.InsertAndGetId(model.BaseDistributorContact{
  97. DistId: req.DistId,
  98. Name: req.Name,
  99. Post: req.Post,
  100. Phone: req.Phone,
  101. Wechat: req.Wechat,
  102. Mail: req.Mail,
  103. Honorific: req.Honorific,
  104. Territory: req.Territory,
  105. Remark: req.Remark,
  106. CreatedBy: int(s.userInfo.Id),
  107. CreatedName: s.userInfo.NickName,
  108. CreatedTime: gtime.Now(),
  109. UpdatedBy: int(s.userInfo.Id),
  110. UpdatedName: s.userInfo.NickName,
  111. UpdatedTime: gtime.Now(),
  112. })
  113. if err != nil {
  114. return 0, err
  115. }
  116. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  117. err = s.distSrv.AddDynamicsByCurrentUser(tx, req.DistId, "创建联系人", map[string]interface{}{"id": id})
  118. return err
  119. })
  120. return int(id), err
  121. }
  122. func (s BaseDistributorContactService) Update(ctx context.Context, req *model.BaseDistributorContactUpdateReq) error {
  123. validErr := gvalid.CheckStruct(ctx, req, nil)
  124. if validErr != nil {
  125. return myerrors.TipsError(validErr.Current().Error())
  126. }
  127. ent, err := s.Dao.Where("id = ?", req.Id).One()
  128. if err != nil {
  129. return err
  130. }
  131. if ent == nil {
  132. return myerrors.TipsError(fmt.Sprintf("联系人不存在: %d", req.Id))
  133. }
  134. dao := &s.Dao.BaseDistributorContactDao
  135. toupdate := map[string]interface{}{}
  136. if req.Name != "" {
  137. toupdate["name"] = req.Name
  138. }
  139. if req.Post != "" {
  140. toupdate["post"] = req.Post
  141. }
  142. if req.Phone != "" {
  143. toupdate["phone"] = req.Phone
  144. }
  145. if req.Wechat != nil {
  146. toupdate["wechat"] = *req.Wechat
  147. }
  148. if req.Mail != nil {
  149. toupdate["mail"] = *req.Mail
  150. }
  151. if req.Honorific != nil {
  152. toupdate["honorific"] = *req.Honorific
  153. }
  154. if req.Territory != nil {
  155. toupdate["territory"] = *req.Territory
  156. }
  157. if req.Remark != nil {
  158. toupdate["remark"] = *req.Remark
  159. }
  160. if len(toupdate) != 0 {
  161. toupdate["updated_by"] = int(s.userInfo.Id)
  162. toupdate["updated_name"] = s.userInfo.NickName
  163. toupdate["updated_time"] = gtime.Now()
  164. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  165. if err != nil {
  166. return err
  167. }
  168. }
  169. return nil
  170. }
  171. func (s BaseDistributorContactService) Delete(ctx context.Context, id []int) error {
  172. if len(id) == 0 {
  173. return nil
  174. }
  175. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  176. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  177. for _, i := range id {
  178. ent, err := s.Dao.Where("id = ?", i).Unscoped().One()
  179. if err != nil {
  180. glog.Error(err)
  181. continue
  182. }
  183. err = s.distSrv.AddDynamicsByCurrentUser(tx, ent.DistId, "删除联系人", map[string]interface{}{"id": i})
  184. if err != nil {
  185. glog.Error(err)
  186. continue
  187. }
  188. }
  189. return nil
  190. })
  191. return err
  192. }