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

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.core.entity.system.qa.SysQaSearchPageEntity">

        select count(distinct qa.id)
        from sys_qa qa
        <if test="roleId != null ">
            left join user_role_qa urqa on urqa.qa_id=qa.id
        </if>
        <include refid="selectCondition"></include>

    </select>

    <select id="pageList" resultType="com.crm.rely.backend.core.pojo.table.SysQaTable"
            parameterType="com.crm.rely.backend.core.entity.system.qa.SysQaSearchPageEntity">

        select qa.* from sys_qa qa
        <if test="roleId != null ">
            left join user_role_qa urqa on urqa.qa_id=qa.id
        </if>
        <include refid="selectCondition"></include>

        order by qa.add_time desc
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>


    <sql id="selectCondition">
        <where>
            <if test="roleId != null ">
                and urqa.role_id=#{roleId}
            </if>
            <if test="subject != null and subject != ''">
                and qa.subject like binary concat('%',#{subject},'%')
            </if>
            <if test="lang != null and lang != ''">
                and qa.`lang`=#{lang}
            </if>
            <if test="startDate != null">
                and qa.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and qa.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
    </sql>
</mapper>