<?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.siaccount.InsuranceAccountBatchMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.siaccount.po.InsuranceAccountBatchPO">
        <result column="id" property="id"/>
        <result column="bill_month" property="billMonth"/>
        <result column="bill_status" property="billStatus"/>
        <result column="social_num" property="socialNum"/>
        <result column="fund_num" property="fundNum"/>
        <result column="other_num" property="otherNum"/>
        <result column="social_pay" property="socialPay"/>
        <result column="fund_pay" property="fundPay"/>
        <result column="other_pay" property="otherPay"/>
        <result column="accountant" property="accountant"/>
        <result column="remarks" property="remarks"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="tenant_key" property="tenantKey"/>
        <result column="payment_organization" property="paymentOrganization"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.bill_month
        , t.bill_status
        , t.social_num
        , t.fund_num
        , t.other_num
        , t.social_pay
        , t.fund_pay
        , t.other_pay
        , t.accountant
        , t.remarks
        , t.creator
        , t.delete_type
        , t.create_time
        , t.update_time
        , t.tenant_key
        , t.payment_organization
    </sql>

    <insert id="insert" parameterType="com.engine.salary.entity.siaccount.po.InsuranceAccountBatchPO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_bill_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            bill_month,
            bill_status,
            social_num,
            fund_num,
            other_num,
            social_pay,
            fund_pay,
            other_pay,
            accountant,
            remarks,
            creator,
            delete_type,
            create_time,
            update_time,
            tenant_key,
            payment_organization,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{billMonth},
            #{billStatus},
            #{socialNum},
            #{fundNum},
            #{otherNum},
            #{socialPay},
            #{fundPay},
            #{otherPay},
            #{accountant},
            #{remarks},
            #{creator},
            #{deleteType},
            #{createTime},
            #{updateTime},
            #{tenantKey},
            #{paymentOrganization},
        </trim>
    </insert>


    <insert id="insert" parameterType="com.engine.salary.entity.siaccount.po.InsuranceAccountBatchPO"
            databaseId="oracle"
    >
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            select nvl(max(id),0)+1 from hrsa_bill_batch
        </selectKey>
        INSERT INTO hrsa_bill_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            bill_month,
            bill_status,
            social_num,
            fund_num,
            other_num,
            social_pay,
            fund_pay,
            other_pay,
            accountant,
            remarks,
            creator,
            delete_type,
            create_time,
            update_time,
            tenant_key,
            payment_organization,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{billMonth},
            #{billStatus},
            #{socialNum},
            #{fundNum},
            #{otherNum},
            #{socialPay},
            #{fundPay},
            #{otherPay},
            #{accountant},
            #{remarks},
            #{creator},
            #{deleteType},
            #{createTime},
            #{updateTime},
            #{tenantKey},
            #{paymentOrganization},
        </trim>
    </insert>


    <sql id="paramSql">
        <if test="param.startTime != null and param.startTime != '' and param.endTime != null and param.endTime != ''">
            AND
            t.bill_month between #{param.startTime} and #{param.endTime}
        </if>
        <if test="param.paymentOrganization != null">
            AND
            t.payment_organization = #{param.paymentOrganization}
        </if>
        <if test="param.taxAgents != null and param.taxAgents.size() > 0 ">
            AND
            t.payment_organization in
            <foreach collection="param.taxAgents" open="(" item="taxAgent" separator="," close=")">
                #{taxAgent}
            </foreach>
        </if>
    </sql>


    <select id="list" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_bill_batch t
        left join hrsa_tax_agent t1 on t.payment_organization = t1.id
        WHERE t.delete_type = 0
        <include refid="paramSql"/>
        ORDER BY t.bill_month DESC, t.update_time DESC
    </select>

    <select id="listByTimeRange" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_bill_batch t
        WHERE t.delete_type = 0
        AND t.bill_month between #{minDate} and #{maxDate}
        ORDER BY t.bill_month DESC
    </select>


    <select id="getByBillMonth" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_bill_batch t
        WHERE t.delete_type = 0
        AND t.bill_month = #{billMonth}
        AND t.payment_organization = #{paymentOrganization}
    </select>


    <select id="getByBillStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_bill_batch t
        WHERE t.delete_type = 0
        AND t.bill_month = #{billMonth}
        AND t.bill_status = #{billStatus}
        <if test="paymentOrganization != null">
            AND t.payment_organization = #{paymentOrganization}
        </if>
    </select>


    <delete id="deleteById">
        UPDATE hrsa_bill_batch
        SET delete_type = 1
        WHERE delete_type = 0
          AND id = #{id}
    </delete>


    <update id="updateById" parameterType="com.engine.salary.entity.siaccount.po.InsuranceAccountBatchPO">
        UPDATE hrsa_bill_batch
        <set>
            <if test="accountant != null">
            accountant=#{accountant},
            </if>
            <if test="updateTime != null">
            update_time=#{updateTime},
            </if>
            <if test="socialPay != null">
            social_pay=#{socialPay},
            </if>
            <if test="socialNum != null">
            social_num=#{socialNum},
            </if>
            <if test="fundNum != null">
            fund_num=#{fundNum},
            </if>
            <if test="fundPay != null">
            fund_pay=#{fundPay},
            </if>
            <if test="otherNum != null">
            other_num=#{otherNum},
            </if>
            <if test="otherPay != null">
            other_pay=#{otherPay},
            </if>
            <if test="billStatus != null">
            bill_status=#{billStatus}
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
        <if test="paymentOrganization != null">
            AND payment_organization = #{paymentOrganization}
        </if>
    </update>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrsa_bill_batch t
        WHERE t.delete_type = 0
        AND t.id = #{id}
    </select>

    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_bill_batch t
        WHERE delete_type = 0
    </select>

    <update id="batchUpdate" parameterType="java.util.List">
        update hrsa_bill_batch
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="other_pay =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.otherPay!=null">
                        when id=#{item.id} then #{item.otherPay}
                    </if>
                </foreach>
            </trim>
            <trim prefix="social_pay =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.socialPay!=null">
                        when id=#{item.id} then #{item.socialPay}
                    </if>
                </foreach>
            </trim>
            <trim prefix="fund_pay =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.fundPay!=null">
                        when id=#{item.id} then #{item.fundPay}
                    </if>
                </foreach>
            </trim>
            <trim prefix="delete_type =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.deleteType!=null">
                        when id=#{item.id} then #{item.deleteType}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach collection="collection" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>
</mapper>