business_contact.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package proj
  2. import (
  3. "context"
  4. baseDao "dashoo.cn/micro/app/dao/base"
  5. custDao "dashoo.cn/micro/app/dao/cust"
  6. projDao "dashoo.cn/micro/app/dao/proj"
  7. projModel "dashoo.cn/micro/app/model/proj"
  8. "dashoo.cn/micro/app/service"
  9. "dashoo.cn/micro/app/service/base"
  10. "dashoo.cn/micro/app/service/cust"
  11. "dashoo.cn/opms_libary/myerrors"
  12. "github.com/gogf/gf/container/gset"
  13. "github.com/gogf/gf/database/gdb"
  14. "github.com/gogf/gf/frame/g"
  15. "github.com/gogf/gf/util/gconv"
  16. )
  17. type businessContactService struct {
  18. *service.ContextService
  19. Dao *projDao.ProjBusinessContactDao
  20. }
  21. func NewBusinessContactService(ctx context.Context) (svc *businessContactService, err error) {
  22. svc = new(businessContactService)
  23. if svc.ContextService, err = svc.Init(ctx); err != nil {
  24. return nil, err
  25. }
  26. svc.Dao = projDao.NewProjBusinessContactDao(svc.Tenant)
  27. return svc, nil
  28. }
  29. func (p *businessContactService) GetList(req *projModel.BusinessContactSearchReq) (total int, contactList []*projModel.BusinessContact, err error) {
  30. db := p.Dao.As("bus").Unscoped().WhereNull("bus."+p.Dao.C.DeletedTime).
  31. LeftJoin(custDao.CustCustomerContact.Table, "cust", "bus.contact_id=cust.id AND bus.contact_type='10' AND `cust`.`deleted_time` IS NULL ").
  32. LeftJoin(baseDao.BaseDistributorContact.Table, "dist", "bus.contact_id=dist.id AND bus.contact_type='20' AND `dist`.`deleted_time` IS NULL ").
  33. Where("bus."+p.Dao.C.BusId, req.BusId)
  34. if req.CuctId != 0 {
  35. db = db.Where("contact.cuct_id", req.CuctId)
  36. }
  37. if req.CuctName != "" {
  38. db = db.Where("contact.cuct_name = ? or dist.name = ?", req.CuctName, req.CuctName)
  39. }
  40. total, err = db.Count()
  41. if err != nil {
  42. g.Log().Error(err)
  43. return
  44. }
  45. fields := "bus.*, case when bus.contact_type ='10' then cust.cuct_name when bus.contact_type ='20' then dist.name end as name," +
  46. "case when bus.contact_type ='10' then cust.cuct_name when bus.contact_type ='20' then dist.name end as cuct_name," +
  47. "case when bus.contact_type ='10' then cust.postion when bus.contact_type ='20' then dist.post end as postion," +
  48. "case when bus.contact_type ='10' then cust.telephone when bus.contact_type ='20' then dist.phone end as telephone," +
  49. "case when bus.contact_type ='10' then cust.wechat when bus.contact_type ='20' then dist.wechat end as wechat," +
  50. "case when bus.contact_type ='10' then cust.email when bus.contact_type ='20' then dist.mail end as email," +
  51. "case when bus.contact_type ='10' then cust.office_location when bus.contact_type ='20' then dist.territory end as office_location"
  52. err = db.Fields(fields).Page(req.GetPage()).Order("bus.id desc").Scan(&contactList)
  53. return
  54. }
  55. func (p *businessContactService) Create(req *projModel.BusinessContactReq) (err error) {
  56. dbContactIds, err := p.Dao.Fields(p.Dao.C.ContactId).Where(p.Dao.C.BusId, req.BusId).Where(p.Dao.C.ContactType, req.ContactType).Array()
  57. if err != nil {
  58. g.Log().Error(err)
  59. return myerrors.DbError("查询项目联系人失败")
  60. }
  61. contactIds := gset.NewIntSetFrom(req.ContactIds, true)
  62. ids := gset.NewIntSetFrom(gconv.Ints(dbContactIds), true)
  63. contactIds = contactIds.Diff(ids)
  64. if contactIds.Size() == 0 {
  65. return myerrors.TipsError("该联系人已关联,请勿重复操作")
  66. }
  67. contactList := make([]*projModel.ProjBusinessContact, 0)
  68. for _, v := range contactIds.Slice() {
  69. data := new(projModel.ProjBusinessContact)
  70. data.BusId = req.BusId
  71. data.ContactId = v
  72. data.ContactType = req.ContactType
  73. data.Remark = req.Remark
  74. service.SetCreatedInfo(data, p.GetCxtUserId(), p.GetCxtUserName())
  75. contactList = append(contactList, data)
  76. }
  77. busSrv, _ := NewBusinessService(p.Ctx)
  78. busInfo, err := busSrv.GetEntityById(int64(req.BusId))
  79. if err != nil {
  80. return err
  81. }
  82. updateBusData := g.Map{}
  83. if busInfo.ContactId == 0 && req.ContactType == "10" {
  84. custSrv, _ := cust.NewCustomerContactService(p.Ctx)
  85. info, err := custSrv.GetEntityById(int64(req.ContactIds[0]))
  86. if err != nil {
  87. return err
  88. }
  89. updateBusData[projDao.ProjBusiness.C.ContactId] = info.Id
  90. updateBusData[projDao.ProjBusiness.C.ContactName] = info.CuctName
  91. updateBusData[projDao.ProjBusiness.C.ContactPostion] = info.Postion
  92. updateBusData[projDao.ProjBusiness.C.ContactTelephone] = info.Telephone + " / " + info.Wechat
  93. }
  94. if busInfo.DealerSalesId == 0 && req.ContactType == "20" {
  95. distSrv, _ := base.NewBaseDistributorContactService(p.Ctx)
  96. info, err := distSrv.GetEntityById(int64(req.ContactIds[0]))
  97. if err != nil {
  98. return err
  99. }
  100. updateBusData[projDao.ProjBusiness.C.DealerSalesId] = info.Id
  101. updateBusData[projDao.ProjBusiness.C.DealerSalesName] = info.Name
  102. updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = info.Phone + " / " + info.Wechat
  103. }
  104. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  105. _, err = p.Dao.Insert(&contactList)
  106. if err != nil {
  107. return err
  108. }
  109. if len(updateBusData) > 0 {
  110. // 清理项目联系人信息
  111. err = busSrv.UpdateBusinessContactInfo(tx, busInfo.Id, updateBusData)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. // 添加项目动态
  117. dynamics := projModel.ProjBusinessDynamics{
  118. BusId: req.BusId,
  119. OpnType: OpnAssociation,
  120. }
  121. _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
  122. return err
  123. })
  124. return
  125. }
  126. func (p *businessContactService) DeleteByIds(req *projModel.DeleteBusinessContactReq) (err error) {
  127. busSrv, _ := NewBusinessService(p.Ctx)
  128. busInfo, err := busSrv.GetEntityById(req.BusId)
  129. if err != nil {
  130. return err
  131. }
  132. list, err := p.Dao.Where(p.Dao.C.BusId, req.BusId).All()
  133. if err != nil {
  134. return err
  135. }
  136. if list == nil || len(list) == 0 {
  137. return myerrors.TipsError("联系人不存在")
  138. }
  139. delIds := gset.NewIntSetFrom(req.ContactIds, true)
  140. allIds := gset.NewIntSet(true)
  141. for _, item := range list {
  142. allIds.Add(item.Id)
  143. }
  144. if delIds.Diff(allIds).Size() > 0 {
  145. return myerrors.TipsError("联系人不存在")
  146. }
  147. updateBusData := g.Map{}
  148. var custContactId int
  149. var dealerSalesId int
  150. for _, item := range list {
  151. if item.ContactType == "10" {
  152. if item.ContactId == busInfo.ContactId && delIds.Contains(item.Id) {
  153. updateBusData[projDao.ProjBusiness.C.ContactId] = 0
  154. updateBusData[projDao.ProjBusiness.C.ContactName] = ""
  155. updateBusData[projDao.ProjBusiness.C.ContactPostion] = ""
  156. updateBusData[projDao.ProjBusiness.C.ContactTelephone] = ""
  157. } else {
  158. custContactId = item.ContactId
  159. }
  160. }
  161. if item.ContactType == "20" {
  162. if item.ContactId == busInfo.DealerSalesId && delIds.Contains(item.Id) {
  163. updateBusData[projDao.ProjBusiness.C.DealerSalesId] = 0
  164. updateBusData[projDao.ProjBusiness.C.DealerSalesName] = ""
  165. updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = ""
  166. } else {
  167. dealerSalesId = item.ContactId
  168. }
  169. }
  170. }
  171. if _, ok := updateBusData[projDao.ProjBusiness.C.ContactId]; ok && custContactId != 0 {
  172. custSrv, _ := cust.NewCustomerContactService(p.Ctx)
  173. info, err := custSrv.GetEntityById(int64(custContactId))
  174. if err != nil {
  175. return err
  176. }
  177. updateBusData[projDao.ProjBusiness.C.ContactId] = info.Id
  178. updateBusData[projDao.ProjBusiness.C.ContactName] = info.CuctName
  179. updateBusData[projDao.ProjBusiness.C.ContactPostion] = info.Postion
  180. updateBusData[projDao.ProjBusiness.C.ContactTelephone] = info.Telephone + "/" + info.Wechat
  181. }
  182. if _, ok := updateBusData[projDao.ProjBusiness.C.DealerSalesId]; ok && dealerSalesId != 0 {
  183. distSrv, _ := base.NewBaseDistributorContactService(p.Ctx)
  184. info, err := distSrv.GetEntityById(int64(dealerSalesId))
  185. if err != nil {
  186. return err
  187. }
  188. updateBusData[projDao.ProjBusiness.C.DealerSalesId] = info.Id
  189. updateBusData[projDao.ProjBusiness.C.DealerSalesName] = info.Name
  190. updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = info.Phone + "/" + info.Wechat
  191. }
  192. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  193. _, err = p.Dao.Where(p.Dao.C.BusId, req.BusId).WhereIn(p.Dao.C.Id, req.ContactIds).Delete()
  194. if err != nil {
  195. return err
  196. }
  197. if len(updateBusData) > 0 {
  198. // 清理项目联系人信息
  199. err = busSrv.UpdateBusinessContactInfo(tx, busInfo.Id, updateBusData)
  200. if err != nil {
  201. return err
  202. }
  203. }
  204. // 添加项目动态
  205. dynamics := projModel.ProjBusinessDynamics{
  206. BusId: busInfo.Id,
  207. OpnType: OpnDisassociation,
  208. }
  209. _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
  210. return err
  211. })
  212. return
  213. }