135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package newbond
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.lxtend.com/lixiangwuxian/qqbot/action"
|
|
"git.lxtend.com/lixiangwuxian/qqbot/model"
|
|
"git.lxtend.com/lixiangwuxian/qqbot/sqlite3"
|
|
"git.lxtend.com/lixiangwuxian/qqbot/util"
|
|
)
|
|
|
|
func init() {
|
|
// 使用GORM自动迁移替代手写SQL
|
|
sqlite3.AutoMigrate(&NewBondListenGroup{}, &BondData{})
|
|
go RoundCheckNewBond()
|
|
}
|
|
|
|
func GetBondsData() ([]BondData, error) {
|
|
url := "https://datacenter-web.eastmoney.com/api/data/v1/get?sortColumns=PUBLIC_START_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1&pageSize=50&pageNumber=1&reportName=RPT_BOND_CB_LIST&columns=ALL"eColumns=f2~01~CONVERT_STOCK_CODE~CONVERT_STOCK_PRICE%2Cf235~10~SECURITY_CODE~TRANSFER_PRICE%2Cf236~10~SECURITY_CODE~TRANSFER_VALUE%2Cf2~10~SECURITY_CODE~CURRENT_BOND_PRICE%2Cf237~10~SECURITY_CODE~TRANSFER_PREMIUM_RATIO%2Cf239~10~SECURITY_CODE~RESALE_TRIG_PRICE%2Cf240~10~SECURITY_CODE~REDEEM_TRIG_PRICE%2Cf23~01~CONVERT_STOCK_CODE~PBV_RATIO"eType=0&source=WEB&client=WEB"
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("status code %d", resp.StatusCode)
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var apiResponse BondResponse
|
|
err = json.Unmarshal(body, &apiResponse)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return apiResponse.Result.Data, nil
|
|
}
|
|
|
|
func AddGroupListen(groupID int) error {
|
|
db := sqlite3.GetGormDB()
|
|
listenGroup := NewBondListenGroup{GroupID: groupID}
|
|
result := db.Create(&listenGroup)
|
|
return result.Error
|
|
}
|
|
|
|
func RemoveGroupListen(groupID int) error {
|
|
db := sqlite3.GetGormDB()
|
|
result := db.Where("group_id = ?", groupID).Delete(&NewBondListenGroup{})
|
|
return result.Error
|
|
}
|
|
|
|
func BondDataExists(securityCode string) (bool, error) {
|
|
db := sqlite3.GetGormDB()
|
|
var count int64
|
|
result := db.Model(&BondData{}).Where("SecurityCode = ?", securityCode).Count(&count)
|
|
if result.Error != nil {
|
|
return false, result.Error
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func AddBondData(data BondData) error {
|
|
db := sqlite3.GetGormDB()
|
|
result := db.Create(&data)
|
|
return result.Error
|
|
}
|
|
|
|
func GetGroupListens() ([]int, error) {
|
|
db := sqlite3.GetGormDB()
|
|
var listenGroups []NewBondListenGroup
|
|
result := db.Find(&listenGroups)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
|
|
var groupIDs []int
|
|
for _, group := range listenGroups {
|
|
groupIDs = append(groupIDs, group.GroupID)
|
|
}
|
|
return groupIDs, nil
|
|
}
|
|
|
|
func RoundCheckNewBond() {
|
|
time.Sleep(5 * time.Minute)
|
|
for !action.ActionManager.Started() {
|
|
time.Sleep(5 * time.Minute)
|
|
}
|
|
// once := true
|
|
util.AddCycleTask("checkNewBond", 5*time.Minute, 5*time.Minute, func() {
|
|
bonds, err := GetBondsData()
|
|
if bonds == nil || err != nil {
|
|
log.Printf("Error getting bonds data: %v\n", err)
|
|
return
|
|
}
|
|
groups, err := GetGroupListens()
|
|
if err != nil {
|
|
log.Printf("Error getting group listens: %v\n", err)
|
|
return
|
|
}
|
|
for _, bond := range bonds {
|
|
exists, err := BondDataExists(bond.SecurityCode)
|
|
if err != nil {
|
|
log.Printf("Error checking bond data exists: %v\n", err)
|
|
return
|
|
}
|
|
if !exists {
|
|
for _, group := range groups {
|
|
msg := model.Reply{
|
|
ReplyMsg: fmt.Sprintf("号外号外,%s开始申购了", bond.SecurityNameAbbr),
|
|
ReferOriginMsg: false,
|
|
FromMsg: model.Message{
|
|
GroupInfo: model.GroupInfo{
|
|
GroupId: int64(group),
|
|
},
|
|
},
|
|
}
|
|
action.ActionManager.SendMsg(&msg)
|
|
}
|
|
}
|
|
AddBondData(bond)
|
|
}
|
|
})
|
|
}
|