<?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="org.jeecg.modules.system.app.mapper.EvaluateMapper">

    <select id="findPage" resultType="org.jeecg.modules.system.app.dto.evaluate.FindEvaluateResponseDTO">
        select ne.id,ns.id as siteId,ns.name as siteName,
            ne.score,ne.evaluate_content,su.avatar,ne.images,ne.check_status,ne.reply_status,ne.reply_content,ne.reply_time,ne.type,ne.create_time,sd.depart_name,ns.address,su.username,su.phone,ne.is_anonymous
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN nm_site ns on  ne.site_id =ns.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        where 1=1
        <if test="findEvaluateRequestDTO!=null and findEvaluateRequestDTO!=''">
            <if test="findEvaluateRequestDTO.orgCode!=null and findEvaluateRequestDTO.orgCode!=''">
                and ne.org_code like CONCAT('',#{findEvaluateRequestDTO.orgCode},'%')
            </if>
        <if test="findEvaluateRequestDTO.deptId!=null and findEvaluateRequestDTO.deptId!=''">
            AND ne.deptId = #{findEvaluateRequestDTO.deptId}
        </if>
        <if test="findEvaluateRequestDTO.score!=null">
            AND ne.score = #{findEvaluateRequestDTO.score}
        </if>
        <if test="findEvaluateRequestDTO.checkStatus!=null">
             AND ne.check_status = #{findEvaluateRequestDTO.checkStatus}
        </if>
        <if test="findEvaluateRequestDTO.replyStatus!=null">
            AND ne.reply_status = #{findEvaluateRequestDTO.replyStatus}
        </if>
        <if test="findEvaluateRequestDTO.endTime!=null  and findEvaluateRequestDTO.endTime!=''">
            <![CDATA[   AND DATE_FORMAT(ne.create_time, '%Y-%m-%d')<=  DATE_FORMAT(#{findEvaluateRequestDTO.endTime}, '%Y-%m-%d') ]]>
        </if>
        <if test="findEvaluateRequestDTO.startTime!=null  and findEvaluateRequestDTO.startTime!=''">
            <![CDATA[   AND DATE_FORMAT(ne.create_time, '%Y-%m-%d')>=  DATE_FORMAT(#{findEvaluateRequestDTO.startTime}, '%Y-%m-%d') ]]>
        </if>
        </if>
        order by ne.create_time desc
    </select>
    <select id="findBySiteId" resultType="org.jeecg.modules.system.app.dto.evaluate.FindEvaluateResponseDTO">
        select ne.id,
               ne.score,ne.evaluate_content,su.avatar,ne.images,ne.check_status,ne.reply_status,ne.reply_content,ne.reply_time,ne.type,ne.create_time,sd.depart_name,ns.address,su.username,su.phone
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN nm_site ns on  ne.site_id =ns.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        where 1=1 and ne.check_status=1 and ne.site_id =#{siteId}


    </select>
    <select id="findByCoursesId"
            resultType="org.jeecg.modules.system.app.dto.evaluate.FindEvaluateResponseDTO">
        select ne.id,
               ne.score,ne.evaluate_content,su.avatar,ne.images,ne.check_status,ne.reply_status,ne.reply_content,ne.reply_time,ne.type,ne.create_time,sd.depart_name,su.username,su.phone
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        left join  nm_order o on ne.order_id = o.id
        where  ne.check_status=1 and o.product_ids in
        <foreach collection="coursesIds" item="coursesId" open="(" separator="," close=")">
            #{coursesId}
        </foreach>
    </select>
    <select id="findByOrderId" resultType="org.jeecg.modules.system.app.entity.Evaluate">
        select * from nm_evaluate where order_id =#{orderId}
    </select>
    <select id="findByAverageScore" resultType="java.lang.Long">
        select sum(ne.score)
                from nm_evaluate ne
                 left join  sys_depart sd on  ne.dept_id = sd.id
                 LEFT JOIN nm_site ns on  ne.site_id =ns.id
                 LEFT JOIN sys_user su on  ne.user_id =su.id
        where 1=1 and  ne.check_status=1 and  ne.site_id =#{siteId}
    </select>
    <select id="findByScoreNum" resultType="java.lang.Long">
        select count(1)
        from nm_evaluate ne
                 left join  sys_depart sd on  ne.dept_id = sd.id
                 LEFT JOIN nm_site ns on  ne.site_id =ns.id
                 LEFT JOIN sys_user su on  ne.user_id =su.id
        where 1=1 and  ne.check_status=1 and  ne.site_id =#{siteId}
    </select>
    <select id="findByAverageScoreByCoursesId" resultType="java.lang.Long">
        select sum(ne.score)
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        left join  nm_order o on ne.order_id = o.id
        where  ne.check_status=1 and  o.product_ids in
        <foreach collection="coursesIds" item="coursesId" open="(" separator="," close=")">
            #{coursesId}
        </foreach>
    </select>
    <select id="findByScoreNumByCoursesId" resultType="java.lang.Long">
        select count(1)
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        left join  nm_order o on ne.order_id = o.id
        where  ne.check_status=1 and  o.product_ids in
        <foreach collection="coursesIds" item="coursesId" open="(" separator="," close=")">
            #{coursesId}
        </foreach>
    </select>
    <select id="findByGoodScoreNum" resultType="java.lang.Long">
        select count(1)
        from nm_evaluate ne
                 left join  sys_depart sd on  ne.dept_id = sd.id
                 LEFT JOIN nm_site ns on  ne.site_id =ns.id
                 LEFT JOIN sys_user su on  ne.user_id =su.id
        where 1=1 and  ne.check_status=1 and  ne.site_id =#{siteId} and score >= 4
    </select>
    <select id="findByGoodScoreNumByCoursesId" resultType="java.lang.Long">
        select count(1)
        from nm_evaluate ne
        left join  sys_depart sd on  ne.dept_id = sd.id
        LEFT JOIN sys_user su on  ne.user_id =su.id
        left join  nm_order o on ne.order_id = o.id
        where  ne.check_status=1 and score >= 4 and  o.product_ids in
        <foreach collection="coursesIds" item="coursesId" open="(" separator="," close=")">
            #{coursesId}
        </foreach>
    </select>
</mapper>