<?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.ningdatech.carapi.driver.mapper.StarManageMapper">

    <select id="getAvgStar" resultType="java.lang.Double">
        SELECT avg(current_star) AS avg_star FROM nd_star_manage
        as nsm
        left join nd_company_info c on nsm.company_id = c.id
        left join nd_region r on c.region_id = r.id
        WHERE current_star >=0
        <if test="param.regionIdList != null and param.regionIdList.size >0 ">
            AND nsm.region_Id IN
            <foreach collection="param.regionIdList" item="regionId" index="index" open="("
                     close=")"
                     separator=",">
                #{regionId}
            </foreach>
        </if>
        <if test="param.companyId != null ">
            AND nsm.company_id = #{param.companyId}
        </if>
        <if test="param.companyName != null and param.companyName != '' ">
            AND nsm.company_name LIKE CONCAT('%',#{param.companyName},'%')
        </if>
        <if test="param.driverName != null and param.driverName != '' ">
            AND nsm.driver_name LIKE CONCAT('%',#{param.driverName},'%')
        </if>
        ${param.dataScopeSql}
    </select>
    <select id="countTotalStar" resultType="java.lang.Double">
        SELECT COUNT(*) FROM nd_star_manage
        as nsm
        left join nd_company_info c on nsm.company_id = c.id
        left join nd_region r on c.region_id = r.id
        WHERE current_star >=0
        <if test="param.regionIdList != null and param.regionIdList.size >0 ">
            AND nsm.region_Id IN
            <foreach collection="param.regionIdList" item="regionId" index="index" open="("
                     close=")"
                     separator=",">
                #{regionId}
            </foreach>
        </if>
        <if test="param.companyId != null ">
            AND nsm.company_id = #{param.companyId}
        </if>
        <if test="param.companyName != null and param.companyName != '' ">
            AND nsm.company_name LIKE CONCAT('%',#{param.companyName},'%')
        </if>
        <if test="param.driverName != null and param.driverName != '' ">
            AND nsm.driver_name LIKE CONCAT('%',#{param.driverName},'%')
        </if>
        ${param.dataScopeSql}
    </select>
    <select id="getCompanyAvgStar" resultType="java.lang.Double">
        SELECT avg(current_star) AS avg_star FROM nd_star_manage as nsm
        left join nd_company_info c on nsm.company_id = c.id
        left join nd_region r on c.region_id = r.id
        WHERE nsm.company_id =  #{param.companyId}
        ${param.dataScopeSql}
    </select>
</mapper>
