<?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.gic.haoban.manage.service.dao.mapper.OpenStaffMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabOpenStaff">
        <id column="open_staff_id" javaType="long" jdbcType="BIGINT" property="openStaffId"/>
        <result column="wx_enterprise_id" javaType="String" jdbcType="CHAR" property="wxEnterpriseId"/>
        <result column="enterprise_id" property="enterpriseId"/>
        <result column="staff_id" javaType="String" jdbcType="CHAR" property="staffId"/>
        <result column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="uuid" javaType="String" jdbcType="VARCHAR" property="uuid"/>
        <result column="delete_flag" javaType="int" jdbcType="INTEGER" property="deleteFlag"/>
        <result column="status_flag" javaType="int" jdbcType="INTEGER" property="statusFlag"/>
        <result column="step" javaType="int" jdbcType="INTEGER" property="step"/>
        <result column="qw_user_id" javaType="long" jdbcType="BIGINT" property="qwUserId"/>
        <result column="wx_corp_id" javaType="long" jdbcType="BIGINT" property="wxCorpId"/>
        <result column="qr_code_1" javaType="String" jdbcType="VARCHAR" property="qrCode1"/>
        <result column="key_1" javaType="String" jdbcType="VARCHAR" property="key1"/>
        <result column="qr_code_2" javaType="String" jdbcType="VARCHAR" property="qrCode2"/>
        <result column="key_2" javaType="String" jdbcType="VARCHAR" property="key2"/>
        <result column="valid_flag" javaType="int" jdbcType="INTEGER" property="validFlag"/>
        <result column="auth_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="authTime"/>
        <result column="first_auth_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="firstAuthTime"/>
        <result column="valid_qrcode_flag" property="validQrcodeFlag"/>
        <result column="login_remark" property="loginRemark" />
    </resultMap>

    <sql id="Base_Column_List">
        open_staff_id
        , wx_enterprise_id,enterprise_id, staff_id, create_time, update_time, uuid, delete_flag,status_flag, step, qw_user_id, wx_corp_id, qr_code_1, key_1,
	    qr_code_2, key_2, valid_flag, auth_time,first_auth_time , valid_qrcode_flag , login_remark
    </sql>

    <insert id="insert" parameterType="com.gic.haoban.manage.service.dao.mapper.OpenStaffMapper">
            <![CDATA[
        INSERT INTO tab_haoban_open_staff(open_staff_id, wx_enterprise_id, staff_id, create_time, update_time, uuid,
                                          delete_flag, status_flag, step, qw_user_id, wx_corp_id, qr_code_1, key_1,
                                          qr_code_2, key_2, valid_flag, auth_time, first_auth_time, enterprise_id,
                                          valid_qrcode_flag)
        VALUES (#{openStaffId,jdbcType=BIGINT}, #{wxEnterpriseId,jdbcType=CHAR}, #{staffId,jdbcType=CHAR},
                #{createTime,jdbcType=TIMESTAMP},
                #{updateTime,jdbcType=TIMESTAMP}, #{uuid,jdbcType=VARCHAR}, #{deleteFlag,jdbcType=INTEGER},
                #{statusFlag,jdbcType=INTEGER},
                #{step,jdbcType=INTEGER}, #{qwUserId,jdbcType=BIGINT}, #{wxCorpId,jdbcType=BIGINT},
                #{qrCode1,jdbcType=VARCHAR},
                #{key1,jdbcType=VARCHAR}, #{qrCode2,jdbcType=VARCHAR}, #{key2,jdbcType=VARCHAR},
                #{validFlag,jdbcType=INTEGER},
                #{authTime,jdbcType=TIMESTAMP}, #{firstAuthTime,jdbcType=TIMESTAMP}, #{enterpriseId},
                #{validQrcodeFlag})
        ]]>
    </insert>

    <update id="update" parameterType="com.gic.haoban.manage.service.entity.TabOpenStaff">
        update tab_haoban_open_staff set uuid = #{uuid} , status_flag=#{statusFlag},
        step=#{step}, qr_code_1=#{qrCode1}, key_1=#{key1}, qr_code_2=#{qrCode2}, key_2=#{key2}, valid_flag=#{validFlag},
        valid_qrcode_flag = #{validQrcodeFlag} ,
        <if test="null != qwUserId">
            qw_user_id=#{qwUserId,jdbcType=BIGINT},
        </if>
        <if test="null != wxCorpId">
            wx_corp_id=#{wxCorpId,jdbcType=BIGINT},
        </if>
        <if test="null != authTime">
            auth_time=#{authTime,jdbcType=TIMESTAMP},
        </if>
        <if test="null != firstAuthTime">
            first_auth_time=#{firstAuthTime} ,
        </if>
        <if test="null != loginRemark">
            login_remark = #{loginRemark} ,
        </if>
        update_time=now()
        where open_staff_id = #{openStaffId}
    </update>

    <update id="updateValidFlag">
        update tab_haoban_open_staff
        set valid_flag = 1
        where uuid = #{uuid}
    </update>

    <update id="update2QrcodeFlag">
        update tab_haoban_open_staff
        set valid_qrcode_flag = 1
        where id = #{id}
    </update>

    <update id="update2Qrcode">
        update tab_haoban_open_staff
        set qr_code_2 = #{qrCode},
            key_2     = #{key}
        where id = #{id}
    </update>

    <update id="updateLoginStep">
        update tab_haoban_open_staff
        set step = #{step}
        where open_staff_id = #{id}
    </update>

    <delete id="deleteByPrimaryKey">
        update tab_haoban_open_staff
        set delete_flag = 1,
            update_time =now()
        where open_staff_id = #{id}
    </delete>

    <select id="getByStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_open_staff where staff_id = #{staffId} and delete_flag = 0
    </select>

    <select id="getById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_open_staff where open_staff_id = #{id} and delete_flag = 0
    </select>

    <select id="getByUUID" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_open_staff where uuid = #{uuid} and delete_flag = 0
    </select>

    <select id="list" resultType="com.gic.haoban.manage.api.dto.OpenStaffListDTO"  parameterType="com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO">
        select a.open_staff_id  openStaffId  ,
               a.wx_enterprise_id  wxEnterpriseId  ,
               a.enterprise_id  enterpriseId  ,
               a.staff_id  staffId  ,
               a.create_time  createTime  ,
               a.update_time  updateTime  ,
               a.uuid  uuid  ,
               a.delete_flag  deleteFlag  ,
               a.status_flag  statusFlag  ,
               a.step  step  ,
               a.qw_user_id  qwUserId  ,
               a.wx_corp_id  wxCorpId  ,
               a.qr_code_1  qrCode1  ,
               a.key_1  key1  ,
               a.qr_code_2  qrCode2  ,
               a.key_2  key2  ,
               a.valid_flag  validFlag  ,
               a.auth_time  authTime  ,
               a.first_auth_time  firstAuthTime  ,
               a.valid_qrcode_flag  validQrcodeFlag  ,
               a.login_remark  loginRemark  ,
               b.staff_name staffName ,
               b.wx_user_id wxUserId ,
               b.wx_open_user_id wxOpenUserId
        from tab_haoban_open_staff a left JOIN tab_haoban_staff b on a.staff_id = b.staff_id
        where a.wx_enterprise_id = #{wxEnterpriseId} and a.enterprise_id=#{enterpriseId}
              <if test="status==1">
                  and a.status_flag = 1
              </if>
            <if test="status==2">
                and a.status_flag = 2 and  a.first_auth_time is not  null
            </if>
            <if test="status==3">
                and a.status_flag = 2 and a.valid_qrcode_flag = 1
            </if>
            <if test="status==4">
                and a.status_flag = 2 and  a.first_auth_time is null
            </if>

            <if test="staffParams != null and staffParams != ''">
                and (b.staff_name like CONCAT('%',#{staffParams},'%') or b.wx_user_id like CONCAT('%',#{staffParams},'%') or b.wx_open_user_id like CONCAT('%',#{staffParams},'%') )
            </if>

            <if test="null != startDate">
                and a.create_time <![CDATA[>=]]> #{startDate}
            </if>
            <if test="null != endDate">
                and a.create_time <![CDATA[<=]]> #{endDate}
            </if>

            <if test="staffIds != null and staffIds.size >0">
                and a.staff_id IN
                <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
                    #{id,jdbcType=VARCHAR}
                </foreach>
            </if>

        order by a.create_time desc
    </select>

    <select id="getByQwUserId"  resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_open_staff where qw_user_id = #{qwUserId} and delete_flag = 0
    </select>

    <select id="listAllStaffId" resultType="java.lang.String">
        select staff_id from tab_haoban_open_staff where wx_enterprise_id = #{wxEnterpriseId} and delete_flag = 0
    </select>

    <select id="listOpenStaffByClerk" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select a.staff_id staffId , b.clerk_id clerkId , b.store_id storeId from tab_haoban_open_staff a left join tab_haoban_staff_clerk_relation b
           on a.staff_id = b.staff_id and b.status_flag = 1
        where a.wx_enterprise_id = #{wxEnterpriseId} and a.enterprise_id = #{enterpriseId}  and a.status_flag = 1
            and b.clerk_id in
        <foreach collection="list" index="idex" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </select>
    <select id="listOpenStaffByStore" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select a.staff_id staffId , b.clerk_id clerkId , b.store_id storeId from tab_haoban_open_staff a left join tab_haoban_staff_clerk_relation b
        on a.staff_id = b.staff_id and b.status_flag = 1
        where a.wx_enterprise_id = #{wxEnterpriseId} and a.enterprise_id = #{enterpriseId}  and a.status_flag = 1
        and b.store_id in
        <foreach collection="list" index="idex" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </select>
    <select id="listAll" resultType="java.lang.String">
        select staff_id from tab_haoban_open_staff where status_flag = 1
    </select>


</mapper>