staff_assess.go 3.3 KB
package pushdata

import (
	"fmt"
	"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(nowTime time.Time) (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(?) `
	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, 0, len(dataList)),
	}
	for i := range dataList {
		data.Data = append(data.Data, dataList[i].MapData())
	}
	return
}

func SendDataStaffAssess(nowTime time.Time) error {
	c := NewClient()
	entry := StaffAssessItem{}
	data, err := entry.DataForAppend(nowTime)
	if err != nil {
		return fmt.Errorf("获取每日评估的数据 %s", err)
	}
	err = c.AppendData(data)
	return err
}