<?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.att.dao.WechatPushDao">

    <insert id="insertWebFace">
        insert into sys_user_face(user_id,applied_face,applied_face_features,new_face,new_face_features,is_check,collection_time,examine_time)
        values(#{userId},#{appliedFace},#{appliedFaceFeatures},#{newFace},#{newFaceFeatures},#{isCheck},#{collectionTime},#{examineTime})
    </insert>
    <insert id="insertWebLeave" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into leave_apply(leave_form_id,submit_id,user_id,user_name,org_id,org_name,type,
        leave_type,leave_reason,
        leave_start_date,
        <if test="leaveStartInterval != null">
            leave_start_interval,
        </if>
        leave_end_date,
        <if test="leaveEndInterval != null">
            leave_end_interval,
        </if>
        leave_duration,examine_user_id,examine_opinion,examine_time,examine_status,host_user_id,
        host_user_name,is_agree,location,source,create_user_id,create_time,update_user_id,update_time,remark,is_plateau,plateau_subsidy)
        values(#{leaveFormId},#{submitId},#{userId},#{userName},#{orgId},#{orgName},#{type},
               #{leaveType},#{leaveReason},
                #{leaveStartDate},
                <if test="leaveStartInterval != null">
                    #{leaveStartInterval},
                </if>
                #{leaveEndDate},
                <if test="leaveEndInterval != null">
                    #{leaveEndInterval},
                </if>
                #{leaveDuration},#{examineUserId},#{examineOpinion},#{examineTime},
               #{examineStatus},#{hostUserId},#{hostUserName},#{isAgree},#{location},#{source},
               #{createUserId},#{createTime},#{updateUserId},#{updateTime},#{remark},#{isPlateau},#{plateauSubsidy})
    </insert>

    <insert id="pushAttDataToQsy">
        <foreach collection="list" item="item" separator=";">
            replace INTO gz_cloud.fc_sup_attendance (
            name,
            id_number,
            org_id,
            pro_id,
            attendance_type,
            attendance_date,
            attendance_time,
            lon,
            lat,
            attendance_external,
            attendance_remark,
            is_wechat
            ) VALUES (
            #{item.name},
            #{item.idNumber},
            #{item.orgId},
            #{item.proId},
            CASE
            WHEN #{item.attType} = '1' THEN '上班'
            WHEN #{item.attType} = '2' THEN '下班'
            ELSE '默认'
            END,
            #{item.attCurrentDay},
            #{item.attCurrentTime},
            #{item.attLon},
            #{item.attLat},
            CASE
            WHEN #{item.isOutsideAtt} = '1' THEN '外勤打卡'
            WHEN #{item.isOutsideAtt} = '2' THEN '正常打卡'
            ELSE '默认'
            END,
            #{item.remark},
            '1'
            )
        </foreach>
    </insert>

    <update id="updateWebPassword">
        update sys_user set password = #{password},update_time = #{updateTime} where user_id = #{userId}
    </update>
    <update id="updateWebOpenId">
        update sys_user set open_id = #{openId} where user_id = #{userId}
    </update>

    <update id="updateWebFace">
        update sys_user_face set new_face = #{newFace},new_face_features = #{newFaceFeatures},is_check = #{isCheck},
                                 collection_time = #{collectionTime},examine_time = #{examineTime}
        where user_id = #{userId}
    </update>
    <update id="updateWechatLeave">
        update leave_apply set
        leave_reason = #{leaveReason},
        leave_start_date = #{leaveStartDate},
        leave_start_interval = #{leaveStartInterval},
        leave_end_date = #{leaveEndDate},
        leave_end_interval = #{leaveEndInterval},
        leave_duration = #{leaveDuration},
        examine_user_id = #{examineUserId},
        examine_opinion = #{examineOpinion},
        examine_time = #{examineTime},
        examine_status = #{examineStatus},
        host_user_id = #{hostUserId},
        host_user_name = #{hostUserName},
        is_agree = #{isAgree},
        location = #{location},
        source = #{source},
        create_user_id = #{createUserId},
        create_time = #{createTime},
        update_user_id = #{updateUserId},
        update_time = #{updateTime},
        remark = #{remark},
        is_plateau = #{isPlateau},
        plateau_subsidy = #{plateauSubsidy}
        where id = #{id}
    </update>

    <select id="getPersonList" resultType="com.bonus.system.api.domain.SysUser">
        SELECT DISTINCT
            su.user_id,
            su.user_name,
            su.phone,
            su.`password`,
            IF
            ( applied_face IS NULL, 1, su.is_face ) AS isFace,
            suo.org_id,
            IF
            ( pd.user_id IS NOT NULL, 1, 0 ) isPd,
            su.update_time,
            su.open_id
        FROM
            sys_user su
                LEFT JOIN sys_user_org suo ON suo.user_id = su.user_id
                AND suo.is_active = 1
                LEFT JOIN (
                SELECT
                    gp.user_id
                FROM
                    att_group g
                        LEFT JOIN att_group_person_relation gp ON g.id = gp.group_id
                WHERE
                    g.is_active = 1
                  AND g.att_type = 2
            ) pd ON pd.user_id = su.user_id
                LEFT JOIN sys_user_face sw ON sw.user_id = su.user_id
        WHERE
            su.is_active = '1'
        GROUP BY
            user_id
    </select>

    <select id="getWebFaceList" resultType="com.bonus.system.att.entity.AttFaceBean">
        SELECT
            *
        FROM
            sys_user_face
    </select>

    <select id="getOrgInfoByUserId" resultType="com.bonus.system.basic.domain.SysOrg">
        SELECT DISTINCT
            user_id,
            org_id AS id,
            org_name
        FROM
            sys_user_org suo
                LEFT JOIN sys_organization so ON so.id = suo.org_id
                AND so.is_active = '1'
        WHERE
            suo.user_id = #{userId}
            LIMIT 1
    </select>
    <select id="getWebLeaveList" resultType="com.bonus.system.evection.entity.EvectionBean">
        SELECT
            *
        from
            leave_apply
        where is_active = '1'
    </select>
    <select id="getOrgInfoByUserName" resultType="com.bonus.system.basic.domain.SysOrg">
        SELECT
            distinct
            ag.user_id,
            suo.org_id AS id,
            so.org_name,
            agt.att_range,
            so.lon,
            so.lat,
            su.id_number
        FROM
            att_group_person_relation ag
                LEFT JOIN sys_user su ON su.user_id = ag.user_id AND su.is_active = '1'
                LEFT JOIN sys_user_org suo ON suo.user_id = su.user_id and suo.is_active = '1'
                LEFT JOIN sys_organization so ON so.id = suo.org_id AND so.is_active = '1'
                LEFT JOIN att_group_setting agt on agt.group_id = ag.group_id and agt.is_active = '1'

        WHERE
            su.user_name = #{userName}
        LIMIT 1
    </select>
    <select id="getProOrgToQsy" resultType="com.bonus.system.index.entity.MapBean">
        SELECT
            If(PROJECT_ID is null,0,PROJECT_ID) AS proId,
            If(ORG_ID is null,0,ORG_ID) AS orgId
        FROM
            gz_cloud.bm_sup_attend_history
        WHERE
            ID_NUMBER = #{idNumber}
        limit 1
    </select>
    <select id="getMonthCheckList" resultType="com.bonus.system.holiday.entity.WorkReportBean">
        SELECT
            *
        FROM
            leave_check
        WHERE
            is_active = '1'
    </select>


</mapper>