<?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.ruoyi.web.arap.mapper.ArapRecvMapper">

    <resultMap id="RecvResult" type="com.ruoyi.web.arap.domain.ArapRecv">
        <id property="id" column="id"/>
        <result property="recvDate" column="recv_date"/>
        <result property="recvNo" column="recv_no"/>
        <result property="custNo" column="cust_no"/>
        <result property="custName" column="cust_name"/>
        <result property="recvBy" column="recv_by"/>
        <result property="recvType" column="recv_type"/>
        <result property="bankNo" column="bank_no"/>
        <result property="recvAmt" column="recv_amt"/>
        <result property="deducted" column="is_deduct"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <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="baseSelect">
        select id, recv_date, recv_no, cust_no, cust_name, recv_by, recv_type, bank_no, recv_amt, is_deduct, remark,
               del_flag, org_id, create_by, create_time, update_by, update_time
        from arap_recv
    </sql>

    <select id="selectById" parameterType="Long" resultMap="RecvResult">
        <include refid="baseSelect"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectList" parameterType="com.ruoyi.web.arap.domain.ArapRecv" resultMap="RecvResult">
        <include refid="baseSelect"/>
        <where>
            del_flag = '0'
            <if test="orgId != null and orgId != ''">and org_id = #{orgId}</if>
            <if test="recvNo != null and recvNo != ''">and recv_no = #{recvNo}</if>
            <if test="recvDate != null">and recv_date = #{recvDate}</if>
            <if test="custNo != null and custNo != ''">and cust_no = #{custNo}</if>
            <if test="custName != null and custName != ''">and cust_name like concat('%', #{custName}, '%')</if>
        </where>
        order by id desc
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.arap.domain.ArapRecv" useGeneratedKeys="true" keyProperty="id">
        insert into arap_recv (
            recv_date, recv_no, cust_no, cust_name, recv_by, recv_type, bank_no, recv_amt, is_deduct, remark,
            del_flag, create_by, create_time, update_by, update_time, org_id
        ) values (
            #{recvDate}, #{recvNo}, #{custNo}, #{custName}, #{recvBy}, #{recvType}, #{bankNo}, #{recvAmt}, #{deducted}, #{remark},
            #{delFlag}, #{createBy}, now(), #{updateBy}, now(), #{orgId}
        )
    </insert>

    <update id="updateById" parameterType="com.ruoyi.web.arap.domain.ArapRecv">
        update arap_recv
        <set>
            recv_date = #{recvDate},
            recv_no = #{recvNo},
            cust_no = #{custNo},
            cust_name = #{custName},
            recv_by = #{recvBy},
            recv_type = #{recvType},
            bank_no = #{bankNo},
            recv_amt = #{recvAmt},
            is_deduct = #{deducted},
            remark = #{remark},
            update_by = #{updateBy},
            update_time = now()
        </set>
        where id = #{id}
          and del_flag = '0'
          and org_id = #{orgId}
    </update>

    <update id="deleteById">
        update arap_recv
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = now()
        where id = #{id}
          and del_flag = '0'
          and org_id = #{orgId}
    </update>

    <select id="selectTodayMaxSeq" resultType="java.lang.Integer">
        select max(cast(right(recv_no, 4) as unsigned))
        from arap_recv
        where org_id = #{orgId}
          and date_format(recv_date, '%Y%m%d') = #{dateText}
          and del_flag = '0'
    </select>

</mapper>

