<?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.pz.merchant.mapper.EmployeesMapper">

    <resultMap type="com.pz.merchant.domain.Employees" id="EmployeesResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="startYear" column="startYear"/>
        <result property="uid" column="uid"/>
        <result property="workingHours" column="working_hours"/>
        <result property="status" column="status"/>
        <result property="companyId" column="company_id"/>
        <result property="cardFront" column="card_front"/>
        <result property="cardReverse" column="card_reverse"/>
        <result property="healthCertificate" column="health_certificate"/>
        <result property="lng" column="lng"/>
        <result property="lat" column="lat"/>
        <result property="cityId" column="city_id"/>
        <result property="nowType" column="now_type"/>
        <result property="totalRevenue" column="total_revenue"/>
        <result property="balance" column="balance"/>
        <result property="freezeBalance" column="freeze_balance"/>
        <result property="killOrder" column="kill_order"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>
    <select id="selectEmployeesList" resultType="com.pz.merchant.domain.vo.EmployeesListVo">
        select employees.id as emId,
        employees.name,
        employees.working_hours,
        user.avatar
        from employees
        left join sys_user as user on user.user_id = employees.uid
        <where>
            <if test="ew.emptyOfWhere == false">
                and ${ew.sqlSegment}
            </if>
        </where>
    </select>

    <select id="selectEmployeesInfoById" resultType="com.pz.merchant.domain.vo.TodayOrderListVo">
        select employees.id as emId,
        employees.name,
        employees.working_hours,
        employees.status,
        year(current_date) - ifnull(employees.start_year, 0) as workYear,
        employees.now_type,
        user.avatar,
        concat(ifnull(round((sum(if(total_order.is_satisfaction = 1, 1, 0)) / count(total_order.id)) * 100, 2),0), '%')
        as favorableRate
        from employees
        left join sys_user as user on user.user_id = employees.uid
        left join total_order on total_order.em_id = employees.id and total_order.business_id != 0
        <where>
            <if test="id != null and id > 0">
                employees.id = #{id}
            </if>
        </where>
        group by employees.id
    </select>

    <select id="selectEmployees" resultType="com.pz.merchant.domain.vo.EmployeesVo">
        SELECT e.id,
        s.avatar as avatar,
        e.name,
        e.start_year,
        e.working_hours,
        ifnull(round((sum(if(o.is_satisfaction = 1, 1, 0)) / count(o.id)) * 100, 2), 0) AS satisfaction_rate
        FROM employees e
        left join total_order o on o.em_id = e.id
        left join company c on c.id = e.company_id
        left join sys_user s on s.user_id = e.uid
        WHERE e.`status` = 1 and e.now_type= 1 and e.del_flag= 0
        <if test="bo.companyId != 0">
            and e.company_id = #{bo.companyId}
        </if>
        GROUP BY
        e.id,
        e.`name`
        <!--<if test="bo.favorableRate = 0">
            HAVING
            satisfaction_rate >= #{bo.favorableRate};
        </if>-->
        <if test="bo.favorableRate == 1">
            HAVING
            satisfaction_rate &gt;= 90 and satisfaction_rate &lt;= 100
        </if>
        <if test="bo.favorableRate == 2">
            HAVING
            satisfaction_rate &gt;= 80 and satisfaction_rate &lt;= 90
        </if>
        <if test="bo.favorableRate == 3">
            HAVING
            satisfaction_rate &lt;= 80
        </if>
        ORDER BY satisfaction_rate desc
    </select>
    <select id="selectEmployeesById" resultType="com.pz.merchant.domain.vo.EmployeesVo">
        SELECT e.id,
               s.avatar,
               e.name,
               e.working_hours,
               ifnull(round((sum(if(o.is_satisfaction = 1, 1, 0)) / count(o.id)) * 100, 2), 0) AS satisfaction_rate
        FROM employees e
                 INNER join total_order o on o.em_id = e.id
                 left join company c on c.id = e.company_id
                 left join sys_user s on s.user_id = e.uid
        WHERE e.`status` = 1
          and e.now_type = 1
          and e.del_flag = 0
          and e.id = #{id}
        GROUP BY e.id,
                 e.`name`
        HAVING satisfaction_rate >= #{favorableRate};
    </select>

    <select id="selectOrder" resultType="com.pz.merchant.domain.vo.OrderInfoVO">
        select total_order.id as orderId,
        total_order.status as orderStatus,
        services.name as project,
        services.cover,
        services.price,
        total_order.is_satisfaction as evaluation_flag,
        employees.name as emName,
        CONVERT((1 - services.fenmo / 100), decimal(10, 2)) * CONVERT(services.price, decimal(10, 2)) as commission,
        total_order.business_id as businessId,
        business.name as businessName
        from total_order
        left join business on total_order.business_id = business.id
        left join employees on total_order.em_id = employees.id
        left join services on total_order.service_id = services.id
        <where>
            <if test="ew.emptyOfWhere == false">
                and ${ew.sqlSegment}
            </if>
        </where>
    </select>


</mapper>