<?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.engine.salary.mapper.datacollection.AttendQuoteMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.datacollection.po.AttendQuotePO">
        <result column="id" property="id"/>
        <result column="salary_year_month" property="salaryYearMonth"/>
        <result column="year" property="year"/>
        <result column="month" property="month"/>
        <result column="salary_sob_id" property="salarySobId"/>
        <result column="source_type" property="sourceType"/>
        <result column="salary_accounting_status" property="salaryAccountingStatus"/>
        <result column="attend_cycle" property="attendCycle"/>
        <result column="salary_cycle" property="salaryCycle"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.salary_year_month
        , t.year
        , t.month
        , t.salary_sob_id
        , t.source_type
        , t.salary_accounting_status
        , t.attend_cycle
        , t.salary_cycle
        , t.description
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_attend_quote t
        WHERE delete_type = 0
    </select>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_attend_quote t
        WHERE id = #{id} AND delete_type = 0
    </select>

    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.AttendQuotePO">
        INSERT INTO hrsa_attend_quote
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="salaryYearMonth != null">
                salary_year_month,
            </if>
            <if test="year != null">
                year,
            </if>
            <if test="month != null">
                month,
            </if>
            <if test="salarySobId != null">
                salary_sob_id,
            </if>
            <if test="sourceType != null">
                source_type,
            </if>
            <if test="salaryAccountingStatus != null">
                salary_accounting_status,
            </if>
            <if test="attendCycle != null">
                attend_cycle,
            </if>
            <if test="salaryCycle != null">
                salary_cycle,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="salaryYearMonth != null">
                #{salaryYearMonth},
            </if>
            <if test="year != null">
                #{year},
            </if>
            <if test="month != null">
                #{month},
            </if>
            <if test="salarySobId != null">
                #{salarySobId},
            </if>
            <if test="sourceType != null">
                #{sourceType},
            </if>
            <if test="salaryAccountingStatus != null">
                #{salaryAccountingStatus},
            </if>
            <if test="attendCycle != null">
                #{attendCycle},
            </if>
            <if test="salaryCycle != null">
                #{salaryCycle},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.datacollection.po.AttendQuotePO">
        UPDATE hrsa_attend_quote
        <set>
            salary_year_month=#{salaryYearMonth},
            year=#{year},
            month=#{month},
            salary_sob_id=#{salarySobId},
            source_type=#{sourceType},
            salary_accounting_status=#{salaryAccountingStatus},
            attend_cycle=#{attendCycle},
            salary_cycle=#{salaryCycle},
            description=#{description},
            create_time=#{createTime},
            update_time=#{updateTime},
            creator=#{creator},
            delete_type=#{deleteType},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.AttendQuotePO">
        UPDATE hrsa_attend_quote
        <set>
            <if test="salaryYearMonth != null">
                salary_year_month=#{salaryYearMonth},
            </if>
            <if test="year != null">
                year=#{year},
            </if>
            <if test="month != null">
                month=#{month},
            </if>
            <if test="salarySobId != null">
                salary_sob_id=#{salarySobId},
            </if>
            <if test="sourceType != null">
                source_type=#{sourceType},
            </if>
            <if test="salaryAccountingStatus != null">
                salary_accounting_status=#{salaryAccountingStatus},
            </if>
            <if test="attendCycle != null">
                attend_cycle=#{attendCycle},
            </if>
            <if test="salaryCycle != null">
                salary_cycle=#{salaryCycle},
            </if>
            <if test="description != null">
                description=#{description},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete" parameterType="com.engine.salary.entity.datacollection.po.AttendQuotePO">
        UPDATE hrsa_attend_quote
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>


    <sql id="attendQuoteColumn">
        t1
        .
        id
        ,
        t1.salary_year_month,
        t1.salary_sob_id AS salarySobId,
        t2.name AS salary_sob_name,
        t1.source_type,
        t1.attend_cycle,
        t1.salary_cycle,
        t1.description,
        e.lastname AS creator,
        t1.salary_accounting_status
    </sql>

    <sql id="paramSql">
        <if test="param.ids != null and param.ids.size()>0">
            AND t1.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.salarySobIds != null and param.salarySobIds.size()>0">
            AND t1.salary_sob_id IN
            <foreach collection="param.salarySobIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <!-- 薪资所属月 -->
        <if test="param.salaryYearMonthDate != null">
            <if test="param.salaryYearMonthDate.size() == 1">
                AND t1.salary_year_month = #{param.salaryYearMonthDate[0]}
            </if>
            <if test="param.salaryYearMonthDate.size() == 2">
                AND (t1.salary_year_month BETWEEN #{param.salaryYearMonthDate[0]} AND #{param.salaryYearMonthDate[1]})
            </if>
        </if>
    </sql>

    <select id="list" resultType="com.engine.salary.entity.datacollection.dto.AttendQuoteListDTO">
        SELECT
        <include refid="attendQuoteColumn"/>
        FROM
        hrsa_attend_quote t1
        LEFT JOIN hrsa_salary_sob t2 ON t1.salary_sob_id = t2.id
        LEFT JOIN hrmresource e ON e.id = t1.creator
        WHERE
        t1.delete_type = 0 AND t2.delete_type = 0
        <include refid="paramSql"/>
        ORDER BY t1.id DESC
    </select>

    <update id="deleteByIds">
        UPDATE hrsa_attend_quote
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="listSome" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_attend_quote t
        WHERE delete_type = 0
        <if test="param.salaryYearMonth != null">
            AND t.salary_year_month = #{param.salaryYearMonth}
        </if>
        <if test="param.salarySobId != null">
            AND t.salary_sob_id = #{param.salarySobId}
        </if>
    </select>


</mapper>