<?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.hzy.project.business.salary.mapper.SalaryMapper">
    
    <resultMap type="Salary" id="SalaryResult">
        <result property="salaryId"    column="salaryId"    />
        <result property="userId"    column="user_id"    />
        <result property="salaryDate"    column="salaryDate"    />
        <result property="sumAmount"    column="sumAmount"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap id="SalarySalaryDetailResult" type="Salary" extends="SalaryResult">
        <collection property="salaryDetailList" ofType="SalaryDetail" column="salaryId" select="selectSalaryDetailList" />
    </resultMap>

    <resultMap type="SalaryDetail" id="SalaryDetailResult">
        <result property="id"    column="id"    />
        <result property="salaryId"    column="salaryId"    />
        <result property="salarySubjectId"    column="salarySubjectId"    />
        <result property="subjectName"    column="subjectName"    />
        <result property="amount"    column="amount"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSalaryVo">
        select salaryId, user_id, salaryDate, sumAmount, delFlag, remark, create_by, create_time, update_by, update_time from t_salary
    </sql>

    <select id="selectSalaryList" parameterType="Salary" resultMap="SalaryResult">
        <include refid="selectSalaryVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="params.beginSalaryDate != null and params.beginSalaryDate != '' and params.endSalaryDate != null and params.endSalaryDate != ''"> and salaryDate between #{params.beginSalaryDate} and #{params.endSalaryDate}</if>
        </where>
    </select>
    
    <select id="selectSalaryBySalaryId" parameterType="Long" resultMap="SalarySalaryDetailResult">
        select salaryId, user_id, salaryDate, sumAmount, delFlag, remark, create_by, create_time, update_by, update_time
        from t_salary
        where salaryId = #{salaryId}
    </select>

    <select id="selectSalaryDetailList" resultMap="SalaryDetailResult">
        select id, salaryId, salarySubjectId, subjectName, amount, delFlag, create_by, create_time, update_by, update_time
        from t_salary_detail
        where salaryId = #{salaryId}
    </select>

    <insert id="insertSalary" parameterType="Salary" useGeneratedKeys="true" keyProperty="salaryId">
        insert into t_salary
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="salaryDate != null">salaryDate,</if>
            <if test="sumAmount != null">sumAmount,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="salaryDate != null">#{salaryDate},</if>
            <if test="sumAmount != null">#{sumAmount},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSalary" parameterType="Salary">
        update t_salary
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="salaryDate != null">salaryDate = #{salaryDate},</if>
            <if test="sumAmount != null">sumAmount = #{sumAmount},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where salaryId = #{salaryId}
    </update>

    <delete id="deleteSalaryBySalaryId" parameterType="Long">
        delete from t_salary where salaryId = #{salaryId}
    </delete>

    <delete id="deleteSalaryBySalaryIds" parameterType="String">
        delete from t_salary where salaryId in 
        <foreach item="salaryId" collection="array" open="(" separator="," close=")">
            #{salaryId}
        </foreach>
    </delete>
    
    <delete id="deleteSalaryDetailBySalaryIds" parameterType="String">
        delete from t_salary_detail where salaryId in 
        <foreach item="salaryId" collection="array" open="(" separator="," close=")">
            #{salaryId}
        </foreach>
    </delete>

    <delete id="deleteSalaryDetailBySalaryId" parameterType="Long">
        delete from t_salary_detail where salaryId = #{salaryId}
    </delete>

    <insert id="batchSalaryDetail">
        insert into t_salary_detail( id, salaryId, salarySubjectId, subjectName, amount, delFlag, create_by, create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.salaryId}, #{item.salarySubjectId}, #{item.subjectName}, #{item.amount}, #{item.delFlag}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

</mapper>