<?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.bonus.system.wechat.dao.WechatDao">
    <select id="getWechatList" resultType="com.bonus.system.wechat.entity.WechatBean">
        SELECT *,
        count(a.user_id) AS days,
        CASE
        WHEN a.att_current_day = CURDATE() THEN
        '已考勤'
        ELSE '未考勤'
        END AS isToday
        FROM (SELECT ad.user_id,
        su.user_name,
        ad.org_id,
        att_current_day,
        ad.att_status,
        sd.dict_label,
        ad.error_remake,
        ad.att_current_time,
        ad.att_type
        FROM att_data_update ad
        LEFT JOIN sys_user su ON ad.user_id = su.user_id
        LEFT JOIN sys_user_org suo on su.user_id =  suo.user_id
        LEFT JOIN (SELECT * FROM sys_dict_data WHERE dict_type = 'att_status') sd
        ON ad.att_status = sd.dict_value
        WHERE ad.is_active = '1'
        AND ad.data_source = '3'
        AND sd.dict_value IN ('1', '2', '4', '7', '8', '9', '10', '19')
        <if test="bean.userName!=null and bean.userName!=''">
            AND su.user_name like concat('%', #{bean.userName}, '%')
        </if>

        <if test="bean.startDate!=null and bean.startDate!='' and bean.endDate!=null and bean.endDate!=''">
            AND ad.att_current_day BETWEEN #{bean.startDate} AND #{bean.endDate}
        </if>

        <if test='bean.orgList != null and bean.orgList.size() > 0'>
            and suo.org_id in (
            <foreach collection="bean.orgList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>

        <if test="bean.orgId != null and bean.orgId != ''">
            AND ad.org_id = #{bean.orgId}
        </if>

        <if test="bean.userId!=null and bean.userId!=''">
            and ad.user_id = #{bean.userId}
        </if>

        GROUP BY ad.user_id,
        ad.org_id,
        ad.att_current_day
        ORDER BY ad.att_current_day DESC) a
        GROUP BY a.user_id,
        a.org_id
    </select>

    <select id="getWechatUserList" resultType="com.bonus.system.wechat.entity.WechatBean">
        SELECT
        ad.user_id,
        su.user_name,
        ad.org_id,
        att_current_day,
        ad.toWorkAttStatus,
        ad.toWorkAttAddress,
        ad.toWorkAttCurrentTime,
        sd1.dict_label as status,
        ad.toWorkErrorRemake,
        ad.offWorkAttStatus,
        ad.offWorkAttAddress,
        ad.offWorkAttCurrentTime,
        sd2.dict_label as statusOff,
        ad.offWorkErrorRemake
        FROM
        v_att_update_data ad
        LEFT JOIN sys_user su ON ad.user_id = su.user_id
        LEFT JOIN sys_user_org suo ON su.user_id = suo.user_id
        LEFT JOIN ( SELECT * FROM sys_dict_data WHERE dict_type = 'att_status' ) sd1 ON ad.toWorkAttStatus = sd1.dict_value
        LEFT JOIN ( SELECT * FROM sys_dict_data WHERE dict_type = 'att_status' ) sd2 ON ad.offWorkAttStatus = sd2.dict_value
        WHERE ad.dataSource = '3'
        AND (sd1.dict_value IN ( '1', '2', '4', '7', '8', '9', '10', '19' ) or sd2.dict_value IN ( '1', '2', '4', '7', '8', '9', '10', '19' ) )
        <if test="bean.userId!=null and bean.userId!=''">
            AND ad.user_id = #{bean.userId}
        </if>

        <if test="bean.userName!=null and bean.userName!=''">
            AND su.user_name like concat('%', #{bean.userName}, '%')
        </if>

        <if test="bean.startDate!=null and bean.startDate!='' and bean.endDate!=null and bean.endDate!=''">
            AND ad.att_current_day BETWEEN #{bean.startDate} AND #{bean.endDate}
        </if>

        <if test='bean.orgList != null and bean.orgList.size() > 0'>
            and suo.org_id in (
            <foreach collection="bean.orgList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
        ORDER BY ad.toWorkAttCurrentTime DESC
    </select>
</mapper>