|
|
@@ -2,10 +2,13 @@ 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"
|
|
|
@@ -28,26 +31,34 @@ func NewBusinessContactService(ctx context.Context) (svc *businessContactService
|
|
|
}
|
|
|
|
|
|
func (p *businessContactService) GetList(req *projModel.BusinessContactSearchReq) (total int, contactList []*projModel.BusinessContact, err error) {
|
|
|
- db := p.Dao.As("bus").LeftJoin(custDao.CustCustomerContact.Table, "contact", "bus.contact_id=contact.id").
|
|
|
+ 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", req.BusId)
|
|
|
+ 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
|
|
|
}
|
|
|
-
|
|
|
- err = db.Fields("bus.*, contact.*,bus.id AS id").Page(req.GetPage()).Order("bus.id desc").Scan(&contactList)
|
|
|
+ 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.name 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.Where(p.Dao.C.BusId, req.BusId).Fields(p.Dao.C.ContactId).Array()
|
|
|
+ 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("查询项目联系人失败")
|
|
|
@@ -63,47 +74,151 @@ func (p *businessContactService) Create(req *projModel.BusinessContactReq) (err
|
|
|
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)
|
|
|
}
|
|
|
- b, _ := NewBusinessService(p.Ctx)
|
|
|
+ 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 = b.CreateProjBusinessDynamics(tx, dynamics, nil)
|
|
|
+ _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
|
|
|
return err
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func (p *businessContactService) DeleteByIds(ids []int64) (err error) {
|
|
|
- result, err := p.Dao.Where(projDao.ProjBusinessContact.C.Id+" IN(?)", ids).One()
|
|
|
+func (p *businessContactService) DeleteByIds(req *projModel.DeleteBusinessContactReq) (err error) {
|
|
|
+ busSrv, _ := NewBusinessService(p.Ctx)
|
|
|
+ busInfo, err := busSrv.GetEntityById(req.BusId)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if result == nil {
|
|
|
+ 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("联系人不存在")
|
|
|
}
|
|
|
- b, _ := NewBusinessService(p.Ctx)
|
|
|
+ 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] = ""
|
|
|
+ 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] = ""
|
|
|
+ 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.WhereIn(projDao.ProjBusinessContact.C.Id, ids).Delete()
|
|
|
+ _, 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: result.BusId,
|
|
|
+ BusId: busInfo.Id,
|
|
|
OpnType: OpnDisassociation,
|
|
|
}
|
|
|
- _, err = b.CreateProjBusinessDynamics(tx, dynamics, nil)
|
|
|
+ _, err = busSrv.CreateProjBusinessDynamics(tx, dynamics, nil)
|
|
|
return err
|
|
|
})
|
|
|
return
|