activity_special.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/models/cygx"
  7. "hongze/hongze_task/models/data_manage"
  8. "hongze/hongze_task/services/alarm_msg"
  9. "hongze/hongze_task/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func ActivitySpecialCompanyTryOutReduce(comapnyId int) (err error) {
  15. defer func() {
  16. if err != nil {
  17. fmt.Println(err)
  18. go alarm_msg.SendAlarmMsg("正式转试用定时任务更新专项调研扣点失败:"+err.Error(), 2)
  19. }
  20. }()
  21. comapny, e := models.GetCompanyById(comapnyId)
  22. if e != nil {
  23. err = errors.New("GetCompanyById err:" + e.Error())
  24. return
  25. }
  26. userType, tripRemaining, mapChartName, e := GetChartPermissionSpecialSurplusByCompany(comapnyId)
  27. if e != nil {
  28. err = errors.New("GetChartPermissionSpecialSurplusByCompany err:" + e.Error())
  29. return
  30. }
  31. //获取需要添加的流水信息
  32. itemBill := new(data_manage.CygxActivitySpecialTripBill)
  33. itemBill.CreateTime = time.Now()
  34. itemBill.Source = 2
  35. itemBill.DoType = 1
  36. itemBill.Way = 3
  37. itemBill.BillDetailed = -tripRemaining
  38. itemBill.CompanyId = comapnyId
  39. itemBill.CompanyName = comapny.CompanyName
  40. itemBill.Content = "到期清零"
  41. itemBill.Total = "0"
  42. err = data_manage.AddCygxActivitySpecialTripBill(itemBill)
  43. if err != nil {
  44. return
  45. }
  46. //把公司下的所有点数清零
  47. e = cygx.UpdateCygxActivitySpecialPermissionPointsTryOutReduce(comapnyId)
  48. if e != nil {
  49. err = errors.New("GetChartPermissionSpecialSurplusByCompany err:" + e.Error())
  50. return
  51. }
  52. // 删除公司之前的继承表
  53. err = data_manage.DelCygxActivitySpecialInheritPointsByCompanyId(comapnyId)
  54. if err != nil {
  55. return
  56. }
  57. 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}
  58. //添加继承点数表
  59. inheritItems := make([]*data_manage.CygxActivitySpecialInheritPointsCompany, 0)
  60. if userType == 2 {
  61. inheritItem := new(data_manage.CygxActivitySpecialInheritPointsCompany)
  62. inheritItem.CompanyId = comapnyId
  63. inheritItem.CompanyName = comapny.CompanyName
  64. inheritItem.CreateTime = time.Now()
  65. inheritItem.ModifyTime = time.Now()
  66. inheritItem.Points = tripRemaining
  67. inheritItems = append(inheritItems, inheritItem)
  68. } else {
  69. for k, v := range mapChartName {
  70. inheritItem := new(data_manage.CygxActivitySpecialInheritPointsCompany)
  71. inheritItem.CompanyId = comapnyId
  72. inheritItem.CompanyName = comapny.CompanyName
  73. inheritItem.CreateTime = time.Now()
  74. inheritItem.ModifyTime = time.Now()
  75. inheritItem.Points = v
  76. inheritItem.ChartPermissionName = k
  77. if _, ok := chartMap[k]; ok {
  78. inheritItem.ChartPermissionId = chartMap[k]
  79. }
  80. inheritItems = append(inheritItems, inheritItem)
  81. }
  82. }
  83. if len(inheritItems) > 0 {
  84. err = data_manage.AddCygxActivitySpecialInheritPointsCompanyMulti(inheritItems)
  85. if err != nil {
  86. return
  87. }
  88. }
  89. return
  90. }
  91. // 获取 用户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户
  92. func GetUserType(companyId int) (userType, packageType int, permissionStrnew, companyStatus string, err error) {
  93. var permissionStr, permissionZhengShiStr string
  94. if companyId <= 1 {
  95. userType = 0
  96. } else {
  97. total, errs := data_manage.GetCountCompanyDetailByIdGroup(companyId)
  98. if errs != nil {
  99. err = errs
  100. return
  101. }
  102. if total == 0 {
  103. userType = 0
  104. } else {
  105. companyDetail, errs := data_manage.GetCompanyDetailByIdGroup(companyId)
  106. if errs != nil {
  107. err = errs
  108. return
  109. }
  110. companyStatus = companyDetail.Status
  111. permissionStr, errs = data_manage.GetCompanyPermission(companyId)
  112. if errs != nil {
  113. err = errs
  114. return
  115. }
  116. permissionStrnew = permissionStr
  117. //大套餐客户,数据库添加标识,
  118. companyUserTypeDetail, errs := data_manage.GetCygxCompanyUserType(companyId)
  119. if errs != nil && errs.Error() != utils.ErrNoRow() {
  120. err = errs
  121. return
  122. }
  123. if companyUserTypeDetail != nil {
  124. packageType = companyUserTypeDetail.PackageType
  125. if companyUserTypeDetail.CustomerTypeId != 0 {
  126. userType = companyUserTypeDetail.CustomerTypeId
  127. return
  128. }
  129. }
  130. permissionZhengShiStr, errs = data_manage.GetCompanyPermissionByUserZhengShi(companyId)
  131. if errs != nil {
  132. err = errs
  133. return
  134. }
  135. //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户、 10: 30W套餐客户
  136. //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开)
  137. if companyDetail.Status == "永续" {
  138. userType = 1
  139. } else if companyDetail.Status == "试用" {
  140. userType = 5
  141. } else if companyDetail.Status == "冻结" {
  142. userType = 6
  143. } else if companyDetail.Status == "流失" {
  144. userType = 7
  145. }
  146. //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开)
  147. if userType == 0 && companyDetail.Status == "正式" {
  148. var permissionZhegnshiNum int
  149. if strings.Count(permissionZhengShiStr, "医药") == 2 {
  150. permissionZhegnshiNum++
  151. }
  152. if strings.Count(permissionZhengShiStr, "消费") == 2 {
  153. permissionZhegnshiNum++
  154. }
  155. if strings.Count(permissionZhengShiStr, "科技") == 2 {
  156. permissionZhegnshiNum++
  157. }
  158. if strings.Count(permissionZhengShiStr, "智造") == 2 {
  159. permissionZhegnshiNum++
  160. }
  161. if strings.Count(permissionZhengShiStr, "策略") == 1 {
  162. permissionZhegnshiNum++
  163. }
  164. if strings.Count(permissionZhengShiStr, "路演服务") == 1 {
  165. permissionZhegnshiNum++
  166. }
  167. //if permissionZhegnshiNum == 6 {
  168. // userType = 2
  169. //} else
  170. //大套餐客户,数据库添加标识,条件大于等于四的都是 30W套餐客户
  171. if permissionZhegnshiNum >= 4 {
  172. userType = 10
  173. } else {
  174. userType = 3
  175. }
  176. }
  177. }
  178. }
  179. permissionStrnew = permissionStr
  180. return
  181. }
  182. // GetChartPermissionSpecialSurplusByCompany 获取公司专项调研次数-分品种
  183. func GetChartPermissionSpecialSurplusByCompany(companyId int) (userType int, tripRemaining int, mapChartName map[string]int, err error) {
  184. companyDetail, e := data_manage.GetCompanyDetailByIdGroup(companyId)
  185. if e != nil {
  186. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  187. }
  188. if companyDetail == nil {
  189. return
  190. }
  191. if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
  192. return
  193. }
  194. //var specialSurplus string
  195. //if companyDetail.Status == "永续" {
  196. // specialSurplus = "不限次数"
  197. //}
  198. //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}
  199. //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
  200. //var packageType int
  201. // 获取继承点数
  202. inheritList, e := data_manage.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
  203. if e != nil && e.Error() != utils.ErrNoRow() {
  204. err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
  205. }
  206. listPermissionNameRai,e:= models.GetPermissionByProductIdAndClassifyName(2,"权益")
  207. if e != nil && e.Error() != utils.ErrNoRow() {
  208. err = errors.New("GetPermissionByProductIdAndClassifyName, Err: " + e.Error())
  209. }
  210. mapPermissionName := make(map[int]string)
  211. for _,v:= range listPermissionNameRai{
  212. mapPermissionName[v.ChartPermissionId] = v.ChartPermissionName
  213. }
  214. if companyDetail.Status == "正式" {
  215. userType, _, _, _, _ = GetUserType(companyId)
  216. var condition string
  217. var pars []interface{}
  218. condition += ` AND company_id = ? `
  219. pars = append(pars, companyId)
  220. //查询当年的数据
  221. condition += ` AND b.create_time >= ? `
  222. pars = append(pars, time.Now().Format("2006")+"-01-01")
  223. listTripBill, e := data_manage.GetCygxActivitySpecialTripBillList(condition, pars)
  224. if e != nil {
  225. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  226. return
  227. }
  228. for k, v := range listTripBill {
  229. listTripBill[k].ChartPermissionName = mapPermissionName[v.ChartPermissionId]
  230. }
  231. if userType == 2 {
  232. //condition += ` AND is_valid = 1 `
  233. //TripBillNumMap := make(map[int]int)
  234. //for _, v := range listTripBill {
  235. // TripBillNumMap[v.ChartPermissionId] += v.BillDetailed
  236. //}
  237. var TripBillNum int
  238. for _, v := range listTripBill {
  239. TripBillNum += v.BillDetailed
  240. }
  241. // CRM13.2 70w客户有16次专项调研, 45w有10次
  242. //packageTypeMap := map[int]int{1: 16, 2: 10}
  243. //totalTrip := packageTypeMap[packageType]
  244. tripRemaining = TripBillNum
  245. if tripRemaining < 0 {
  246. tripRemaining = 0
  247. }
  248. //for k, _ := range chartNumMap {
  249. // tripRemaining := totalTrip - ariborneMap[k] + TripBillNumMap[k]
  250. // if tripRemaining < 0 {
  251. // tripRemaining = 0
  252. // }
  253. // specialSurplus += chartMap[k] + strconv.Itoa(tripRemaining) + "次,"
  254. //}
  255. //specialSurplus = strings.TrimRight(specialSurplus, ",")
  256. //tripRemaining = 12 - tripTota
  257. } else {
  258. list, e := GetCompanyReportPermission(companyId, 2)
  259. if e != nil && e.Error() != utils.ErrNoRow() {
  260. err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
  261. }
  262. if len(list) == 0 {
  263. return
  264. }
  265. var chartPermissionIdSlice []string
  266. mapChartName = make(map[string]int)
  267. mapUpgradeId := make(map[int]int)
  268. mapInheritChartName := make(map[string]int)
  269. mapPermissionNameTrip := make(map[string]int)
  270. //mapPermissionName := make(map[int]string)
  271. for _, v := range list {
  272. chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
  273. //是升级套餐才有点数
  274. if v.IsUpgrade == 1 {
  275. mapUpgradeId[v.ChartPermissionId] = 1
  276. }
  277. }
  278. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  279. if e != nil {
  280. err = errors.New("获取品种信息失败, Err:" + e.Error())
  281. return
  282. }
  283. if len(chartList) == 0 {
  284. return
  285. }
  286. //var TripBillNum int
  287. for _, v := range listTripBill {
  288. if v.ActivityId == 0 {
  289. continue
  290. }
  291. mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
  292. }
  293. for _, v := range chartList {
  294. //如果是升级则加点
  295. if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
  296. mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
  297. } else {
  298. mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
  299. }
  300. }
  301. // 通过继承获得的加点
  302. for _, v := range inheritList {
  303. mapInheritChartName[v.ChartPermissionName] = v.Points
  304. }
  305. for _, v := range mapChartName {
  306. if v > 0 {
  307. tripRemaining += v
  308. }
  309. }
  310. }
  311. }
  312. return
  313. }