<?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="org.dromara.main.mapper.MainAuditMapper">

    <resultMap type="org.dromara.main.domain.vo.MainAuditVo" id="AuditDetailResult">
        <id     property="id"             column="id"              />
        <result property="auditType"      column="audit_type"      />
        <result property="targetId"       column="target_id"       />
        <result property="auditResult"    column="audit_result"    />
        <result property="auditRemark"    column="audit_remark"    />
        <result property="deposit"        column="deposit"         />
        <result property="finalPayment"   column="final_payment"   />
        <result property="auditTime"      column="audit_time"      />
        <result property="auditBy"        column="audit_by"        />
        <result property="tenantId"       column="tenant_id"       />
        <result property="createTime"     column="create_time"     />

        <result property="applyNo"        column="apply_no"        />
        <result property="companyName"    column="company_name"    />
        <result property="officialAccount" column="official_account" />
        <result property="officeAddress"  column="office_address"  />
        <result property="creditCode"     column="credit_code"     />
        <result property="authLetter"     column="auth_letter"     />
        <result property="mobile"         column="mobile"          />
        <result property="surname"        column="surname"         />
        <result property="name"           column="name"            />
        <result property="avatar"         column="avatar"          />
        <result property="applyCreateTime" column="apply_create_time" />
        <result property="auditByName"    column="audit_by_name"   />

        <result property="postName"       column="post_name"       />
        <result property="postDescription" column="post_description" />
        <result property="workProvince"   column="work_province"   />
        <result property="workCity"       column="work_city"       />
        <result property="workDistrict"   column="work_district"   />
        <result property="workAddress"    column="work_address"    />
        <result property="postType"       column="post_type"       />
        <result property="educationRequirement" column="education_requirement" />
        <result property="salaryType"     column="salary_type"     />
        <result property="salaryRange"    column="salary_range"    />
        <result property="minSalary"      column="min_salary"      />
        <result property="maxSalary"      column="max_salary"      />
        <result property="recruitNum"     column="recruit_num"     />
        <result property="registrationStartDate" column="registration_start_date" />
        <result property="registrationEndDate" column="registration_end_date" />
        <result property="isUrgent"       column="is_urgent"       />
        <result property="schoolRequirement" column="school_requirement" />
        <result property="genderRequirement" column="gender_requirement" />
        <result property="gradeRequirement" column="grade_requirement" />
        <result property="arrivalTime"    column="arrival_time"    />
        <result property="internshipDuration" column="internship_duration" />
        <result property="willingToTravel" column="willing_to_travel" />
        <result property="welfareTags"    column="welfare_tags"    />
        <result property="jobRequirement" column="job_requirement" />
        <result property="postLevel"      column="post_level"      />
        <result property="assessmentTime" column="assessment_time" />
        <result property="gradeA"         column="grade_a"         />
        <result property="gradeB"         column="grade_b"         />
        <result property="gradeC"         column="grade_c"         />
    </resultMap>

    <sql id="selectAuditDetailVo">
        SELECT
            a.id,
            a.audit_type,
            a.target_id,
            a.audit_result,
            a.audit_remark,
            a.deposit,
            a.final_payment,
            a.audit_time,
            a.audit_by,
            a.create_time,

            CASE
                WHEN a.audit_type = 1 THEN ca.tenant_id
                WHEN a.audit_type = 2 THEN pa.tenant_id
                ELSE a.tenant_id
                END as tenant_id,

            u.nick_name as audit_by_name,

            ca.apply_no,
            ca.official_account,
            ca.office_address,
            ca.credit_code,
            ca.auth_letter,
            ca.mobile,
            ca.surname,
            ca.name,
            ca.avatar,
            ca.create_time as apply_create_time,

            pa.post_name,
            pa.post_description,
            pa.work_province,
            pa.work_city,
            pa.work_district,
            pa.work_address,
            pa.post_type,
            pa.education_requirement,
            pa.salary_type,
            pa.salary_range,
            pa.min_salary,
            pa.max_salary,
            pa.recruit_num,
            pa.registration_start_date,
            pa.registration_end_date,
            pa.is_urgent,
            pa.school_requirement,
            pa.gender_requirement,
            pa.grade_requirement,
            pa.arrival_time,
            pa.internship_duration,
            pa.willing_to_travel,
            pa.welfare_tags,
            pa.job_requirement,
            pa.post_level,
            pa.assessment_time,
            pa.grade_a,
            pa.grade_b,
            pa.grade_c,

            CASE
                WHEN a.audit_type = 1 THEN ca.company_name
                WHEN a.audit_type = 2 THEN COALESCE(t.company_name, pa.company_name)
                ELSE NULL
                END as company_name

        FROM main_audit a
                 LEFT JOIN main_company_apply ca ON a.audit_type = 1 AND a.target_id = ca.id
                 LEFT JOIN main_post_apply pa ON a.audit_type = 2 AND a.target_id = pa.id
                 LEFT JOIN sys_tenant t ON (
            (a.audit_type = 1 AND ca.tenant_id = t.tenant_id) OR
            (a.audit_type = 2 AND pa.tenant_id = t.tenant_id)
            )
                 LEFT JOIN sys_user u ON a.audit_by = u.user_id
    </sql>

    <select id="selectAuditDetailPage" resultMap="AuditDetailResult">
        <include refid="selectAuditDetailVo"/>
        WHERE a.del_flag = '0'
        <if test="ew != null and ew.sqlSegment != null and ew.sqlSegment != ''">
            AND ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectAuditDetailList" resultMap="AuditDetailResult">
        <include refid="selectAuditDetailVo"/>
        WHERE a.del_flag = '0'
        <if test="ew != null and ew.sqlSegment != null and ew.sqlSegment != ''">
            AND ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectAuditDetailById" resultMap="AuditDetailResult">
        <include refid="selectAuditDetailVo"/>
        WHERE a.id = #{id} AND a.del_flag = '0'
    </select>

</mapper>
