staff_assess_dao_2.go 8.2 KB
package dao

import (
	"fmt"

	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)

type DataStaffAssessContent2 struct {
	CycleId             string                       `pg:"cycle_id"`              //周期id
	CycleName           string                       `pg:"cycle_name"`            //周期名称
	EvaluationProjectId string                       `pg:"evaluation_project_id"` //
	TargetUserId        string                       `pg:"target_user_id"`        //被评估人的id
	TargetUserName      string                       `pg:"target_user_name"`      //被评估人的名称
	BeginDay            string                       `pg:"begin_day"`             //评估的日期
	Value               string                       `pg:"value"`                 //评估填写的值
	Category            string                       `pg:"category"`              //评估项分类
	ContentName         string                       `pg:"content_name"`          //评估项名称
	Weight              float64                      `pg:"weight"`                //权重
	PromptText          string                       `pg:"prompt_text"`           //评估标准
	Remark              []domain.AssessContemtRemark `pg:"remark"`
}

// SearchStaffAssessContent2 用于员工绩效-综合管理-导出绩效-个人
// 获取所有评估的填写内容
// companyId 公司id
// cycleId 周期id
// operaterId 操作人、查看人的id
// hrbp 操作人、查看人是否hrbp 1:是;-1:否
func (d *StaffAssessDao) SearchStaffAssessContent2(companyId int, cycleId int, operaterId int, hrbp int, exportUserIds []string) (
	[]DataStaffAssessContent2, error) {
	withSql := d.catchProjectIdByPermission(companyId, cycleId, operaterId, hrbp)
	sqlStr := ` select 
	staff_assess.target_user ->>'userId' as target_user_id,
	staff_assess.evaluation_project_id,
	staff_assess.target_user ->>'userName' as target_user_name,
	to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
	staff_assess.cycle_id,
	staff_assess.cycle_name,
	staff_assess_content.value ,
	staff_assess_content.category ,
	staff_assess_content."name" as content_name ,
	staff_assess_content.weight,
	staff_assess_content.prompt_text,
	staff_assess_content.remark 
from staff_assess
join staff_assess_content  on staff_assess.id = staff_assess_content.staff_assess_id 
where 1=1 
and staff_assess.cycle_id =? and "types" ='self'
and staff_assess.evaluation_project_id in (
	select t_project_4.project_id from t_project_4
) `

	condition := []interface{}{cycleId}
	if len(exportUserIds) > 0 {
		sqlStr += ` and staff_assess.target_user->>'userId' in(?) `
		condition = append(condition, pg.In(exportUserIds))
	}

	sqlStr = withSql + sqlStr + ` order by staff_assess.begin_time `
	result := []DataStaffAssessContent2{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

type DataEvaluationItemUsed2 struct {
	CycleId             string  `pg:"cycle_id"`              //周期id
	CycleName           string  `pg:"cycle_name"`            //周期名称
	EvaluationProjectId string  `pg:"evaluation_project_id"` //评估的项目
	TargetUserId        string  `pg:"target_user_id"`        //被评估人的id
	TargetUserName      string  `pg:"target_user_name"`      //被评估人的名称
	BeginDay            string  `pg:"begin_day"`             //评估的日期
	Category            string  `pg:"category"`              //评估项分类
	ContentName         string  `pg:"content_name"`          //评估项名称
	Weight              float64 `pg:"weight"`                //权重
	PromptText          string  `pg:"prompt_text"`           //评估标准
}

// SearchEvaluationItemUsed2 用于员工绩效-综合管理-导出绩效-个人
// 获取所有评估的填写项
// companyId  公司id
// cycleId 周期id
// operaterId 操作人、查看人的id
// hrbp 操作人、查看人是否hrbp 1:是;-1:否
func (d *StaffAssessDao) SearchEvaluationItemUsed2(companyId int, cycleId int, operaterId int, hrbp int, exportUserIds []string) (
	[]DataEvaluationItemUsed2, error) {
	withSql := d.catchProjectIdByPermission(companyId, cycleId, operaterId, hrbp)
	sqlStr := `select 
	staff_assess.target_user ->>'userId' as target_user_id,
	staff_assess.evaluation_project_id ,
	staff_assess.target_user ->>'userName' as target_user_name,
	to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
	staff_assess.cycle_id,
	staff_assess.cycle_name,
	evaluation_item_used.category ,
	evaluation_item_used."name" as content_name ,
	evaluation_item_used.weight,
	evaluation_item_used.prompt_text 
from staff_assess
join evaluation_item_used  on evaluation_item_used.evaluation_project_id  = staff_assess.evaluation_project_id 
where 1=1 
and staff_assess.cycle_id =? and "types" ='self'
and staff_assess.evaluation_project_id in (
	select t_project_4.project_id  from t_project_4 
) `
	condition := []interface{}{cycleId}
	if len(exportUserIds) > 0 {
		sqlStr += ` and staff_assess.target_user->>'userId' in(?) `
		condition = append(condition, pg.In(exportUserIds))
	}
	sqlStr = withSql + sqlStr
	result := []DataEvaluationItemUsed2{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// sql 语句片段
// 按照人员可查看权限的过滤
// companyId int 公司id (必填)
// cycleId int, 评估周期id (必填)
// userId int, 用户id,谁要查看数据 (必填)
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否 (必填)
func (d *StaffAssessDao) catchProjectIdByPermission(companyId int, cycleId int, operaterId int, hrbp int) string {
	withSql := `
	with 
	t_project_0 as(
		select evaluation_project.id as project_id,
		jsonb_array_elements_text(evaluation_project.recipients) as target_user_id,
		evaluation_project.pmp,
		evaluation_project.pmp_ids,
		evaluation_project.hr_bp
		from evaluation_project
		where evaluation_project.cycle_id =%d 
		and evaluation_project.deleted_at isnull
	),
	-- 查我的下级员工
	t_user_1 as (
		select  "user".id::text  as user_id  from "user" 
		where "user".parent_id =%d and company_id=%d
	),
	-- 如果是HRBP
	t_project_1 as(
		select t_project_0.project_id,t_project_0.target_user_id
		from t_project_0 
		where t_project_0.hr_bp =%d
	),
	-- 如果的项目管理员
	t_project_2 as(
		select t_project_0.project_id,t_project_0.target_user_id
		from t_project_0 
		where t_project_0.pmp =1
		and t_project_0.pmp_ids @>'["%d"]'
	),
	-- 如果是上级员工
	t_project_3 as (
		select t_project_0.project_id,t_project_0.target_user_id
		from t_project_0 
		join t_user_1 on t_user_1.user_id = t_project_0.target_user_id
	),
	-- 合并数据
	t_project_4 as (
		select t_project_2.project_id,t_project_2.target_user_id from t_project_2
		union 
		select t_project_1.project_id,t_project_1.target_user_id from t_project_1
		union
		select t_project_3.project_id,t_project_3.target_user_id from t_project_3
	)`
	params := []interface{}{cycleId, operaterId, companyId, hrbp, operaterId}

	return fmt.Sprintf(withSql, params...)
}

type TargetUserCycleProject struct {
	CycleId              string   `pg:"cycle_id" json:"cycleId"`     //周期id
	CycleName            string   `pg:"cycle_name" json:"cycleName"` //周期名称
	EvaluationProjectIds []string `pg:"evaluation_project_ids,array" json:"evaluationProjectIds"`
}

// 获取目标员工的自评周期和项目
func (d *StaffAssessDao) SearchTargetUserCycleProject(companyId int, targetUserId int, limit int, offset int) ([]TargetUserCycleProject, error) {
	sqlStr := ` with t_staff_assess as ( 
		select distinct staff_assess.cycle_id ,
		staff_assess.cycle_name,
		staff_assess.evaluation_project_id 
		from staff_assess 
		where 1=1
		and staff_assess."types" ='self'
		and staff_assess.deleted_at isnull 
		and staff_assess.target_user ->>'userId'='?'
		and staff_assess.company_id=?
	)
	select 
	t_staff_assess.cycle_id,
	t_staff_assess.cycle_name,
	array_agg(to_char(t_staff_assess.evaluation_project_id,'9999999999999999999')) as evaluation_project_ids
	from  t_staff_assess
	group by cycle_id,cycle_name 
	limit ? offset ?
	`
	condition := []interface{}{targetUserId, companyId, limit, offset}
	result := []TargetUserCycleProject{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}