rai_serve_count.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/models/company"
  8. "hongze/hongze_cygx/models/rai_serve"
  9. "hongze/hongze_cygx/models/roadshow"
  10. "hongze/hongze_cygx/utils"
  11. "strings"
  12. "time"
  13. )
  14. // var startWeekDate = time.Now().AddDate(0, 0, -75).Format(utils.FormatDate) + " 23:59:59" // 查询12周以内的数据
  15. //var startWeekDate = time.Now().AddDate(0, 0, -75).Format(utils.FormatDateTime) // 查询12周以内的数据
  16. //权益服务统计
  17. //上线跑的脚本
  18. //func initJob() {
  19. // UpdateRaiServeCompany() //1
  20. // UpdateCygxRaiServeBillByCompanyIds() //2
  21. // UpdateCygxRaiServeBillWeek() // 3
  22. // UpdateWeekAvg() // 4
  23. //}
  24. //func init() {
  25. // UpdateWeekAvg() //1
  26. //}
  27. //func init() {
  28. // UpdateRaiServeCompany() //1
  29. // UpdateCygxRaiServeBillByCompanyIds() //2
  30. // UpdateCygxRaiServeBillWeekTask() // 3
  31. // UpdateWeekAvg() // 4
  32. //}
  33. // 定时任务处理权益服务公司
  34. func UpdateRaiServeCompanyDate(cont context.Context) (err error) {
  35. defer func() {
  36. if err != nil {
  37. fmt.Println(err)
  38. go utils.SendAlarmMsg(fmt.Sprint("定时任务处理权益服务公司 失败 UpdateRaiServeCompanyDate ,err:", err.Error()), 2)
  39. }
  40. }()
  41. err = UpdateRaiServeCompany()
  42. return
  43. }
  44. // 定时任务处理权益服务记录明细
  45. func UpdateRaiServeBillDate(cont context.Context) (err error) {
  46. //func UpdateRaiServeBillDate() (err error) {
  47. defer func() {
  48. if err != nil {
  49. fmt.Println(err)
  50. go utils.SendAlarmMsg(fmt.Sprint("定时任务处理权益服务公司 失败 UpdateRaiServeBillDate ,err:", err.Error()), 2)
  51. }
  52. }()
  53. err = UpdateRaiServeCompany() // 处理共享机构信息
  54. if err != nil {
  55. return
  56. }
  57. err = UpdateCygxRaiServeBillByCompanyIds() // 处理个人流水记录
  58. if err != nil {
  59. return
  60. }
  61. err = UpdateCygxRaiServeBillWeekTask() // 更新周度服务明细
  62. if err != nil {
  63. return
  64. }
  65. err = UpdateWeekAvg() //更新最近四周平均覆盖率,是否标红,权限,时间,金额
  66. if err != nil {
  67. return
  68. }
  69. return
  70. }
  71. // 更新现有的权益服务客户数据
  72. func UpdateRaiServeCompany() (err error) {
  73. defer func() {
  74. if err != nil {
  75. fmt.Println(err)
  76. go utils.SendAlarmMsg("更新现有的权益服务客户数据失败 UpdateRaiServeCompany:"+err.Error(), 2)
  77. }
  78. }()
  79. var condition string
  80. var pars []interface{}
  81. condition = ` AND p.product_id = 2 AND (p.group_id IN (` + utils.RAI_SERVE_GROUP_ID + `) OR p.share_group_id IN (` + utils.RAI_SERVE_GROUP_ID + `) ) `
  82. //pars = append(pars, utils.RAI_SERVE_GROUP_ID, utils.RAI_SERVE_GROUP_ID)
  83. listCompanyProduct, e := company.GetCompanyProductAndCompanyListByCondition(condition, pars)
  84. if e != nil {
  85. err = errors.New("GetCompanyProductListByCondition, Err: " + e.Error())
  86. return
  87. }
  88. //fmt.Println(len(listCompanyProduct))
  89. //return
  90. raiServeCompanyList, e := rai_serve.GetCygxRaiServeCompanyAllList()
  91. if e != nil {
  92. err = errors.New("GetCygxRaiServeCompanyAllList, Err: " + e.Error())
  93. return
  94. }
  95. mapOLdCompany := make(map[int]bool)
  96. mapOLdCompanyDate := make(map[int]string)
  97. for _, v := range raiServeCompanyList {
  98. mapOLdCompany[v.CompanyId] = true
  99. mapOLdCompanyDate[v.CompanyId] = fmt.Sprint(v.CompanyId, v.CompanyName, v.SellerId, v.StartDate, v.EndDate, v.ShareSeller, v.ShareSellerId, v.Status, v.GroupId, v.ShareGroupId)
  100. }
  101. mapNewCompany := make(map[int]bool)
  102. var delCompanyIds []int // 需要删除的公司ID
  103. for _, v := range listCompanyProduct {
  104. mapNewCompany[v.CompanyId] = true
  105. }
  106. for _, v := range raiServeCompanyList {
  107. if !mapNewCompany[v.CompanyId] {
  108. delCompanyIds = append(delCompanyIds, v.CompanyId)
  109. }
  110. }
  111. var items []*rai_serve.CygxRaiServeCompany
  112. var itemsUpdate []*rai_serve.CygxRaiServeCompany
  113. for _, v := range listCompanyProduct {
  114. item := new(rai_serve.CygxRaiServeCompany)
  115. item.CompanyId = v.CompanyId
  116. item.CompanyName = v.CompanyName
  117. item.CreateTime = time.Now()
  118. item.SellerId = v.SellerId
  119. item.SellerName = v.SellerName
  120. item.StartDate = v.StartDate
  121. item.EndDate = v.EndDate
  122. item.ShareSeller = v.ShareSeller
  123. item.ShareSellerId = v.ShareSellerId
  124. item.Status = v.Status
  125. item.GroupId = v.GroupId
  126. item.ShareGroupId = v.ShareGroupId
  127. if !mapOLdCompany[v.CompanyId] {
  128. items = append(items, item) // 如果原有数据不存在就新增
  129. } else {
  130. if mapOLdCompanyDate[v.CompanyId] != fmt.Sprint(v.CompanyId, v.CompanyName, v.SellerId, v.StartDate, v.EndDate, v.ShareSeller, v.ShareSellerId, v.Status, v.GroupId, v.ShareGroupId) {
  131. itemsUpdate = append(itemsUpdate, item) // 新老数据不一致就进行修改
  132. }
  133. }
  134. }
  135. //fmt.Println("items", len(items))
  136. //fmt.Println("itemsUpadte222", len(itemsUpdate))
  137. //fmt.Println("delCompanyIds", len(delCompanyIds))
  138. //fmt.Println("delCompanyIds", (delCompanyIds))
  139. //return
  140. //先删除老的再添加新的
  141. e = rai_serve.AddCygxRaiServeCompanyMulti(items, itemsUpdate, delCompanyIds)
  142. if e != nil {
  143. err = errors.New("AddCygxRaiServeCompanyMulti, Err: " + e.Error())
  144. return
  145. }
  146. return
  147. }
  148. // GetCompanyContractPermissionNameMapById 获取合并之后的合同所对应的权限种类名称
  149. func GetCompanyContractPermissionNameMapById(companyContractIds []int) (mapContractResp map[int]string, err error) {
  150. //return
  151. lenArr := len(companyContractIds)
  152. if lenArr == 0 {
  153. return
  154. }
  155. var condition string
  156. var pars []interface{}
  157. condition += " AND company_contract_id IN (" + utils.GetOrmInReplace(lenArr) + ")"
  158. pars = append(pars, companyContractIds)
  159. //companyContractList, e := company.GetCompanyContractList(condition, pars)
  160. //if e != nil && e.Error() != utils.ErrNoRow() {
  161. // err = errors.New("GetCompanyContractList, Err: " + e.Error())
  162. // return
  163. //}
  164. permissionList, e := models.GetChartPermissionList()
  165. if e != nil && e.Error() != utils.ErrNoRow() {
  166. err = errors.New("GetChartPermissionList, Err: " + e.Error())
  167. return
  168. }
  169. mappermissionName := make(map[int]string)
  170. for _, v := range permissionList {
  171. if v.ProductId == 2 {
  172. mappermissionName[v.ChartPermissionId] = v.Remark
  173. }
  174. }
  175. mapContractIdPerssion := make(map[int]string) //单个合同ID所对应的套餐或者权限名称
  176. //for _, v := range companyContractList {
  177. // if v.RaiPackageType == 1 {
  178. // mapContractIdPerssion[v.CompanyContractId] = "70w套餐,"
  179. // }
  180. // if v.RaiPackageType == 2 {
  181. // mapContractIdPerssion[v.CompanyContractId] = "45w套餐,"
  182. // }
  183. //}
  184. companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  185. if e != nil && e.Error() != utils.ErrNoRow() {
  186. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  187. return
  188. }
  189. mapIsUpgrade := make(map[string]bool) //合同ID对应的行业是否有升级
  190. for _, v := range companyContractPermissionList {
  191. //如果开通的不是整个套餐,那么就做单独的子权限处理
  192. if strings.Count(mapContractIdPerssion[(v.CompanyContractId)], "w套餐") == 0 {
  193. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  194. if v.ExpensiveYx == 1 {
  195. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  196. } else if v.ExpensiveYx == 2 {
  197. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  198. } else {
  199. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  200. }
  201. } else {
  202. mapContractIdPerssion[(v.CompanyContractId)] += mappermissionName[v.ChartPermissionId] + ","
  203. if v.IsUpgrade == 1 {
  204. //合同ID,权限名称形成唯一的主键索引
  205. perssionName := mappermissionName[v.ChartPermissionId]
  206. perssionName = strings.Replace(perssionName, "(客观)", "", -1)
  207. perssionName = strings.Replace(perssionName, "(主观)", "", -1)
  208. mapIsUpgrade[fmt.Sprint(v.CompanyContractId, "perssionName", perssionName)] = true
  209. }
  210. }
  211. } else {
  212. if v.ChartPermissionId == utils.ZHUAN_JIA_ID {
  213. mapContractIdPerssion[(v.CompanyContractId)] += utils.ZHUAN_JIA_NAME + ","
  214. }
  215. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  216. if v.ExpensiveYx == 1 {
  217. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  218. } else if v.ExpensiveYx == 2 {
  219. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  220. } else {
  221. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  222. }
  223. }
  224. if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  225. mapContractIdPerssion[(v.CompanyContractId)] += utils.YAN_XUAN_KOU_DIAN_BAO_NAME + ","
  226. }
  227. }
  228. }
  229. //合并客观权限
  230. for k, v := range mapContractIdPerssion {
  231. perssionName := v
  232. if strings.Count(v, utils.YI_YAO_NAME) == 2 {
  233. perssionName = strings.Replace(perssionName, "医药(主观)", "医药", -1)
  234. perssionName = strings.Replace(perssionName, "医药(客观)", "", -1)
  235. }
  236. if strings.Count(v, utils.XIAO_FEI_NAME) == 2 {
  237. perssionName = strings.Replace(perssionName, "消费(主观)", "消费", -1)
  238. perssionName = strings.Replace(perssionName, "消费(客观)", "", -1)
  239. }
  240. if strings.Count(v, utils.KE_JI_NAME) == 2 {
  241. perssionName = strings.Replace(perssionName, "科技(主观)", "科技", -1)
  242. perssionName = strings.Replace(perssionName, "科技(客观)", "", -1)
  243. }
  244. if strings.Count(v, utils.ZHI_ZAO_NAME) == 2 {
  245. perssionName = strings.Replace(perssionName, "智造(主观)", "智造", -1)
  246. perssionName = strings.Replace(perssionName, "智造(客观)", "", -1)
  247. }
  248. perssionName = strings.TrimRight(perssionName, ",")
  249. mapContractIdPerssion[k] = perssionName
  250. }
  251. mapContractResp = make(map[int]string, 0)
  252. //过滤多余的","
  253. for k, v := range mapContractIdPerssion {
  254. sliceName := strings.Split(v, ",")
  255. var nameArr []string
  256. for _, vName := range sliceName {
  257. if vName == "" {
  258. continue
  259. }
  260. if mapIsUpgrade[fmt.Sprint(k, "perssionName", vName)] {
  261. vName += "(升级)"
  262. }
  263. nameArr = append(nameArr, vName)
  264. }
  265. mapContractResp[k] = strings.Join(nameArr, ",")
  266. }
  267. return
  268. }
  269. // 根据公司ID获取目前行业所对应的正式权限
  270. func GetCompanyReportPermissionNameMapByCompanyId(companyIds []int) (mapContractResp map[int]string, err error) {
  271. lenArr := len(companyIds)
  272. if lenArr == 0 {
  273. return
  274. }
  275. var condition string
  276. var pars []interface{}
  277. condition += " AND company_id IN (" + utils.GetOrmInReplace(lenArr) + ") AND product_id = 2 AND status IN ( '正式','永续') "
  278. pars = append(pars, companyIds)
  279. permissionList, e := models.GetChartPermissionList()
  280. if e != nil && e.Error() != utils.ErrNoRow() {
  281. err = errors.New("GetChartPermissionList, Err: " + e.Error())
  282. return
  283. }
  284. mappermissionName := make(map[int]string)
  285. for _, v := range permissionList {
  286. if v.ProductId == 2 {
  287. mappermissionName[v.ChartPermissionId] = v.Remark
  288. }
  289. }
  290. mapContractIdPerssion := make(map[int]string) //单个合同ID所对应的套餐或者权限名称
  291. companyReportPermissionList, e := models.GetCompanyReportPermissionList(condition, pars)
  292. if e != nil && e.Error() != utils.ErrNoRow() {
  293. err = errors.New("GetCompanyReportPermissionList, Err: " + e.Error())
  294. return
  295. }
  296. mapIsUpgrade := make(map[string]bool) //合同ID对应的行业是否有升级
  297. for _, v := range companyReportPermissionList {
  298. //如果开通的不是整个套餐,那么就做单独的子权限处理
  299. if strings.Count(mapContractIdPerssion[(v.CompanyId)], "w套餐") == 0 {
  300. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  301. if v.ExpensiveYx == 1 {
  302. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  303. } else if v.ExpensiveYx == 2 {
  304. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  305. } else {
  306. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  307. }
  308. } else {
  309. mapContractIdPerssion[(v.CompanyId)] += mappermissionName[v.ChartPermissionId] + ","
  310. if v.IsUpgrade == 1 {
  311. //合同ID,权限名称形成唯一的主键索引
  312. perssionName := mappermissionName[v.ChartPermissionId]
  313. perssionName = strings.Replace(perssionName, "(客观)", "", -1)
  314. perssionName = strings.Replace(perssionName, "(主观)", "", -1)
  315. mapIsUpgrade[fmt.Sprint(v.CompanyId, "perssionName", perssionName)] = true
  316. }
  317. }
  318. } else {
  319. if v.ChartPermissionId == utils.ZHUAN_JIA_ID {
  320. mapContractIdPerssion[(v.CompanyId)] += utils.ZHUAN_JIA_NAME + ","
  321. }
  322. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  323. if v.ExpensiveYx == 1 {
  324. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  325. } else if v.ExpensiveYx == 2 {
  326. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  327. } else {
  328. mapContractIdPerssion[(v.CompanyId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  329. }
  330. }
  331. if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  332. mapContractIdPerssion[(v.CompanyId)] += utils.YAN_XUAN_KOU_DIAN_BAO_NAME + ","
  333. }
  334. }
  335. }
  336. //合并客观权限
  337. for k, v := range mapContractIdPerssion {
  338. perssionName := v
  339. if strings.Count(v, utils.YI_YAO_NAME) == 2 {
  340. perssionName = strings.Replace(perssionName, "医药(主观)", "医药", -1)
  341. perssionName = strings.Replace(perssionName, "医药(客观)", "", -1)
  342. }
  343. if strings.Count(v, utils.XIAO_FEI_NAME) == 2 {
  344. perssionName = strings.Replace(perssionName, "消费(主观)", "消费", -1)
  345. perssionName = strings.Replace(perssionName, "消费(客观)", "", -1)
  346. }
  347. if strings.Count(v, utils.KE_JI_NAME) == 2 {
  348. perssionName = strings.Replace(perssionName, "科技(主观)", "科技", -1)
  349. perssionName = strings.Replace(perssionName, "科技(客观)", "", -1)
  350. }
  351. if strings.Count(v, utils.ZHI_ZAO_NAME) == 2 {
  352. perssionName = strings.Replace(perssionName, "智造(主观)", "智造", -1)
  353. perssionName = strings.Replace(perssionName, "智造(客观)", "", -1)
  354. }
  355. perssionName = strings.TrimRight(perssionName, ",")
  356. mapContractIdPerssion[k] = perssionName
  357. }
  358. mapContractResp = make(map[int]string, 0)
  359. //过滤多余的","
  360. for k, v := range mapContractIdPerssion {
  361. sliceName := strings.Split(v, ",")
  362. var nameArr []string
  363. for _, vName := range sliceName {
  364. if vName == "" {
  365. continue
  366. }
  367. if mapIsUpgrade[fmt.Sprint(k, "perssionName", vName)] {
  368. vName += "(升级)"
  369. }
  370. nameArr = append(nameArr, vName)
  371. }
  372. mapContractResp[k] = strings.Join(nameArr, ",")
  373. }
  374. return
  375. }
  376. // Redis对列消息中的结构体
  377. type RaiServeBillRedis struct {
  378. Content string `comment:"服务内容说明"`
  379. UserId int `comment:"用户ID"`
  380. ComapnyId int `comment:"公司ID"`
  381. SourceId int `comment:"来源ID"`
  382. Source string `comment:"来源"`
  383. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  384. ViewTime time.Time `comment:"浏览时间"`
  385. }
  386. // 批量更新企业服务明细记录
  387. func UpdateCygxRaiServeBillByCompanyIds() (err error) {
  388. defer func() {
  389. if err != nil {
  390. fmt.Println(err)
  391. go utils.SendAlarmMsg("批量更新企业服务明细记录失败: UpdateCygxRaiServeBillByCompanyIds Err"+err.Error(), 2)
  392. }
  393. }()
  394. var delBillIds []int
  395. var startWeekDate = time.Now().AddDate(0, 0, -75).Format(utils.FormatDate) + " 23:59:59"
  396. fmt.Println("UpdateCygxRaiServeBillByCompanyIds start")
  397. companyList, e := rai_serve.GetCygxRaiServeCompanyAllList()
  398. if e != nil {
  399. err = errors.New("GetCygxRaiServeCompanyAllList, Err: " + e.Error())
  400. return
  401. }
  402. var companyIds []int
  403. var items []*rai_serve.CygxRaiServeBill
  404. for _, v := range companyList {
  405. companyIds = append(companyIds, v.CompanyId)
  406. }
  407. lencompanyId := len(companyIds)
  408. listisMakeruser, e := models.GetWxUserisMakerByCompanyIds(companyIds)
  409. if e != nil {
  410. err = errors.New("GetWxUserisMakerByCompanyIds, Err: " + e.Error())
  411. return
  412. }
  413. mapUserMaker := make(map[int]bool)
  414. mapUserMobileUserId := make(map[string]int)
  415. for _, v := range listisMakeruser {
  416. mapUserMaker[v.UserId] = true
  417. mapUserMobileUserId[v.Mobile] = v.UserId
  418. }
  419. var condition string
  420. var pars []interface{}
  421. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND art.create_time > ? "
  422. pars = append(pars, companyIds, startWeekDate)
  423. fmt.Println("startWeekDate", startWeekDate)
  424. //研选专栏阅读记录内容
  425. {
  426. listYanxuanSpecialRecord, e := models.GetCygxYanxuanSpecialRecordRespListAll(condition+" GROUP BY art.user_id, art.yanxuan_special_id ORDER BY art.create_time ASC ", pars)
  427. if e != nil {
  428. err = errors.New("GetCygxYanxuanSpecialRecordRespListAll, Err: " + e.Error())
  429. return
  430. }
  431. var yanxuanspecialIds []int
  432. for _, v := range listYanxuanSpecialRecord {
  433. yanxuanspecialIds = append(yanxuanspecialIds, v.YanxuanSpecialId)
  434. }
  435. var conditionYanxuanSpecial string
  436. var parsYanxuanSpecial []interface{}
  437. conditionYanxuanSpecial += ` AND id IN (` + utils.GetOrmInReplace(len(yanxuanspecialIds)) + `) `
  438. parsYanxuanSpecial = append(parsYanxuanSpecial, yanxuanspecialIds)
  439. listYanxuanSpecial, e := models.GetYanxuanSpecialListBycondition(conditionYanxuanSpecial, parsYanxuanSpecial, 0, len(yanxuanspecialIds))
  440. if e != nil && e.Error() != utils.ErrNoRow() {
  441. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  442. return
  443. }
  444. mapYanxuanSpecialTitle := make(map[int]string)
  445. for _, v := range listYanxuanSpecial {
  446. mapYanxuanSpecialTitle[v.Id] = v.Title
  447. }
  448. //研选阅读明细
  449. var conditionBill string
  450. var parsBill []interface{}
  451. conditionBill = " AND source = 'yanxuanspecial' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  452. parsBill = append(parsBill, companyIds, startWeekDate)
  453. listBillByYanxuanspecial, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  454. if e != nil {
  455. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  456. return
  457. }
  458. mapYanxuanspecialBill := make(map[string]bool)
  459. for _, v := range listBillByYanxuanspecial {
  460. mapYanxuanspecialBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] = true
  461. }
  462. for _, v := range listYanxuanSpecialRecord {
  463. if mapYanxuanspecialBill[fmt.Sprint("UID_", v.UserId, "SID_", v.YanxuanSpecialId)] { // 如果内容存在就不插入
  464. continue
  465. }
  466. item := new(rai_serve.CygxRaiServeBill)
  467. item.Content = mapYanxuanSpecialTitle[v.YanxuanSpecialId]
  468. item.ServeTypeId = 5
  469. item.ServeTypeName = "阅读uv"
  470. item.UserId = v.UserId
  471. item.Mobile = v.Mobile
  472. item.Email = v.Email
  473. item.CompanyId = v.CompanyId
  474. item.CompanyName = v.CompanyName
  475. item.RealName = v.RealName
  476. item.RegisterPlatform = v.RegisterPlatform
  477. item.ServeCount = 0.5
  478. if mapUserMaker[v.UserId] {
  479. item.IsKp = 1
  480. item.ServeCount = item.ServeCount * 3
  481. }
  482. item.SourceId = v.YanxuanSpecialId
  483. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  484. item.CreateTime = time.Now()
  485. item.ViewTime = v.CreateTime
  486. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  487. items = append(items, item)
  488. }
  489. }
  490. //研选专栏阅读记录内容end
  491. //文章阅读记录内容
  492. {
  493. listArticleRecord, e := models.GetCygxArticleHistoryRecordAllListNoLimit(condition+" GROUP BY art.user_id, art.article_id ORDER BY art.create_time ASC ", pars)
  494. if e != nil {
  495. err = errors.New("GetCygxArticleHistoryRecordAllListNoLimit, Err: " + e.Error())
  496. return
  497. }
  498. //文章阅读明细
  499. var conditionBill string
  500. var parsBill []interface{}
  501. conditionBill = " AND source = 'article' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  502. parsBill = append(parsBill, companyIds, startWeekDate)
  503. listBillByArticle, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  504. if e != nil {
  505. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  506. return
  507. }
  508. mapArticleBill := make(map[string]bool)
  509. for _, v := range listBillByArticle {
  510. mapArticleBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] = true
  511. }
  512. //部分数据来源没有存用户姓名,重新查一下
  513. var userIds []int
  514. for _, v := range listArticleRecord {
  515. userIds = append(userIds, v.UserId)
  516. }
  517. ListUser, e := models.GetWxUserListByUserIdsArr(userIds)
  518. if e != nil {
  519. err = errors.New("GetWxUserListByUserIdsArr, Err: " + e.Error())
  520. return
  521. }
  522. mapUserName := make(map[int]string)
  523. for _, v := range ListUser {
  524. mapUserName[v.UserId] = v.RealName
  525. }
  526. for _, v := range listArticleRecord {
  527. if mapArticleBill[fmt.Sprint("UID_", v.UserId, "SID_", v.ArticleId)] { // 如果内容存在就不插入
  528. continue
  529. }
  530. item := new(rai_serve.CygxRaiServeBill)
  531. item.Content = v.Title
  532. item.ServeTypeId = 5
  533. item.ServeTypeName = "阅读uv"
  534. item.UserId = v.UserId
  535. item.Mobile = v.Mobile
  536. item.Email = v.Email
  537. item.CompanyId = v.CompanyId
  538. item.CompanyName = v.CompanyName
  539. item.RealName = mapUserName[v.UserId]
  540. item.RegisterPlatform = v.RegisterPlatform
  541. item.ServeCount = 0.5
  542. if mapUserMaker[v.UserId] {
  543. item.IsKp = 1
  544. item.ServeCount = item.ServeCount * 3
  545. }
  546. item.SourceId = v.ArticleId
  547. item.Source = utils.CYGX_OBJ_ARTICLE
  548. item.CreateTime = time.Now()
  549. item.ViewTime = v.CreateTime
  550. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  551. if strings.Contains(v.CategoryName, "固收") {
  552. item.ChartPermissionName = utils.GU_SHOU_NAME
  553. item.ChartPermissionId = utils.GU_SHOU_ID_CHART
  554. }
  555. if strings.Contains(v.CategoryName, "策略") {
  556. item.ChartPermissionName = utils.CE_LUE_NAME
  557. item.ChartPermissionId = utils.CE_LUE_ID
  558. }
  559. items = append(items, item)
  560. }
  561. }
  562. //文章阅读记录内容 end
  563. //活动记录内容
  564. {
  565. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND a.activity_time > ? "
  566. signUpDetailList, e := models.GetSignupDetailBySignupjoinactivity(condition+" ORDER BY a.activity_time ASC ", pars)
  567. if e != nil {
  568. err = errors.New("GetSignupDetailBySignupjoinactivity, Err: " + e.Error())
  569. return
  570. }
  571. //文章阅读明细
  572. var conditionBill string
  573. var parsBill []interface{}
  574. conditionBill = " AND source = 'activity' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  575. parsBill = append(parsBill, companyIds, startWeekDate)
  576. listBillByActivity, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  577. if e != nil {
  578. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  579. return
  580. }
  581. mapActivityeBill := make(map[string]bool)
  582. for _, v := range listBillByActivity {
  583. mapActivityeBill[fmt.Sprint("UID_", v.Mobile, "SID_", v.SourceId)] = true
  584. }
  585. mapArticleBillDel := make(map[string]bool)
  586. for _, v := range signUpDetailList {
  587. mapArticleBillDel[fmt.Sprint("UID_", v.UserId, "SID_", v.ActivityId)] = true
  588. }
  589. mapDelActivityBillDel := make(map[string]bool)
  590. for _, v := range signUpDetailList {
  591. mapDelActivityBillDel[fmt.Sprint("UID_", v.UserId, "SID_", v.ActivityId)] = true
  592. }
  593. for _, v := range listBillByActivity {
  594. if !mapDelActivityBillDel[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] { // 如果内容存在就插入删除ID中
  595. delBillIds = append(delBillIds, v.BillId)
  596. }
  597. }
  598. for _, v := range signUpDetailList {
  599. if v.UserId == 0 && v.Mobile != "" {
  600. v.UserId = mapUserMobileUserId[v.Mobile] // 空降的部分联系人 userid 为0这里做一下修复
  601. }
  602. if mapActivityeBill[fmt.Sprint("UID_", v.Mobile, "SID_", v.ActivityId)] { // 如果内容存在就不插入
  603. continue
  604. }
  605. item := new(rai_serve.CygxRaiServeBill)
  606. item.Content = v.ActivityName
  607. if v.ActivityType == 1 {
  608. item.ServeTypeId = 1
  609. item.ServeTypeName = "线上活动"
  610. } else {
  611. item.ServeTypeId = 2
  612. item.ServeTypeName = "线下活动"
  613. }
  614. item.UserId = v.UserId
  615. item.Mobile = v.Mobile
  616. item.Email = v.Email
  617. item.CompanyId = v.CompanyId
  618. item.CompanyName = v.CompanyName
  619. item.RealName = v.RealName
  620. //item.RegisterPlatform = v.RegisterPlatform
  621. item.ServeCount = 1
  622. if mapUserMaker[v.UserId] {
  623. item.IsKp = 1
  624. item.ServeCount = item.ServeCount * 3
  625. }
  626. item.SourceId = v.ActivityId
  627. item.Source = utils.CYGX_OBJ_ACTIVITY
  628. item.CreateTime = time.Now()
  629. item.ViewTime = v.ActivityTime
  630. item.ChartPermissionName = v.ChartPermissionName
  631. item.ChartPermissionId = v.ChartPermissionId
  632. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  633. items = append(items, item)
  634. }
  635. }
  636. //活动记录内容 end
  637. //专项调研活动记录内容
  638. {
  639. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND a.activity_time > ? "
  640. activitySpecialList, e := models.GetCygxActivitySpecialMeetingDetailList(condition+" ORDER BY a.activity_time ASC ", pars)
  641. if e != nil {
  642. err = errors.New("GetCygxActivitySpecialMeetingDetailList, Err: " + e.Error())
  643. return
  644. }
  645. //专项调研活动明细
  646. var conditionBill string
  647. var parsBill []interface{}
  648. conditionBill = " AND source = 'activityspecial' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  649. parsBill = append(parsBill, companyIds, startWeekDate)
  650. listBillByActivitySpecial, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  651. if e != nil {
  652. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  653. return
  654. }
  655. mapActivitySpecial := make(map[string]bool)
  656. for _, v := range listBillByActivitySpecial {
  657. mapActivitySpecial[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] = true
  658. }
  659. mapDelActivitySpecial := make(map[string]bool)
  660. for _, v := range activitySpecialList {
  661. mapDelActivitySpecial[fmt.Sprint("UID_", v.UserId, "SID_", v.ActivityId)] = true
  662. }
  663. for _, v := range listBillByActivitySpecial {
  664. if !mapDelActivitySpecial[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] { // 如果内容存在就插入删除ID中
  665. delBillIds = append(delBillIds, v.BillId)
  666. }
  667. }
  668. for _, v := range activitySpecialList {
  669. if mapActivitySpecial[fmt.Sprint("UID_", v.UserId, "SID_", v.ActivityId)] { // 如果内容存在就不插入
  670. continue
  671. }
  672. item := new(rai_serve.CygxRaiServeBill)
  673. item.Content = v.ResearchTheme
  674. item.ServeTypeId = 3
  675. item.ServeTypeName = "专项调研"
  676. item.UserId = v.UserId
  677. item.Mobile = v.Mobile
  678. item.Email = v.Email
  679. item.CompanyId = v.CompanyId
  680. item.CompanyName = v.CompanyName
  681. item.RealName = v.RealName
  682. //item.RegisterPlatform = v.RegisterPlatform
  683. item.ServeCount = 1
  684. if mapUserMaker[v.UserId] {
  685. item.IsKp = 1
  686. item.ServeCount = item.ServeCount * 3
  687. }
  688. item.SourceId = v.ActivityId
  689. item.Source = utils.CYGX_OBJ_ACTIVITYSPECIAL
  690. item.CreateTime = time.Now()
  691. item.ViewTime = v.ActivityTime
  692. item.ChartPermissionName = v.ChartPermissionName
  693. item.ChartPermissionId = v.ChartPermissionId
  694. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  695. items = append(items, item)
  696. }
  697. }
  698. //专项调研活动记录内容 end
  699. //1v1路演
  700. {
  701. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND rs.start_date > ? "
  702. calendarMeetingUserList, e := roadshow.GetRsCalendarMeetingUserList(condition+" ORDER BY rs.start_date ASC ", pars)
  703. if e != nil {
  704. err = errors.New("GetRsCalendarMeetingUserList, Err: " + e.Error())
  705. return
  706. }
  707. var rsCalendarIds []int
  708. for _, v := range calendarMeetingUserList {
  709. rsCalendarIds = append(rsCalendarIds, v.RsCalendarId)
  710. }
  711. //获取研究员信息
  712. researchers, e := roadshow.GetRsCalendarResearcherListByIds(rsCalendarIds)
  713. if e != nil {
  714. err = errors.New("GetRsCalendarResearcherListByIds, Err: " + e.Error())
  715. return
  716. }
  717. mapResearcherName := make(map[int][]string)
  718. for _, v := range researchers {
  719. mapResearcherName[v.RsCalendarId] = append(mapResearcherName[v.RsCalendarId], v.ResearcherName)
  720. }
  721. listEmail, e := models.GetAskEmail()
  722. if e != nil {
  723. err = errors.New("GetAskEmail, Err: " + e.Error())
  724. return
  725. }
  726. var ceLueResearcherName []string
  727. var guShouResearcherName []string
  728. for _, v := range listEmail {
  729. if v.ChartPermissionName == utils.CE_LUE_NAME {
  730. ceLueResearcherName = append(ceLueResearcherName, v.Name)
  731. }
  732. if v.ChartPermissionName == utils.GU_SHOU_NAME {
  733. guShouResearcherName = append(guShouResearcherName, v.Name)
  734. }
  735. }
  736. //专项调研活动明细
  737. var conditionBill string
  738. var parsBill []interface{}
  739. conditionBill = " AND source = 'rscalendar' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  740. parsBill = append(parsBill, companyIds, startWeekDate)
  741. listBillByRscalendar, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  742. if e != nil {
  743. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  744. return
  745. }
  746. mapRscalendarBill := make(map[string]bool)
  747. for _, v := range listBillByRscalendar {
  748. mapRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] = true
  749. }
  750. mapDelRscalendarBill := make(map[string]bool)
  751. for _, v := range calendarMeetingUserList {
  752. mapDelRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.RsCalendarId)] = true
  753. }
  754. for _, v := range listBillByRscalendar {
  755. if !mapDelRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] { // 如果内容存在就插入删除ID中
  756. delBillIds = append(delBillIds, v.BillId)
  757. }
  758. }
  759. for _, v := range calendarMeetingUserList {
  760. if mapRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.RsCalendarId)] { // 如果内容存在就不插入
  761. continue
  762. }
  763. item := new(rai_serve.CygxRaiServeBill)
  764. item.Content = strings.Join(mapResearcherName[v.RsCalendarId], ",")
  765. item.ServeTypeId = 4
  766. item.ServeTypeName = "一对一路演"
  767. item.UserId = v.UserId
  768. item.Mobile = v.Mobile
  769. item.Email = v.Email
  770. item.CompanyId = v.CompanyId
  771. item.CompanyName = v.CompanyName
  772. item.RealName = v.RealName
  773. //item.RegisterPlatform = v.RegisterPlatform
  774. item.ServeCount = 1.5
  775. if mapUserMaker[v.UserId] {
  776. item.IsKp = 1
  777. item.ServeCount = item.ServeCount * 3
  778. }
  779. item.SourceId = v.RsCalendarId
  780. item.Source = utils.CYGX_OBJ_RS_CALENDAR
  781. item.CreateTime = time.Now()
  782. item.ViewTime = v.StartDate + " " + v.StartTime
  783. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  784. if utils.InArrayByStr(guShouResearcherName, item.Content) {
  785. item.ChartPermissionName = utils.GU_SHOU_NAME
  786. item.ChartPermissionId = utils.GU_SHOU_ID_CHART
  787. }
  788. if utils.InArrayByStr(ceLueResearcherName, item.Content) {
  789. item.ChartPermissionName = utils.CE_LUE_NAME
  790. item.ChartPermissionId = utils.CE_LUE_ID
  791. }
  792. items = append(items, item)
  793. }
  794. }
  795. //1v1路演 end
  796. //交流反馈
  797. {
  798. condition = " AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND create_time > ? "
  799. userFeedbackList, e := rai_serve.GetCygxUserFeedbackList(condition+" ORDER BY user_feedback_id ASC ", pars)
  800. if e != nil {
  801. err = errors.New("GetCygxUserFeedbackList, Err: " + e.Error())
  802. return
  803. }
  804. //交流反馈明细
  805. var conditionBill string
  806. var parsBill []interface{}
  807. conditionBill = " AND source = 'userfeedback' AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  808. parsBill = append(parsBill, companyIds, startWeekDate)
  809. listBillByuserFeedback, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  810. if e != nil {
  811. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  812. return
  813. }
  814. mapRscalendarBill := make(map[string]bool)
  815. for _, v := range listBillByuserFeedback {
  816. mapRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] = true
  817. }
  818. mapDelRscalendarBill := make(map[string]bool)
  819. for _, v := range userFeedbackList {
  820. mapDelRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.UserFeedbackId)] = true
  821. }
  822. for _, v := range listBillByuserFeedback {
  823. if !mapDelRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.SourceId)] { // 如果内容存在就插入删除ID中
  824. delBillIds = append(delBillIds, v.BillId)
  825. }
  826. }
  827. for _, v := range userFeedbackList {
  828. if mapRscalendarBill[fmt.Sprint("UID_", v.UserId, "SID_", v.UserFeedbackId)] { // 如果内容存在就不插入
  829. continue
  830. }
  831. item := new(rai_serve.CygxRaiServeBill)
  832. item.Content = v.Content
  833. item.ServeTypeId = 6
  834. item.ServeTypeName = "交流反馈"
  835. item.UserId = v.UserId
  836. item.Mobile = v.Mobile
  837. item.Email = v.Email
  838. item.CompanyId = v.CompanyId
  839. item.CompanyName = v.CompanyName
  840. item.RealName = v.RealName
  841. item.ServeCount = 1
  842. if mapUserMaker[v.UserId] {
  843. item.IsKp = 1
  844. item.ServeCount = item.ServeCount * 3
  845. }
  846. item.SourceId = v.UserFeedbackId
  847. item.Source = utils.CYGX_OBJ_USER_FEEDBACK
  848. item.CreateTime = time.Now()
  849. item.ViewTime = v.CreateTime
  850. item.WeekStartDate, item.WeekEndDate = utils.GetMondayAndSundayByTimeString(item.ViewTime)
  851. items = append(items, item)
  852. }
  853. }
  854. //交流反馈 end
  855. if len(items) > 0 {
  856. var itemsAdd []*rai_serve.CygxRaiServeBill
  857. for k, v := range items {
  858. itemsAdd = append(itemsAdd, v)
  859. if len(itemsAdd)%2000 == 0 {
  860. fmt.Println(k)
  861. e = rai_serve.AddCygxRaiServeBillMulti(itemsAdd)
  862. if e != nil {
  863. err = errors.New("AddCygxRaiServeBillMulti, Err: " + e.Error())
  864. return
  865. }
  866. itemsAdd = make([]*rai_serve.CygxRaiServeBill, 0)
  867. }
  868. }
  869. e = rai_serve.AddCygxRaiServeBillMulti(itemsAdd)
  870. if e != nil {
  871. err = errors.New("AddCygxRaiServeBillMulti, Err: " + e.Error())
  872. return
  873. }
  874. }
  875. fmt.Println("delBillIds", (delBillIds))
  876. fmt.Println("UpdateCygxRaiServeBillByCompanyIds end")
  877. return
  878. }
  879. // 更新周度服务明细
  880. func UpdateCygxRaiServeBillWeek() (err error) {
  881. defer func() {
  882. if err != nil {
  883. fmt.Println(err)
  884. go utils.SendAlarmMsg("批量更新企业服务明细记录失败: UpdateCygxRaiServeBillByCompanyIds Err"+err.Error(), 2)
  885. }
  886. }()
  887. now := time.Now()
  888. weeks := 12
  889. companyList, e := rai_serve.GetCygxRaiServeCompanyAllList()
  890. if e != nil {
  891. err = errors.New("GetCygxRaiServeCompanyAllList, Err: " + e.Error())
  892. return
  893. }
  894. var companyIds []int
  895. for _, v := range companyList {
  896. companyIds = append(companyIds, v.CompanyId)
  897. }
  898. listCompanyUserNum, e := models.GetWxUserNumByCompanyIds(companyIds)
  899. if e != nil {
  900. err = errors.New("GetWxUserisMakerByCompanyIds, Err: " + e.Error())
  901. return
  902. }
  903. mapCompanyUserNum := make(map[int]int) //企业对应的用户数量
  904. for _, v := range listCompanyUserNum {
  905. mapCompanyUserNum[v.CompanyId] = v.UserNum
  906. }
  907. lencompanyId := len(companyIds)
  908. //研选阅读明细
  909. var conditionBill string
  910. var parsBill []interface{}
  911. var startWeekDate = time.Now().AddDate(0, 0, -75).Format(utils.FormatDate) + " 23:59:59"
  912. conditionBill = " AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  913. parsBill = append(parsBill, companyIds, startWeekDate)
  914. listBillByYanxuanspecial, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  915. if e != nil {
  916. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  917. return
  918. }
  919. mapWeekServeCount := make(map[string]float64)
  920. for _, v := range listBillByYanxuanspecial {
  921. mapWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] += v.ServeCount
  922. }
  923. fmt.Println(companyIds)
  924. var items []*rai_serve.CygxRaiServeWeekBill
  925. for _, v := range companyList {
  926. for i := 0; i < weeks; i++ {
  927. item := new(rai_serve.CygxRaiServeWeekBill)
  928. // 计算当前周的周一
  929. monday := now.AddDate(0, 0, -int(now.Weekday()-time.Monday)-i*7)
  930. // 计算当前周的周日
  931. sunday := monday.AddDate(0, 0, 6)
  932. item.WeekStartDate = monday.Format(utils.FormatDate)
  933. item.WeekEndDate = sunday.Format(utils.FormatDate)
  934. item.CompanyId = v.CompanyId
  935. item.CompanyName = v.CompanyName
  936. item.UserTotal = mapCompanyUserNum[v.CompanyId]
  937. item.WeekServeCount = mapWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)]
  938. if mapCompanyUserNum[v.CompanyId] > 0 {
  939. item.CoverageRate = item.WeekServeCount / float64(mapCompanyUserNum[v.CompanyId]) * 100
  940. }
  941. item.CreateTime = time.Now()
  942. item.ModifyTime = time.Now()
  943. items = append(items, item)
  944. }
  945. }
  946. e = rai_serve.AddCygxRaiServeWeekBillMulti(items)
  947. if e != nil {
  948. err = errors.New("AddCygxRaiServeWeekBillMulti, Err: " + e.Error())
  949. return
  950. }
  951. return
  952. }
  953. func UpdateCygxRaiServeBillWeekTask() (err error) {
  954. defer func() {
  955. if err != nil {
  956. fmt.Println(err)
  957. go utils.SendAlarmMsg("批量更新企业服务明细记录失败: UpdateCygxRaiServeBillByCompanyIds Err"+err.Error(), 2)
  958. }
  959. }()
  960. fmt.Println("UpdateCygxRaiServeBillWeekTask start")
  961. now := time.Now()
  962. weeks := 11
  963. companyList, e := rai_serve.GetCygxRaiServeCompanyAllList()
  964. if e != nil {
  965. err = errors.New("GetCygxRaiServeCompanyAllList, Err: " + e.Error())
  966. return
  967. }
  968. var companyIds []int
  969. for _, v := range companyList {
  970. companyIds = append(companyIds, v.CompanyId)
  971. }
  972. listCompanyUserNum, e := models.GetWxUserNumByCompanyIds(companyIds)
  973. if e != nil {
  974. err = errors.New("GetWxUserisMakerByCompanyIds, Err: " + e.Error())
  975. return
  976. }
  977. mapCompanyUserNum := make(map[int]int) //企业对应的用户数量
  978. for _, v := range listCompanyUserNum {
  979. mapCompanyUserNum[v.CompanyId] = v.UserNum
  980. }
  981. lencompanyId := len(companyIds)
  982. if lencompanyId == 0 {
  983. return
  984. }
  985. //明细
  986. var conditionBill string
  987. var parsBill []interface{}
  988. var startWeekDate = time.Now().AddDate(0, 0, -75).Format(utils.FormatDate) + " 23:59:59"
  989. conditionBill = " AND company_id IN (" + utils.GetOrmInReplace(lencompanyId) + ") AND view_time > ? "
  990. parsBill = append(parsBill, companyIds, startWeekDate)
  991. listBill, e := rai_serve.GetCygxRaiServeBillListAll(conditionBill, parsBill)
  992. if e != nil {
  993. err = errors.New("GetCygxRaiServeBillListAll, Err: " + e.Error())
  994. return
  995. }
  996. mapWeekServeCount := make(map[string]float64)
  997. for _, v := range listBill {
  998. mapWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] += v.ServeCount
  999. }
  1000. var conditionWeek string
  1001. var parsWeek []interface{}
  1002. conditionWeek = ` AND week_start_date > ? `
  1003. parsWeek = append(parsWeek, startWeekDate)
  1004. listWeekBill, e := rai_serve.GetCygxRaiServeWeekBillListAll(conditionWeek, parsWeek)
  1005. if e != nil {
  1006. err = errors.New("GetCygxRaiServeWeekBillListAll, Err: " + e.Error())
  1007. return
  1008. }
  1009. //return
  1010. mapWeekbill := make(map[string]bool)
  1011. mapWeekbill2 := make(map[string]int)
  1012. mapWeekbillUserTotal := make(map[string]int)
  1013. mapWeekbillWeekServeCount := make(map[string]float64)
  1014. mapWeekbillWeekCoverageRate := make(map[string]float64)
  1015. for _, v := range listWeekBill {
  1016. mapWeekbill[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] = true
  1017. mapWeekbill2[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] = v.CompanyId
  1018. mapWeekbillUserTotal[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] = v.UserTotal
  1019. mapWeekbillWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] = v.WeekServeCount
  1020. mapWeekbillWeekCoverageRate[fmt.Sprint("CID_", v.CompanyId, "WEEK_", v.WeekStartDate)] = v.CoverageRate
  1021. }
  1022. var items []*rai_serve.CygxRaiServeWeekBill
  1023. var itemsUpdate []*rai_serve.CygxRaiServeWeekBill
  1024. for _, v := range companyList {
  1025. for i := 0; i < weeks; i++ {
  1026. if i == 10 {
  1027. continue
  1028. }
  1029. item := new(rai_serve.CygxRaiServeWeekBill)
  1030. // 计算当前周的周一
  1031. monday := now.AddDate(0, 0, -int(now.Weekday()-time.Monday)-i*7)
  1032. // 计算当前周的周日
  1033. sunday := monday.AddDate(0, 0, 6)
  1034. item.WeekStartDate = monday.Format(utils.FormatDate)
  1035. item.WeekEndDate = sunday.Format(utils.FormatDate)
  1036. item.CompanyId = v.CompanyId
  1037. item.CompanyName = v.CompanyName
  1038. //超过本周的,互动总人数不做修改
  1039. if i > 0 && mapWeekbillUserTotal[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)] > 0 {
  1040. item.UserTotal = mapWeekbillUserTotal[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)]
  1041. } else {
  1042. item.UserTotal = mapCompanyUserNum[v.CompanyId]
  1043. }
  1044. item.WeekServeCount = mapWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)]
  1045. if mapCompanyUserNum[v.CompanyId] > 0 {
  1046. item.CoverageRate = item.WeekServeCount / float64(mapCompanyUserNum[v.CompanyId]) * 100
  1047. }
  1048. item.CreateTime = time.Now()
  1049. item.ModifyTime = time.Now()
  1050. if mapWeekbill[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)] {
  1051. if mapWeekbillUserTotal[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)] == item.UserTotal && mapWeekbillWeekServeCount[fmt.Sprint("CID_", v.CompanyId, "WEEK_", item.WeekStartDate)] == item.WeekServeCount {
  1052. continue // 如果互动量相等,且人数相等,就不修改
  1053. }
  1054. itemsUpdate = append(itemsUpdate, item)
  1055. } else {
  1056. items = append(items, item)
  1057. }
  1058. }
  1059. }
  1060. fmt.Println("itemsadd", len(items))
  1061. if len(items) > 0 {
  1062. e = rai_serve.AddCygxRaiServeWeekBillMulti(items)
  1063. if e != nil {
  1064. err = errors.New("AddCygxRaiServeWeekBillMulti, Err: " + e.Error())
  1065. return
  1066. }
  1067. }
  1068. fmt.Println("itemsUpdate111", len(itemsUpdate))
  1069. if len(itemsUpdate) > 0 {
  1070. e = rai_serve.UpdateCygxRaiServeWeekBillMulti(itemsUpdate)
  1071. if e != nil {
  1072. err = errors.New("AddCygxRaiServeWeekBillMulti, Err: " + e.Error())
  1073. return
  1074. }
  1075. }
  1076. fmt.Println("UpdateCygxRaiServeBillWeekTask end")
  1077. return
  1078. }
  1079. //func init() {
  1080. // UpdateWeekAvg()
  1081. //}
  1082. // 更新最近四周平均覆盖率,是否标红,权限,时间,金额
  1083. func UpdateWeekAvg() (err error) {
  1084. defer func() {
  1085. if err != nil {
  1086. fmt.Println(err)
  1087. go utils.SendAlarmMsg("批量更新企业服务明细记录失败: UpdateCygxRaiServeBillByCompanyIds Err"+err.Error(), 2)
  1088. }
  1089. }()
  1090. fmt.Println(" UpdateWeekAvg start")
  1091. now := time.Now()
  1092. weeks := 3
  1093. monday := now.AddDate(0, 0, -int(now.Weekday()-time.Monday)-weeks*7)
  1094. startDate := monday.Format(utils.FormatDate)
  1095. var condition string
  1096. var pars []interface{}
  1097. condition = ` AND week_start_date >= ? `
  1098. pars = append(pars, startDate)
  1099. listWeekBill, e := rai_serve.GetCygxRaiServeWeekBillListAll(condition, pars)
  1100. if e != nil {
  1101. err = errors.New("GetCygxRaiServeWeekBillListAll, Err: " + e.Error())
  1102. return
  1103. }
  1104. mapWeekServeCount := make(map[int]float64) //四周平均率总合
  1105. for _, v := range listWeekBill {
  1106. mapWeekServeCount[v.CompanyId] += v.CoverageRate
  1107. }
  1108. //获取近四周含有Kp 的公司
  1109. condition = ""
  1110. pars = make([]interface{}, 0)
  1111. condition += ` AND week_start_date >= ? AND is_kp = 1 GROUP BY company_id `
  1112. pars = append(pars, startDate)
  1113. listIsKpWeekBill, e := rai_serve.GetCygxRaiServeBillListAll(condition, pars)
  1114. if e != nil {
  1115. err = errors.New("GetCygxRaiServeWeekBillListAll, Err: " + e.Error())
  1116. return
  1117. }
  1118. mapWeekIsKp := make(map[int]bool) //最近四周是否包含KP
  1119. for _, v := range listIsKpWeekBill {
  1120. mapWeekIsKp[v.CompanyId] = true
  1121. }
  1122. companyList, e := rai_serve.GetCygxRaiServeCompanyAllList()
  1123. if e != nil {
  1124. err = errors.New("GetCygxRaiServeCompanyAllList, Err: " + e.Error())
  1125. return
  1126. }
  1127. var formalCompanyIds []int // 当前正式客户ID
  1128. var formalNoCompanyIds []int // 当前非正式客户ID
  1129. //永续客户当做正式客户处理
  1130. for _, v := range companyList {
  1131. if v.Status == "正式" || v.Status == "永续" {
  1132. formalCompanyIds = append(formalCompanyIds, v.CompanyId)
  1133. } else {
  1134. formalNoCompanyIds = append(formalNoCompanyIds, v.CompanyId)
  1135. }
  1136. }
  1137. var itemsUpdate []*rai_serve.CygxRaiServeCompany
  1138. if len(formalCompanyIds) > 0 { //正式客户信息
  1139. condition = ""
  1140. pars = make([]interface{}, 0)
  1141. condition = ` AND product_id = 2 AND company_id IN (` + utils.GetOrmInReplace(len(formalCompanyIds)) + `) AND status = 1 AND end_date > ? `
  1142. pars = append(pars, formalCompanyIds, time.Now())
  1143. listCompanyContract, e := company.GetCompanyContractList(condition, pars)
  1144. if e != nil {
  1145. err = errors.New("GetCompanyContractList, Err: " + e.Error())
  1146. return
  1147. }
  1148. companyMoney := make(map[int]float64)
  1149. for _, v := range listCompanyContract {
  1150. companyMoney[v.CompanyId] += v.Money
  1151. }
  1152. permissionNameMapByCompanyId, e := GetCompanyReportPermissionNameMapByCompanyId(formalCompanyIds)
  1153. if e != nil {
  1154. err = errors.New("GetCompanyReportPermissionNameMapByCompanyId, Err: " + e.Error())
  1155. return
  1156. }
  1157. for _, v := range formalCompanyIds {
  1158. item := new(rai_serve.CygxRaiServeCompany)
  1159. item.CompanyId = v
  1160. item.Money = companyMoney[v]
  1161. item.PermissionName = permissionNameMapByCompanyId[v]
  1162. item.ServeCoverageRate = utils.SubFloatToString(mapWeekServeCount[v]/4, 2)
  1163. item.IsUserMaker = -1
  1164. if mapWeekIsKp[v] {
  1165. item.IsUserMaker = 1
  1166. }
  1167. itemsUpdate = append(itemsUpdate, item)
  1168. }
  1169. }
  1170. //正式客户数据修改
  1171. e = rai_serve.UpdateCygxRaiServeCompanyMulti(itemsUpdate)
  1172. if e != nil {
  1173. err = errors.New("UpdateCygxRaiServeCompanyMulti, Err: " + e.Error())
  1174. return
  1175. }
  1176. var itemsUpdateFormalNo []*rai_serve.CygxRaiServeCompany //非正式客户数据修改
  1177. var itemsUpdateFormalNoContract []*rai_serve.CygxRaiServeCompany //非正式客户没有合同的客户修改
  1178. mapContractCompanyIds := make(map[int]bool) //签过合同的试用客户ID
  1179. if len(formalNoCompanyIds) > 0 { //非正式客户信息
  1180. condition = ""
  1181. pars = make([]interface{}, 0)
  1182. condition = ` AND product_id = 2 AND company_id IN (` + utils.GetOrmInReplace(len(formalNoCompanyIds)) + `) AND status = 1 AND end_date < ? GROUP BY company_id ORDER BY company_contract_id DESC `
  1183. pars = append(pars, formalNoCompanyIds, time.Now())
  1184. listCompanyContractLastId, e := company.GetCompanyContractLastList(condition, pars) //获取关联的最后一笔合同信息
  1185. if e != nil {
  1186. err = errors.New("GetCompanyContractLastList, Err: " + e.Error())
  1187. return
  1188. }
  1189. var companyContractIds []int
  1190. for _, v := range listCompanyContractLastId {
  1191. companyContractIds = append(companyContractIds, v.CompanyContractId)
  1192. }
  1193. condition = ""
  1194. pars = make([]interface{}, 0)
  1195. condition = ` AND product_id = 2 AND company_id IN (` + utils.GetOrmInReplace(len(formalNoCompanyIds)) + `) AND status = 1 AND company_contract_id IN (` + utils.GetOrmInReplace(len(companyContractIds)) + `) AND end_date < ? GROUP BY company_id ORDER BY company_contract_id DESC `
  1196. pars = append(pars, formalNoCompanyIds, companyContractIds, time.Now())
  1197. listCompanyContractLast, e := company.GetCompanyContractList(condition, pars)
  1198. if e != nil {
  1199. err = errors.New("GetCompanyContractList, Err: " + e.Error())
  1200. return
  1201. }
  1202. permissionNameMapByContractId, e := GetCompanyContractPermissionNameMapById(companyContractIds)
  1203. if e != nil {
  1204. err = errors.New("GetCompanyReportPermissionNameMapByCompanyId, Err: " + e.Error())
  1205. return
  1206. }
  1207. for _, v := range listCompanyContractLast {
  1208. item := new(rai_serve.CygxRaiServeCompany)
  1209. item.CompanyId = v.CompanyId
  1210. item.Money = v.Money
  1211. item.StartDate = v.StartDate
  1212. item.EndDate = v.EndDate
  1213. item.ServeCoverageRate = utils.SubFloatToString(mapWeekServeCount[v.CompanyId]/4, 2)
  1214. item.IsUserMaker = -1
  1215. if mapWeekIsKp[v.CompanyId] {
  1216. item.IsUserMaker = 1
  1217. }
  1218. item.PermissionName = permissionNameMapByContractId[v.CompanyContractId]
  1219. itemsUpdateFormalNo = append(itemsUpdateFormalNo, item)
  1220. mapContractCompanyIds[v.CompanyId] = true
  1221. }
  1222. for _, v := range formalNoCompanyIds {
  1223. if mapContractCompanyIds[v] {
  1224. continue
  1225. }
  1226. //fmt.Println(v)
  1227. item := new(rai_serve.CygxRaiServeCompany)
  1228. item.CompanyId = v
  1229. item.ServeCoverageRate = utils.SubFloatToString(mapWeekServeCount[v]/4, 2)
  1230. item.IsUserMaker = -1
  1231. if mapWeekIsKp[v] {
  1232. item.IsUserMaker = 1
  1233. }
  1234. itemsUpdateFormalNoContract = append(itemsUpdateFormalNoContract, item)
  1235. }
  1236. }
  1237. e = rai_serve.UpdateCygxRaiServeCompanyFormalNoMulti(itemsUpdateFormalNo)
  1238. if e != nil {
  1239. err = errors.New("UpdateCygxRaiServeCompanyFormalNoMulti, Err: " + e.Error())
  1240. return
  1241. }
  1242. e = rai_serve.UpdateCygxRaiServeCompanyFormalNoContractMulti(itemsUpdateFormalNoContract)
  1243. if e != nil {
  1244. err = errors.New("UpdateCygxRaiServeCompanyFormalNoMulti, Err: " + e.Error())
  1245. return
  1246. }
  1247. fmt.Println(" UpdateWeekAvg end")
  1248. return
  1249. }