123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- "log"
- "time"
- "strings"
- qu "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
- cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/entity"
- "github.com/gogf/gf/v2/util/gconv"
- )
- // 线索相关
- type SaleChanceService struct {
- BaseUserId int64
- PositionId int64
- EntUserId int64
- EntId int64
- AppId string
- AccountId int64
- EntDeptId int64
- ChanceName string //机会名称
- Owner string //机会所有者
- Summary string //概要信息
- ChanceClassify int64 //机会分类
- ExpectedOrderTime int64 //最初预计落单段时间 时间戳
- ExpectedMoney float64 //最初预计落单金额
- CustomName string //客户全称
- BusinessType int64 //业务类型
- Remarks string //备注
- NextfollowUpTime int64 //下次跟进时间戳
- Types int64 //处理方式 1自办;2转办
- User []int64 //企业用户id
- EmployInfoId int64 //资讯收录id
- CustomId int64 //客户id
- CreateName string
- ChanceSource int64
- }
- // Add 创建机会
- func (this *SaleChanceService) Add(ctx context.Context) (int64, string) {
- nowtime := time.Now().Format(date.Date_Full_Layout)
- args := []interface{}{}
- argsTask := []interface{}{}
- argsTaskTeam := []interface{}{}
- //落单时间戳转时间
- expect_deal_time := time.Unix(this.ExpectedOrderTime, 0).Format(date.Date_Full_Layout)
- //下次跟进时间
- nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
- baseUserIdArr := []int64{}
- chanceId, taskId := int64(-1), int64(-1)
- groupId := ""
- //判断处理方式
- stageId := 0
- if stageData := cm.CrmMysql.SelectBySql("select id from config_tpl_stage where tpl_id =? order by `order` limit 1", this.BusinessType); stageData != nil && len(*stageData) > 0 {
- stageId = gconv.Int((*stageData)[0]["id"])
- }
- //转办
- if this.Types == 2 {
- transferArr := []int64{}
- for _, v := range this.User {
- i_entuserid := v
- resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
- Id: i_entuserid,
- })
- if err != nil {
- log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
- return -1, groupId
- } else if resp == nil {
- log.Println("entuser用户", i_entuserid, "没有找到职位信息")
- return -1, groupId
- }
- transferArr = append(transferArr, resp.PositionId)
- baseUserIdArr = append(baseUserIdArr, resp.UserId)
- }
- args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.CustomId, this.BusinessType, this.ChanceName, this.Summary, this.ChanceClassify, qu.If(this.ExpectedOrderTime == 0, nil, expect_deal_time), qu.If(this.ExpectedMoney == 0, nil, this.ExpectedMoney), qu.If(this.Remarks == "", nil, this.Remarks), nowtime, this.CreateName, this.ChanceSource)
- //任务
- argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
- chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, transferArr)
- if chanceId < 0 {
- return -1, groupId
- }
- } else if this.Types == 1 {
- //线索机会
- args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.CustomId, this.BusinessType, this.ChanceName, this.Summary, this.ChanceClassify, qu.If(this.ExpectedOrderTime == 0, nil, expect_deal_time), qu.If(this.ExpectedMoney == 0, nil, this.ExpectedMoney), qu.If(this.Remarks == "", nil, this.Remarks), nowtime, this.CreateName, this.ChanceSource)
- //任务
- argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
- //任务团队
- argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
- //存库
- chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, []int64{})
- if chanceId < 0 {
- return -1, groupId
- }
- baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
- // //自办 创建 群聊
- // gp := &Group{
- // EntId: this.EntId,
- // PositionId: this.PositionId,
- // UserIdArr: []int64{},
- // AppId: this.AppId,
- // EntUserId: this.EntUserId,
- // AccountId: this.AccountId,
- // }
- // groupId = gp.GroupAdd()
- } else if this.Types == 3 {
- transferArr := []int64{}
- transferArr = append(transferArr, this.PositionId)
- args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.CustomId, this.BusinessType, this.ChanceName, this.Summary, this.ChanceClassify, qu.If(this.ExpectedOrderTime == 0, nil, expect_deal_time), qu.If(this.ExpectedMoney == 0, nil, this.ExpectedMoney), qu.If(this.Remarks == "", nil, this.Remarks), nowtime, this.CreateName, this.ChanceSource)
- //任务
- argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
- chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, transferArr)
- if chanceId < 0 {
- return -1, groupId
- }
- baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
- }
- u := &User{BaseUserIds: baseUserIdArr}
- if this.Types != 3 {
- var kb strings.Builder
- var vb strings.Builder
- for k, v := range u.GetUserId(this.EntId) {
- kb.WriteString(k + ",")
- vb.WriteString(gconv.String(v) + ",")
- userId := strings.TrimRight(kb.String(), ",")
- positionId := strings.TrimRight(vb.String(), ",")
- next := strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
- content := ""
- pcHref := ""
- if this.Types == 1 {
- pcHref = fmt.Sprintf(cm.Push.SaleChance.Create.MyselfPcHref, positionId, taskId, chanceId)
- content = fmt.Sprintf(cm.Push.SaleChance.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
- } else if this.Types == 2 {
- pcHref = fmt.Sprintf(cm.Push.SaleChance.Create.TransferPcHref, positionId)
- content = fmt.Sprintf(cm.Push.SaleChance.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
- }
- StationMailPush(userId, positionId, cm.Push.SaleChance.Create.Title, content, pcHref, cm.Push.SaleChance.Create.MobileHref, "11")
- }
- }
- return 1, groupId
- }
- // SaleChangeAdd 机会存储
- func SaleChangeAdd(tx *sql.Tx, args []interface{}) int64 {
- fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "custom_id", "tpl_id", "name", "summary", "classify", "expect_deal_time", "expect_deal_amount", "remark", "create_time", "create_person", "source"}
- _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CHANCE, fields, args)
- //
- return id
- }
- // Save 存库
- func (this *SaleChanceService) SaveChange(ctx context.Context, argsChange, argsTask, argsTaskTeam []interface{}, transferArr []int64) (int64, int64) {
- //存库
- changeId, taskId := int64(-1), int64(-1)
- cm.CrmMysql.ExecTx("创建机会", func(tx *sql.Tx) bool {
- //
- employ_info_id_new, _ := EmployUpdate(tx, this.EmployInfoId, 0, this.PositionId, "is_create_chance", this.EntId, this.EntUserId, this.EntDeptId)
- argsChange[3] = employ_info_id_new
- //插入机会
- changeId = SaleChangeAdd(tx, argsChange)
- //传过来的argTask没有来源id,需要append
- argsTask = append(argsTask, changeId)
- //任务车存储
- taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, this.PositionId)
- //操作台帐
- ok1 := SaveLedger(ctx, this.PositionId, changeId, taskId, "创建销售机会", fmt.Sprintf("%s创建了销售机会", this.CreateName), this.CreateName)
- //任务执行计划
- planId := TaskExecutePlanAdd(tx, this.BusinessType, taskId)
- //销售漏斗
- nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
- argsSalesFunnel := []interface{}{
- changeId,
- taskId,
- this.ChanceName,
- this.Summary,
- nextFollowTime,
- this.ExpectedMoney,
- this.BusinessType,
- this.EntId,
- this.EmployInfoId,
- }
- SalesFunnel := saveSalesFunnel(tx, argsSalesFunnel, this.BusinessType)
- if changeId > 0 && taskId > 0 && ok1 && planId > 0 && SalesFunnel > 0 {
- return true
- }
- log.Println("save change err: ", changeId, taskId, ok1, planId, SalesFunnel)
- return false
- })
- return changeId, taskId
- }
- // TaskExecutePlanAdd 任务执行计划存储
- func TaskExecutePlanAdd(tx *sql.Tx, tplId, taskId int64) int64 {
- args := []interface{}{}
- data := cm.CrmMysql.SelectBySqlByTx(tx, `SELECT a.name,b.name,b.id FROM config_tpl_stage a
- INNER JOIN config_stage_matter b ON (b.stage_id=a.id)
- where a.tpl_id=?
- order by a.id,b.id `, tplId)
- if data != nil && len(*data) > 0 {
- for _, v := range *data {
- matterId := gconv.Int(v["id"])
- args = append(args, taskId, matterId)
- }
- _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_EXECUTE_PLAN, []string{"task_id", "matter_id"}, args)
- return id
- }
- return 0
- }
|