data_entry.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //数据录入
  12. // 日度消息推送
  13. func FrequencyByDay() (err error) {
  14. defer func() {
  15. if err != nil {
  16. fmt.Println("FrequencyByDay Err:", err.Error())
  17. utils.FileLog.Info("FrequencyByDay err: %s", err.Error())
  18. }
  19. }()
  20. frequency := "日度"
  21. items, err := models.GetEdbInfoByFrequency(frequency)
  22. if err != nil {
  23. fmt.Println("GetFrequencyByDay err:", err.Error())
  24. return
  25. }
  26. if len(items) <= 0 {
  27. err = errors.New("没有需要推送的数据")
  28. return
  29. }
  30. for _, v := range items {
  31. noticeTime := v.NoticeTime
  32. if noticeTime == "" {
  33. err = errors.New("NoticeTime 参数错误:NoticeTime is empty ")
  34. return
  35. }
  36. noticeTimeArr := strings.Split(noticeTime, ":")
  37. if len(noticeTimeArr) < 3 {
  38. err = errors.New("NoticeTime 参数错误,noticeTime:" + noticeTime)
  39. return
  40. }
  41. noticeHour, err := strconv.Atoi(noticeTimeArr[0])
  42. if err != nil {
  43. return err
  44. }
  45. noticeMinute, err := strconv.Atoi(noticeTimeArr[1])
  46. if err != nil {
  47. return err
  48. }
  49. noticeSecond, err := strconv.Atoi(noticeTimeArr[2])
  50. if err != nil {
  51. return err
  52. }
  53. noticeSecondTotal := noticeHour*3600 + noticeMinute*60 + noticeSecond
  54. mbefor, _ := time.ParseDuration("-1m")
  55. mafter, _ := time.ParseDuration("1m")
  56. nowBefor := time.Now().Add(mbefor)
  57. nowAfter := time.Now().Add(mafter)
  58. nowBeforSecond := nowBefor.Hour()*3600 + nowBefor.Minute()*60 + nowBefor.Second()
  59. nowAfterSecond := nowAfter.Hour()*3600 + nowAfter.Minute()*60 + nowAfter.Second()
  60. if noticeSecondTotal >= nowBeforSecond && noticeSecondTotal <= nowAfterSecond {
  61. //获取用户信息
  62. var openId string
  63. admin, err := models.GetAdminByAdminId(v.UserId)
  64. if err != nil {
  65. if err.Error() == utils.ErrNoRow() {
  66. err = errors.New("账号不存在")
  67. return err
  68. }
  69. return err
  70. }
  71. openId = admin.OpenId
  72. //发送消息
  73. if openId != "" {
  74. openIdList := make([]*models.OpenIdList, 0)
  75. openIdItem := new(models.OpenIdList)
  76. openIdItem.OpenId = openId
  77. openIdList = append(openIdList, openIdItem)
  78. first := "数据录入提醒"
  79. keyword1 := v.SecName + "该更新了"
  80. keyword2 := "每日 " + v.NoticeTime
  81. remark := v.SecName + "该更新了"
  82. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. }
  88. }
  89. return
  90. }
  91. // 周度消息推送
  92. func FrequencyByWeek() (err error) {
  93. defer func() {
  94. if err != nil {
  95. fmt.Println("FrequencyByWeek Err:", err.Error())
  96. utils.FileLog.Info("FrequencyByWeek err: %s", err.Error())
  97. }
  98. }()
  99. frequency := "周度"
  100. nowWeekDay := utils.GetWeekDay()
  101. fmt.Println("nowWeekDay:", nowWeekDay)
  102. items, err := models.GetEdbInfoByFrequencyWeek(frequency, nowWeekDay)
  103. if err != nil {
  104. fmt.Println("GetFrequencyByDay err:", err.Error())
  105. return
  106. }
  107. if len(items) <= 0 {
  108. err = errors.New("没有需要推送的数据,FrequencyByWeek")
  109. return
  110. }
  111. nowDate := time.Now().Format(utils.FormatDate)
  112. for _, v := range items {
  113. //判断指标当天数据是否已录入
  114. dataCount, err := models.GetEdbdataCount(v.TradeCode, nowDate)
  115. if err != nil {
  116. return err
  117. }
  118. if dataCount > 0 {
  119. utils.FileLog.Info("%s", v.TradeCode+" :当天数据已录入,无需提醒")
  120. continue
  121. }
  122. count, err := models.GetEdbinfoSendMsgCount(v.UserId, v.TradeCode)
  123. if err != nil {
  124. fmt.Println("GetEdbinfoSendMsgCount Err:" + err.Error())
  125. return err
  126. }
  127. if count > 0 {
  128. fmt.Println(v.UserId, v.TradeCode, "当天已发送")
  129. continue
  130. }
  131. noticeTime := v.NoticeTime
  132. if noticeTime == "" {
  133. err = errors.New("NoticeTime 参数错误:NoticeTime is empty ")
  134. return err
  135. }
  136. weekArr := strings.Split(noticeTime, " ")
  137. if len(weekArr) < 2 {
  138. err = errors.New("NoticeTime 参数错误: " + noticeTime)
  139. return err
  140. }
  141. weekVal := weekArr[0]
  142. weekVal = strings.Trim(weekVal, " ")
  143. if weekVal == "" {
  144. err = errors.New("weekVal 参数错误: weekVal:" + weekVal)
  145. return err
  146. }
  147. fmt.Println("weekVal:", weekVal, " nowWeek:", nowWeekDay)
  148. if weekVal != nowWeekDay {
  149. err = errors.New("不是当前日期,不需要发送消息: weekVal:" + weekVal + "; nowWeek:" + nowWeekDay)
  150. return err
  151. }
  152. noticeStr := weekArr[1]
  153. if noticeStr == "" {
  154. err = errors.New("noticeStr 参数错误: noticeStr:" + noticeStr)
  155. return err
  156. }
  157. noticeTimeArr := strings.Split(noticeStr, ":")
  158. if len(noticeTimeArr) < 3 {
  159. err = errors.New("NoticeTime 参数错误,noticeTime:" + noticeTime)
  160. return err
  161. }
  162. noticeHour, err := strconv.Atoi(noticeTimeArr[0])
  163. if err != nil {
  164. return err
  165. }
  166. noticeMinute, err := strconv.Atoi(noticeTimeArr[1])
  167. if err != nil {
  168. return err
  169. }
  170. noticeSecond, err := strconv.Atoi(noticeTimeArr[2])
  171. if err != nil {
  172. return err
  173. }
  174. noticeSecondTotal := noticeHour*3600 + noticeMinute*60 + noticeSecond
  175. mbefor, _ := time.ParseDuration("-1m")
  176. mafter, _ := time.ParseDuration("1m")
  177. nowBefor := time.Now().Add(mbefor)
  178. nowAfter := time.Now().Add(mafter)
  179. nowBeforSecond := nowBefor.Hour()*3600 + nowBefor.Minute()*60 + nowBefor.Second()
  180. nowAfterSecond := nowAfter.Hour()*3600 + nowAfter.Minute()*60 + nowAfter.Second()
  181. if noticeSecondTotal >= nowBeforSecond && noticeSecondTotal <= nowAfterSecond {
  182. //获取用户信息
  183. var openId string
  184. admin, err := models.GetAdminByAdminId(v.UserId)
  185. if err != nil {
  186. if err.Error() == utils.ErrNoRow() {
  187. err = errors.New("账号不存在")
  188. return err
  189. }
  190. return err
  191. }
  192. openId = admin.OpenId
  193. //发送消息
  194. if openId != "" {
  195. openIdList := make([]*models.OpenIdList, 0)
  196. openIdItem := new(models.OpenIdList)
  197. openIdItem.OpenId = openId
  198. openIdList = append(openIdList, openIdItem)
  199. first := "数据录入提醒"
  200. keyword1 := v.SecName + "该更新了"
  201. keyword2 := "每周 " + v.NoticeTime
  202. remark := v.SecName + "该更新了"
  203. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  204. if err != nil {
  205. return err
  206. }
  207. //发送成功,记录发送日志
  208. {
  209. sendRecord := new(models.EdbinfoSendMsgRecord)
  210. sendRecord.UserId = v.UserId
  211. sendRecord.TradeCode = v.TradeCode
  212. sendRecord.CreateTime = time.Now()
  213. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. }
  219. } else {
  220. utils.FileLog.Info("%s", v.TradeCode+":不在推送消息时间范围内")
  221. }
  222. }
  223. return
  224. }
  225. // 周度消息推送
  226. func FrequencyByMonth() (err error) {
  227. defer func() {
  228. if err != nil {
  229. fmt.Println("FrequencyByMonth Err:", err.Error())
  230. utils.FileLog.Info("FrequencyByMonth err: %s", err.Error())
  231. }
  232. }()
  233. frequency := "月度"
  234. items, err := models.GetEdbInfoByFrequency(frequency)
  235. if err != nil {
  236. fmt.Println("GetFrequencyByDay err:", err.Error())
  237. return
  238. }
  239. if len(items) <= 0 {
  240. err = errors.New("没有需要推送的数据")
  241. return
  242. }
  243. for _, v := range items {
  244. noticeTime := v.NoticeTime
  245. if noticeTime == "" {
  246. err = errors.New("NoticeTime 参数错误:NoticeTime is empty ")
  247. return
  248. }
  249. monthArr := strings.Split(noticeTime, " ")
  250. if len(monthArr) < 2 {
  251. err = errors.New("NoticeTime 参数错误: " + noticeTime)
  252. return
  253. }
  254. monthStr := monthArr[0]
  255. if monthStr == "" {
  256. err = errors.New("NoticeTime 参数错误: NoticeTime:" + noticeTime + ";monthStr:" + monthStr)
  257. return
  258. }
  259. monthVal := strings.Replace(monthStr, "日", "", -1)
  260. monthVal = strings.Trim(monthVal, " ")
  261. if monthVal == "" {
  262. err = errors.New("weekVal 参数错误: monthVal:" + monthVal)
  263. return
  264. }
  265. monthValInt, err := strconv.Atoi(monthVal)
  266. if err != nil {
  267. return err
  268. }
  269. nowDay := time.Now().Day()
  270. if monthValInt != nowDay {
  271. err = errors.New("不是当前日期,不需要发送消息: monthVal:" + monthVal + "; nowWeek:" + strconv.Itoa(nowDay))
  272. return err
  273. }
  274. noticeStr := monthArr[1]
  275. if noticeStr == "" {
  276. err = errors.New("noticeStr 参数错误: noticeStr:" + noticeStr)
  277. return err
  278. }
  279. noticeTimeArr := strings.Split(noticeStr, ":")
  280. if len(noticeTimeArr) < 3 {
  281. err = errors.New("NoticeTime 参数错误,noticeTime:" + noticeTime)
  282. return err
  283. }
  284. noticeHour, err := strconv.Atoi(noticeTimeArr[0])
  285. if err != nil {
  286. return err
  287. }
  288. noticeMinute, err := strconv.Atoi(noticeTimeArr[1])
  289. if err != nil {
  290. return err
  291. }
  292. noticeSecond, err := strconv.Atoi(noticeTimeArr[2])
  293. if err != nil {
  294. return err
  295. }
  296. noticeSecondTotal := noticeHour*3600 + noticeMinute*60 + noticeSecond
  297. mbefor, _ := time.ParseDuration("-1m")
  298. mafter, _ := time.ParseDuration("1m")
  299. nowBefor := time.Now().Add(mbefor)
  300. nowAfter := time.Now().Add(mafter)
  301. nowBeforSecond := nowBefor.Hour()*3600 + nowBefor.Minute()*60 + nowBefor.Second()
  302. nowAfterSecond := nowAfter.Hour()*3600 + nowAfter.Minute()*60 + nowAfter.Second()
  303. if noticeSecondTotal >= nowBeforSecond && noticeSecondTotal <= nowAfterSecond {
  304. //获取用户信息
  305. var openId string
  306. admin, err := models.GetAdminByAdminId(v.UserId)
  307. if err != nil {
  308. if err.Error() == utils.ErrNoRow() {
  309. err = errors.New("账号不存在")
  310. return err
  311. }
  312. return err
  313. }
  314. openId = admin.OpenId
  315. //发送消息
  316. if openId != "" {
  317. openIdList := make([]*models.OpenIdList, 0)
  318. openIdItem := new(models.OpenIdList)
  319. openIdItem.OpenId = openId
  320. openIdList = append(openIdList, openIdItem)
  321. first := "数据录入提醒"
  322. keyword1 := v.SecName + "该更新了"
  323. keyword2 := "每月 " + v.NoticeTime
  324. remark := v.SecName + "该更新了"
  325. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  326. if err != nil {
  327. return err
  328. }
  329. }
  330. }
  331. }
  332. return
  333. }