business_contact.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package proj
  2. import (
  3. "context"
  4. custDao "dashoo.cn/micro/app/dao/cust"
  5. projDao "dashoo.cn/micro/app/dao/proj"
  6. projModel "dashoo.cn/micro/app/model/proj"
  7. "dashoo.cn/micro/app/service"
  8. "dashoo.cn/opms_libary/myerrors"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/frame/g"
  11. )
  12. type businessContactService struct {
  13. *service.ContextService
  14. Dao *projDao.ProjBusinessContactDao
  15. }
  16. func NewBusinessContactService(ctx context.Context) (svc *businessContactService, err error) {
  17. svc = new(businessContactService)
  18. if svc.ContextService, err = svc.Init(ctx); err != nil {
  19. return nil, err
  20. }
  21. svc.Dao = projDao.NewProjBusinessContactDao(svc.Tenant)
  22. return svc, nil
  23. }
  24. func (p *businessContactService) GetList(req *projModel.BusinessReq) (total int, contactList []*projModel.BusinessContact, err error) {
  25. db := p.Dao.As("bus").LeftJoin(custDao.CustCustomerContact.Table, "contact", "bus.contact_id=contact.id").
  26. Where("bus."+p.Dao.Columns.BusId, req.BusId)
  27. if req.CuctId != 0 {
  28. db = db.Where("contact.cuct_id", req.CuctId)
  29. }
  30. if req.CuctName != "" {
  31. db = db.Where("contact.cuct_name", req.BusId)
  32. }
  33. total, err = db.Count()
  34. if err != nil {
  35. g.Log().Error(err)
  36. return
  37. }
  38. err = db.Fields("bus.*, contact.*,bus.id AS id").Page(req.GetPage()).Order("bus.id desc").Scan(&contactList)
  39. return
  40. }
  41. func (p *businessContactService) Create(req *projModel.BusinessContactReq) (err error) {
  42. contactList := make([]*projModel.ProjBusinessContact, 0)
  43. for _, v := range req.ContactIds {
  44. data := new(projModel.ProjBusinessContact)
  45. data.BusId = req.BusId
  46. data.ContactId = v
  47. data.Remark = req.Remark
  48. service.SetCreatedInfo(data, p.GetCxtUserId(), p.GetCxtUserName())
  49. contactList = append(contactList, data)
  50. }
  51. b, _ := NewBusinessService(p.Ctx)
  52. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  53. _, err = p.Dao.Insert(&contactList)
  54. if err != nil {
  55. return err
  56. }
  57. // 添加项目动态
  58. dynamics := projModel.ProjBusinessDynamics{
  59. BusId: req.BusId,
  60. OpnType: "70",
  61. }
  62. err = b.CreateProjBusinessDynamics(tx, dynamics, nil)
  63. return err
  64. })
  65. return
  66. }
  67. func (p *businessContactService) DeleteByIds(ids []int64) (err error) {
  68. result, err := p.Dao.Where(projDao.ProjBusinessContact.Columns.Id+" IN(?)", ids).One()
  69. if err != nil {
  70. return err
  71. }
  72. if result == nil {
  73. return myerrors.NewMsgError(nil, "联系人不存在")
  74. }
  75. b, _ := NewBusinessService(p.Ctx)
  76. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  77. _, err = p.Dao.WhereIn(projDao.ProjBusinessContact.Columns.Id, ids).Delete()
  78. if err != nil {
  79. return err
  80. }
  81. // 添加项目动态
  82. dynamics := projModel.ProjBusinessDynamics{
  83. BusId: result.BusId,
  84. OpnType: "80",
  85. }
  86. err = b.CreateProjBusinessDynamics(tx, dynamics, nil)
  87. return err
  88. })
  89. return
  90. }