| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package proj
- import (
- "context"
- baseDao "dashoo.cn/micro/app/dao/base"
- custDao "dashoo.cn/micro/app/dao/cust"
- projDao "dashoo.cn/micro/app/dao/proj"
- projModel "dashoo.cn/micro/app/model/proj"
- "dashoo.cn/micro/app/service"
- "dashoo.cn/micro/app/service/base"
- "dashoo.cn/micro/app/service/cust"
- "dashoo.cn/opms_libary/myerrors"
- "github.com/gogf/gf/container/gset"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/util/gconv"
- )
- type businessContactService struct {
- *service.ContextService
- Dao *projDao.ProjBusinessContactDao
- }
- func NewBusinessContactService(ctx context.Context) (svc *businessContactService, err error) {
- svc = new(businessContactService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = projDao.NewProjBusinessContactDao(svc.Tenant)
- return svc, nil
- }
- func (p *businessContactService) GetList(req *projModel.BusinessContactSearchReq) (total int, contactList []*projModel.BusinessContact, err error) {
- db := p.Dao.As("bus").Unscoped().WhereNull("bus."+p.Dao.C.DeletedTime).
- LeftJoin(custDao.CustCustomerContact.Table, "cust", "bus.contact_id=cust.id AND bus.contact_type='10' AND `cust`.`deleted_time` IS NULL ").
- LeftJoin(baseDao.BaseDistributorContact.Table, "dist", "bus.contact_id=dist.id AND bus.contact_type='20' AND `dist`.`deleted_time` IS NULL ").
- Where("bus."+p.Dao.C.BusId, req.BusId)
- if req.CuctId != 0 {
- db = db.Where("contact.cuct_id", req.CuctId)
- }
- if req.CuctName != "" {
- db = db.Where("contact.cuct_name = ? or dist.name = ?", req.CuctName, req.CuctName)
- }
- total, err = db.Count()
- if err != nil {
- g.Log().Error(err)
- return
- }
- fields := "bus.*, case when bus.contact_type ='10' then cust.cuct_name when bus.contact_type ='20' then dist.name end as name," +
- "case when bus.contact_type ='10' then cust.cuct_name when bus.contact_type ='20' then dist.name end as cuct_name," +
- "case when bus.contact_type ='10' then cust.postion when bus.contact_type ='20' then dist.post end as postion," +
- "case when bus.contact_type ='10' then cust.telephone when bus.contact_type ='20' then dist.phone end as telephone," +
- "case when bus.contact_type ='10' then cust.wechat when bus.contact_type ='20' then dist.wechat end as wechat," +
- "case when bus.contact_type ='10' then cust.email when bus.contact_type ='20' then dist.mail end as email," +
- "case when bus.contact_type ='10' then cust.office_location when bus.contact_type ='20' then dist.territory end as office_location"
- err = db.Fields(fields).Page(req.GetPage()).Order("bus.id desc").Scan(&contactList)
- return
- }
- func (p *businessContactService) Create(req *projModel.BusinessContactReq) (err error) {
- dbContactIds, err := p.Dao.Fields(p.Dao.C.ContactId).Where(p.Dao.C.BusId, req.BusId).Where(p.Dao.C.ContactType, req.ContactType).Array()
- if err != nil {
- g.Log().Error(err)
- return myerrors.DbError("查询项目联系人失败")
- }
- contactIds := gset.NewIntSetFrom(req.ContactIds, true)
- ids := gset.NewIntSetFrom(gconv.Ints(dbContactIds), true)
- contactIds = contactIds.Diff(ids)
- if contactIds.Size() == 0 {
- return myerrors.TipsError("该联系人已关联,请勿重复操作")
- }
- contactList := make([]*projModel.ProjBusinessContact, 0)
- for _, v := range contactIds.Slice() {
- data := new(projModel.ProjBusinessContact)
- data.BusId = req.BusId
- data.ContactId = v
- data.ContactType = req.ContactType
- data.Remark = req.Remark
- service.SetCreatedInfo(data, p.GetCxtUserId(), p.GetCxtUserName())
- contactList = append(contactList, data)
- }
- busSrv, _ := NewBusinessService(p.Ctx)
- busInfo, err := busSrv.GetEntityById(int64(req.BusId))
- if err != nil {
- return err
- }
- updateBusData := g.Map{}
- if busInfo.ContactId == 0 && req.ContactType == "10" {
- custSrv, _ := cust.NewCustomerContactService(p.Ctx)
- info, err := custSrv.GetEntityById(int64(req.ContactIds[0]))
- if err != nil {
- return err
- }
- updateBusData[projDao.ProjBusiness.C.ContactId] = info.Id
- updateBusData[projDao.ProjBusiness.C.ContactName] = info.CuctName
- updateBusData[projDao.ProjBusiness.C.ContactPostion] = info.Postion
- updateBusData[projDao.ProjBusiness.C.ContactTelephone] = info.Telephone + " / " + info.Wechat
- }
- if busInfo.DealerSalesId == 0 && req.ContactType == "20" {
- distSrv, _ := base.NewBaseDistributorContactService(p.Ctx)
- info, err := distSrv.GetEntityById(int64(req.ContactIds[0]))
- if err != nil {
- return err
- }
- updateBusData[projDao.ProjBusiness.C.DealerSalesId] = info.Id
- updateBusData[projDao.ProjBusiness.C.DealerSalesName] = info.Name
- updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = info.Phone + " / " + info.Wechat
- }
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- _, err = p.Dao.Insert(&contactList)
- if err != nil {
- return err
- }
- if len(updateBusData) > 0 {
- // 清理项目联系人信息
- err = busSrv.UpdateBusinessContactInfo(tx, busInfo.Id, updateBusData)
- if err != nil {
- return err
- }
- }
- // 添加项目动态
- dynamics := projModel.ProjBusinessDynamics{
- BusId: req.BusId,
- OpnType: OpnAssociation,
- }
- _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
- return err
- })
- return
- }
- func (p *businessContactService) DeleteByIds(req *projModel.DeleteBusinessContactReq) (err error) {
- busSrv, _ := NewBusinessService(p.Ctx)
- busInfo, err := busSrv.GetEntityById(req.BusId)
- if err != nil {
- return err
- }
- list, err := p.Dao.Where(p.Dao.C.BusId, req.BusId).All()
- if err != nil {
- return err
- }
- if list == nil || len(list) == 0 {
- return myerrors.TipsError("联系人不存在")
- }
- delIds := gset.NewIntSetFrom(req.ContactIds, true)
- allIds := gset.NewIntSet(true)
- for _, item := range list {
- allIds.Add(item.Id)
- }
- if delIds.Diff(allIds).Size() > 0 {
- return myerrors.TipsError("联系人不存在")
- }
- updateBusData := g.Map{}
- var custContactId int
- var dealerSalesId int
- for _, item := range list {
- if item.ContactType == "10" {
- if item.ContactId == busInfo.ContactId && delIds.Contains(item.Id) {
- updateBusData[projDao.ProjBusiness.C.ContactId] = 0
- updateBusData[projDao.ProjBusiness.C.ContactName] = ""
- updateBusData[projDao.ProjBusiness.C.ContactPostion] = ""
- updateBusData[projDao.ProjBusiness.C.ContactTelephone] = ""
- } else {
- custContactId = item.ContactId
- }
- }
- if item.ContactType == "20" {
- if item.ContactId == busInfo.DealerSalesId && delIds.Contains(item.Id) {
- updateBusData[projDao.ProjBusiness.C.DealerSalesId] = 0
- updateBusData[projDao.ProjBusiness.C.DealerSalesName] = ""
- updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = ""
- } else {
- dealerSalesId = item.ContactId
- }
- }
- }
- if _, ok := updateBusData[projDao.ProjBusiness.C.ContactId]; ok && custContactId != 0 {
- custSrv, _ := cust.NewCustomerContactService(p.Ctx)
- info, err := custSrv.GetEntityById(int64(custContactId))
- if err != nil {
- return err
- }
- updateBusData[projDao.ProjBusiness.C.ContactId] = info.Id
- updateBusData[projDao.ProjBusiness.C.ContactName] = info.CuctName
- updateBusData[projDao.ProjBusiness.C.ContactPostion] = info.Postion
- updateBusData[projDao.ProjBusiness.C.ContactTelephone] = info.Telephone + "/" + info.Wechat
- }
- if _, ok := updateBusData[projDao.ProjBusiness.C.DealerSalesId]; ok && dealerSalesId != 0 {
- distSrv, _ := base.NewBaseDistributorContactService(p.Ctx)
- info, err := distSrv.GetEntityById(int64(dealerSalesId))
- if err != nil {
- return err
- }
- updateBusData[projDao.ProjBusiness.C.DealerSalesId] = info.Id
- updateBusData[projDao.ProjBusiness.C.DealerSalesName] = info.Name
- updateBusData[projDao.ProjBusiness.C.DealerSalesContact] = info.Phone + "/" + info.Wechat
- }
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- _, err = p.Dao.Where(p.Dao.C.BusId, req.BusId).WhereIn(p.Dao.C.Id, req.ContactIds).Delete()
- if err != nil {
- return err
- }
- if len(updateBusData) > 0 {
- // 清理项目联系人信息
- err = busSrv.UpdateBusinessContactInfo(tx, busInfo.Id, updateBusData)
- if err != nil {
- return err
- }
- }
- // 添加项目动态
- dynamics := projModel.ProjBusinessDynamics{
- BusId: busInfo.Id,
- OpnType: OpnDisassociation,
- }
- _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
- return err
- })
- return
- }
|