作者 tangxvhui

更新,推送每日评估数据到字库

@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "time" 4 "time"
5 5
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/pushdata"
7 8
8 "github.com/beego/beego/v2/server/web" 9 "github.com/beego/beego/v2/server/web"
9 serviceNodeTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task" 10 serviceNodeTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
@@ -23,6 +24,7 @@ func main() { @@ -23,6 +24,7 @@ func main() {
23 go notify.RunTaskSmsNotify() 24 go notify.RunTaskSmsNotify()
24 //go notify.AppMessageRun() 25 //go notify.AppMessageRun()
25 go consumer.Run() 26 go consumer.Run()
  27 + pushdata.PushData()
26 web.Run() 28 web.Run()
27 } 29 }
28 30
1 package pushdata 1 package pushdata
2 2
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "fmt"
  7 + "net/http"
  8 + "time"
  9 +
  10 + "github.com/dgrijalva/jwt-go"
  11 +)
  12 +
3 type FieldName struct { 13 type FieldName struct {
4 Name string `json:"name"` 14 Name string `json:"name"`
5 } 15 }
6 16
7 type RespData struct { 17 type RespData struct {
8 - Code int `json:"code"` 18 + Code int `json:"code"`
  19 + Msg string `json:"msg"`
9 } 20 }
10 21
11 func (resp *RespData) IsOk() bool { 22 func (resp *RespData) IsOk() bool {
12 return resp.Code == 0 23 return resp.Code == 0
13 } 24 }
14 25
15 -type ReqCreateTable struct {  
16 - Name string `json:"name"`  
17 - Fields []FieldName `json:"fields"`  
18 - Data []map[string]string `json:"data"` 26 +type Client struct {
  27 + Host string
  28 + AppSecret string // 调试用 3Oo4dG64X0
  29 + AppKey string // 调试用 GnAmG4jybB
19 } 30 }
20 31
21 -type ReqAppendData struct {  
22 - Name string `json:"name"`  
23 - Fields []FieldName `json:"fields"`  
24 - Data []map[string]string `json:"data"` 32 +func NewClient() *Client {
  33 + return &Client{
  34 + Host: "http://character-library-metadata-bastion-test.fjmaimaimai.com",
  35 + AppSecret: "3Oo4dG64X0",
  36 + AppKey: "GnAmG4jybB",
  37 + }
25 } 38 }
26 39
27 -type AppTable interface {  
28 - AppTableName() string // 应用表名称  
29 - AppTableField() []FieldName // 应用字段 40 +func (c *Client) useHeader() (h http.Header) {
  41 + token := jwt.New(jwt.SigningMethodHS256)
  42 + tk, err := token.SignedString([]byte(c.AppSecret))
  43 + if err != nil {
  44 + fmt.Printf("err:%s \n", err)
  45 + }
  46 + h = http.Header{}
  47 + h.Add("x-mmm-accesstoken", tk)
  48 + h.Add("x-mmm-appkey", c.AppKey)
  49 + return
30 } 50 }
31 51
32 -type GenData interface {  
33 - DataForAppend() (ReqAppendData, error) //生成需要追加的数据 52 +type ReqAppendData struct {
  53 + Name string `json:"name"` // 应用表名称
  54 + Fields []FieldName `json:"fields"` // 应用字段
  55 + Data []map[string]string `json:"data"` // 数据
34 } 56 }
35 57
36 -type AppTableFile interface {  
37 - AppTable  
38 - GenData 58 +// AppendData 追加应用表数据
  59 +func (c *Client) AppendData(reqData ReqAppendData) error {
  60 + apiUrl := c.Host + `/api/app-table-file/append-data`
  61 + bodyData, err := json.Marshal(reqData)
  62 + if err != nil {
  63 + return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
  64 + }
  65 + req, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewReader(bodyData))
  66 + if err != nil {
  67 + return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
  68 + }
  69 + req.Header = c.useHeader()
  70 + req.Header.Add("Content-Type", "application/json")
  71 + httpClient := http.DefaultClient
  72 + httpClient.Timeout = 60 * time.Second
  73 + resp, err := httpClient.Do(req)
  74 + if err != nil {
  75 + return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
  76 + }
  77 + defer resp.Body.Close()
  78 + var respData RespData
  79 + j := json.NewDecoder(resp.Body)
  80 + err = j.Decode(&respData)
  81 + if err != nil {
  82 + return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
  83 + }
  84 + if !respData.IsOk() {
  85 + return fmt.Errorf("推送数据到字库,追加应用表数据响应:%s", respData.Msg)
  86 + }
  87 + return nil
39 } 88 }
40 89
41 -type Client struct {  
42 - entry []AppTableFile  
43 - host string 90 +type ReqSearchTable struct {
  91 + Name string `json:"name"`
44 } 92 }
45 93
46 -// RegistEntry 注册数据推送项  
47 -func (c *Client) RegistEntry(item AppTableFile) {  
48 - c.entry = append(c.entry, item) 94 +// SearchTable 查询应用数据表
  95 +func (c *Client) SearchTable(name string) {
  96 + apiUrl := `/api/app-table-file/list`
  97 + _ = apiUrl
49 } 98 }
50 99
51 -// CreateTable 创建应用表  
52 -func (c *Client) CreateTable() {  
53 - 100 +type ReqCreateTable struct {
  101 + Name string `json:"name"` // 应用表名称
  102 + Fields []FieldName `json:"fields"` // 应用字段
  103 + Data []map[string]string `json:"data"`
54 } 104 }
55 105
56 -// AppendData 追加应用表数据  
57 -func (c *Client) AppendData() {  
58 - 106 +// CreateTable 创建应用表
  107 +func (c *Client) CreateTable() {
  108 + apiUrl := `/api/app-table-file/create`
  109 + _ = apiUrl
59 } 110 }
  1 +package pushdata
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "testing"
  6 + "time"
  7 +)
  8 +
  9 +func TestToken(t *testing.T) {
  10 + c := NewClient()
  11 + h := c.useHeader()
  12 + t.Logf("%v", h)
  13 +}
  14 +
  15 +func TestField(t *testing.T) {
  16 + item := StaffAssessItem{}
  17 + fields := item.AppTableField()
  18 + bt, _ := json.Marshal(fields)
  19 + t.Log(string(bt))
  20 +}
  21 +
  22 +func TestPushData(t *testing.T) {
  23 + nowTime := time.Unix(1688572800, 0)
  24 + err := SendDataStaffAssess(nowTime)
  25 + if err != nil {
  26 + t.Error(err)
  27 + }
  28 +}
  1 +package pushdata
  2 +
  3 +import (
  4 + "time"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
  7 +)
  8 +
  9 +func PushData() {
  10 + nowTime := time.Now()
  11 + y, m, d := nowTime.Date()
  12 + t1 := time.Date(y, m, d, 10, 0, 0, 0, time.Local) //今天的10点
  13 + interval := t1.Sub(nowTime)
  14 + if interval < 0 {
  15 + interval = (24 * time.Hour) + interval
  16 + }
  17 + timer := time.NewTimer(interval)
  18 + for {
  19 + <-timer.C
  20 + nowTime = time.Now()
  21 + err := SendDataStaffAssess(nowTime)
  22 + if err != nil {
  23 + log.Logger.Error("发送每日评估任务")
  24 + }
  25 +
  26 + timer.Reset(24 * time.Hour)
  27 + }
  28 +}
