<?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.AccountReportChangeMapper">

    <insert id="save" parameterType="com.crm.rely.backend.core.pojo.table.AccountReportChangeTable">

    </insert>

    <select id="findOne" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.AccountReportChangeTable">
        select *
        from account_report_change
        where id=#{id}
    </select>

    <select id="countSearch"
            parameterType="com.crm.rely.backend.core.entity.account.belong.AccountReportChangeListEntity"
            resultType="Integer">
        select count(arc.id)
        from account_report_change arc
        inner join user_info fs on arc.from_sales_id = fs.id
        inner join user_info ts on arc.to_sales_id = ts.id
        <where>
            <if test="login != null and login>0">
                and arc.login = #{login}
            </if>
            <if test="startDate != null">
                and arc.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and arc.add_time <![CDATA[ < ]]> #{endDate}
            </if>
            <if test="fromSalesNo != null and fromSalesNo != ''">
                fs.ib_no like concat('%',#{fromSalesNo},'%')
            </if>
            <if test="toSalesNo != null and toSalesNo != ''">
                ts.ib_no like concat('%',#{toSalesNo},'%')
            </if>
        </where>
    </select>

    <select id="listSearch"
            parameterType="com.crm.rely.backend.core.entity.account.belong.AccountReportChangeListEntity"
            resultType="com.crm.rely.backend.core.pojo.view.AccountReportChangeView">
        select arc.*,fs.ib_no from_sales_no,ts.ib_no to_sales_no
        from account_report_change arc
        inner join user_info fs on arc.from_sales_id = fs.id
        inner join user_info ts on arc.to_sales_id = ts.id
        <where>
            <if test="login != null and login>0">
                and arc.login = #{login}
            </if>
            <if test="startDate != null">
                and arc.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and arc.add_time <![CDATA[ < ]]> #{endDate}
            </if>
            <if test="fromSalesNo != null and fromSalesNo != ''">
                fs.ib_no like concat('%',#{fromSalesNo},'%')
            </if>
            <if test="toSalesNo != null and toSalesNo != ''">
                ts.ib_no like concat('%',#{toSalesNo},'%')
            </if>
        </where>
        order by arc.add_time desc
        limit #{page.offset},#{page.row}
    </select>

    <select id="findOneView" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.view.AccountReportChangeView">
        select arc.*,fs.ib_no from_sales_no,ts.ib_no to_sales_no
        from account_report_change arc
        inner join user_info fs on arc.from_sales_id = fs.id
        inner join user_info ts on arc.to_sales_id = ts.id
        where id=#{id}
    </select>
</mapper>