123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- package cygx
- import (
- "errors"
- "fmt"
- "hongze/hongze_task/models"
- "hongze/hongze_task/models/cygx"
- "hongze/hongze_task/models/data_manage"
- "hongze/hongze_task/services/alarm_msg"
- "hongze/hongze_task/utils"
- "strconv"
- "strings"
- "time"
- )
- func ActivitySpecialCompanyTryOutReduce(comapnyId int) (err error) {
- defer func() {
- if err != nil {
- fmt.Println(err)
- go alarm_msg.SendAlarmMsg("正式转试用定时任务更新专项调研扣点失败:"+err.Error(), 2)
- }
- }()
- comapny, e := models.GetCompanyById(comapnyId)
- if e != nil {
- err = errors.New("GetCompanyById err:" + e.Error())
- return
- }
- userType, tripRemaining, mapChartName, e := GetChartPermissionSpecialSurplusByCompany(comapnyId)
- if e != nil {
- err = errors.New("GetChartPermissionSpecialSurplusByCompany err:" + e.Error())
- return
- }
- //获取需要添加的流水信息
- itemBill := new(data_manage.CygxActivitySpecialTripBill)
- itemBill.CreateTime = time.Now()
- itemBill.Source = 2
- itemBill.DoType = 1
- itemBill.Way = 3
- itemBill.BillDetailed = -tripRemaining
- itemBill.CompanyId = comapnyId
- itemBill.CompanyName = comapny.CompanyName
- itemBill.Content = "到期清零"
- itemBill.Total = "0"
- err = data_manage.AddCygxActivitySpecialTripBill(itemBill)
- if err != nil {
- return
- }
- //把公司下的所有点数清零
- e = cygx.UpdateCygxActivitySpecialPermissionPointsTryOutReduce(comapnyId)
- if e != nil {
- err = errors.New("GetChartPermissionSpecialSurplusByCompany err:" + e.Error())
- return
- }
- // 删除公司之前的继承表
- err = data_manage.DelCygxActivitySpecialInheritPointsByCompanyId(comapnyId)
- if err != nil {
- return
- }
- chartMap := map[string]int{utils.YI_YAO_NAME: utils.YI_YAO_ID, utils.XIAO_FEI_NAME: utils.XIAO_FEI_ID, utils.KE_JI_NAME: utils.KE_JI_ID, utils.ZHI_ZAO_NAME: utils.ZHI_ZAO_ID}
- //添加继承点数表
- inheritItems := make([]*data_manage.CygxActivitySpecialInheritPointsCompany, 0)
- if userType == 2 {
- inheritItem := new(data_manage.CygxActivitySpecialInheritPointsCompany)
- inheritItem.CompanyId = comapnyId
- inheritItem.CompanyName = comapny.CompanyName
- inheritItem.CreateTime = time.Now()
- inheritItem.ModifyTime = time.Now()
- inheritItem.Points = tripRemaining
- inheritItems = append(inheritItems, inheritItem)
- } else {
- for k, v := range mapChartName {
- inheritItem := new(data_manage.CygxActivitySpecialInheritPointsCompany)
- inheritItem.CompanyId = comapnyId
- inheritItem.CompanyName = comapny.CompanyName
- inheritItem.CreateTime = time.Now()
- inheritItem.ModifyTime = time.Now()
- inheritItem.Points = v
- inheritItem.ChartPermissionName = k
- if _, ok := chartMap[k]; ok {
- inheritItem.ChartPermissionId = chartMap[k]
- }
- inheritItems = append(inheritItems, inheritItem)
- }
- }
- if len(inheritItems) > 0 {
- err = data_manage.AddCygxActivitySpecialInheritPointsCompanyMulti(inheritItems)
- if err != nil {
- return
- }
- }
- return
- }
- // 获取 用户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户
- func GetUserType(companyId int) (userType, packageType int, permissionStrnew, companyStatus string, err error) {
- var permissionStr, permissionZhengShiStr string
- if companyId <= 1 {
- userType = 0
- } else {
- total, errs := data_manage.GetCountCompanyDetailByIdGroup(companyId)
- if errs != nil {
- err = errs
- return
- }
- if total == 0 {
- userType = 0
- } else {
- companyDetail, errs := data_manage.GetCompanyDetailByIdGroup(companyId)
- if errs != nil {
- err = errs
- return
- }
- companyStatus = companyDetail.Status
- permissionStr, errs = data_manage.GetCompanyPermission(companyId)
- if errs != nil {
- err = errs
- return
- }
- permissionStrnew = permissionStr
- //大套餐客户,数据库添加标识,
- companyUserTypeDetail, errs := data_manage.GetCygxCompanyUserType(companyId)
- if errs != nil && errs.Error() != utils.ErrNoRow() {
- err = errs
- return
- }
- if companyUserTypeDetail != nil {
- packageType = companyUserTypeDetail.PackageType
- if companyUserTypeDetail.CustomerTypeId != 0 {
- userType = companyUserTypeDetail.CustomerTypeId
- return
- }
- }
- permissionZhengShiStr, errs = data_manage.GetCompanyPermissionByUserZhengShi(companyId)
- if errs != nil {
- err = errs
- return
- }
- //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户、 10: 30W套餐客户
- //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开)
- if companyDetail.Status == "永续" {
- userType = 1
- } else if companyDetail.Status == "试用" {
- userType = 5
- } else if companyDetail.Status == "冻结" {
- userType = 6
- } else if companyDetail.Status == "流失" {
- userType = 7
- }
- //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开)
- if userType == 0 && companyDetail.Status == "正式" {
- var permissionZhegnshiNum int
- if strings.Count(permissionZhengShiStr, "医药") == 2 {
- permissionZhegnshiNum++
- }
- if strings.Count(permissionZhengShiStr, "消费") == 2 {
- permissionZhegnshiNum++
- }
- if strings.Count(permissionZhengShiStr, "科技") == 2 {
- permissionZhegnshiNum++
- }
- if strings.Count(permissionZhengShiStr, "智造") == 2 {
- permissionZhegnshiNum++
- }
- if strings.Count(permissionZhengShiStr, "策略") == 1 {
- permissionZhegnshiNum++
- }
- if strings.Count(permissionZhengShiStr, "路演服务") == 1 {
- permissionZhegnshiNum++
- }
- //if permissionZhegnshiNum == 6 {
- // userType = 2
- //} else
- //大套餐客户,数据库添加标识,条件大于等于四的都是 30W套餐客户
- if permissionZhegnshiNum >= 4 {
- userType = 10
- } else {
- userType = 3
- }
- }
- }
- }
- permissionStrnew = permissionStr
- return
- }
- // GetChartPermissionSpecialSurplusByCompany 获取公司专项调研次数-分品种
- func GetChartPermissionSpecialSurplusByCompany(companyId int) (userType int, tripRemaining int, mapChartName map[string]int, err error) {
- companyDetail, e := data_manage.GetCompanyDetailByIdGroup(companyId)
- if e != nil {
- err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
- }
- if companyDetail == nil {
- return
- }
- if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
- return
- }
- //var specialSurplus string
- //if companyDetail.Status == "永续" {
- // specialSurplus = "不限次数"
- //}
- //chartMap := map[int]string{utils.YI_YAO_ID:utils.YI_YAO_NAME, utils.XIAO_FEI_ID:utils.XIAO_FEI_NAME, utils.KE_JI_ID:utils.KE_JI_NAME, utils.ZHI_ZAO_ID:utils.ZHI_ZAO_NAME}
- //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
- //var packageType int
- // 获取继承点数
- inheritList, e := data_manage.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
- }
- listPermissionNameRai,e:= models.GetPermissionByProductIdAndClassifyName(2,"权益")
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetPermissionByProductIdAndClassifyName, Err: " + e.Error())
- }
- mapPermissionName := make(map[int]string)
- for _,v:= range listPermissionNameRai{
- mapPermissionName[v.ChartPermissionId] = v.ChartPermissionName
- }
- if companyDetail.Status == "正式" {
- userType, _, _, _, _ = GetUserType(companyId)
- var condition string
- var pars []interface{}
- condition += ` AND company_id = ? `
- pars = append(pars, companyId)
- //查询当年的数据
- condition += ` AND b.create_time >= ? `
- pars = append(pars, time.Now().Format("2006")+"-01-01")
- listTripBill, e := data_manage.GetCygxActivitySpecialTripBillList(condition, pars)
- if e != nil {
- err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
- return
- }
- for k, v := range listTripBill {
- listTripBill[k].ChartPermissionName = mapPermissionName[v.ChartPermissionId]
- }
- if userType == 2 {
- //condition += ` AND is_valid = 1 `
- //TripBillNumMap := make(map[int]int)
- //for _, v := range listTripBill {
- // TripBillNumMap[v.ChartPermissionId] += v.BillDetailed
- //}
- var TripBillNum int
- for _, v := range listTripBill {
- TripBillNum += v.BillDetailed
- }
- // CRM13.2 70w客户有16次专项调研, 45w有10次
- //packageTypeMap := map[int]int{1: 16, 2: 10}
- //totalTrip := packageTypeMap[packageType]
- tripRemaining = TripBillNum
- if tripRemaining < 0 {
- tripRemaining = 0
- }
- //for k, _ := range chartNumMap {
- // tripRemaining := totalTrip - ariborneMap[k] + TripBillNumMap[k]
- // if tripRemaining < 0 {
- // tripRemaining = 0
- // }
- // specialSurplus += chartMap[k] + strconv.Itoa(tripRemaining) + "次,"
- //}
- //specialSurplus = strings.TrimRight(specialSurplus, ",")
- //tripRemaining = 12 - tripTota
- } else {
- list, e := GetCompanyReportPermission(companyId, 2)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
- }
- if len(list) == 0 {
- return
- }
- var chartPermissionIdSlice []string
- mapChartName = make(map[string]int)
- mapUpgradeId := make(map[int]int)
- mapInheritChartName := make(map[string]int)
- mapPermissionNameTrip := make(map[string]int)
- //mapPermissionName := make(map[int]string)
- for _, v := range list {
- chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
- //是升级套餐才有点数
- if v.IsUpgrade == 1 {
- mapUpgradeId[v.ChartPermissionId] = 1
- }
- }
- chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
- if e != nil {
- err = errors.New("获取品种信息失败, Err:" + e.Error())
- return
- }
- if len(chartList) == 0 {
- return
- }
- //var TripBillNum int
- for _, v := range listTripBill {
- if v.ActivityId == 0 {
- continue
- }
- mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
- }
- for _, v := range chartList {
- //如果是升级则加点
- if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
- mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
- } else {
- mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
- }
- }
- // 通过继承获得的加点
- for _, v := range inheritList {
- mapInheritChartName[v.ChartPermissionName] = v.Points
- }
- for _, v := range mapChartName {
- if v > 0 {
- tripRemaining += v
- }
- }
- }
- }
- return
- }
|