DROP VIEW "public"."periodic_evaluations_view";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "process_score";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "price_score";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "delivery_score";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "self_evaluation_score";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "total_score";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "participation_bonus";--> statement-breakpoint
ALTER TABLE "periodic_evaluations" DROP COLUMN "quality_deduction";--> statement-breakpoint
CREATE VIEW "public"."periodic_evaluations_view" AS (select "periodic_evaluations"."id", "periodic_evaluations"."evaluation_target_id", "evaluation_targets"."evaluation_year", "evaluation_targets"."division", "evaluation_targets"."vendor_id", "evaluation_targets"."vendor_code", "evaluation_targets"."vendor_name", "evaluation_targets"."domestic_foreign", "evaluation_targets"."material_type", "periodic_evaluations"."evaluation_period", "periodic_evaluations"."documents_submitted", "periodic_evaluations"."submission_date", "periodic_evaluations"."submission_deadline", "periodic_evaluations"."final_score", "periodic_evaluations"."final_grade", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'processScore'
        AND re.is_completed = true
      ) as "process_score", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'priceScore'
        AND re.is_completed = true
      ) as "price_score", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'deliveryScore'
        AND re.is_completed = true
      ) as "delivery_score", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'selfEvaluationScore'
        AND re.is_completed = true
      ) as "self_evaluation_score", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'bonus'
        AND re.is_completed = true
      ) as "participation_bonus", (
        SELECT COALESCE(AVG(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'penalty'
        AND re.is_completed = true
      ) as "quality_deduction", (
        SELECT COALESCE(
          (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'processScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'priceScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'deliveryScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'selfEvaluationScore' AND re.is_completed = true), 0
          )
      ) as "total_score", (
        SELECT (
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'processScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'priceScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'deliveryScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'selfEvaluationScore' AND re.is_completed = true), 0
          ) +
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'bonus' AND re.is_completed = true), 0
          ) -
          COALESCE(
            (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'penalty' AND re.is_completed = true), 0
          )
        )
      ) as "evaluation_score", (
        CASE 
          WHEN (
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'processScore' AND re.is_completed = true), 0
            ) +
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'priceScore' AND re.is_completed = true), 0
            ) +
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'deliveryScore' AND re.is_completed = true), 0
            ) +
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'selfEvaluationScore' AND re.is_completed = true), 0
            ) +
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'bonus' AND re.is_completed = true), 0
            ) -
            COALESCE(
              (SELECT AVG(CAST(red.score AS DECIMAL(5,2))) FROM "reviewer_evaluation_details" red JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id WHERE re.periodic_evaluation_id = "periodic_evaluations"."id" AND rec.category2 = 'penalty' AND re.is_completed = true), 0
            )
          ) >= 90 THEN 'S'
          WHEN (/* 위와 동일한 계산 */) >= 80 THEN 'A'
          WHEN (/* 위와 동일한 계산 */) >= 70 THEN 'B'
          WHEN (/* 위와 동일한 계산 */) >= 60 THEN 'C'
          ELSE 'D'
        END
      ) as "evaluation_grade", "periodic_evaluations"."status", "periodic_evaluations"."review_completed_at", "periodic_evaluations"."finalized_at", "periodic_evaluations"."finalized_by", "periodic_evaluations"."evaluation_note", "periodic_evaluations"."created_at", "periodic_evaluations"."updated_at", "evaluation_targets"."admin_comment", "evaluation_targets"."consolidated_comment", "evaluation_targets"."consensus_status", "evaluation_targets"."confirmed_at", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
      ) as "total_reviewers", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND re.is_completed = true
      ) as "completed_reviewers", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND re.is_completed = false
      ) as "pending_reviewers", "users"."name", "users"."email" from "periodic_evaluations" left join "evaluation_targets" on "periodic_evaluations"."evaluation_target_id" = "evaluation_targets"."id" left join "users" on "periodic_evaluations"."finalized_by" = "users"."id" order by "periodic_evaluations"."created_at");