作者 tangxvhui

初始创建功能, 推送数据到字库

package pushdata
type FieldName struct {
Name string `json:"name"`
}
type RespData struct {
Code int `json:"code"`
}
func (resp *RespData) IsOk() bool {
return resp.Code == 0
}
type ReqCreateTable struct {
Name string `json:"name"`
Fields []FieldName `json:"fields"`
Data []map[string]string `json:"data"`
}
type ReqAppendData struct {
Name string `json:"name"`
Fields []FieldName `json:"fields"`
Data []map[string]string `json:"data"`
}
type AppTable interface {
AppTableName() string // 应用表名称
AppTableField() []FieldName // 应用字段
}
type GenData interface {
DataForAppend() (ReqAppendData, error) //生成需要追加的数据
}
type AppTableFile interface {
AppTable
GenData
}
type Client struct {
entry []AppTableFile
host string
}
// RegistEntry 注册数据推送项
func (c *Client) RegistEntry(item AppTableFile) {
c.entry = append(c.entry, item)
}
// CreateTable 创建应用表
func (c *Client) CreateTable() {
}
// AppendData 追加应用表数据
func (c *Client) AppendData() {
}
... ...
package pushdata
import (
"github.com/go-pg/pg/v10"
pgdb "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg"
)
func getDB() *pg.DB {
return pgdb.DB
}
... ...
package pushdata
import (
"time"
)
type StaffAssessItem struct {
TargetUser string `pg:"target_user"` //评估的目标人员
TargetUserAccount string `pg:"target_user_account"` //目标人员账号
ItemCategory string `pg:"item_category"` //分类
ItemName string `pg:"item_name"` //名称
ItemValue string `pg:"item_value"` //评分填写的值
ItemWeight string `pg:"item_weight"` //评分权重
BeginDay string `pg:"begin_day"` //日期
ProjectName string `pg:"project_name"` //项目名称
ProjectId string `pg:"project_id"` //项目id
CycleId string `pg:"cycle_id"` //周期id
CycleName string `pg:"cycle_name"` //周期名称
}
func (t *StaffAssessItem) AppTableName() string {
return "每日绩效评估"
}
func (t *StaffAssessItem) AppTableField() []FieldName {
return []FieldName{
{Name: "用户姓名"},
{Name: "手机号"},
{Name: "分类"},
{Name: "名称"},
{Name: "评分"},
{Name: "权重"},
{Name: "评估日期"},
{Name: "项目ID"},
{Name: "项目名称"},
{Name: "周期ID"},
{Name: "周期"},
}
}
func (t *StaffAssessItem) MapData() (data map[string]string) {
data = map[string]string{
"用户姓名": t.TargetUser,
"手机号": t.TargetUserAccount,
"分类": t.ItemCategory,
"名称": t.ItemName,
"评分": t.ItemValue,
"权重": t.ItemWeight,
"评估日期": t.BeginDay,
"项目ID": t.ProjectId,
"项目名称": t.ProjectName,
"周期ID": t.CycleId,
"周期名称": t.CycleName,
}
return
}
func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
sqlStr := `select
staff_assess.target_user ->>'account' as target_user_account,
staff_assess.target_user ->>'userName' as target_user,
to_char(staff_assess.begin_time at time zone 'PRC','YYYY-MM-DD') as begin_day,
staff_assess.evaluation_project_id as project_id,
staff_assess.evaluation_project_name as project_name,
staff_assess.cycle_id ,
staff_assess.cycle_name ,
staff_assess_content.category item_category ,
staff_assess_content.value as item_value,
staff_assess_content."name" as item_name,
staff_assess_content.weight as item_weight
from staff_assess
join staff_assess_content on staff_assess.id =staff_assess_content.staff_assess_id
where 1=1
and staff_assess.end_time between to_timestamp(?) and to_timestamp(?) `
nowTime := time.Now()
yesterday := nowTime.AddDate(0, 0, -1)
y, m, d := yesterday.Local().Date()
yesterdayZero := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
yesterdayEnd := time.Date(y, m, d, 23, 59, 59, 0, time.Local)
db := getDB()
dataList := []StaffAssessItem{}
_, err = db.Query(&dataList, sqlStr, yesterdayZero.Unix(), yesterdayEnd.Unix())
if len(dataList) == 0 {
return
}
data = ReqAppendData{
Name: dataList[0].AppTableName(),
Fields: dataList[0].AppTableField(),
Data: make([]map[string]string, len(dataList)),
}
for i := range dataList {
data.Data = append(data.Data, dataList[i].MapData())
}
return
}
... ...