<?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.crm.manager.dao.mapper.SalesSalaryRecordMapper">

    <insert id="save" parameterType="com.crm.rely.backend.core.pojo.table.SalesSalaryRecordTable">
        INSERT INTO `sales_salary_record` (
            `sales_id`,
            `sales_no`,
            `amount`,
            `record_type`,
            `operate_type`,
            `serial`,
            `salary_login`,
            `status`,
            `add_time`,
            `add_user`,
            `add_ip`,
            `note`
        )
        VALUES
        (
         #{salesId},
         #{salesNo},
         #{amount},
         #{recordType},
         #{operateType},
         #{serial},
         #{salaryLogin},
         #{status},
         #{addTime},
         #{addUser},
         #{addIp},
         #{note}
         )
    </insert>

    <select id="findOne" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.view.SalesSalaryRecordView">
        select ssr.*
        from sales_salary_record ssr
        where ssr.id=#{id}
    </select>

    <select id="countSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesSalaryRecordListEntity" resultType="Integer">
        select count(ssr.id)
        from sales_salary_record ssr
        <include refid="whereSql"></include>
    </select>

    <select id="listSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesSalaryRecordListEntity" resultType="com.crm.rely.backend.core.pojo.view.SalesSalaryRecordView">
        select ssr.*
        from sales_salary_record ssr
        <include refid="whereSql"></include>
        order by ssr.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <sql id="whereSql">
        <where>
            <if test="salesNo != null and salesNo != ''">
                and ssr.sales_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and ssr.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and ssr.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
    </sql>
</mapper>