<?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="cn.com.countrygarden.bi.main.dao.wcx.WcxUserMapper">
    <!-- todo 兼容 -->
    <select id="selectPageData" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxUserDto">
        SELECT
        u.open_id          openId,
        u.head_pic_url     wechatUrl,
        u.employee_name    wechatName,
        u.create_date      createDate,
        u.employee_name    userName,
        u.employee_phone   userPhone,
        u.employee_card_id employeeCardId,
        u.employee_id      employeeId,
        u.user_id          userId,
        r.role_name        roleName
        FROM wcx_user u
        LEFT JOIN wcx_role r ON r.role_id = u.role_id
        <where>
            <if test="userName != null and userName != ''">
                u.employee_name like concat('%', #{userName}, '%')
            </if>
            <if test="roleId != null and roleId != ''">
                u.role_id = #{roleId}
            </if>
        </where>
        limit #{pageOffset}, #{pageSize}
    </select>

    <select id="selectPageCount" resultType="int">
        SELECT
        count(*)
        FROM wcx_user u
        <where>
            <if test="userName != null and userName != ''">
                u.employee_name like concat('%', #{userName}, '%')
            </if>
            <if test="roleId != null and roleId != ''">
                u.role_id = #{roleId}
            </if>
        </where>
    </select>

    <select id="selectPageData2" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxUserDto">
        SELECT
        u.openid     openId,
        u.wechaturl  wechatUrl,
        u.wechatname wechatName,
        u.createdate createDate,
        p.name       userName,
        p.mobile     userPhone,
        p.BID        employeeId
        FROM performance_app.bd_psndoc p
        LEFT JOIN biapp.wechat_user u ON u.employeeid = p.BID
        <where>
            <if test="userName != null and userName != ''">
                p.name like concat('%', #{userName}, '%')
            </if>
            <if test="roleId != null and roleId != ''">
                u.roleId = #{roleId}
            </if>
        </where>
        limit #{pageOffset}, #{pageSize}
    </select>

    <select id="selectPageCount2" resultType="int">
        SELECT
        COUNT(*)
        FROM performance_app.bd_psndoc p
        LEFT JOIN biapp.wechat_user u ON u.employeeId = p.BID
        <where>
            <if test="userName != null and userName != ''">
                p.name like concat('%', #{userName}, '%')
            </if>
            <if test="roleId != null and roleId != ''">
                u.roleId = #{roleId}
            </if>
        </where>
    </select>

    <update id="updateEmpRole">
        UPDATE wcx_user SET role_id = #{roleId} WHERE user_id = #{userId}
    </update>

    <select id="selectAllJoinNumber" resultType="java.lang.Long">
        SELECT COUNT(tem.user_id) FROM ( SELECT user_id
        FROM wcx_innovation
        WHERE create_date <![CDATA[>=]]> #{startDate} and create_date <![CDATA[<=]]> #{endDate}
        GROUP BY user_id) tem
    </select>
    <!-- 排除掉 baseline 7 -->
    <select id="selectAllJoinNumberExclude" resultType="java.lang.Long">
        SELECT COUNT(tem.user_id) FROM ( SELECT user_id
        FROM wcx_innovation
        WHERE create_date <![CDATA[>=]]> #{startDate} and create_date <![CDATA[<=]]> #{endDate} and baseline !='7'
        GROUP BY user_id) tem
    </select>

    <resultMap id="BaseUserDtoMap" type="cn.com.countrygarden.bi.main.dto.wcx.WcxUserDto">
        <id column="user_id" property="userId"/>
        <result column="open_id" property="openId"/>
        <result column="head_pic_url" property="headPicUrl"/>
        <result column="nick_name" property="nickName"/>
        <result column="role_id" property="roleId"/>
        <result column="employee_id" property="employeeId"/>
        <result column="employee_name" property="employeeName"/>
        <result column="employee_phone" property="employeePhone"/>
        <result column="employee_card_id" property="employeeCardId"/>
        <result column="create_date" property="createDate"/>
        <result column="role_name" property="roleName"/>
    </resultMap>

    <select id="findBestPutPoint" resultMap="BaseUserDtoMap">
        SELECT
          u.*
        FROM (
            SELECT count(t.user_id) thumbNum,t.user_id
            FROM wcx_thumb t
            WHERE t.create_date <![CDATA[>=]]> #{startDate} and t.create_date <![CDATA[<=]]> #{endDate}
            GROUP BY t.user_id) allUser
        JOIN wcx_user u ON u.user_id = allUser.user_id
        WHERE allUser.thumbNum = #{thumbNum}
    </select>

    <select id="findBestNumberInn" resultMap="BaseUserDtoMap">
        SELECT
          u.*
        FROM (
            SELECT COUNT(i.innovation_id) innNum, i.user_id
            FROM wcx_innovation i
            WHERE i.create_date <![CDATA[>=]]> #{startDate} AND i.create_date <![CDATA[<=]]> #{endDate}
            GROUP BY i.user_id
        ) allUser
        JOIN wcx_user u ON u.user_id = allUser.user_id AND allUser.innNum = #{innNum}
    </select>

    <select id="findUserIdsInPrizeItem" resultType="string">
        SELECT pi.prize_user_id
        FROM wcx_prize p
        JOIN wcx_prize_item pi ON p.prize_id = pi.prize_id
        WHERE p.prize_type in ('5', '7')
    </select>

    <select id="findUserIdsInPrizeItemOfStatus" resultType="string">
        SELECT pi.prize_user_id
        FROM wcx_prize p
        JOIN wcx_prize_item pi ON p.prize_id = pi.prize_id and p.status = #{itemStatus}
        WHERE p.prize_type in ('5', '7')
    </select>

    <select id="findHeadPicUrlByPrimaryKey" resultType="string">
        SELECT u.head_pic_url
        FROM wcx_user u
        where u.user_id = #{userId}
    </select>

    <!--<select id="findUserLeaderBestJoin" resultMap="BaseUserDtoMap">-->
    <!--SELECT u.*-->
    <!--FROM (-->
    <!--SELECT t.user_id, count(t.thumb_id) countNum-->
    <!--FROM wcx_thumb t-->
    <!--JOIN wcx_user u on u.user_id = t.user_id AND t.create_date <![CDATA[>=]]> #{startDate} and t.create_date <![CDATA[<=]]> #{endDate}-->
    <!--JOIN wcx_role r on r.role_type = '2' and u.role_id = r.role_id-->
    <!--GROUP BY t.user_id-->
    <!--) temp-->
    <!--JOIN wcx_user u ON u.user_id = temp.user_id-->
    <!--WHERE temp.countNum = #{thumbCountNum}-->
    <!--</select>-->

    <select id="findUserLeaderBestJoin" resultMap="BaseUserDtoMap">
        SELECT u.*
        FROM (
           SELECT t.user_id, count(t.thumb_number) thumbCountNum
            FROM wcx_thumb t
            JOIN wcx_user u on u.user_id = t.user_id AND t.create_date <![CDATA[>=]]> #{startDate} and t.create_date <![CDATA[<=]]> #{endDate}
            WHERE t.role_id IN (SELECT tr.role_id FROM wcx_role tr WHERE tr.role_type = '2')
            GROUP BY t.user_id
        ) temp
        JOIN wcx_user u ON u.user_id = temp.user_id
        WHERE temp.thumbCountNum >= #{thumbThumbNumMin}
    </select>

    <select id="selectDocPageData" resultMap="BaseUserDtoMap">
        SELECT
            temp.wechatName,
            temp.userName,
            temp.employee_card_id,
            r.role_name
        FROM (
            SELECT
            emp.NAME    wechatName,
            emp.NAME    userName,
            emp.ID      employee_card_id
            FROM performance_app.bd_psndoc emp
            WHERE
            <if test="userName != null and userName != ''">
                emp.`NAME` like concat(concat('%',#{userName}),'%')
            </if>
            GROUP BY emp.ID, emp.NAME
            limit #{pageOffset}, #{pageSize}
            ) temp
        LEFT JOIN wcx_user u ON u.employee_card_id = temp.employee_card_id
        LEFT JOIN wcx_role r ON r.role_id = u.role_id
    </select>

    <select id="selectDocPageCount" resultType="long">
        SELECT COUNT(tem.employeeCardId) FROM (SELECT
        emp.ID      employeeCardId
        FROM performance_app.bd_psndoc emp
        WHERE
        <if test="userName != null and userName != ''">
            emp.`NAME` like concat(concat('%',#{userName}),'%')
        </if>
        GROUP BY emp.ID, emp.NAME) tem
    </select>

    <select id="findByEmpCardId" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxUserDto">
        SELECT user_id userId, open_id openId, employee_card_id employeeCardId ,nick_name nickName ,role_id  roleId
        FROM wcx_user
        WHERE employee_card_id = #{empCardId}
        limit 1
    </select>

    <insert id="insertOne">
        insert into wcx_user
        (
        user_id,
        open_id,
        head_pic_url,
        nick_name,
        role_id,
        employee_id,
        employee_name,
        employee_phone,
        employee_card_id,
        status,
        create_date
        )
        values
        (
        #{userId},
        #{openId},
        #{headPicUrl},
        #{nickName},
        #{roleId},
        #{employeeId},
        #{employeeName},
        #{employeePhone},
        #{employeeCardId},
        #{status},
        #{createDate}
        )
    </insert>

    <select id="selectEmpIdByNameAndCardId" resultType="cn.com.countrygarden.bi.main.model.wcx.ErpEmp">
        SELECT BID employeeId, NAME employeeName, ID employeeCardId
        FROM performance_app.bd_psndoc emp
        WHERE emp.ID = #{employeeCardId}
        <if test="employeeName != null">
            AND emp.`NAME` = #{employeeName}
        </if>
        order BY TS DESC, BID DESC
        LIMIT 1
    </select>


    <select id="findRoleId" resultType="String">
         select role_id as roleId from wcx_role where role_code='admin'
    </select>
    <update id="updateUserSug" >
         UPDATE wcx_user SET nick_name = #{nickName} WHERE user_id = #{userId}
    </update>

    <select id="selectWcxUser" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxUserDto">
        SELECT
        u.open_id          openId,
        u.head_pic_url     wechatUrl,
        u.employee_name    wechatName,
        u.create_date      createDate,
        u.employee_name    userName,
        u.employee_phone   userPhone,
        u.employee_card_id employeeCardId,
        u.employee_id      employeeId,
        u.user_id          userId
        FROM wcx_user u
        <where>
            <if test="userName != null and userName != ''">
                u.employee_name like concat('%', #{userName}, '%')
            </if>
            <if test="nickName != null and nickName != ''">
                u.nick_name = #{nickName}
            </if>
        </where>
        limit #{pageOffset}, #{pageSize}
    </select>


    <select id="selectWcxUserCount" resultType="int">
        SELECT
        count(*)
        FROM wcx_user u
        <where>
            <if test="userName != null and userName != ''">
                u.employee_name like concat('%', #{userName}, '%')
            </if>
            <if test="nickName != null and nickName != ''">
                u.nick_name = #{nickName}
            </if>
        </where>
    </select>

    <select id="findRoleName" resultType="String">
        select  role_name    from   wcx_role  where role_code=#{roleCode}
    </select>

    <update id="deleteUserNickName" >
        update wcx_user set nick_name = null where employee_card_id = #{empCardId}
    </update>
</mapper>
