审查视图

pkg/infrastructure/dao/staff_assess_dao.go 46.9 KB
郑周 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
package dao

import (
	"fmt"
	"strconv"
	"time"

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

type StaffAssessDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewStaffAssessDao(options map[string]interface{}) *StaffAssessDao {
	var transactionContext *pgTransaction.TransactionContext
	if value, ok := options["transactionContext"]; ok {
		transactionContext = value.(*pgTransaction.TransactionContext)
	}
	return &StaffAssessDao{
		transactionContext: transactionContext,
	}
}

type CountData struct {
	TargetUserId    int
	InviteTotal     int
	InviteCompleted int
}

// 获取员工邀请的人完成360评估的数量
func (d *StaffAssessDao) CountInviteAssessByTargetUser(userIds []int, assessTaskId int) ([]CountData, error) {
	sqlStr := `SELECT staff_assess.target_user->>'userId' as target_user_id,
 count(staff_assess."id") AS invite_total,
 sum(
	case WHEN staff_assess.status='completed' THEN 1
	ELSE 0 END
 ) as invite_completed
 FROM staff_assess 
 WHERE staff_assess.target_user->>'userId' IN(?)
 AND staff_assess.types IN ('invite_same_super','invite_diff_super')
 AND staff_assess.staff_assess_task_id = ?
 GROUP BY target_user_id`

	userIdList := []string{}
	for _, v := range userIds {
		uid := strconv.Itoa(v)
		userIdList = append(userIdList, uid)
	}
	condition := []interface{}{
		pg.In(userIdList), assessTaskId,
	}
	tx := d.transactionContext.PgTx
	result := []CountData{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

func (d *StaffAssessDao) CountTargetUserInviteAssess1(userIds []int, cycleId int, beginDay string) ([]CountData, error) {
	sqlStr := `SELECT 
	staff_assess.target_user->>'userId' as target_user_id,
	count(staff_assess."id") AS invite_total,
	sum(
	   case WHEN staff_assess.status='completed' THEN 1
	   ELSE 0 END
	) as invite_completed
	FROM staff_assess 
	WHERE staff_assess.target_user->>'userId' IN(?)
	AND staff_assess.types IN ('invite_same_super','invite_diff_super')
	and to_char(staff_assess.begin_time at time zone 'PRC','YYYY-MM-DD') = ?
	and staff_assess.cycle_id =?
	GROUP BY target_user_id`
	userIdStr := []string{}
	for _, v := range userIds {
		str := strconv.Itoa(v)
		userIdStr = append(userIdStr, str)
	}
	condition := []interface{}{
		pg.In(userIdStr), beginDay, cycleId,
	}
	tx := d.transactionContext.PgTx
	result := []CountData{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// 获取所以已经执行的评估周期
type AssessCycle struct {
	CycleId   string `json:"cycleId"` //周期id
	CompanyId string `json:"companyId"`
	CycleName string `json:"cycleName"` //周期名称
}

// 获取所以已经执行的评估周期
func (d *StaffAssessDao) AllAssessCycleList(companyId int) ([]AssessCycle, error) {
	sqlStr := `select  
	distinct 
	staff_assess_task.cycle_id ,
	staff_assess_task.company_id ,
	staff_assess_task.cycle_name 
103 104
	from staff_assess_task 
	where staff_assess_task.company_id = ?
郑周 authored
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	and staff_assess_task.deleted_at isnull
	order by staff_assess_task.cycle_id desc
	`

	tx := d.transactionContext.PgTx
	condition := []interface{}{
		companyId,
	}
	result := []AssessCycle{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// 获取评估周期中的绩效考核日期
type AssessCycleDay struct {
	BeginDay  string `json:"beginDay"`
	CycleId   int    `json:"cycleId"`
	CycleName string `json:"cycleName"`
	CompanyId string `json:"companyId"`
}

// 获取评估周期中的绩效考核日期
func (d *StaffAssessDao) AllAssessCycleDayList(companyId int, cycleId int) ([]AssessCycleDay, error) {
	sqlStr := `select distinct staff_assess_task.begin_day ,
	staff_assess_task.cycle_id ,
	staff_assess_task.cycle_name,
	staff_assess_task.company_id 
	from staff_assess_task 
	where staff_assess_task.cycle_id = ?
	and company_id =?
	and staff_assess_task.deleted_at isnull 
	order by staff_assess_task.begin_day desc `

	tx := d.transactionContext.PgTx
	condition := []interface{}{
		cycleId, companyId,
	}
	result := []AssessCycleDay{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// 获取员工填写评估内容
type UserAssessContent struct {
	AssessId            string                `json:"assessId"`
	ContentId           int                   `json:"contentId"`
	TargetUserId        string                `json:"targetUserId"`        //被评估人的id
	TargetUserName      string                `json:"targetUserName"`      //被评估人的名称
	BeginDay            string                `json:"beginDay"`            //评估的日期
	EvaluationProjectId string                `json:"evaluationProjectId"` //项目id
	Value               string                `json:"value"`               //评估填写的值
	LevelValue          string                `json:"levelValue"`          //评级的值
	Rule                domain.EvaluationRule `json:"rule"`                //规则
	SortBy              int                   `json:"sortBy"`              //评估项顺序
	Category            string                `json:"category"`            //评估项分类
	ContentName         string                `json:"contentName"`         //评估项名称
	Weight              float64               `json:"weight"`              //权重
	CycleId             string                `json:"cycleId"`             //周期id
}

type SearchConditin1 struct {
	CompanyId      int      //公司id
	CycleId        int      //周期id
	BeginDay       string   //评估的日期
	TargetUserName string   //被评估人的名称
	TargetUserId   []string //查询指定的人
	Limit          int      //分页
	Offset         int      //分页
	OperaterId     int      //用户的id是谁在搜索数据
	Hrbp           int      //
}

// 项目管理-成员列表 根据周期的id和日期获取员工填写的评估内容
// companyId int 公司id
// cycleId int, 评估周期id
// userId int, 用户id,谁要查看数据
// beginDay string, 周期中执行项目的时间
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否
// limit int, 分页条数
// offset int 分页偏移
func (d *StaffAssessDao) SearchUserAssessContent(param SearchConditin1) ([]UserAssessContent, error) {
	if param.Offset < 0 {
		param.Offset = 0
	}
	if param.Limit < 0 {
		param.Limit = 20
	}
	sqlStr := ` select 
	t_staff_assess_1.target_user_id,t_staff_assess_1.target_user_name,t_staff_assess_1.begin_day,
	t_staff_assess_1.assess_id,staff_assess_content.id as content_id,
	staff_assess_content.value ,staff_assess_content.sort_by ,t_staff_assess_1.cycle_id,
	staff_assess_content.category ,staff_assess_content."name" as content_name ,
	staff_assess_content.weight,staff_assess_content.level_value,staff_assess_content."rule" 
	from t_staff_assess_1
	left join staff_assess_content  on t_staff_assess_1.assess_id = staff_assess_content.staff_assess_id 
	where 1=1
	`
	condition := []interface{}{}
	if len(param.TargetUserName) > 0 {
		sqlStr += ` and t_staff_assess_1.target_user_name like ? `
		condition = append(condition, "%"+param.TargetUserName+"%")
	}
tangxvhui authored
207
	condition = append(condition, param.Limit, param.Offset)
郑周 authored
208
	//加入排序
tangxvhui authored
209
	sqlStr += ` order by convert_to(t_staff_assess_1.target_user_name,'GBK'),staff_assess_content.sort_by  limit ? offset ?`
郑周 authored
210
	//获取前置sql语句
tangxvhui authored
211
	sqlStr0 := d.useTStaffAssess(param.CompanyId, param.CycleId, param.OperaterId, param.BeginDay, param.Hrbp, 99999, 0, string(domain.AssessSelf))
郑周 authored
212 213 214 215 216 217 218
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	result := []UserAssessContent{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
type UserAssessContentItem struct {
	Category string `pg:"category"` //指标分类
	Name     string `pg:"name"`     //指标名称
}

// 项目管理-成员列表 根据周期的id和日期获取员工的评估指标
// companyId int 公司id
// cycleId int, 评估周期id
// userId int, 用户id,谁要查看数据
// beginDay string, 周期中执行项目的时间
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否
// limit int, 分页条数 不需要
// offset int 分页偏移 不需要
func (d *StaffAssessDao) SearchUserAssessContentItem(param SearchConditin1) ([]UserAssessContentItem, error) {
	param.Offset = 0
	param.Limit = 10000

	sqlStr := `select
	staff_assess_content.category,
	staff_assess_content."name" 
	from staff_assess_content
	join t_staff_assess_1 on staff_assess_content.staff_assess_id  = t_staff_assess_1.assess_id
	group by staff_assess_content.category,
	staff_assess_content."name" 
	`
	//获取前置sql语句
	sqlStr0 := d.useTStaffAssess(param.CompanyId, param.CycleId, param.OperaterId, param.BeginDay, param.Hrbp, param.Limit, param.Offset, string(domain.AssessSelf))
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	result := []UserAssessContentItem{}
	_, err := tx.Query(&result, sqlStr)
	return result, err
}
郑周 authored
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
// 项目管理-成员列表  根据周期的id和日期获取员工填写的评估内容,数量统计
// companyId int 公司id
// cycleId int, 评估周期id
// userId int, 用户id,谁要查看数据
// beginDay string, 周期中执行项目的时间
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否
// limit int, 分页条数
// offset int 分页偏移
func (d *StaffAssessDao) CountUserAssess(param SearchConditin1) (int, error) {
	sqlStr := ` select 
	count( distinct t_staff_assess_1.target_user_id) cnt
	from t_staff_assess_1
	where 1=1 `
	condition := []interface{}{}
	if len(param.TargetUserName) > 0 {
		sqlStr += ` and t_staff_assess_1.target_user_name like ? `
		condition = append(condition, "%"+param.TargetUserName+"%")
	}
	//获取前置sql语句
	sqlStr0 := d.useTStaffAssess(param.CompanyId, param.CycleId, param.OperaterId, param.BeginDay, param.Hrbp, 5000, 0, string(domain.AssessSelf))
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	var result int
	_, err := tx.QueryOne(pg.Scan(&result), sqlStr, condition...)
	return result, err
}

// 生成的sql 根据用户的查看权限 ,获取可查看的评估任务,
tangxvhui authored
281 282 283 284 285 286 287 288
// companyId int 公司id (必填)
// cycleId int, 评估周期id (必填)
// userId int, 用户id,谁要查看数据 (必填)
// beginDay string, 周期中执行项目的时间 (选填)
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否 (必填)
// limit int, 分页条数 (必填)
// offset int 分页偏移 (必填)
// assessType string 评估的类型 (选填)
郑周 authored
289 290 291
func (d *StaffAssessDao) useTStaffAssess(companyId int, cycleId int, userId int, beginDay string, hrbp int, limit int, offset int, assessType string) string {
	sqlstr := `
	set time zone 'PRC';
tangxvhui authored
292
tangxvhui authored
293
	with t_user_1 as (
tangxvhui authored
294
		select  "user".id::text  as user_id  from "user" where "user".parent_id =%d and "user".deleted_at  isnull 
郑周 authored
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	),
	-- 如果是hrbp
	t_project_1 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.hr_bp = %d
		and evaluation_project.deleted_at isnull
	),
	-- 如果的项目管理员
	t_project_2 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.pmp =1
		and evaluation_project.pmp_ids @>'["%d"]'
		and evaluation_project.deleted_at isnull
	),
	-- 合并数据
	t_project_3 as (
		select t_project_2.project_id from t_project_2
		union 
		select t_project_1.project_id from t_project_1
	),
	-- 初步过滤数据
	t_staff_assess_0 as (
		select staff_assess.id as assess_id,
		staff_assess.cycle_id,
tangxvhui authored
323
		staff_assess.cycle_name,
郑周 authored
324 325 326 327 328 329 330 331
		staff_assess.target_user->>'userId' as target_user_id,
		staff_assess.target_user->>'userName' as target_user_name,
		to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
		staff_assess.evaluation_project_id
		from staff_assess
		join staff_assess_task on staff_assess.staff_assess_task_id = staff_assess_task.id 
		and staff_assess_task.deleted_at isnull 
		where staff_assess.cycle_id = %d
tangxvhui authored
332 333
		%s   
		-- 根据条件拼接查询条件
郑周 authored
334 335 336 337 338 339 340
	),
	-- 根据查看权限过滤合并数据
	t_staff_assess_1 as (
		(select t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
tangxvhui authored
341
		t_staff_assess_0.cycle_name,
郑周 authored
342 343
		t_staff_assess_0.cycle_id,
		t_staff_assess_0.evaluation_project_id
郑周 authored
344 345 346 347 348 349
		from t_staff_assess_0
		join t_project_3  on t_staff_assess_0.evaluation_project_id = t_project_3.project_id
		) union (select t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
tangxvhui authored
350
		t_staff_assess_0.cycle_name,
郑周 authored
351 352
		t_staff_assess_0.cycle_id,
		t_staff_assess_0.evaluation_project_id
郑周 authored
353 354 355 356 357 358
		from t_staff_assess_0
		join t_user_1 on t_staff_assess_0.target_user_id=t_user_1.user_id
		)
		limit %d offset %d
	)
	`
tangxvhui authored
359 360 361 362 363
	//根据条件拼接查询条件
	staffAssessWhere := map[string]string{
		"beginDay":   ` and to_char(staff_assess.begin_time,'YYYY-MM-DD') like '%s' `,
		"assessType": ` and staff_assess."types" ='%s' `,
	}
郑周 authored
364
	params := []interface{}{
tangxvhui authored
365
		userId, cycleId, hrbp, cycleId, userId, cycleId,
郑周 authored
366
	}
Your Name authored
367
	extWhere := ""
tangxvhui authored
368 369
	if len(beginDay) > 0 {
		str := fmt.Sprintf(staffAssessWhere["beginDay"]+"\n", beginDay)
Your Name authored
370
		extWhere += str
tangxvhui authored
371 372 373 374
	}

	if len(assessType) > 0 {
		str := fmt.Sprintf(staffAssessWhere["assessType"]+"\n", assessType)
Your Name authored
375
		extWhere += str
tangxvhui authored
376
	}
Your Name authored
377
	params = append(params, extWhere, limit, offset)
郑周 authored
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	sqlstr = fmt.Sprintf(sqlstr, params...)
	return sqlstr
}

type SummaryAssess struct {
	AssessId            string                   `json:"assessId"`
	TargetUserId        string                   `json:"targetUserId"`        // 被评估人的id
	TargetUserName      string                   `json:"targetUserName"`      // 被评估人的名称
	BeginDay            string                   `json:"beginDay"`            // 评估的开始日期
	EndTime             time.Time                `json:"endTime"`             // 评估的截止日期
	EvaluationProjectId string                   `json:"evaluationProjectId"` // 项目id
	CycleId             string                   `json:"cycleId"`             // 周期id
	Types               domain.StaffAssessType   `json:"types"`               // 评估类型
	Status              domain.StaffAssessStatus `json:"status"`              // 评估状态
}

func (d *StaffAssessDao) SummaryAssess(companyId int, operatorId int, cycleId int, beginDay string, hrbp int) ([]SummaryAssess, error) {
395 396 397 398 399 400 401 402 403 404 405 406 407 408
	//`
	//with t_user_department as (
	//	select "user".id as user_id ,jsonb_array_elements_text ("user".department_id)  as depart_id from "user"
	//	where "user".company_id= %d and "user".deleted_at  isnull
	//),
	//t_department as (
	//	select  department.id::text  as depart_id  from  department where charge_user_ids @>'[%d]'
	//	and "department".deleted_at  isnull
	//),
	//-- 部门主管(所有下级用户ID)
	//t_user_1 as (
	//	select t_user_department.user_id::text from t_user_department
	//	join t_department on t_user_department.depart_id = t_department.depart_id
	//),`
郑周 authored
409 410
	sqlString := `
	set time zone 'PRC';
411 412
	with t_user_1 as (
		select  "user".id::text  as user_id  from "user" where "user".parent_id =%d
郑周 authored
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	),
	-- 如果是HRBP
	t_project_1 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.hr_bp = %d
		and evaluation_project.deleted_at isnull
	),
	-- 如果的项目管理员
	t_project_2 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.pmp =1
		and evaluation_project.pmp_ids @>'["%d"]'
		and evaluation_project.deleted_at isnull
	),
	-- 合并数据
	t_project_3 as (
		select t_project_2.project_id from t_project_2
		union 
		select t_project_1.project_id from t_project_1
	),
	-- 初步过滤数据
	t_staff_assess_0 as (
		select 
        staff_assess.id as assess_id,
		staff_assess.cycle_id,
		staff_assess.target_user->>'userId' as target_user_id,
		staff_assess.target_user->>'userName' as target_user_name,
		to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
		staff_assess.evaluation_project_id,
        staff_assess.types,
        staff_assess.status,
        staff_assess.end_time
		from staff_assess
		join staff_assess_task on staff_assess.staff_assess_task_id = staff_assess_task.id 
		and staff_assess_task.deleted_at isnull 
		where staff_assess.cycle_id = %d 
		and to_char(staff_assess.begin_time,'YYYY-MM-DD')='%s'
	),
	-- 根据查看权限过滤合并数据
	t_staff_assess_1 as (
		( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id,
        t_staff_assess_0.evaluation_project_id,
        t_staff_assess_0.types,
        t_staff_assess_0.status,
        t_staff_assess_0.end_time
		from t_staff_assess_0 
		join t_project_3 on t_staff_assess_0.evaluation_project_id = t_project_3.project_id
        ) union 
        ( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id,
        t_staff_assess_0.evaluation_project_id,
        t_staff_assess_0.types,
        t_staff_assess_0.status,
        t_staff_assess_0.end_time
		from t_staff_assess_0 
		join t_user_1 on t_staff_assess_0.target_user_id = t_user_1.user_id
		)
	)
	`
485 486
	params := []interface{}{operatorId, cycleId, hrbp, cycleId, operatorId, cycleId, beginDay}
	//params := []interface{}{companyId, operatorId, cycleId, hrbp, cycleId, operatorId, cycleId, beginDay}
郑周 authored
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

	sqlString = fmt.Sprintf(sqlString, params...)

	sqlString = sqlString + ` select 
	t_staff_assess_1.target_user_id,
    t_staff_assess_1.target_user_name,
    t_staff_assess_1.begin_day,
    t_staff_assess_1.cycle_id,
	t_staff_assess_1.assess_id,
	t_staff_assess_1.evaluation_project_id,
	t_staff_assess_1.types,
	t_staff_assess_1.status,
    t_staff_assess_1.end_time
	from t_staff_assess_1
	where 1=1 
	`

	tx := d.transactionContext.PgTx
	var result = make([]SummaryAssess, 0)
	_, err := tx.Query(&result, sqlString)
	return result, err
}
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
type MemberSummaryAssess struct {
	TargetUserId    string `json:"targetUserId"`    // 被评估人的id
	TargetUserName  string `json:"targetUserName"`  // 被评估人名称
	SelfCompleted   int    `json:"selfCompleted"`   // 自评完成数量
	SupperCompleted int    `json:"supperCompleted"` // 上级评价完成数量
	InviteCompleted int    `json:"inviteCompleted"` // 邀请完成数量
	InviteTotal     int    `json:"inviteTotal"`     // 邀请总数
}

func (d *StaffAssessDao) MemberSummaryList(likeUserName string, companyId int, operatorId int, cycleId int, hrbp int, pageNumber int, pageSize int) (int, []MemberSummaryAssess, error) {
	limit := pageSize
	if limit < 0 {
		limit = 20
	}
	offset := limit * (pageNumber - 1)
	if offset < 0 {
		offset = 0
	}
	sqlString := `
	set time zone 'PRC';
530 531
	with t_user_1 as (
		select  "user".id::text  as user_id  from "user" where "user".parent_id =%d
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	),
	-- 如果是HRBP
	t_project_1 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.hr_bp = %d
		and evaluation_project.deleted_at isnull
	),
	-- 如果的项目管理员
	t_project_2 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.pmp =1
		and evaluation_project.pmp_ids @>'["%d"]'
		and evaluation_project.deleted_at isnull
	),
	-- 合并数据
	t_project_3 as (
		select t_project_2.project_id from t_project_2
		union 
		select t_project_1.project_id from t_project_1
	),
	-- 初步过滤数据
	t_staff_assess_0 as (
		select 
        staff_assess.id as assess_id,
		staff_assess.cycle_id,
		staff_assess.target_user->>'userId' as target_user_id,
		staff_assess.target_user->>'userName' as target_user_name,
		staff_assess.evaluation_project_id,
        staff_assess.types,
        staff_assess.status
		from staff_assess
		join staff_assess_task on staff_assess.staff_assess_task_id = staff_assess_task.id 
		and staff_assess_task.deleted_at isnull 
		where staff_assess.cycle_id = %d 
	),
	-- 根据查看权限过滤合并数据
	merge_assess as (
		( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.cycle_id,
        t_staff_assess_0.evaluation_project_id,
        t_staff_assess_0.types,
        t_staff_assess_0.status
		from t_staff_assess_0 
		join t_project_3 on t_staff_assess_0.evaluation_project_id = t_project_3.project_id
        ) union 
        ( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.cycle_id,
        t_staff_assess_0.evaluation_project_id,
        t_staff_assess_0.types,
        t_staff_assess_0.status
		from t_staff_assess_0 
		join t_user_1 on t_staff_assess_0.target_user_id = t_user_1.user_id
		)
	)
	`
597
	params := []interface{}{operatorId, cycleId, hrbp, cycleId, operatorId, cycleId}
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	// 筛选结果的所有评估数据
	sqlString = fmt.Sprintf(sqlString, params...)

	// 根据用户ID进行分组
	groupString := ` 
    SELECT 
	    merge_assess.target_user_id,
		merge_assess.target_user_name,
		SUM ( CASE WHEN merge_assess.types = 'self' AND merge_assess.status = 'completed' THEN 1 ELSE 0 END ) AS self_completed,
		SUM ( CASE WHEN merge_assess.types = 'supper' AND merge_assess.status = 'completed' THEN 1 ELSE 0 END ) AS supper_completed,
		SUM ( CASE WHEN ( merge_assess.types = 'invite_diff_super' OR merge_assess.types = 'invite_same_super' ) AND merge_assess.status = 'completed' THEN 1 ELSE 0 END ) AS invite_completed,
		SUM ( CASE WHEN merge_assess.types = 'invite_diff_super' OR merge_assess.types = 'invite_same_super' THEN 1 ELSE 0 END ) AS invite_total
    `
	whereFrom := `
    FROM 
	merge_assess 
	WHERE 1=1 
    `
	if len(likeUserName) > 0 {
		whereFrom += fmt.Sprintf(" AND merge_assess.target_user_name LIKE '%s'", "%"+likeUserName+"%")
	}
	groupBy := ` 
    GROUP BY 
    merge_assess.target_user_id,
    merge_assess.target_user_name
	ORDER BY 
    convert_to( merge_assess.target_user_name, 'GBK' )
    `
	groupString += whereFrom
	groupString += groupBy
	groupString = fmt.Sprintf("%s limit %d offset %d", groupString, limit, offset)

	// 查询所有目标用户数量
	countSql := ` SELECT COUNT ( DISTINCT merge_assess.target_user_id ) ` + whereFrom

	tx := d.transactionContext.PgTx
	var result = make([]MemberSummaryAssess, 0)
	_, err := tx.Query(&result, sqlString+groupString)
	if err != nil {
		return 0, result, err
	}

	var total int // 获取总数量
	_, err = tx.QueryOne(pg.Scan(&total), sqlString+countSql)
	if err != nil {
		return 0, result, err
	}

	return total, result, err
}
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
type PerformanceIndicatorAssess struct {
	AssessId        int    `json:"assessId"`        // ID
	TargetUserId    string `json:"targetUserId"`    // 被评估人的id
	TargetUserName  string `json:"targetUserName"`  // 被评估人的名称
	BeginDay        string `json:"beginDay"`        // 评估的开始日期
	CycleId         int    `json:"cycleId"`         // 周期ID
	ContentId       int    `json:"contentId"`       // 评估内容ID
	ContentCategory string `json:"contentCategory"` // 评估内容分类
	ContentName     string `json:"contentName"`     // 评估内容名称
	SortBy          int    `json:"sort_by"`         // 评估内容排序
}

func (d *StaffAssessDao) MemberPerformanceIndicator(likeUserName string, companyId int, operatorId int, cycleId int, hrbp int, assessType string, userIds []string) ([]PerformanceIndicatorAssess, error) {
	sqlString := `
	set time zone 'PRC';
664 665
	with t_user_1 as (
		select  "user".id::text  as user_id  from "user" where "user".parent_id =%d
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	),
	-- 如果是HRBP
	t_project_1 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.hr_bp = %d
		and evaluation_project.deleted_at isnull
	),
	-- 如果的项目管理员
	t_project_2 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.pmp =1
		and evaluation_project.pmp_ids @>'["%d"]'
		and evaluation_project.deleted_at isnull
	),
	-- 合并数据
	t_project_3 as (
		select t_project_2.project_id from t_project_2
		union 
		select t_project_1.project_id from t_project_1
	),
	-- 初步过滤数据
	t_staff_assess_0 as (
		select 
        staff_assess.id as assess_id,
		staff_assess.cycle_id,
		staff_assess.target_user->>'userId' as target_user_id,
		staff_assess.target_user->>'userName' as target_user_name,
		to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
		staff_assess.evaluation_project_id
		from staff_assess
		join staff_assess_task on staff_assess.staff_assess_task_id = staff_assess_task.id 
		and staff_assess_task.deleted_at isnull 
		where 
        staff_assess.cycle_id = %d 
		and staff_assess.types ='%s'
	),
	-- 根据查看权限过滤合并数据
	t_staff_assess_1 as (
		( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id
		from t_staff_assess_0 
		join t_project_3 on t_staff_assess_0.evaluation_project_id = t_project_3.project_id
        ) union  ( 
        select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id
		from t_staff_assess_0 
		join t_user_1 on t_staff_assess_0.target_user_id = t_user_1.user_id
		)
	)
	`
728
	params := []interface{}{operatorId, cycleId, hrbp, cycleId, operatorId, cycleId, assessType}
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	sqlString = fmt.Sprintf(sqlString, params...)

	sqlString += ` select 
	t_staff_assess_1.target_user_id,
    t_staff_assess_1.target_user_name,
    t_staff_assess_1.begin_day,
    t_staff_assess_1.cycle_id,
	t_staff_assess_1.assess_id,
	staff_assess_content.id as content_id,
	staff_assess_content.name as content_name,
	staff_assess_content.category as content_category,
    staff_assess_content.sort_by
	from t_staff_assess_1
	left join staff_assess_content on t_staff_assess_1.assess_id = staff_assess_content.staff_assess_id
	where 1=1 
    -- AND staff_assess_content.id NOTNULL 部分脏数据
	`
	condition := make([]interface{}, 0)
	if len(likeUserName) > 0 {
		sqlString += ` and t_staff_assess_1.target_user_name like ? `
		condition = append(condition, "%"+likeUserName+"%")
	}
	if len(userIds) > 0 {
		sqlString += ` and t_staff_assess_1.target_user_id in (?) `
		condition = append(condition, pg.In(userIds))
	}
	sqlString += ` order by convert_to(t_staff_assess_1.target_user_name,'GBK'), staff_assess_content.sort_by`

	tx := d.transactionContext.PgTx
	var result = make([]PerformanceIndicatorAssess, 0)
	_, err := tx.Query(&result, sqlString, condition...)

	return result, err

}
郑周 authored
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
type IndicatorUserProject struct {
	AssessId            int    `json:"assessId"`            // ID
	TargetUserId        int    `json:"targetUserId"`        // 目标用户ID
	TargetUserName      string `json:"targetUserName"`      // 目标用户名称
	EvaluationProjectId int    `json:"evaluationProjectId"` // 项目ID

}

func (d *StaffAssessDao) MemberAllProjectId(companyId int, likeUserName string, operatorId int, cycleId int, hrbp int) ([]IndicatorUserProject, error) {
	sqlStr := ` select
	t_staff_assess_1.evaluation_project_id,
    t_staff_assess_1.target_user_id,
    t_staff_assess_1.target_user_name,
    t_staff_assess_1.assess_id
	from t_staff_assess_1
    where 1=1 
	`
	var condition []interface{}
	if len(likeUserName) > 0 {
		sqlStr += ` and t_staff_assess_1.target_user_name like ? `
		condition = append(condition, "%"+likeUserName+"%")
	}
	// 获取前置sql语句
郑周 authored
788
	sqlStr0 := d.useTStaffAssess(companyId, cycleId, operatorId, "", hrbp, 5000, 0, string(domain.AssessSelf))
郑周 authored
789 790 791 792 793 794 795 796
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	var result []IndicatorUserProject
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err

}
郑周 authored
797 798 799 800 801 802 803 804 805 806 807 808 809
type ExportPerformanceIndicator struct {
	AssessId        int                          `json:"assessId"`        // ID
	TargetUserId    string                       `json:"targetUserId"`    // 被评估人的id
	TargetUserName  string                       `json:"targetUserName"`  // 被评估人的名称
	BeginDay        string                       `json:"beginDay"`        // 评估的开始日期
	CycleId         int                          `json:"cycleId"`         // 周期ID
	ContentId       int                          `json:"contentId"`       // 评估内容ID
	ContentCategory string                       `json:"contentCategory"` // 评估内容分类
	ContentName     string                       `json:"contentName"`     // 评估内容名称
	SortBy          int                          `json:"sort_by"`         // 评估内容排序
	Remark          []domain.AssessContemtRemark `json:"remark"`          // 评估内容填写反馈内容
}
810 811 812 813 814 815 816 817
func (d *StaffAssessDao) ExportPerformanceIndicator(
	companyId int,
	operatorId int,
	cycleId int,
	beginDays []string,
	hrbp int,
	assessType string,
	userIds []string) ([]ExportPerformanceIndicator, error) {
郑周 authored
818 819
	sqlString := `
	set time zone 'PRC';
820 821
	with t_user_1 as (
		select  "user".id::text  as user_id  from "user" where "user".parent_id =%d
郑周 authored
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	),
	-- 如果是HRBP
	t_project_1 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.hr_bp = %d
		and evaluation_project.deleted_at isnull
	),
	-- 如果的项目管理员
	t_project_2 as(
		select evaluation_project.id as project_id
		from evaluation_project 
		where evaluation_project.cycle_id =%d
		and evaluation_project.pmp =1
		and evaluation_project.pmp_ids @>'["%d"]'
		and evaluation_project.deleted_at isnull
	),
	-- 合并数据
	t_project_3 as (
		select t_project_2.project_id from t_project_2
		union 
		select t_project_1.project_id from t_project_1
	),
	-- 初步过滤数据
	t_staff_assess_0 as (
		select 
        staff_assess.id as assess_id,
		staff_assess.cycle_id,
		staff_assess.target_user->>'userId' as target_user_id,
		staff_assess.target_user->>'userName' as target_user_name,
		to_char(staff_assess.begin_time,'YYYY-MM-DD') as begin_day,
		staff_assess.evaluation_project_id
		from staff_assess
		join staff_assess_task on staff_assess.staff_assess_task_id = staff_assess_task.id 
		and staff_assess_task.deleted_at isnull 
		where 
        staff_assess.cycle_id = %d 
		and staff_assess.types ='%s'
	),
	-- 根据查看权限过滤合并数据
	t_staff_assess_1 as (
		( select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id
		from t_staff_assess_0 
		join t_project_3 on t_staff_assess_0.evaluation_project_id = t_project_3.project_id
        ) union  ( 
        select 
        t_staff_assess_0.assess_id,
		t_staff_assess_0.target_user_id,
		t_staff_assess_0.target_user_name,
		t_staff_assess_0.begin_day,
		t_staff_assess_0.cycle_id
		from t_staff_assess_0 
		join t_user_1 on t_staff_assess_0.target_user_id = t_user_1.user_id
		)
	)
	`
884
	params := []interface{}{operatorId, cycleId, hrbp, cycleId, operatorId, cycleId, assessType}
郑周 authored
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
	sqlString = fmt.Sprintf(sqlString, params...)

	sqlString += ` select 
	t_staff_assess_1.target_user_id,
    t_staff_assess_1.target_user_name,
    t_staff_assess_1.begin_day,
    t_staff_assess_1.cycle_id,
	t_staff_assess_1.assess_id,
	staff_assess_content.id as content_id,
	staff_assess_content.name as content_name,
	staff_assess_content.category as content_category,
	staff_assess_content.remark,
    staff_assess_content.sort_by
	from t_staff_assess_1
	left join staff_assess_content on t_staff_assess_1.assess_id = staff_assess_content.staff_assess_id
	where 1=1 
    -- AND staff_assess_content.id NOTNULL 部分脏数据
	`
	condition := make([]interface{}, 0)
	//if len(likeUserName) > 0 {
	//	sqlString += ` and t_staff_assess_1.target_user_name like ? `
	//	condition = append(condition, "%"+likeUserName+"%")
	//}
	if len(userIds) > 0 {
		sqlString += ` and t_staff_assess_1.target_user_id in (?) `
		condition = append(condition, pg.In(userIds))
	}
912 913 914 915 916
	if len(beginDays) > 0 {
		sqlString += ` and t_staff_assess_1.begin_day in (?) `
		condition = append(condition, pg.In(beginDays))
	}
郑周 authored
917 918 919 920 921 922 923 924 925 926 927
	//sqlString += ` order by convert_to(t_staff_assess_1.target_user_name,'GBK'), staff_assess_content.sort_by`
	sqlString += ` order by t_staff_assess_1.begin_day`

	tx := d.transactionContext.PgTx
	var result = make([]ExportPerformanceIndicator, 0)
	_, err := tx.Query(&result, sqlString, condition...)

	return result, err

}
郑周 authored
928 929 930
type ExportData1 struct {
	AssessId            string
	ContentId           int
tangxvhui authored
931 932
	CycleId             string  //周期id
	CycleName           string  //周期名称
郑周 authored
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	TargetUserId        string  //被评估人的id
	TargetUserName      string  //被评估人的名称
	BeginDay            string  //评估的日期
	EvaluationProjectId string  //项目id
	Value               string  //评估填写的值
	SortBy              int     //评估项顺序
	Category            string  //评估项分类
	ContentName         string  //评估项名称
	Weight              float64 //权重
	PromptText          string  //评估标准
	Remark              []domain.AssessContemtRemark
}

// 项目管理-成员列表  导出数据
func (d *StaffAssessDao) ExportDataUserAssess(param SearchConditin1) ([]ExportData1, error) {
	if param.Offset < 0 {
		param.Offset = 0
	}
	if param.Limit < 0 {
		param.Limit = 5000
	}
	sqlStr := ` select 
tangxvhui authored
955 956 957 958 959 960 961 962 963 964 965 966 967 968
	t_staff_assess_1.target_user_id,
	t_staff_assess_1.target_user_name,
	t_staff_assess_1.begin_day,
	t_staff_assess_1.assess_id,
	t_staff_assess_1.cycle_id,
	t_staff_assess_1.cycle_name,
	staff_assess_content.id as content_id,
	staff_assess_content.value ,
	staff_assess_content.sort_by ,
	staff_assess_content.category ,
	staff_assess_content."name" as content_name ,
	staff_assess_content.weight,
	staff_assess_content.prompt_text ,
	staff_assess_content.remark 
郑周 authored
969 970 971 972 973 974 975 976 977 978 979 980 981 982
	from t_staff_assess_1
	left join staff_assess_content  on t_staff_assess_1.assess_id = staff_assess_content.staff_assess_id 
	where 1=1 
	`
	condition := []interface{}{}
	if len(param.TargetUserName) > 0 {
		sqlStr += ` and t_staff_assess_1.target_user_name like ? `
		condition = append(condition, "%"+param.TargetUserName+"%")
	}
	if len(param.TargetUserId) > 0 {
		sqlStr += ` and t_staff_assess_1.target_user_id in (?) `
		condition = append(condition, pg.In(param.TargetUserId))
	}
	//加入排序
983
	sqlStr += ` order by convert_to(t_staff_assess_1.target_user_name,'GBK'),t_staff_assess_1.begin_day,staff_assess_content.sort_by `
郑周 authored
984 985 986 987 988 989 990 991 992
	//获取前置sql语句
	sqlStr0 := d.useTStaffAssess(param.CompanyId, param.CycleId, param.OperaterId, param.BeginDay, param.Hrbp, param.Limit, param.Offset, string(domain.AssessSelf))
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	var result []ExportData1
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}
993 994 995 996 997 998
type AssessCycleDayExecutor struct {
	BeginDay  string `pg:"begin_day"`
	CycleId   string `pg:"cycle_id"`
	CycleName string `pg:"cycle_name"`
	EndTime   string `pg:"end_time"`
	BeginTime string `pg:"begin_time"`
郑周 authored
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
}

// 根据评估的人执行人id,统计executorId参与的评估周期
func (d *StaffAssessDao) CountAssessCycleMe(executorId int, companyId int) (int, error) {
	sqlStr := `
	select count(DISTINCT (staff_assess_task.cycle_id,staff_assess_task.begin_day )) as cnt
	FROM staff_assess_task 
	JOIN staff_assess ON staff_assess_task."id" = staff_assess."staff_assess_task_id"
	WHERE staff_assess.company_id=?
	and staff_assess_task.deleted_at isnull
	and staff_assess.executor->>'userId'='?'
	`
	tx := d.transactionContext.PgTx
	condition := []interface{}{
		companyId, executorId,
	}
	var result int
	_, err := tx.QueryOne(pg.Scan(&result), sqlStr, condition...)
	return result, err
}
tangxvhui authored
1019 1020 1021

// 评估的指标
type ContentCategoryName struct {
tangxvhui authored
1022 1023 1024 1025 1026 1027 1028 1029 1030
	Category       string  `pg:"category"`         //指标分类
	Name           string  `pg:"name"`             //指标名称
	Weight         float64 `pg:"weight"`           //指标权重
	CycleId        string  `pg:"cycle_id"`         //周期id
	CycleName      string  `pg:"cycle_name"`       //周期名称
	SortBy         int     `pg:"sort_by"`          //
	TargetUserId   string  `pg:"target_user_id"`   //评估的目标员工id
	TargetUserName string  `pg:"target_user_name"` //评估的目标员工名称
	//Cnt            int     `pg:"cnt"`              //排序
tangxvhui authored
1031 1032 1033 1034
}

// 员工绩效-综合管理-导出绩效指标
// 抽取出评估的指标
tangxvhui authored
1035
func (d *StaffAssessDao) SearchContentCategoryName(companyId int, cycleId int, userId int, hrbp int, exportUserIds []string) ([]ContentCategoryName, error) {
tangxvhui authored
1036 1037 1038 1039 1040
	sqlStr := `
	select
		staff_assess_content.category,
		staff_assess_content."name" ,
		staff_assess_content.weight ,
1041
		staff_assess_content.sort_by ,
1042 1043
		t_staff_assess_1.cycle_id ,
		t_staff_assess_1.cycle_name,
tangxvhui authored
1044
		t_staff_assess_1.target_user_id,
1045
		t_staff_assess_1.target_user_name
tangxvhui authored
1046
	from staff_assess_content
1047
	join t_staff_assess_1 on staff_assess_content.staff_assess_id  = t_staff_assess_1.assess_id
tangxvhui authored
1048 1049 1050 1051 1052 1053 1054
	where 1=1
	`
	condition := []interface{}{}
	if len(exportUserIds) > 0 {
		condition = append(condition, pg.In(exportUserIds))
		sqlStr += ` and t_staff_assess_1.target_user_id in(?) `
	}
1055 1056 1057
	sqlStr += ` group by category,"name" ,cycle_id ,cycle_name,weight ,
	target_user_id,target_user_name,sort_by 
	order by target_user_id,sort_by `
tangxvhui authored
1058
	sqlStr0 := d.useTStaffAssess(companyId, cycleId, userId, "", hrbp, 5000, 0, string(domain.AssessSelf))
tangxvhui authored
1059 1060 1061
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
	result := []ContentCategoryName{}
tangxvhui authored
1062
	_, err := tx.Query(&result, sqlStr, condition...)
tangxvhui authored
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	return result, err
}

type ExportData2 struct {
	CycleId        string                       `pg:"cycle_id"`         //周期id
	CycleName      string                       `pg:"cycle_name"`       //周期名称
	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"`
}
郑周 authored
1080
// 员工绩效-综合管理-导出绩效-个人
tangxvhui authored
1081 1082 1083 1084
// companyId int 公司id
// cycleId int, 评估周期id
// userId int, 用户id,谁要查看数据
// hrbp 是否搜索HRBP角色的用户可以查看,1:是;-1:否
tangxvhui authored
1085 1086
// exportUserIds  只导出选中的员工的数据
func (d *StaffAssessDao) ExportDataUserAssess2(companyId int, cycleId int, operaterId int, hrbp int, exportUserIds []string) ([]*ExportData2, error) {
tangxvhui authored
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	sqlStr := ` select 
	t_staff_assess_1.target_user_id,
	t_staff_assess_1.target_user_name,
	t_staff_assess_1.begin_day,
	t_staff_assess_1.cycle_id,
	t_staff_assess_1.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 t_staff_assess_1
	left join staff_assess_content  on t_staff_assess_1.assess_id = staff_assess_content.staff_assess_id 
	where 1=1 
	`
	condition := []interface{}{}
tangxvhui authored
1104 1105 1106 1107
	if len(exportUserIds) > 0 {
		condition = append(condition, pg.In(exportUserIds))
		sqlStr += ` and t_staff_assess_1.target_user_id in(?) `
	}
tangxvhui authored
1108 1109 1110 1111 1112 1113
	//加入排序
	sqlStr += ` order by t_staff_assess_1.begin_day`
	//获取前置sql语句
	sqlStr0 := d.useTStaffAssess(companyId, cycleId, operaterId, "", hrbp, 5000, 0, string(domain.AssessSelf))
	sqlStr = sqlStr0 + sqlStr
	tx := d.transactionContext.PgTx
Your Name authored
1114
	result := []*ExportData2{}
tangxvhui authored
1115 1116 1117
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}
1118 1119

type ContentValue struct {
tangxvhui authored
1120
	StaffAssessId int    `pg:"staff_assess_id"`
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	Value         string `pg:"value"`
}

func (d *StaffAssessDao) SearchContentValueByAssessId(assessId []int) map[int][]string {
	if len(assessId) == 0 {
		return map[int][]string{}
	}
	sqlStr := `select 
	staff_assess_content.staff_assess_id ,
	staff_assess_content.value 
	from staff_assess_content 
	where staff_assess_content.staff_assess_id in (?)`
	tx := d.transactionContext.PgTx
	result := []ContentValue{}
	condition := []interface{}{pg.In(assessId)}
	_, err := tx.Query(&result, sqlStr, condition...)
	if err != nil {
		return map[int][]string{}
	}
	valueMap := map[int][]string{}
	for _, v := range result {
		if _, ok := valueMap[v.StaffAssessId]; !ok {
			valueMap[v.StaffAssessId] = []string{}
		}
		if len(v.Value) > 0 {
			valueMap[v.StaffAssessId] = append(valueMap[v.StaffAssessId], v.Value)
		}
	}

	return valueMap
}
tangxvhui authored
1152 1153

type AssessContentLevelCode struct {
tangxvhui authored
1154 1155 1156 1157
	Cnt        int    `pg:"cnt"`         //数量
	LevelValue string `pg:"level_value"` //评估的等级值
	Category   string `pg:"category"`    //评估的分类
	Name       string `pg:"name"`        //名称
tangxvhui authored
1158 1159
}
tangxvhui authored
1160
func (d *StaffAssessDao) CountAssessContentLevelCode(projectId int, targetUserId int, assessType domain.StaffAssessType, cycleId int) ([]AssessContentLevelCode, error) {
tangxvhui authored
1161 1162 1163 1164 1165 1166 1167 1168
	sqlStr := `
select  
count(staff_assess_content.level_value) as cnt ,
staff_assess_content.level_value ,
staff_assess_content.category ,
staff_assess_content."name" 
from staff_assess_content  
join staff_assess on staff_assess_content.staff_assess_id  = staff_assess.id 
tangxvhui authored
1169 1170
where 1=1 
and staff_assess.deleted_at isnull 
tangxvhui authored
1171
and target_user ->>'userId'='?'
tangxvhui authored
1172
and staff_assess."types" = ?
tangxvhui authored
1173 1174
and staff_assess_content.level_value notnull 
and staff_assess.cycle_id =?
tangxvhui authored
1175
and staff_assess.evaluation_project_id=?
tangxvhui authored
1176 1177 1178 1179
group by level_value,category,"name" `

	var result []AssessContentLevelCode
	condition := []interface{}{
tangxvhui authored
1180
		targetUserId, string(assessType), cycleId, projectId,
tangxvhui authored
1181 1182 1183 1184 1185
	}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206

type CountUncompletedSelfAssess struct {
	EvaluationProjectId int
	TargetUserId        int
	Cnt                 int
}

// CountUncompletedSelfAssess 统计整个项目周期内,相关人自评未完成数量
func (d *StaffAssessDao) CountUncompletedSelfAssess(companyId int, projectIds []int) ([]CountUncompletedSelfAssess, error) {
	sqlStr := `
    SELECT
	    staff_assess.evaluation_project_id,
		staff_assess.target_user ->> 'userId' AS target_user_id,
		COUNT ( staff_assess.ID ) AS cnt
	FROM
	staff_assess
	WHERE
	staff_assess.company_id = ?
	AND staff_assess.types = 'self'
	AND staff_assess.evaluation_project_id IN (?)
	AND staff_assess.status = 'uncompleted'
1207
	AND staff_assess.end_time < now()
1208 1209 1210 1211 1212 1213 1214 1215 1216
	GROUP BY
	evaluation_project_id, target_user_id`

	condition := []interface{}{companyId, pg.In(projectIds)}
	tx := d.transactionContext.PgTx
	result := make([]CountUncompletedSelfAssess, 0)
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}
tangxvhui authored
1217
1218 1219
// SearchExecutorAssesBeforeNow
func (d *StaffAssessDao) SearchExecutorAssessBeforeNow(executorId int, companyId int, limit int, offset int) ([]AssessCycleDayExecutor, int, error) {
tangxvhui authored
1220
	sqlStr1 := `select  
1221 1222 1223 1224 1225 1226 1227
	distinct on(cycle_id ,begin_day)
	staff_assess.cycle_id ,
	staff_assess.cycle_name ,
	to_char(staff_assess.begin_time  at time zone 'PRC','YYYY-MM-DD') as begin_day,
	to_char(staff_assess.end_time  at time zone 'PRC','YYYY-MM-DD HH24:MI:SS') as end_time,
	to_char(staff_assess.begin_time  at time zone 'PRC','YYYY-MM-DD HH24:MI:SS') as begin_time 
	from staff_assess 
tangxvhui authored
1228
	join staff_assess_task  on staff_assess.staff_assess_task_id = staff_assess_task.id 
1229 1230
	WHERE staff_assess.company_id=?
	and staff_assess.deleted_at isnull 
tangxvhui authored
1231
	and staff_assess_task.deleted_at isnull 
1232
	and (
tangxvhui authored
1233
	   staff_assess.end_time < now() 
1234
	)
1235
	and staff_assess.executor ->> 'userId'='?'
1236 1237 1238
	order by begin_day desc 
	limit ? offset ?`
tangxvhui authored
1239
	condition1 := []interface{}{
1240 1241 1242
		companyId, executorId, limit, offset,
	}
1243 1244 1245 1246 1247
	sqlStr2 := `select  
	count( 
	 distinct (staff_assess.cycle_id ,to_char(staff_assess.begin_time at time zone 'PRC','YYYY-MM-DD'))
	) as cnt
	from staff_assess 
tangxvhui authored
1248
	join staff_assess_task  on staff_assess.staff_assess_task_id = staff_assess_task.id 
1249 1250 1251
	WHERE staff_assess.company_id=?
	and staff_assess.deleted_at isnull 
	and (
tangxvhui authored
1252
	staff_assess.end_time < now() 
1253
	)
tangxvhui authored
1254
	and staff_assess_task.deleted_at isnull 
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	and staff_assess.executor ->> 'userId'='?'
	`
	condition2 := []interface{}{
		companyId, executorId,
	}
	tx := d.transactionContext.PgTx
	result := []AssessCycleDayExecutor{}
	_, err := tx.Query(&result, sqlStr1, condition1...)
	if err != nil {
		return nil, 0, err
	}
	var cnt int
	_, err = tx.QueryOne(pg.Scan(&cnt), sqlStr2, condition2...)
	if err != nil {
		return nil, 0, err
	}
	return result, cnt, nil
1272 1273

}
tangxvhui authored
1274
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
// SearchExecutorAssessAfterNow SearchExecutorAssesAfterNow
func (d *StaffAssessDao) SearchExecutorAssessAfterNow(executorId int, companyId int) ([]AssessCycleDayExecutor, error) {
	sqlStr := `select  
	distinct on(cycle_id ,begin_day)
	staff_assess.cycle_id ,
	staff_assess.cycle_name ,
	to_char(staff_assess.begin_time  at time zone 'PRC','YYYY-MM-DD') as begin_day,
	to_char(staff_assess.end_time  at time zone 'PRC','YYYY-MM-DD HH24:MI:SS') as end_time,
	to_char(staff_assess.begin_time  at time zone 'PRC','YYYY-MM-DD HH24:MI:SS') as begin_time 
	from staff_assess 
tangxvhui authored
1285
	join staff_assess_task  on staff_assess.staff_assess_task_id = staff_assess_task.id 
1286 1287 1288
	WHERE staff_assess.company_id=?
	and staff_assess.deleted_at isnull 
	and (
tangxvhui authored
1289
	   staff_assess.end_time >= now() 
1290
	)
tangxvhui authored
1291
	and staff_assess_task.deleted_at isnull 
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	and staff_assess.executor ->> 'userId'='?'
	order by begin_day`
	condition1 := []interface{}{
		companyId, executorId,
	}
	tx := d.transactionContext.PgTx
	result := []AssessCycleDayExecutor{}
	_, err := tx.Query(&result, sqlStr, condition1...)
	return result, err
}
Your Name authored
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

type AssessSelfContentRemark struct {
	Category   string                       `pg:"category"` //评估的分类
	Name       string                       `pg:"name"`     //名称
	Remark     []domain.AssessContemtRemark `pg:"remark"`
	BeginDay   string                       `pg:"begin_day"` //开始的天数
	LevelValue string                       `pg:"level_value"`
}

func (d *StaffAssessDao) SearchAssessSelfContentRemark(projectId int, targetUserId int, category string, name string, levalvalue string) ([]AssessSelfContentRemark, error) {
	sqlStr := ` select  
	staff_assess_content.level_value ,
	staff_assess_content.category ,
	staff_assess_content."name" ,
	staff_assess_content.remark ,
	to_char(staff_assess.begin_time at time zone 'PRC', 'YYYY-MM-DD') as begin_day
	from staff_assess_content  
	join staff_assess on staff_assess_content.staff_assess_id  = staff_assess.id 
	where 1=1 
	and staff_assess.deleted_at isnull 
	and staff_assess_content.category =?
	and staff_assess_content."name" = ?
tangxvhui authored
1324
	and staff_assess."types" ='self'
Your Name authored
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	and staff_assess_content.level_value =?
	and staff_assess.evaluation_project_id=?
	and target_user ->>'userId'='?'
	ORDER BY staff_assess.begin_time  `

	var result []AssessSelfContentRemark
	condition := []interface{}{
		category, name, levalvalue, projectId, targetUserId,
	}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}