作者 tangxvhui

调整sql

  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  7 +)
  8 +
  9 +type TaskDao struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +func NewTaskDao(options map[string]interface{}) *TaskDao {
  14 + var transactionContext *pgTransaction.TransactionContext
  15 + if value, ok := options["transactionContext"]; ok {
  16 + transactionContext = value.(*pgTransaction.TransactionContext)
  17 + }
  18 + return &TaskDao{
  19 + transactionContext: transactionContext,
  20 + }
  21 +}
  22 +
  23 +func (d *TaskDao) catchTaskIdByPermission(userId int) string {
  24 + sqlStr := `with
  25 + -- 人员自身以及全下级
  26 + recursive t_user as (
  27 + (
  28 + select "user".id,"user".parent_id
  29 + from "user"
  30 + where company_id =233 and "user".id=%d
  31 + )
  32 + union
  33 + (
  34 + select "child_user".id,"child_user".parent_id
  35 + from "user" as "child_user"
  36 + join t_user as "parent_user" on "parent_user".id="child_user".parent_id
  37 + )
  38 + ),
  39 + -- 根据任务负责人和相关人员查询
  40 + t_task_0 as (
  41 + (select task.id from task
  42 + join t_user on task.leader ->>'id'=t_user.id::text
  43 + )
  44 + union
  45 + (select task.id from task where task.related_user@>'[%d]')
  46 + ),
  47 + t_task_ignore as (
  48 + select * from task_ignore where task_ignore.user_id =%d
  49 + ),
  50 + -- 过滤取消关注的
  51 + t_task_1 as (
  52 + select t_task_0.id
  53 + from t_task_0
  54 + left join t_task_ignore on t_task_0.id=t_task_ignore.task_id
  55 + where t_task_ignore.id isnull
  56 + )`
  57 + return fmt.Sprintf(sqlStr, userId, userId, userId)
  58 +}
  59 +
  60 +type ListTaskCondition struct {
  61 + Limit int //分页
  62 + Offset int //分页
  63 + UserId int //谁要查看任务数据
  64 + TaskName string //任务名称
  65 + LevelName string //优先级
  66 + OnlyMy bool //只查看我负责的任务
  67 + LeaderId string //任务负责人id
  68 +}
  69 +
  70 +// 获取任务以及里程碑列表,用于页面展示; 有过滤查看权限
  71 +// userid 谁要查看任务数据
  72 +// limit 分页
  73 +// offset 分页
  74 +func (d *TaskDao) ListTaskStageNotHrbp(param ListTaskCondition) {
  75 + task1 := d.catchTaskIdByPermission(param.UserId)
  76 + withSql := task1 + `,
  77 + -- 获取的里程碑数据,以及排序
  78 + t_task_tage_1 as(
  79 + select
  80 + task.id as task_id,
  81 + task."name" as task_name,
  82 + task.leader ->>'name' as leader_name,
  83 + task.leader ->>'id' as leader_id,
  84 + task.level_name ,
  85 + task.anomaly ,
  86 + task.updated_at ,
  87 + task.created_at ,
  88 + task."level" ,
  89 + task_stage."name" as stage_name,
  90 + task_stage.sort_by as stage_sort_by,
  91 + task_stage.status as stage_status,
  92 + task_stage.plan_completed_at,
  93 + (case
  94 + when task_stage.real_completed_at =0
  95 + then task_stage.plan_completed_at - floor( extract(epoch from now()))
  96 + else task_stage.plan_completed_at - task_stage.real_completed_at
  97 + end) as diff_time
  98 + from task
  99 + join t_task_1 on task.id=t_task_1.id
  100 + join task_stage on task.id =task_stage.task_id
  101 + where 1=1
  102 + order by diff_time,task."level",task.created_at
  103 + ),
  104 + -- 按任务数据分页获取
  105 + t_task_page as (
  106 + select distinct t_task_tage_1.task_id
  107 + from t_task_tage_1
  108 + where 1=1
  109 + %s
  110 + limit ? offset ?
  111 + )
  112 + select
  113 + t_task_tage_1.task_id,
  114 + t_task_tage_1.task_name,
  115 + t_task_tage_1.leader_name,
  116 + t_task_tage_1.level_name ,
  117 + t_task_tage_1.anomaly ,
  118 + t_task_tage_1.updated_at ,
  119 + t_task_tage_1.created_at ,
  120 + t_task_tage_1."level" ,
  121 + t_task_tage_1.plan_completed_at,
  122 + t_task_tage_1.stage_name,
  123 + t_task_tage_1.stage_sort_by,
  124 + t_task_tage_1.stage_status,
  125 + from t_task_tage_1
  126 + where t_task_tage_1.task_id in(
  127 + select t_task_page.task_id from t_task_page
  128 + )`
  129 + condition := []interface{}{}
  130 + whereSql := ``
  131 + if param.OnlyMy {
  132 + condition = append(condition, param.UserId)
  133 + whereSql += ` and t_task_tage_1.leader_id = '?' `
  134 + } else if param.LeaderId != "" {
  135 + condition = append(condition, param.LeaderId)
  136 + whereSql += ` and t_task_tage_1.leader_id = ? `
  137 + }
  138 + if len(param.TaskName) > 0 {
  139 + condition = append(condition, param.TaskName)
  140 + whereSql += ` and t_task_tage_1.task_name like ? `
  141 + }
  142 + if len(param.LevelName) > 0 {
  143 + condition = append(condition, param.LevelName)
  144 + whereSql += ` and t_task_tage_1.level_name like ? `
  145 + }
  146 + = withSql
  147 +}
  148 +
  149 +// 获取任务总数,用于页面展示; 有过滤查看权限
  150 +func (d *TaskDao) CountTaskStageNotHrbp(param ListTaskCondition) {
  151 + task1 := d.catchTaskIdByPermission(param.UserId)
  152 + withSql := task1 + `select count(*) from task
  153 + join t_task_1 on task.id =t_task_1.id
  154 + where 1=1 `
  155 + condition := []interface{}{}
  156 + whereSql := ``
  157 + if param.OnlyMy {
  158 + condition = append(condition, param.UserId)
  159 + whereSql += ` and task.leader ->>'id' = '?' `
  160 + } else if param.LeaderId != "" {
  161 + condition = append(condition, param.LeaderId)
  162 + whereSql += ` and task.leader ->>'id' = ? `
  163 + }
  164 + if len(param.TaskName) > 0 {
  165 + condition = append(condition, param.TaskName)
  166 + whereSql += ` and task.name like ? `
  167 + }
  168 + if len(param.LevelName) > 0 {
  169 + condition = append(condition, param.LevelName)
  170 + whereSql += ` and task.level_name like ? `
  171 + }
  172 + sqlStr := withSql + whereSql
  173 + _ = sqlStr
  174 +}
  175 +、、'