<?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.saye.hospitalgd.mapper.UnilateralMapper">

    <select id="findAllUnilateral" parameterType="HashMap" resultType="HashMap">
        select id
        ,PayType
        ,TranID
        ,H_JYLX
        ,trade_date
        ,HisOperNum
        ,Amount
        ,I_JYJE
        ,I_JYLX
        ,I_DDH
        ,I_JYQD
        ,TradeTime
        ,is_manager
        ,manager_time
        ,err_type
        ,remark
        ,create_time
        ,modify_time
        ,managerType
        from unmanger_unilateral
        <where>
            <if test="startTime!=null and startTime!=''">
                and trade_date &gt;=#{startTime}
            </if>
            <if test="startTime!=null and startTime!=''">
                and trade_date &lt;=#{endTime}
            </if>
            <if test="payType!=null and payType!=''">
                and payType =#{payType}
            </if>
            <if test="thirdPay!=null and thirdPay!=''">
                and I_JYQD =#{thirdPay}
            </if>
            <if test="err_type!=null and err_type!=''">
                and err_type =#{err_type}
            </if>
            <if test="is_manager!=null and is_manager!=''">
                and is_manager=#{is_manager}
            </if>
        </where>
        order by trade_date
    </select>

    <insert id="insertUnilateral" parameterType="HashMap">
        insert into unmanger_unilateral( id
                                       , PayType
                                       , TranID
                                       , H_JYLX
                                       , trade_date
                                       , HisOperNum
                                       , Amount
                                       , I_JYJE
                                       , I_JYLX
                                       , I_DDH
                                       , I_JYQD
                                       , TradeTime
                                       , is_manager
                                       , manager_time
                                       , err_type
                                       , remark
                                       , create_time
                                       , modify_time
                                       , join_id)
        values (#{id}, #{payType}, #{tranID}, #{h_jylx}, #{trade_date}, #{hisOperNum},
                #{amount}, #{i_jyje}, #{i_jylx}, #{i_ddh}, #{i_jyqd}, #{trade_time}, #{is_manager},
                #{manager_time}, #{err_type}, #{remark}, #{create_time}, #{modify_time}, #{join_id})
    </insert>

    <update id="updateUnilateral" parameterType="HashMap">
        update unmanger_unilateral
        set
        <if test="is_manager!=null and is_manager!=''">
            is_manager=#{is_manager},
        </if>
        <if test="manager_time!=null and manager_time!=''">
            manager_time=#{manager_time},
        </if>
        <if test="remark!=null and remark!=''">
            remark=#{remark},
        </if>
        <if test="i_jyje!=null and i_jyje!=''">
            I_JYJE=#{i_jyje},
        </if>
        <if test="amount!=null and amount!=''">
            Amount=#{amount},
        </if>
        <if test="err_type!=null and err_type!=''">
            err_type=#{err_type},
        </if>
        modify_time=#{modify_time}
        where 1=1
        <if test="tranID!=null and tranID!=''">
            and TranID=#{tranID}
        </if>
        <if test="h_jylx!=null and h_jylx!=''">
            and H_JYLX=#{h_jylx}
        </if>
        <if test="i_jylx!=null and i_jylx!=''">
            and I_JYLX=#{i_jylx}
        </if>
        <if test="i_ddh!=null and i_ddh!=''">
            and I_DDH=#{i_ddh}
        </if>
    </update>

    <!--按天查询单边账数量-->
    <select id="findDBZNum" parameterType="HashMap" resultType="HashMap">
        SELECT trade_date, count(1) as num
        FROM unmanger_unilateral
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
        group by trade_date
    </select>

    <!--按支付方式和天查询单边账数量-->
    <select id="findDBZFLNumByTime" parameterType="HashMap" resultType="HashMap">
        SELECT (case when C_ZFFS = '' or c_zffs is null then payType else C_ZFFS end) as payType,
               trade_date,
               count(1)                                                               as num
        FROM unmanger_unilateral
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
        group by payType, trade_date
    </select>

    <delete id="deleteUnilateral" parameterType="HashMap">
        delete
        from unmanger_unilateral
        where trade_date = #{trade_date}
    </delete>

    <select id="findUnilateralByParam" parameterType="HashMap" resultType="HashMap">
        select id
        ,PayType
        ,TranID
        ,H_JYLX
        ,trade_date
        ,HisOperNum
        ,Amount
        ,I_JYJE
        ,I_JYLX
        ,I_DDH
        ,I_JYQD
        ,TradeTime
        ,is_manager
        ,manager_time
        ,err_type
        ,remark
        ,create_time
        ,modify_time
        ,managerType

        from unmanger_unilateral
        where trade_date=#{trade_date}
        <if test="hisopernum!=null and hisopernum!=''">
            and HisOperNum=#{hisopernum}
        </if>
        <if test="h_jylx!=null and h_jylx!=''">
            and H_JYLX =#{h_jylx}
        </if>
        <if test="i_ddh!=null and i_ddh!=''">
            and I_DDH=#{i_ddh}
        </if>
        <!--        <if test="payMethod!=null and payMethod!=''">-->
        <!--            and payMethod=#{payMethod}-->
        <!--        </if>-->
        <if test="payType!=null and payType!=''">
            and PayType=#{payType}
        </if>
        <if test="tranid!=null and tranid!=''">
            and TranID=#{tranid}
        </if>
        <if test="i_jyqd!=null and i_jyqd!=''">
            and I_JYQD=#{i_jyqd}
        </if>
        <if test="i_jylx!=null and i_jylx!=''">
            and I_JYLX=#{i_jylx}
        </if>
        <if test="join_id!=null and join_id!=''">
            and join_id=#{join_id}
        </if>
    </select>

    <delete id="deleteUnilateralByNoManager" parameterType="HashMap">
        delete
        from unmanger_unilateral
        where trade_date = #{trade_date}
          and is_manager = '0'
    </delete>

    <update id="updateUnilateralManagerStatus" parameterType="HashMap">
        update unmanger_unilateral
        set managerType=#{managerType},
            is_manager='1',
            remark=#{remark},
            manager_time=#{manager_time}
        where id = #{id}
    </update>

    <!-- 根据ID更新单边账记录 -->
    <update id="updateUnilateralById" parameterType="HashMap">
        update unmanger_unilateral
        <set>
            <if test="is_manager!=null and is_manager!=''">
                is_manager=#{is_manager},
            </if>
            <if test="manager_time!=null and manager_time!=''">
                manager_time=#{manager_time},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="modify_time!=null and modify_time!=''">
                modify_time=#{modify_time},
            </if>
            <if test="managerType!=null and managerType!=''">
                managerType=#{managerType}
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteUnilateralByJoinIdNotNull" parameterType="HashMap">
        delete
        from unmanger_unilateral
        where trade_date = #{trade_date}
          and join_id is not null
          and join_id != ''
    </delete>

    <delete id="deleteUnilateralByIdandStatus" parameterType="HashMap">
        delete
        from unmanger_unilateral
        where trade_date = #{trade_date}
          and TranID = #{tranID}
          and H_JYLX = #{h_jylx}
    </delete>

    <delete id="deleteUnilateralById" parameterType="HashMap">
        delete from unmanger_unilateral where id = #{id}
    </delete>

    <delete id="deleteBankUnilateralByI_DDH" parameterType="HashMap">
        delete
        from unmanger_unilateral
        where trade_date = #{trade_date}
          and I_DDH = #{i_ddh}
          and err_type = '2'
    </delete>
</mapper>