1 package pushdata 1 package pushdata
2 2
3 import ( 3 import (
  4 + "fmt"
4 "time" 5 "time"
5 ) 6 )
6 7
@@ -19,7 +20,7 @@ type StaffAssessItem struct { @@ -19,7 +20,7 @@ type StaffAssessItem struct {
19 } 20 }
20 21
21 func (t *StaffAssessItem) AppTableName() string { 22 func (t *StaffAssessItem) AppTableName() string {
22 - return "每日绩效评估" 23 + return "人员绩效评估"
23 } 24 }
24 25
25 func (t *StaffAssessItem) AppTableField() []FieldName { 26 func (t *StaffAssessItem) AppTableField() []FieldName {
@@ -55,7 +56,7 @@ func (t *StaffAssessItem) MapData() (data map[string]string) { @@ -55,7 +56,7 @@ func (t *StaffAssessItem) MapData() (data map[string]string) {
55 return 56 return
56 } 57 }
57 58
58 -func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) { 59 +func (t *StaffAssessItem) DataForAppend(nowTime time.Time) (data ReqAppendData, err error) {
59 sqlStr := `select 60 sqlStr := `select
60 staff_assess.target_user ->>'account' as target_user_account, 61 staff_assess.target_user ->>'account' as target_user_account,
61 staff_assess.target_user ->>'userName' as target_user, 62 staff_assess.target_user ->>'userName' as target_user,
@@ -72,7 +73,6 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) { @@ -72,7 +73,6 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
72 join staff_assess_content on staff_assess.id =staff_assess_content.staff_assess_id 73 join staff_assess_content on staff_assess.id =staff_assess_content.staff_assess_id
73 where 1=1 74 where 1=1
74 and staff_assess.end_time between to_timestamp(?) and to_timestamp(?) ` 75 and staff_assess.end_time between to_timestamp(?) and to_timestamp(?) `
75 - nowTime := time.Now()  
76 yesterday := nowTime.AddDate(0, 0, -1) 76 yesterday := nowTime.AddDate(0, 0, -1)
77 y, m, d := yesterday.Local().Date() 77 y, m, d := yesterday.Local().Date()
78 yesterdayZero := time.Date(y, m, d, 0, 0, 0, 0, time.Local) 78 yesterdayZero := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
@@ -86,10 +86,21 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) { @@ -86,10 +86,21 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
86 data = ReqAppendData{ 86 data = ReqAppendData{
87 Name: dataList[0].AppTableName(), 87 Name: dataList[0].AppTableName(),
88 Fields: dataList[0].AppTableField(), 88 Fields: dataList[0].AppTableField(),
89 - Data: make([]map[string]string, len(dataList)), 89 + Data: make([]map[string]string, 0, len(dataList)),
90 } 90 }
91 for i := range dataList { 91 for i := range dataList {
92 data.Data = append(data.Data, dataList[i].MapData()) 92 data.Data = append(data.Data, dataList[i].MapData())
93 } 93 }
94 return 94 return
95 } 95 }
  96 +
  97 +func SendDataStaffAssess(nowTime time.Time) error {
  98 + c := NewClient()
  99 + entry := StaffAssessItem{}
  100 + data, err := entry.DataForAppend(nowTime)
  101 + if err != nil {
  102 + return fmt.Errorf("获取每日评估的数据 %s", err)
  103 + }
  104 + err = c.AppendData(data)
  105 + return err
  106 +}