Files
korea-research/admin/src/main/resources/mappers/household/GetHouseholdsStatusMapper.xml

229 lines
9.1 KiB
XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper
namespace="app.module.admin.usecase.household.getHouseholdsStatus.adapters.persistence.GetHouseholdsStatusMapper">
<select id="getHouseholdsStatus"
resultType="app.module.admin.usecase.household.getHouseholdsStatus.ports.application.domain.GetHouseholdsStatus">
/* getHouseholdsStatus : 임가현황 리스트 조회 */
WITH month_check AS (
SELECT #{cmd.recordMonth} = TO_CHAR(CURRENT_DATE, 'YYYYMM') AS is_current_month
),
daily_progress AS (
/* 임가별 이번달 완료된 일 수 계산 */
SELECT
household_seq,
COUNT(*) as completed_days
FROM daily_record
WHERE record_month = #{cmd.recordMonth}
AND all_status IN ('DONE', 'TODAY_NONE')
AND is_deleted = false
GROUP BY household_seq
),
total_days AS (
/* 오늘 날짜 기준 전체 입력 가능 날짜 수 */
SELECT COUNT(*) as total_possible_days
FROM daily_day_group ddg
CROSS JOIN month_check mc
WHERE NOT (mc.is_current_month AND ddg.start_day > EXTRACT(DAY FROM CURRENT_DATE))
AND ddg.is_deleted = false
),
visit_summary AS (
/* 임가별 1차임가방문, 2차임가방문, 답례품전달여부 */
SELECT
rhh.household_seq,
MIN(v.visit_date) AS first_visit_date,
MAX(v.visit_date) AS second_visit_date,
BOOL_OR(v.is_gift_sent) AS is_gift_sent
FROM household_visit v
JOIN researcher_has_household rhh ON v.household_seq = rhh.household_seq
WHERE TO_CHAR(v.visit_date, 'YYYYMM') = #{cmd.recordMonth}
AND rhh.is_deleted = false
GROUP BY rhh.household_seq
)
SELECT
hvr.*,
COUNT(*) OVER () AS total
FROM (
SELECT
r.admin_seq AS adminSeq,
h.seq AS householdSeq,
h.household_num AS householdNum,
h.owner_name AS householdName,
h.phone AS householdPhone,
h.region_main_code AS regionMainCode,
h.region_main_name AS regionMainName,
h.region_sub_code AS regionSubCode,
h.region_sub_name AS regionSubName,
h.region_detail_code AS regionDetailCode,
h.region_detail_name AS regionDetailName,
h.industry_large_code AS industryLargeCode,
h.industry_large_name AS industryLargeName,
h.industry_middle_code AS industryMiddleCode,
h.industry_middle_name AS industryMiddleName,
h.is_panel AS isPanel,
/* 일계부 입력 진행 완료 퍼센테이지 (이번달 작성 완료일 수 / 현재날짜 기준 전체 입력 가능 날짜 수) */
ROUND(
100.0 * COALESCE(dp.completed_days, 0) / NULLIF(td.total_possible_days, 0),
2
) AS progressPercent,
vs.first_visit_date AS firstVisitDate,
vs.second_visit_date AS secondVisitDate,
COALESCE(vs.is_gift_sent, false) AS isGiftSent,
r.researcher_name AS researcherName,
r.researcher_phone AS researcherPhone
FROM v_household_with_code h
CROSS JOIN total_days td
LEFT JOIN daily_progress dp ON h.seq = dp.household_seq
LEFT JOIN visit_summary vs ON h.seq = vs.household_seq
LEFT JOIN v_household_researcher r ON h.seq = r.household_seq
) AS hvr
<where>
1=1
<if test="cmd.ownerValue != null and cmd.ownerValue != ''">
/* 경영주 검색 */
<choose>
<when test="cmd.ownerField == ''">
AND (
hvr.householdName ILIKE '%' || #{cmd.ownerValue} || '%'
OR hvr.householdPhone ILIKE '%' || #{cmd.ownerValue} || '%'
)
</when>
<when test="cmd.ownerField eq 'name'">
AND hvr.householdName ILIKE '%' || #{cmd.ownerValue} || '%'
</when>
<when test="cmd.ownerField eq 'phone'">
AND hvr.householdPhone ILIKE '%' || #{cmd.ownerValue} || '%'
</when>
</choose>
</if>
<if test="cmd.researcherValue != null and cmd.researcherValue != ''">
/* 조사원 검색 */
<choose>
<when test="cmd.researcherField == ''">
AND (
hvr.researcherName ILIKE '%' || #{cmd.researcherValue} || '%'
OR hvr.researcherPhone ILIKE '%' || #{cmd.researcherValue} || '%'
)
</when>
<when test="cmd.researcherField eq 'name'">
AND hvr.researcherName ILIKE '%' || #{cmd.researcherValue} || '%'
</when>
<when test="cmd.researcherField eq 'phone'">
AND hvr.researcherPhone ILIKE '%' || #{cmd.researcherValue} || '%'
</when>
</choose>
</if>
<choose>
<when test="cmd.userRole != null and cmd.userRole != 'ROLE_ADMIN'">
/* 실사 검색 : 지방실사 또는 조사원 */
AND hvr.regionMainCode = #{cmd.userRegionMainCode}
</when>
<when test="cmd.regionMainCode != null and cmd.regionMainCode != ''">
/* 실사 검색 : 관리자 */
AND hvr.regionMainCode = #{cmd.regionMainCode}
</when>
</choose>
<if test="cmd.regionSubCode != null and cmd.regionSubCode != ''">
/* 지역 검색 */
AND hvr.regionSubCode = #{cmd.regionSubCode}
</if>
<if test="cmd.regionDetailCode != null and cmd.regionDetailCode != ''">
/* 세부지역 검색 */
AND hvr.regionDetailCode = #{cmd.regionDetailCode}
</if>
<if test="cmd.industryLargeCode != null and cmd.industryLargeCode != ''">
/* 업종 대분류 검색 */
AND hvr.industryLargeCode = #{cmd.industryLargeCode}
</if>
<if test="cmd.industryMiddleCode != null and cmd.industryMiddleCode != ''">
/* 업종 중분류1 검색*/
AND hvr.industryMiddleCode = #{cmd.industryMiddleCode}
</if>
<if test="cmd.progressType != null and cmd.progressType != ''">
/* 일계부 현황 퍼센테이지 검색 */
<choose>
<when test="cmd.progressType eq 'PROGRESS_25'">
AND hvr.progressPercent &lt; 25
</when>
<when test="cmd.progressType eq 'PROGRESS_50'">
AND hvr.progressPercent &gt;= 25 AND hvr.progressPercent &lt; 50
</when>
<when test="cmd.progressType eq 'PROGRESS_75'">
AND hvr.progressPercent &gt;= 50 AND hvr.progressPercent &lt; 75
</when>
<when test="cmd.progressType eq 'PROGRESS_75_UP'">
AND hvr.progressPercent &gt; 75
</when>
<when test="cmd.progressType eq 'PROGRESS_100'">
AND hvr.progressPercent = 100
</when>
</choose>
</if>
<if test="cmd.isFirstVisited != null">
/* 당월 1차 방문 여부 */
<choose>
<when test="cmd.isFirstVisited">
AND hvr.firstVisitDate is not null
</when>
<when test="!cmd.isFirstVisited">
AND hvr.firstVisitDate is null
</when>
</choose>
</if>
<if test="cmd.isSecondVisited != null">
/* 당월 2차 방문 여부 */
<choose>
<when test="cmd.isSecondVisited">
AND hvr.secondVisitDate is not null
</when>
<when test="!cmd.isSecondVisited">
AND hvr.secondVisitDate is null
</when>
</choose>
</if>
<if test="cmd.isGiftSent != null">
/* 답례품 전달 여부 */
<choose>
<when test="cmd.isGiftSent">
AND hvr.isGiftSent = true
</when>
<when test="!cmd.isGiftSent">
AND hvr.isGiftSent = false
</when>
</choose>
</if>
<if test="cmd.isPanel != null">
/* 패널 유지 여부 */
<choose>
<when test="cmd.isPanel">
AND hvr.isPanel = true
</when>
<when test="!cmd.isPanel">
AND hvr.isPanel = false
</when>
</choose>
</if>
</where>
<if test="cmd.userRole != null and cmd.userRole == 'ROLE_RESEARCHER'">
/* 조사원일 경우 매핑된 임가만 노출 */
AND hvr.adminSeq = #{cmd.adminSeq}
</if>
ORDER BY hvr.householdNum ASC
OFFSET #{pageable.offset} ROWS FETCH NEXT #{pageable.pageSize} ROWS ONLY
</select>
</mapper>