activity_points_set.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/services/alarm_msg"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "time"
  12. )
  13. // 获取公司剩余点数
  14. func GetCygxActivityPointsCompanyByCompanyId(companyId int) (points string) {
  15. var err error
  16. points = "0" // 默认都展示 0
  17. defer func() {
  18. if err != nil {
  19. go alarm_msg.SendAlarmMsg("获取公司剩余点数失败:"+err.Error(), 2)
  20. }
  21. }()
  22. pointsCompany, e := cygx.GetCygxActivityPointsCompanyByCompanyId(companyId)
  23. if e != nil && e.Error() != utils.ErrNoRow() {
  24. err = errors.New("GetCygxActivityPointsCompanyByCompanyId,Err" + e.Error())
  25. return
  26. }
  27. if pointsCompany == nil {
  28. return
  29. }
  30. //转字符串
  31. points = fmt.Sprint(pointsCompany.Points)
  32. return
  33. }
  34. // CheckActivityPointsSet 校验研选扣点内容
  35. func CheckActivityPointsSet(pointsSet *cygx.CygxActivityPointsSetRsq, activityId int) (errMsg string, itemResp *cygx.CygxActivityPointsSet, err error) {
  36. //校验研选扣点内容
  37. if pointsSet == nil {
  38. return
  39. }
  40. switch pointsSet.PointsObject {
  41. case "":
  42. errMsg = "请选择扣点对象"
  43. case "1":
  44. if pointsSet.UserPointsNum == "" {
  45. errMsg = "参会人扣点数量不能为空"
  46. }
  47. case "2":
  48. if pointsSet.CompanyPointsNum == "" {
  49. errMsg = "办会人扣点数量不能为空"
  50. }
  51. if pointsSet.CompanyId == 0 {
  52. errMsg = "请选择对应公司"
  53. }
  54. case "3":
  55. if pointsSet.UserPointsNum == "" {
  56. errMsg = "参会人扣点数量不能为空"
  57. }
  58. if pointsSet.CompanyPointsNum == "" {
  59. errMsg = "办会人扣点数量不能为空"
  60. }
  61. if pointsSet.CompanyId == 0 {
  62. errMsg = "请选择对应公司"
  63. }
  64. }
  65. companyId := pointsSet.CompanyId
  66. if companyId > 0 {
  67. pointsCompany, e := cygx.GetCygxActivityPointsCompanyByCompanyId(companyId)
  68. if e != nil && e.Error() != utils.ErrNoRow() {
  69. err = errors.New("GetCygxActivityPointsCompanyByCompanyId,Err" + e.Error())
  70. return
  71. }
  72. if pointsCompany == nil {
  73. errMsg = "公司剩余点数不足"
  74. return
  75. }
  76. companyPointsNum, _ := strconv.ParseFloat(pointsSet.CompanyPointsNum, 2)
  77. if companyPointsNum == 0 {
  78. errMsg = "办会人扣点数量设置有误" + pointsSet.CompanyPointsNum
  79. }
  80. if activityId == 0 {
  81. if companyPointsNum > pointsCompany.Points {
  82. errMsg = "公司剩余点数不足"
  83. }
  84. } else {
  85. pointsSetDetail, e := cygx.GetCygxActivityPointsSetDetail(activityId)
  86. if e != nil && e.Error() != utils.ErrNoRow() {
  87. err = errors.New("GetCygxActivityPointsSetDetail, Err: " + e.Error())
  88. return
  89. }
  90. if pointsSetDetail != nil {
  91. if pointsSetDetail.CompanyId != companyId {
  92. if companyPointsNum > pointsCompany.Points {
  93. errMsg = "公司剩余点数不足"
  94. }
  95. } else {
  96. //查询最新的一条针对这个公司的扣点记录
  97. var condition string
  98. var pars []interface{}
  99. condition += ` AND activity_id = ? AND company_id = ? AND mobile = '' ORDER BY id DESC LIMIT 1 `
  100. pars = append(pars, activityId, companyId)
  101. activityPointsBillDetail, e := cygx.GetCygxActivityPointsBillDetailByCondition(condition, pars)
  102. if e != nil && e.Error() != utils.ErrNoRow() {
  103. err = errors.New("GetCompanyPoints, Err: " + e.Error())
  104. return
  105. }
  106. if activityPointsBillDetail != nil {
  107. CompanyPointsNumOld, _ := strconv.ParseFloat(pointsSetDetail.CompanyPointsNum, 2)
  108. if pointsCompany.Points-companyPointsNum+CompanyPointsNumOld < 0 {
  109. errMsg = "公司剩余点数不足"
  110. }
  111. }
  112. }
  113. } else {
  114. if companyPointsNum > pointsCompany.Points {
  115. errMsg = "公司剩余点数不足"
  116. }
  117. }
  118. }
  119. }
  120. if errMsg != "" {
  121. return
  122. }
  123. item := new(cygx.CygxActivityPointsSet)
  124. item.PointsObject = pointsSet.PointsObject
  125. item.CompanyId = pointsSet.CompanyId
  126. item.CompanyName = pointsSet.CompanyName
  127. item.UserPointsNum = pointsSet.UserPointsNum
  128. item.PointsType = pointsSet.PointsType
  129. item.CompanyPointsNum = pointsSet.CompanyPointsNum
  130. item.CancelDeadlineType = pointsSet.CancelDeadlineType
  131. item.CreateTime = time.Now()
  132. item.ModifyTime = time.Now()
  133. itemResp = item
  134. return
  135. }
  136. // UpdateYanXuanActivityPointsBillOldComapny 活动内容编辑的时候对原有的公司进行返点
  137. func UpdateYanXuanActivityPointsBillOldComapny(activityInfo *cygx.ActivityDetail, adminId int, pointsSet *cygx.CygxActivityPointsSet, pointsSetOld *cygx.CygxActivityPointsSetRsq) (errMsg string, itemResp *cygx.CygxActivityPointsSet, err error) {
  138. defer func() {
  139. if err != nil {
  140. go alarm_msg.SendAlarmMsg("获取公司剩余点数失败:"+err.Error(), 2)
  141. }
  142. }()
  143. activityId := activityInfo.ActivityId
  144. var companyIdNew int
  145. var companyIdOld int
  146. if pointsSetOld == nil {
  147. return
  148. }
  149. companyIdOld = pointsSetOld.CompanyId
  150. if pointsSet != nil {
  151. companyIdNew = pointsSet.CompanyId
  152. }
  153. if companyIdNew == companyIdOld && companyIdNew > 0 {
  154. go YanXuanActivityPointsBillActivityEdit(activityId, adminId) //活动编辑处理研选扣点
  155. }
  156. //如果新老机构一样或者非办会人扣点,那么不做任何处理
  157. if companyIdNew == companyIdOld || companyIdOld == 0 {
  158. return
  159. }
  160. comapny, e := company.GetCompanyDetailById(companyIdOld)
  161. if e != nil {
  162. err = errors.New("GetCompanyDetailById" + e.Error())
  163. return
  164. }
  165. companyPointsNumOld, _ := strconv.ParseFloat(pointsSetOld.CompanyPointsNum, 2)
  166. var items []*cygx.CygxActivityPointsBill
  167. item := new(cygx.CygxActivityPointsBill)
  168. var itemCompanys []*cygx.CygxActivityPointsCompany
  169. itemCompany := new(cygx.CygxActivityPointsCompany)
  170. // 获取用户所在公司剩余的点
  171. companyPointsNum, e := cygx.GetCompanyPoints(companyIdOld)
  172. if e != nil && e.Error() != utils.ErrNoRow() {
  173. err = errors.New("GetCompanyPoints, Err: " + e.Error())
  174. return
  175. }
  176. item.ActivityId = activityId
  177. item.CreateTime = time.Now()
  178. item.CompanyId = comapny.CompanyId
  179. item.CompanyName = comapny.CompanyName
  180. item.BillDetailed = companyPointsNumOld
  181. item.RegisterPlatform = 0
  182. item.AdminId = int64(adminId)
  183. item.Source = 2
  184. item.ChartPermissionId = activityInfo.ChartPermissionId
  185. item.DoType = 2
  186. item.Content = activityInfo.ActivityName + "--取消办会"
  187. item.Points = companyPointsNum + companyPointsNumOld
  188. items = append(items, item)
  189. //更新对应机构的剩余点数
  190. itemCompany.CompanyId = comapny.CompanyId
  191. itemCompany.Points = item.Points
  192. itemCompany.ModifyTime = time.Now()
  193. itemCompanys = append(itemCompanys, itemCompany)
  194. //fmt.Println("更换公司处理返点")
  195. e = cygx.AddCygxActivityPointsBillMulti(items, itemCompanys)
  196. if e != nil && e.Error() != utils.ErrNoRow() {
  197. err = errors.New("AddCygxActivityPointsBillMulti, Err: " + e.Error())
  198. return
  199. }
  200. return
  201. }
  202. // 1用户报名添加到处理研选扣点
  203. func YanXuanActivityPointsBillSignupAdd(activityId, uid, adminId int) (err error) {
  204. defer func() {
  205. if err != nil {
  206. fmt.Println(err)
  207. msg := fmt.Sprint("activityId:", activityId, "userId:", uid)
  208. go alarm_msg.SendAlarmMsg("用户报名添加到处理研选扣点,写入Redis队列消息失败:"+err.Error()+msg, 2)
  209. }
  210. }()
  211. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  212. log := &cygx.YanXuanActivityPointsRedis{UserId: uid, ActivityId: activityId, SourceType: 1, AdminId: adminId, Source: 2, CreateTime: time.Now()}
  213. if utils.Re == nil {
  214. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  215. if err != nil {
  216. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  217. }
  218. }
  219. return
  220. }
  221. // 2用户取消报名添加到处理研选扣点
  222. func YanXuanActivityPointsBillSignupCancel(activityId, uid, adminId int) (err error) {
  223. defer func() {
  224. if err != nil {
  225. fmt.Println(err)
  226. msg := fmt.Sprint("activityId:", activityId, "userId:", uid)
  227. go alarm_msg.SendAlarmMsg("用户取消报名添加到处理研选扣点,写入Redis队列消息失败:"+err.Error()+msg, 2)
  228. }
  229. }()
  230. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  231. log := &cygx.YanXuanActivityPointsRedis{UserId: uid, ActivityId: activityId, SourceType: 2, AdminId: adminId, Source: 2, CreateTime: time.Now()}
  232. if utils.Re == nil {
  233. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  234. if err != nil {
  235. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  236. }
  237. }
  238. return
  239. }
  240. // 3 活动编辑处理研选扣点
  241. func YanXuanActivityPointsBillActivityEdit(activityId, adminId int) (err error) {
  242. defer func() {
  243. if err != nil {
  244. fmt.Println(err)
  245. msg := fmt.Sprint("activityId:", activityId)
  246. go alarm_msg.SendAlarmMsg("活动编辑理研选扣点,写入Redis队列消息失败:"+err.Error()+msg, 2)
  247. }
  248. }()
  249. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  250. log := &cygx.YanXuanActivityPointsRedis{ActivityId: activityId, SourceType: 3, AdminId: adminId, Source: 2, CreateTime: time.Now()}
  251. if utils.Re == nil {
  252. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  253. if err != nil {
  254. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  255. }
  256. }
  257. return
  258. }
  259. // 4 活动发布以及取消发布处理研选扣点
  260. func YanXuanActivityPointsBillActivityPublishAndCancel(activityId, adminId, publishStatus int) (err error) {
  261. defer func() {
  262. if err != nil {
  263. fmt.Println(err)
  264. msg := fmt.Sprint("activityId:", activityId)
  265. go alarm_msg.SendAlarmMsg("活动发布以及取消发布处理研选扣点,写入Redis队列消息失败:"+err.Error()+msg, 2)
  266. }
  267. }()
  268. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  269. log := &cygx.YanXuanActivityPointsRedis{ActivityId: activityId, SourceType: 4, AdminId: adminId, Source: 2, PublishStatus: publishStatus, CreateTime: time.Now()}
  270. if utils.Re == nil {
  271. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  272. if err != nil {
  273. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  274. }
  275. }
  276. return
  277. }
  278. // 5 活动到会研选扣点处理
  279. func YanXuanActivityPointsBillSubmitMeeting(activityId, adminId int) (err error) {
  280. defer func() {
  281. if err != nil {
  282. fmt.Println(err)
  283. msg := fmt.Sprint("activityId:", activityId)
  284. go alarm_msg.SendAlarmMsg("活动到会研选扣点处理,写入Redis队列消息失败:"+err.Error()+msg, 2)
  285. }
  286. }()
  287. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。6:研选审批通过的时候研选扣点更新、 7:正式专试用定时任务更新研选扣点"`
  288. log := &cygx.YanXuanActivityPointsRedis{ActivityId: activityId, SourceType: 5, AdminId: adminId, Source: 2, CreateTime: time.Now()}
  289. if utils.Re == nil {
  290. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  291. if err != nil {
  292. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  293. }
  294. }
  295. return
  296. }
  297. // 6 研选审批通过的时候研选扣点更新
  298. func YanXuanCompanyApproval(comapnyId, companyContractId int) (err error) {
  299. defer func() {
  300. if err != nil {
  301. fmt.Println(err)
  302. msg := fmt.Sprint("comapnyId:", comapnyId)
  303. go alarm_msg.SendAlarmMsg("研选审批通过的时候研选扣点更新,写入Redis队列消息失败:"+err.Error()+msg, 2)
  304. }
  305. }()
  306. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  307. log := &cygx.YanXuanActivityPointsRedis{ComapnyId: comapnyId, CompanyContractId: companyContractId, SourceType: 6, Source: 2, CreateTime: time.Now()}
  308. if utils.Re == nil {
  309. err := utils.Rc.LPush(utils.CYGX_YANXUAN_POINTS_KEY, log)
  310. if err != nil {
  311. fmt.Println("YanXuanActivityPointsRedis LPush Err:" + err.Error())
  312. }
  313. }
  314. return
  315. }
  316. func init0703() {
  317. //2023.07.03剩余服务点数初始化
  318. path := "0703.xlsx"
  319. xlFile, err := xlsx.OpenFile(path)
  320. if err != nil {
  321. fmt.Println(err)
  322. }
  323. mapcompanyName := make(map[string]string)
  324. fmt.Println(xlFile)
  325. var items []*cygx.CygxActivityPointsCompany
  326. for _, sheet := range xlFile.Sheets {
  327. //遍历行读取
  328. maxRow := sheet.MaxRow
  329. for i := 0; i < maxRow; i++ {
  330. //fmt.Println(i)
  331. if i == 0 {
  332. continue
  333. }
  334. row := sheet.Row(i)
  335. cells := row.Cells
  336. var companyName string
  337. var points float64
  338. item := new(cygx.CygxActivityPointsCompany)
  339. for k, cell := range cells {
  340. if k == 0 {
  341. text := cell.String()
  342. companyName = text
  343. }
  344. if k == 3 {
  345. text := cell.String()
  346. points, _ = strconv.ParseFloat(text, 2)
  347. item.Points = points
  348. }
  349. itemDetail, err := company.CheckCompanyProductByName(companyName, 2)
  350. if err != nil {
  351. fmt.Println(err, companyName)
  352. return
  353. }
  354. item.CompanyName = itemDetail.CompanyName
  355. item.CompanyId = itemDetail.CompanyId
  356. item.CreateTime = time.Now()
  357. item.ModifyTime = time.Now()
  358. if mapcompanyName[itemDetail.CompanyName] != "" {
  359. continue
  360. }
  361. items = append(items, item)
  362. mapcompanyName[itemDetail.CompanyName] = itemDetail.CompanyName
  363. }
  364. }
  365. }
  366. var companyIds string
  367. var itemsBill []*cygx.CygxActivityPointsBill
  368. for _, v := range items {
  369. fmt.Println(v.CompanyName, v.Points)
  370. itemBill := new(cygx.CygxActivityPointsBill)
  371. itemBill.CreateTime = time.Now()
  372. itemBill.CompanyId = v.CompanyId
  373. itemBill.CompanyName = v.CompanyName
  374. itemBill.Source = 3
  375. itemBill.BillDetailed = v.Points
  376. itemBill.Points = v.Points
  377. itemBill.DoType = 2
  378. itemBill.Content = "2023.07.03剩余服务点数初始化"
  379. itemsBill = append(itemsBill, itemBill)
  380. }
  381. err = cygx.AddCygxActivityPointsBillAndCompanyMulti(itemsBill, items)
  382. fmt.Println(len(items))
  383. fmt.Println(len(itemsBill))
  384. utils.FileLog.Info(companyIds)
  385. fmt.Println(companyIds)
  386. fmt.Println(err)
  387. }
  388. func UpdateActivityVivoPointsSet(activityInfo *cygx.ActivityDetail, vivoPointsSet cygx.CygxActivityVivoPointsSetReq, itemPointsSet *cygx.CygxActivityPointsSet, sourceVivo string) (err error) {
  389. activityId := activityInfo.ActivityId
  390. if vivoPointsSet.IsPay == 0 {
  391. return
  392. }
  393. if sourceVivo == "" {
  394. return
  395. }
  396. total, e := cygx.GetCygxActivityVivoPointsSetCountByActivityId(activityId)
  397. if e != nil {
  398. err = errors.New("GetCygxActivityVivoPointsSetCountByActivityId, Err: " + e.Error())
  399. return
  400. }
  401. if vivoPointsSet.PointsNum == "" {
  402. vivoPointsSet.PointsNum = "0"
  403. }
  404. if vivoPointsSet.GoodsMoney == "" {
  405. vivoPointsSet.GoodsMoney = "0"
  406. }
  407. item := new(cygx.CygxActivityVivoPointsSet)
  408. item.ActivityId = activityId
  409. item.PointsObject = vivoPointsSet.PointsObject
  410. item.PointsNum = vivoPointsSet.PointsNum
  411. item.GoodsMoney = vivoPointsSet.GoodsMoney
  412. item.Source = sourceVivo
  413. item.CreateTime = time.Now()
  414. item.ModifyTime = time.Now()
  415. if total == 0 {
  416. e = cygx.AddCygxActivityVivoPointsSet(item)
  417. if e != nil {
  418. err = errors.New("AddCygxActivityVivoPointsSet, Err: " + e.Error())
  419. return
  420. }
  421. } else {
  422. e = cygx.EditCygxActivityVivoPointsSet(item)
  423. if e != nil {
  424. err = errors.New("AddCygxActivityVivoPointsSet, Err: " + e.Error())
  425. return
  426. }
  427. }
  428. return
  429. }