<?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.del_flag= 0
        <if test="bo.cityId != 0 and bo.cityId != null">
            and e.city_id = #{bo.cityId}
        </if>
        <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 == 2">
            HAVING
            satisfaction_rate between 90 and 100
        </if>
        <if test="bo.favorableRate == 3">
            HAVING
            satisfaction_rate between 80 and  90
        </if>
        <if test="bo.favorableRate == 4">
            HAVING
            <![CDATA[ satisfaction_rate <= 80 ]]>
        </if>
        ORDER BY satisfaction_rate desc
    </select>
    <select id="selectEmployeesById" resultType="com.pz.merchant.domain.vo.EmployeesVo">
        SELECT e.id,
               s.avatar,
               e.start_year,
               e.working_hours,
               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
                 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.del_flag = 0
          and e.id = #{id}
        GROUP BY e.id,
                 e.`name`
    </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>
    <select id="selectDataView" resultType="com.pz.system.domain.vo.DataViewVo">
        SELECT count(employees.id) as dataSum
        FROM employees
        left join company on company.id = employees.company_id
        WHERE (employees.`status` = 1 or employees.`status` = 2)
        <if test="dataViewBo.userId != null">
            and company.uid = #{dataViewBo.userId}
        </if>
        <if test="dataViewBo.condition == 2">
            and employees.create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) -- 近7天
        </if>
        <if test="dataViewBo.condition == 3">
            and employees.create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -- 近30天
        </if>
        <if test="dataViewBo.condition == 1">
            and DATE(employees.create_time) = CURDATE()
        </if>
    </select>


</mapper>