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 }