activity_special.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 处理产业与标签
  16. func HandleIndustrialLabel(temporaryLabel, industrialManagementIdS, industrialSubjectIdS string, activityId int) (industrialActivityItems []*cygx.CygxIndustrialActivityGroupManagement, subjectActivityItems []*cygx.CygxIndustrialActivityGroupSubject, err error, industrialNames, industrialSubjectNames string) {
  17. //如果临时标签是空的话,就去关联产业跟标的
  18. var temporarySubject string
  19. if temporaryLabel == "" {
  20. if industrialManagementIdS == "" {
  21. return
  22. }
  23. industrialManagementList := strings.Split(industrialManagementIdS, ",")
  24. for _, v := range industrialManagementList {
  25. industrialActivityItem := new(cygx.CygxIndustrialActivityGroupManagement)
  26. industrialManagementId, e := strconv.Atoi(v)
  27. if e != nil {
  28. err = errors.New("获取微路演音视频列表失败, Err: " + e.Error())
  29. return
  30. }
  31. industrialActivityItem.CreateTime = time.Now()
  32. industrialActivityItem.IndustrialManagementId = industrialManagementId
  33. industrialActivityItem.ActivityId = activityId
  34. industrialActivityItem.Source = 2
  35. industrialActivityItems = append(industrialActivityItems, industrialActivityItem)
  36. }
  37. if industrialSubjectIdS != "" {
  38. industrialSubjectIdList := strings.Split(industrialSubjectIdS, ",")
  39. for _, v := range industrialSubjectIdList {
  40. subjectActivityItem := new(cygx.CygxIndustrialActivityGroupSubject)
  41. industrialSubjectId, e := strconv.Atoi(v)
  42. if e != nil {
  43. err = errors.New("标的ID不规范,Err:" + e.Error() + industrialManagementIdS)
  44. return
  45. }
  46. subjectActivityItem.CreateTime = time.Now()
  47. subjectActivityItem.IndustrialSubjectId = industrialSubjectId
  48. subjectActivityItem.ActivityId = activityId
  49. subjectActivityItem.Source = 2
  50. subjectActivityItems = append(subjectActivityItems, subjectActivityItem)
  51. }
  52. }
  53. //产业名称
  54. if industrialManagementIdS != "" {
  55. industrialNames, err = cygx.GetindustrialManagementNames(industrialManagementIdS)
  56. if err != nil {
  57. return
  58. }
  59. }
  60. //标的名称
  61. if industrialSubjectIdS != "" {
  62. industrialSubjectNames, err = cygx.GetindustrialSubjectNames(industrialSubjectIdS)
  63. if err != nil {
  64. return
  65. }
  66. if temporarySubject != "" {
  67. industrialSubjectNames += "/" + strings.Replace(temporarySubject, ",", "/", -1)
  68. }
  69. } else {
  70. industrialSubjectNames = ""
  71. if temporarySubject != "" {
  72. industrialSubjectNames = strings.Replace(temporarySubject, ",", "/", -1)
  73. }
  74. }
  75. } else {
  76. industrialActivityItems = make([]*cygx.CygxIndustrialActivityGroupManagement, 0)
  77. subjectActivityItems = make([]*cygx.CygxIndustrialActivityGroupSubject, 0)
  78. }
  79. return
  80. }
  81. // HandleDateYmdList 处理时间
  82. func HandleDateYmdList(dateYmdList []*cygx.DateYmdList) (activityTimeTextByDay, activityTime, activityTimeEnd string, dateList []string, err error) {
  83. var timeStr string
  84. timeInit := time.Now()
  85. for k, v := range dateYmdList {
  86. for k2, v2 := range v.DateHmsList {
  87. for k3, v3 := range v2.DateHms {
  88. timeStr = v.DateYmd + " " + v3 + ":00"
  89. resultTime := utils.StrTimeToTime(timeStr) //时间字符串格式转时间格式
  90. timeStrYmd := resultTime.Format(utils.FormatDate)
  91. timeYmd := utils.StrTimeToTime(timeStrYmd + " 12:00:00") //拼接当天中午12点的时间
  92. var amOrPm string
  93. if resultTime.After(timeYmd) {
  94. amOrPm = " PM"
  95. } else {
  96. amOrPm = " AM"
  97. }
  98. week := utils.StrDateTimeToWeek(timeStr)
  99. if k == 0 && k2 == 0 && k3 == 0 {
  100. activityTimeTextByDay = timeStrYmd + "(" + week + ")" + v3 + amOrPm
  101. activityTime = timeStr
  102. }
  103. //下一个时间段必须晚于上一个时间段
  104. res := timeInit.After(resultTime)
  105. if res {
  106. err = errors.New("下一个时间段必须晚于上一个时间段!" + timeStr)
  107. return
  108. } else {
  109. timeInit = resultTime
  110. }
  111. dateList = append(dateList, timeStr)
  112. }
  113. }
  114. }
  115. if len(dateList) > 0 {
  116. activityTimeEnd = dateList[len(dateList)-1]
  117. }
  118. if len(dateList)%2 != 0 {
  119. err = errors.New("缺少开始时间或结束时间!")
  120. return
  121. }
  122. return
  123. }
  124. // 处理日期的插入
  125. func AddActivitySpecial(dateList []string, activityId int) (err error) {
  126. if len(dateList) == 0 {
  127. return
  128. }
  129. var items []*cygx.CygxActivitySpecialDay
  130. for _, v := range dateList {
  131. item := new(cygx.CygxActivitySpecialDay)
  132. item.ActivityId = activityId
  133. item.DayTime = v
  134. item.CreateTime = time.Now()
  135. items = append(items, item)
  136. }
  137. _, err = cygx.AddCygxActivitySpecialDay(items, activityId)
  138. return
  139. }
  140. // GetCygxActivitySpecialDay 处理日程编辑的回显日期格式
  141. func GetCygxActivitySpecialDay(activityId int) (items []*cygx.DateYmdList, err error) {
  142. list, e := cygx.GetCygxActivitySpecialDayByActivityId(activityId)
  143. if e != nil {
  144. err = e
  145. return
  146. }
  147. mapYmd := make(map[string]string)
  148. for _, v := range list {
  149. if _, ok := mapYmd[v.DayTime.Format(utils.FormatDate)]; !ok {
  150. mapYmd[v.DayTime.Format(utils.FormatDate)] = v.DayTime.Format(utils.FormatDate)
  151. var itemYmd = new(cygx.DateYmdList)
  152. itemYmd.DateYmd = v.DayTime.Format(utils.FormatDate)
  153. items = append(items, itemYmd)
  154. }
  155. }
  156. var itemHms = new(cygx.DateHmsList)
  157. for k, v := range list {
  158. for kI, vI := range items {
  159. if v.DayTime.Format(utils.FormatDate) == vI.DateYmd {
  160. itemHms.DateHms = append(itemHms.DateHms, v.DayTime.Format(utils.FormatTimeHm))
  161. if k%2 == 1 {
  162. items[kI].DateHmsList = append(items[kI].DateHmsList, itemHms)
  163. itemHms = new(cygx.DateHmsList)
  164. }
  165. }
  166. }
  167. }
  168. return
  169. }
  170. func HandleActivityListSpecial(list []*cygx.CygxActivitySpecialList, isTrip bool) (items []*cygx.CygxActivitySpecialList) {
  171. for k, v := range list {
  172. if list[k].IsDetermineTravel > 0 {
  173. list[k].IsDetermineTravel = 1
  174. }
  175. if !isTrip {
  176. if v.IsOffline > 0 {
  177. list[k].PublishStatus = 3
  178. }
  179. if v.IsDetermineTravel > 0 {
  180. list[k].PublishStatus = 4
  181. }
  182. }
  183. if v.Days > 0 {
  184. resultTime := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  185. res := time.Now().Before(resultTime)
  186. if !res && v.IsSubmitMeeting == 0 {
  187. list[k].IsShowSubmitMeeting = true
  188. }
  189. if !res && v.IsSubmitMeeting == 1 {
  190. list[k].IsShowAttendanceDetails = true
  191. list[k].IsShowUpdateMeeting = true
  192. }
  193. }
  194. }
  195. items = list
  196. return
  197. }
  198. //func init() {
  199. // specialSurplus, _ := GetSpecialSurplusByCompany(9892)
  200. // fmt.Println(specialSurplus)
  201. //}
  202. // TODO:GetSpecialSurplusByCompany 获取公司专项调研剩余次数
  203. func GetSpecialSurplusByCompany(companyId int) (specialSurplus string, err error) {
  204. companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
  205. if e != nil {
  206. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  207. }
  208. if companyDetail == nil {
  209. return
  210. }
  211. if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
  212. return
  213. }
  214. if companyDetail.Status == "永续" {
  215. specialSurplus = "不限次数"
  216. }
  217. // 获取继承点数
  218. inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
  219. if e != nil && e.Error() != utils.ErrNoRow() {
  220. err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
  221. }
  222. //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}
  223. //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
  224. chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
  225. var condition string
  226. var pars []interface{}
  227. condition += ` AND company_id = ? `
  228. pars = append(pars, companyId)
  229. //查询当年的数据
  230. //condition += ` AND b.create_time >= ? `
  231. //pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
  232. listTripBill, e := cygx.GetCygxActivitySpecialTripBillList(condition, pars)
  233. if e != nil {
  234. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  235. return
  236. }
  237. if companyDetail.Status == "正式" {
  238. userType, _, _, _, _ := GetUserType(companyId)
  239. if userType == 2 {
  240. var tripBillNum int
  241. for _, v := range listTripBill {
  242. //if v.ActivityId == 0 {
  243. // continue
  244. //}
  245. tripBillNum += v.BillDetailed
  246. }
  247. if len(inheritList) > 0 {
  248. for _, v := range inheritList {
  249. if v.ChartPermissionId == 0 {
  250. tripBillNum += v.Points
  251. }
  252. }
  253. }
  254. tripRemaining := tripBillNum
  255. if tripRemaining < 0 {
  256. tripRemaining = 0
  257. }
  258. specialSurplus = strconv.Itoa(tripRemaining) + "次"
  259. } else {
  260. list, e := company.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
  261. if e != nil && e.Error() != utils.ErrNoRow() {
  262. err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
  263. }
  264. if len(list) == 0 {
  265. return
  266. }
  267. var chartPermissionIdSlice []string
  268. mapChartName := make(map[string]int)
  269. mapUpgradeId := make(map[int]int)
  270. mapPermissionNameTrip := make(map[string]int)
  271. mapInheritChartName := make(map[string]int)
  272. //mapPermissionName := make(map[int]string)
  273. for _, v := range list {
  274. chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
  275. //是升级套餐才有点数
  276. if v.IsUpgrade == 1 {
  277. mapUpgradeId[v.ChartPermissionId] = 1
  278. }
  279. }
  280. chartList := make([]*models.ChartPermission, 0)
  281. if len(chartPermissionIdSlice) > 0 {
  282. chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
  283. if e != nil {
  284. err = errors.New("获取品种信息失败, Err:" + e.Error())
  285. return
  286. }
  287. }
  288. if len(chartList) == 0 {
  289. return
  290. }
  291. //var TripBillNum int
  292. for _, v := range listTripBill {
  293. if v.ActivityId == 0 {
  294. continue
  295. }
  296. mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
  297. }
  298. for _, v := range chartList {
  299. //如果是升级则加点
  300. if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
  301. mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
  302. } else {
  303. mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
  304. }
  305. }
  306. // 通过继承获得的加点
  307. for _, v := range inheritList {
  308. mapInheritChartName[v.ChartPermissionName] = v.Points
  309. }
  310. for k, _ := range chartNameMap {
  311. if _, ok := mapChartName[k]; ok {
  312. if inherit, ok2 := mapInheritChartName[k]; ok2 {
  313. mapChartName[k] += inherit
  314. }
  315. }
  316. }
  317. for k, v := range mapChartName {
  318. if v > 0 {
  319. specialSurplus += k + strconv.Itoa(v) + "次+"
  320. }
  321. }
  322. specialSurplus = strings.TrimRight(specialSurplus, "+")
  323. }
  324. }
  325. return
  326. }
  327. // GetSpecialUserType 获取专项产业调研的用户身份类型
  328. func GetSpecialUserType(user *models.WxUser) (userType int, err error) {
  329. companyId := user.CompanyId
  330. companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
  331. if e != nil {
  332. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  333. }
  334. if companyId <= 1 {
  335. userType = 0
  336. } else {
  337. if companyDetail.Status == "永续" {
  338. userType = 1
  339. } else {
  340. if companyDetail.Status == "正式" {
  341. list, e := company.GetCompanyReportPermissionUpgrade(companyId, 2)
  342. if e != nil && e.Error() != utils.ErrNoRow() {
  343. err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
  344. }
  345. if len(list) == 0 {
  346. return
  347. }
  348. if len(list) == 4 {
  349. totalName, e := company.GetCompanyPermissionNameCheck(companyId, 2, "策略")
  350. if e != nil {
  351. err = errors.New("获取品种信息失败, Err:" + e.Error())
  352. return
  353. }
  354. if totalName > 0 {
  355. userType = 2
  356. } else {
  357. userType = 8
  358. }
  359. } else {
  360. userType = 8
  361. }
  362. }
  363. }
  364. }
  365. return
  366. }
  367. // CheckActivitySpecialUpdatePower 校验专项调研活动后台管理员、销售是否有修改权限
  368. func CheckActivitySpecialUpdatePower(adminId int, activityInfo *cygx.ActivitySpecialDetail) (havePower bool, popupMsg string, err error) {
  369. defer func() {
  370. if err != nil {
  371. fmt.Println("err:", err)
  372. go alarm_msg.SendAlarmMsg("校验活动后台管理员、销售是否有修改权限失败,Err:"+err.Error(), 3)
  373. }
  374. }()
  375. adminInfo, e := system.GetSysUserById(adminId)
  376. if e != nil {
  377. err = errors.New("获取管理员身份失败,GetSysUserById" + e.Error())
  378. return
  379. }
  380. //如果这个活动不是自己建的会议,那么就对权益销售跟权益组长做身份限制判断
  381. if activityInfo.AdminId > 0 && activityInfo.AdminId != adminId && (adminInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || adminInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP) {
  382. popupMsg = "只有建会销售可修改。"
  383. return
  384. }
  385. havePower = true
  386. return
  387. }
  388. // GetChartPermissionSpecialSurplusByCompany 获取公司专项调研次数-分品种
  389. func GetChartPermissionSpecialSurplusByCompany(companyId int) (userType int, tripRemaining int, mapChartName map[string]int, err error) {
  390. companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
  391. if e != nil {
  392. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  393. }
  394. if companyDetail == nil {
  395. return
  396. }
  397. if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
  398. return
  399. }
  400. var specialSurplus string
  401. if companyDetail.Status == "永续" {
  402. specialSurplus = "不限次数"
  403. }
  404. //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}
  405. //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
  406. chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
  407. if companyDetail.Status == "正式" {
  408. //var packageType int
  409. userType, _, _, _, _ = GetUserType(companyId)
  410. var condition string
  411. var pars []interface{}
  412. condition += ` AND company_id = ? `
  413. pars = append(pars, companyId)
  414. //查询当年的数据
  415. //condition += ` AND b.create_time >= ? `
  416. //pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
  417. listTripBill, e := cygx.GetCygxActivitySpecialTripBillList(condition, pars)
  418. if e != nil {
  419. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  420. return
  421. }
  422. //// 获取继承点数
  423. //inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
  424. //if e != nil && e.Error() != utils.ErrNoRow() {
  425. // err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
  426. //}
  427. if userType == 2 {
  428. var tripBillNum int
  429. for _, v := range listTripBill {
  430. //if v.ActivityId == 0 {
  431. // continue
  432. //}
  433. tripBillNum += v.BillDetailed
  434. }
  435. //if len(inheritList) > 0 {
  436. // for _, v := range inheritList {
  437. // if v.ChartPermissionId == 0 {
  438. // tripBillNum += v.Points
  439. // }
  440. // }
  441. //}
  442. tripRemaining = tripBillNum
  443. if tripRemaining < 0 {
  444. tripRemaining = 0
  445. }
  446. } else {
  447. list, e := company.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
  448. if e != nil && e.Error() != utils.ErrNoRow() {
  449. err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
  450. }
  451. if len(list) == 0 {
  452. return
  453. }
  454. var chartPermissionIdSlice []string
  455. mapChartName = make(map[string]int)
  456. mapUpgradeId := make(map[int]int)
  457. mapPermissionNameTrip := make(map[string]int)
  458. mapInheritChartName := make(map[string]int)
  459. //mapPermissionName := make(map[int]string)
  460. for _, v := range list {
  461. chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
  462. //是升级套餐才有点数
  463. if v.IsUpgrade == 1 {
  464. mapUpgradeId[v.ChartPermissionId] = 1
  465. }
  466. }
  467. chartList := make([]*models.ChartPermission, 0)
  468. if len(chartPermissionIdSlice) > 0 {
  469. chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
  470. if e != nil {
  471. err = errors.New("获取品种信息失败, Err:" + e.Error())
  472. return
  473. }
  474. }
  475. if len(chartList) == 0 {
  476. return
  477. }
  478. for _, v := range listTripBill {
  479. if v.ActivityId == 0 {
  480. continue
  481. }
  482. mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
  483. }
  484. for _, v := range chartList {
  485. //如果是升级则加点
  486. if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
  487. mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
  488. } else {
  489. mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
  490. }
  491. }
  492. // 通过继承获得的加点
  493. //for _, v := range inheritList {
  494. // mapInheritChartName[v.ChartPermissionName] = v.Points
  495. //}
  496. for k, _ := range chartNameMap {
  497. if _, ok := mapChartName[k]; ok {
  498. if inherit, ok2 := mapInheritChartName[k]; ok2 {
  499. mapChartName[k] += inherit
  500. }
  501. }
  502. }
  503. for k, v := range mapChartName {
  504. if v > 0 {
  505. specialSurplus += k + strconv.Itoa(v) + "次+"
  506. }
  507. }
  508. specialSurplus = strings.TrimRight(specialSurplus, "+")
  509. }
  510. }
  511. return
  512. }
  513. //func init() {
  514. // ActivitySpecialCompanyApproval(7030, 1259, "怀民大套餐客户")
  515. // //格式化合同开始时间
  516. // contractStartDate, tmpErr := time.Parse(utils.FormatDate, "2023-12-19")
  517. //
  518. // if tmpErr != nil {
  519. // fmt.Println(tmpErr)
  520. // return
  521. // }
  522. //
  523. // if time.Now().After(contractStartDate) {
  524. // fmt.Println("go")
  525. // }
  526. //}
  527. // 审批通过的时候专项调研次数更新
  528. func ActivitySpecialCompanyApproval(companyId, companyContractId int, companyName string) (err error) {
  529. //userType, packageType, _, _, _ := GetUserType(companyId)
  530. // 获取继承点数
  531. //inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
  532. //if e != nil && e.Error() != utils.ErrNoRow() {
  533. // err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
  534. //}
  535. //chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
  536. // 获取合同信息-套餐信息
  537. companyContract, e := company.GetCompanyContractById(companyContractId)
  538. if e != nil {
  539. err = errors.New("GetCompanyContractById, Err: " + e.Error())
  540. return
  541. }
  542. packageType := companyContract.RaiPackageType
  543. var items []*cygx.CygxActivitySpecialPermissionPoints
  544. itemBill := new(cygx.CygxActivitySpecialTripBill)
  545. itemBill.CreateTime = time.Now()
  546. itemBill.CompanyId = companyId
  547. itemBill.CompanyName = companyName
  548. itemBill.Source = 2
  549. itemBill.DoType = 2
  550. itemBill.Way = 3
  551. if packageType > 0 {
  552. packageTypeMap := map[int]int{1: 16, 2: 12}
  553. totalTrip := packageTypeMap[packageType]
  554. itemBill.BillDetailed = totalTrip
  555. itemBill.Total = strconv.Itoa(itemBill.BillDetailed) + "次"
  556. if packageType == 2 {
  557. itemBill.Content = "45w大套餐转正"
  558. } else {
  559. itemBill.Content = "70w大套餐转正"
  560. }
  561. //记录公司剩余点数
  562. item := new(cygx.CygxActivitySpecialPermissionPoints)
  563. item.Points = totalTrip
  564. item.CompanyId = companyId
  565. item.CompanyName = companyName
  566. item.CreateTime = time.Now()
  567. item.ModifyTime = time.Now()
  568. items = append(items, item)
  569. } else {
  570. var condition string
  571. var pars []interface{}
  572. pars = make([]interface{}, 0)
  573. condition = " AND company_contract_id = ? AND is_upgrade = 1 "
  574. pars = append(pars, companyContractId)
  575. list, e := company.GetCompanyContractPermissionList(condition, pars) // 获取带有升级的权限
  576. if e != nil && e.Error() != utils.ErrNoRow() {
  577. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  578. return
  579. }
  580. if len(list) == 0 {
  581. return
  582. }
  583. //获取权益主观权限
  584. listRaiSubjectivity, e := models.GetChartPermissionListRaiSubjectivity()
  585. if e != nil && e.Error() != utils.ErrNoRow() {
  586. err = errors.New("GetChartPermissionListRaiSubjectivity, Err: " + e.Error())
  587. }
  588. mapRaiSubjectivity := make(map[int]bool)
  589. mapPermissionName := make(map[int]string)
  590. for _, v := range listRaiSubjectivity {
  591. mapRaiSubjectivity[v.ChartPermissionId] = true
  592. mapPermissionName[v.ChartPermissionId] = v.ChartPermissionName
  593. }
  594. for _, v := range list {
  595. //如果是升级则加点
  596. if v.IsUpgrade == 1 && mapRaiSubjectivity[v.ChartPermissionId] {
  597. item := new(cygx.CygxActivitySpecialPermissionPoints)
  598. item.Points = 5
  599. item.CompanyId = companyId
  600. item.CompanyName = companyName
  601. item.ChartPermissionId = v.ChartPermissionId
  602. item.ChartPermissionName = mapPermissionName[v.ChartPermissionId]
  603. item.CreateTime = time.Now()
  604. item.ModifyTime = time.Now()
  605. items = append(items, item)
  606. }
  607. }
  608. itemBill.Content = "行业升级套餐转正"
  609. }
  610. e = cygx.MultiAddCygxActivitySpecialPermissionPoints(items)
  611. if e != nil {
  612. err = errors.New("MultiAddCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  613. return
  614. }
  615. if packageType == 0 && len(items) > 0 {
  616. var condition string
  617. var pars []interface{}
  618. pars = make([]interface{}, 0)
  619. condition = " AND company_id = ? AND points > 0 ORDER BY chart_permission_id DESC "
  620. pars = append(pars, companyId)
  621. activitySpecialPermissionPointsList, e := cygx.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  622. if e != nil && e.Error() != utils.ErrNoRow() {
  623. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  624. return
  625. }
  626. var itemBillTotal []string
  627. itemBillTotalMap := make(map[string]string)
  628. for _, v := range activitySpecialPermissionPointsList {
  629. itemBillTotal = append(itemBillTotal, fmt.Sprint(v.ChartPermissionName, v.Points, "次"))
  630. itemBillTotalMap[v.ChartPermissionName] = fmt.Sprint(v.ChartPermissionName, v.Points, "次")
  631. }
  632. itemBill.BillDetailed = 5 * len(items)
  633. permissionNameSlice := []string{"医药", "消费", "科技", "智造"}
  634. var itemBillTotalNew []string
  635. for _, v := range permissionNameSlice {
  636. if itemBillTotalMap[v] == "" {
  637. itemBillTotalNew = append(itemBillTotalNew, fmt.Sprint(v, "0次"))
  638. } else {
  639. itemBillTotalNew = append(itemBillTotalNew, itemBillTotalMap[v])
  640. }
  641. }
  642. itemBill.Total = strings.Join(itemBillTotalNew, "+")
  643. }
  644. //如果有升级行业权限,或者大套餐客户就写入流水信息
  645. if itemBill.Total != "" {
  646. e := cygx.AddCygxActivitySpecialTripBill(itemBill)
  647. if e != nil {
  648. err = errors.New("AddCygxActivitySpecialTripBill, Err:" + e.Error())
  649. return
  650. }
  651. }
  652. return
  653. }
  654. // 取消专项调研返点
  655. func ActivitySpecialPublishAndCancel(activityInfo *cygx.ActivitySpecialDetail) (err error) {
  656. //userType, tripRemaining, mapChartName, err := GetChartPermissionSpecialSurplusByCompany(companyId)
  657. //if err != nil {
  658. // br.Msg = "获取专项调研剩余次数失败"
  659. // br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  660. // return
  661. //}
  662. //itemBill := new(cygx.CygxActivitySpecialTripBill)
  663. //itemBill.CreateTime = time.Now()
  664. //itemBill.ActivityId = activityInfo.ActivityId
  665. //itemBill.Content = activityInfo.ResearchTheme + "--活动取消"
  666. //itemBill.Source = 2
  667. //itemBill.DoType = 2
  668. //itemBill.Way = 4
  669. //itemBill.BillDetailed = 1
  670. //err = cygx.AddCygxActivitySpecialTripBill(itemBill)
  671. //if err != nil {
  672. // return
  673. //}
  674. return
  675. }
  676. //func init() {
  677. // fmt.Println(GetSpecialSurplusByCompanyNew(16))
  678. //}
  679. // GetSpecialSurplusByCompanyNew 获取公司专项调研剩余次数-用流水表数据不计算了,计算都丢在流水里
  680. func GetSpecialSurplusByCompanyNew(companyId int) (specialSurplus string, err error) {
  681. companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
  682. if e != nil {
  683. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  684. }
  685. if companyDetail == nil {
  686. return
  687. }
  688. //if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
  689. // return
  690. //}
  691. //if companyDetail.Status == "永续" {
  692. // specialSurplus = "不限次数"
  693. //}
  694. //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}
  695. //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
  696. //if companyDetail.Status == "正式" {
  697. billItem, e := cygx.GetCygxActivitySpecialTripBillByCompanyId(companyId)
  698. if e != nil && e.Error() != utils.ErrNoRow() {
  699. err = e
  700. return
  701. }
  702. if billItem != nil {
  703. specialSurplus = billItem.Total
  704. } else {
  705. specialSurplus = "0次"
  706. }
  707. return
  708. //}
  709. //return
  710. }
  711. // GetSpecialBillMaxChartPermissionId 获取专项调研剩余点数最多的行业
  712. func GetSpecialBillMaxChartPermissionId(user *cygx.UserAndCompanyName) (maxChartPermissionId int, maxChartPermissionName string, err error) {
  713. companyId := user.CompanyId
  714. var condition string
  715. var pars []interface{}
  716. condition = " AND company_id = ? AND points > 0 ORDER BY points DESC LIMIT 1 "
  717. pars = append(pars, companyId)
  718. activitySpecialPermissionPointsList, e := cygx.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  719. if e != nil && e.Error() != utils.ErrNoRow() {
  720. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  721. return
  722. }
  723. for _, v := range activitySpecialPermissionPointsList {
  724. maxChartPermissionId = v.ChartPermissionId
  725. maxChartPermissionName = v.ChartPermissionName
  726. }
  727. return
  728. }
  729. // DeductTripRemainingtimesByUser 扣除用户专项调研剩余次数
  730. func DeductTripRemainingtimesByUser(user *cygx.UserAndCompanyName, activityInfo *cygx.ActivitySpecialDetail, roleTypeCode string) (err error) {
  731. msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
  732. companyId := user.CompanyId
  733. chartPermissionName := activityInfo.ChartPermissionName
  734. companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  735. if e != nil {
  736. err = errors.New("GetCompanyProductByCompanyIdAndProductId, Err:" + e.Error())
  737. return
  738. }
  739. var condition string
  740. var pars []interface{}
  741. condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
  742. pars = append(pars, companyId)
  743. //判断是不是策略、固收行业的活动,如果不是策略就获取对应行业的点数 ,策略的任何一个行业有剩余点数都能参加
  744. if chartPermissionName != utils.CE_LUE_NAME && chartPermissionName != utils.GU_SHOU_NAME {
  745. pars = make([]interface{}, 0)
  746. condition = " AND company_id = ? AND (chart_permission_id = ? OR chart_permission_id = 0 ) ORDER BY points DESC LIMIT 1 "
  747. pars = append(pars, companyId, activityInfo.ChartPermissionId)
  748. }
  749. activitySpecialPermissionPointsList, e := cygx.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  750. if e != nil && e.Error() != utils.ErrNoRow() {
  751. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  752. return
  753. }
  754. //如果永续客户不存在单独的行业剩余点数记录,那么就添加一条 -1 的记录作为初始化数据
  755. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && len(activitySpecialPermissionPointsList) == 0 {
  756. item := new(cygx.CygxActivitySpecialPermissionPoints)
  757. item.CompanyId = companyProduct.CompanyId
  758. item.Points = -1
  759. item.CompanyName = companyProduct.CompanyName
  760. //item.ChartPermissionId = activityInfo.ChartPermissionId
  761. //item.ChartPermissionName = activityInfo.ChartPermissionName
  762. item.CreateTime = time.Now()
  763. item.ModifyTime = time.Now()
  764. e = cygx.AddCygxActivitySpecialPermissionPoints(item)
  765. if e != nil {
  766. err = errors.New("AddCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  767. return
  768. }
  769. return
  770. }
  771. var maxPoints int // 获取最大剩余点数,(写的有点多余)
  772. for _, v := range activitySpecialPermissionPointsList {
  773. if maxPoints < v.Points {
  774. maxPoints = v.Points
  775. }
  776. }
  777. //如果是超管给一个没有行业权限的试用,冻结等相关客户报名,就写入一条对应的行业权限点数信息
  778. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN && len(activitySpecialPermissionPointsList) == 0 {
  779. item := new(cygx.CygxActivitySpecialPermissionPoints)
  780. item.CompanyId = companyProduct.CompanyId
  781. item.Points = -1
  782. item.CompanyName = companyProduct.CompanyName
  783. item.ChartPermissionId = activityInfo.ChartPermissionId
  784. item.ChartPermissionName = activityInfo.ChartPermissionName
  785. item.CreateTime = time.Now()
  786. item.ModifyTime = time.Now()
  787. e = cygx.AddCygxActivitySpecialPermissionPoints(item)
  788. if e != nil {
  789. err = errors.New("AddCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  790. return
  791. }
  792. return
  793. }
  794. //永续客户不做判断、超管不做判断
  795. if companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  796. //如果没有剩余的点数,就返回
  797. if maxPoints <= 0 && roleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  798. err = errors.New(msgTemplate)
  799. return
  800. }
  801. }
  802. var maxChartPermissionId int // 剩余点数最多的行业
  803. var points int // 剩余点数
  804. for _, v := range activitySpecialPermissionPointsList {
  805. maxChartPermissionId = v.ChartPermissionId
  806. points = v.Points - 1
  807. }
  808. //更新对应行业剩余点数
  809. e = cygx.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
  810. if e != nil {
  811. err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  812. return
  813. }
  814. return
  815. }
  816. // RebateTripRemainingtimesByUser 返点用户专项调研剩余次数
  817. func RebateTripRemainingtimesByUser(user *cygx.UserAndCompanyName, activityInfo *cygx.ActivitySpecialDetail) (err error) {
  818. companyId := user.CompanyId
  819. chartPermissionName := activityInfo.ChartPermissionName
  820. var condition string
  821. var pars []interface{}
  822. condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
  823. pars = append(pars, companyId)
  824. //判断是不是策略、固收行业的活动,如果不是策略就获取对应行业的点数 ,策略的任何一个行业有剩余点数都能参加
  825. if chartPermissionName != utils.CE_LUE_NAME && chartPermissionName != utils.GU_SHOU_NAME {
  826. pars = make([]interface{}, 0)
  827. condition = " AND company_id = ? AND (chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
  828. pars = append(pars, companyId, activityInfo.ChartPermissionId)
  829. } else {
  830. lastTripBill, e := cygx.GetCygxActivitySpecialTripBillLastDetialByActivityId(activityInfo.ActivityId, user.UserId)
  831. if e != nil {
  832. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  833. return
  834. }
  835. pars = make([]interface{}, 0)
  836. condition = " AND company_id = ? AND (chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
  837. pars = append(pars, companyId, lastTripBill.ChartPermissionId)
  838. }
  839. activitySpecialPermissionPointsList, e := cygx.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  840. if e != nil && e.Error() != utils.ErrNoRow() {
  841. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  842. return
  843. }
  844. var maxChartPermissionId int // 剩余点数最多的行业(返点行业)
  845. var points int // 剩余点数
  846. for _, v := range activitySpecialPermissionPointsList {
  847. maxChartPermissionId = v.ChartPermissionId
  848. points = v.Points + 1
  849. }
  850. //更新对应行业剩余点数
  851. e = cygx.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
  852. if e != nil {
  853. err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  854. return
  855. }
  856. return
  857. }
  858. // 预处理专项调研流水明细表描述内容
  859. func HandleActivitySpecialTripBillTotalText(companyId int) (totalText string, err error) {
  860. //companyId := user.CompanyId
  861. companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
  862. if e != nil {
  863. err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
  864. return
  865. }
  866. var condition string
  867. var pars []interface{}
  868. pars = make([]interface{}, 0)
  869. condition = " AND company_id = ? AND points != 0 ORDER BY chart_permission_id DESC "
  870. pars = append(pars, companyId)
  871. activitySpecialPermissionPointsList, e := cygx.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  872. if e != nil && e.Error() != utils.ErrNoRow() {
  873. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  874. return
  875. }
  876. //永续客户次数累加
  877. if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
  878. var points int
  879. for _, v := range activitySpecialPermissionPointsList {
  880. points += v.Points
  881. }
  882. totalText = fmt.Sprint(points, "次") //追加永续客户展示规则(2024-02-23)
  883. return
  884. }
  885. var itemBillTotal []string
  886. itemBillTotalMap := make(map[string]string)
  887. for _, v := range activitySpecialPermissionPointsList {
  888. if v.ChartPermissionId > 0 {
  889. itemBillTotal = append(itemBillTotal, fmt.Sprint(v.ChartPermissionName, v.Points, "次")) // 行业套餐客户展示规则
  890. itemBillTotalMap[v.ChartPermissionName] = fmt.Sprint(v.ChartPermissionName, v.Points, "次")
  891. }
  892. if v.ChartPermissionId == 0 {
  893. totalText = fmt.Sprint(v.Points, "次") // 大套餐客户展示规则,
  894. }
  895. }
  896. if len(itemBillTotal) > 0 {
  897. permissionNameSlice := []string{"医药", "消费", "科技", "智造"}
  898. var itemBillTotalNew []string
  899. for _, v := range permissionNameSlice {
  900. if itemBillTotalMap[v] == "" {
  901. itemBillTotalNew = append(itemBillTotalNew, fmt.Sprint(v, "0次"))
  902. } else {
  903. itemBillTotalNew = append(itemBillTotalNew, itemBillTotalMap[v])
  904. }
  905. }
  906. totalText = strings.Join(itemBillTotalNew, "+")
  907. }
  908. return
  909. }
  910. func initnee885() {
  911. list, err := cygx.GetBestNewBillList()
  912. if err != nil {
  913. fmt.Println(err)
  914. }
  915. var items []*cygx.CygxActivitySpecialPermissionPoints
  916. for _, v := range list {
  917. fmt.Println(v.Total)
  918. if v.Total == "" || v.Total == "0" || v.Total == "0次" {
  919. continue
  920. }
  921. //智造5次+科技5次+消费5次+医药4次
  922. sliceTotal := strings.Split(v.Total, "+")
  923. for _, vs := range sliceTotal {
  924. var points string
  925. item := new(cygx.CygxActivitySpecialPermissionPoints)
  926. if strings.Contains(vs, "医药") {
  927. item.ChartPermissionName = "医药"
  928. points := strings.Replace(vs, "医药", "", -1)
  929. fmt.Println(points)
  930. points = strings.Replace(points, "次", "", -1)
  931. item.Points, _ = strconv.Atoi(points)
  932. item.ChartPermissionId = 22
  933. } else if strings.Contains(vs, "消费") {
  934. item.ChartPermissionName = "消费"
  935. points := strings.Replace(vs, "消费", "", -1)
  936. points = strings.Replace(points, "次", "", -1)
  937. item.ChartPermissionId = 21
  938. item.Points, _ = strconv.Atoi(points)
  939. } else if strings.Contains(vs, "科技") {
  940. item.ChartPermissionName = "科技"
  941. points := strings.Replace(vs, "科技", "", -1)
  942. points = strings.Replace(points, "次", "", -1)
  943. item.ChartPermissionId = 20
  944. item.Points, _ = strconv.Atoi(points)
  945. } else if strings.Contains(vs, "智造") {
  946. item.ChartPermissionName = "智造"
  947. points := strings.Replace(vs, "智造", "", -1)
  948. points = strings.Replace(points, "次", "", -1)
  949. item.ChartPermissionId = 19
  950. item.Points, _ = strconv.Atoi(points)
  951. } else {
  952. points = strings.Replace(vs, "次", "", -1)
  953. item.Points, _ = strconv.Atoi(points)
  954. }
  955. fmt.Println("points", points)
  956. fmt.Println(vs)
  957. item.CompanyId = v.CompanyId
  958. item.CompanyName = v.CompanyName
  959. item.CreateTime = time.Now()
  960. item.ModifyTime = time.Now()
  961. fmt.Println(item)
  962. items = append(items, item)
  963. }
  964. }
  965. fmt.Println(len(items))
  966. err = cygx.MultiAddCygxActivitySpecialPermissionPoints(items)
  967. fmt.Println(err)
  968. }