<?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="com.jeeplus.test.changes.dimission.mapper.ChangesDimissionMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
            <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
            <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
            <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
            <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
            <result property="rosterId" column="roster_id" jdbcType="VARCHAR"/>
            <result property="lastWorkDate" column="last_work_date" jdbcType="TIMESTAMP"/>
            <result property="dimissionDate" column="dimission_date" jdbcType="TIMESTAMP"/>
            <result property="reasonDesc" column="reason_desc" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="vettingStatus" column="vetting_status" jdbcType="VARCHAR"/>
            <result property="handoverStatus" column="handover_status" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="payrollSettlementDate" column="payroll_settlement_date" jdbcType="TIMESTAMP"/>
            <result property="requestDate" column="request_date" jdbcType="TIMESTAMP"/>
            <result property="rosterBaseDTO.name" column="rb_name" jdbcType="VARCHAR"/>
            <result property="rosterBaseDTO.jobNo" column="rb_job_no" jdbcType="VARCHAR"/>
            <result property="rosterBaseDTO.mobile" column="rb_mobile" jdbcType="VARCHAR"/>
            <result property="rosterBaseDTO.onboardingDate" column="rb_onboarding_date" jdbcType="TIMESTAMP"/>
            <result property="soName" column="so_name" jdbcType="VARCHAR"/>
            <result property="jpName" column="jp_name" jdbcType="VARCHAR"/>
            <result column="handover_id" jdbcType="VARCHAR" property="handoverId" />
            <result column="social_security_last" jdbcType="TIMESTAMP" property="socialSecurityLast" />
            <result column="provident_fund_last" jdbcType="TIMESTAMP" property="providentFundLast" />
            <result column="proc_ins_id" jdbcType="VARCHAR" property="procInsId" />
            <association property="changesDimissionAuditDTO" column="id" javaType="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionAuditDTO" select="getAudit"></association>
            <collection property="changesDimissionReaDTOList" ofType="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionReaDTO" column="id" select="getRea"></collection>
            <collection property="changesDimissionFilesDTOList" ofType="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionFilesDTO" column="id" select="getFiles"></collection>
    </resultMap>

    <sql id="Base_Column_List">
        cd.id,
        cd.create_by,
        cd.create_date,
        cd.update_by,
        cd.update_date,
        cd.del_flag,
        cd.remarks,
        cd.roster_id,
        cd.last_work_date,
        cd.dimission_date,
        cd.reason_desc,
        cd.type,
        cd.vetting_status,
        cd.handover_status,
        cd.status,
        cd.payroll_settlement_date,
        cd.request_date,
        cd.handover_id,
        cd.social_security_last,
        cd.provident_fund_last,
        cd.proc_ins_id
    </sql>
    <sql id="Rea_Column_List">
        cdr.id,
        cdr.create_by,
        cdr.create_date,
        cdr.update_by,
        cdr.update_date,
        cdr.del_flag,
        cdr.remarks,
        cdr.cd_id,
        cdr.dimission_reason
    </sql>
    <sql id="Files_Column_List">
        cdf.id,
        cdf.create_by,
        cdf.create_date,
        cdf.update_by,
        cdf.update_date,
        cdf.del_flag,
        cdf.remarks,
        cdf.cd_id,
        cdf.url,
        cdf.name
    </sql>
    <sql id="Audit_Column_List">
        cda.id,
        cda.create_by,
        cda.create_date,
        cda.update_by,
        cda.update_date,
        cda.del_flag,
        cda.remarks,
        cda.vetting_status,
        cda.proc_ins_id
    </sql>

    <select id="getAudit" resultType="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionAuditDTO">
        select
        <include refid="Audit_Column_List"></include>
        from changes_dimission_audit cda
        where cda.del_flag = '0' and cda.id = #{id}
    </select>
    <select id="getRea" resultType="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionReaDTO">
        select
        <include refid="Rea_Column_List"></include>
        from changes_dimission_rea cdr
        where cdr.del_flag = '0' and cdr.cd_id = #{id}
    </select>
    <select id="getFiles" resultMap="FilesMap">
        select
        <include refid="Files_Column_List"></include>,
        su.name su_name
        from changes_dimission_files cdf
        left join sys_user su on cdf.create_by = su.id and su.del_flag = '0'
        where cdf.del_flag = '0' and cdf.cd_id = #{id}
        order by cdf.create_date desc
    </select>
    <resultMap id="FilesMap" type="com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionFilesDTO">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy.id" />
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy.id" />
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
        <result column="remarks" jdbcType="VARCHAR" property="remarks" />
        <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
        <result column="cd_id" jdbcType="VARCHAR" property="cdId" />
        <result column="url" jdbcType="VARCHAR" property="url" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="su_name" jdbcType="VARCHAR" property="createBy.name" />
    </resultMap>

    <select id="findList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        rb.name rb_name,
        rb.job_no rb_job_no,
        rb.mobile rb_mobile,
        rb.onboarding_date rb_onboarding_date,
        so.name so_name,
        jp.name jp_name
        from changes_dimission cd
        left join roster_base rb on rb.id = cd.roster_id and rb.del_flag = '0'
        left join sys_office so on so.id = rb.office_id and so.del_flag = '0'
        left join job_position jp on jp.id = rb.job_position_id and jp.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        rb.name rb_name,
        rb.job_no rb_job_no,
        rb.mobile rb_mobile,
        rb.onboarding_date rb_onboarding_date,
        so.name so_name,
        jp.name jp_name
        from changes_dimission cd
        left join roster_base rb on rb.id = cd.roster_id and rb.del_flag = '0'
        left join sys_office so on so.id = rb.office_id and so.del_flag = '0'
        left join job_position jp on jp.id = rb.job_position_id and jp.del_flag = '0'
        where cd.del_flag = '0' and cd.id = #{id}
    </select>

</mapper>
