<?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.cftech.shop.qrcode.dao.WxQrcodeMapper">

    <resultMap id="resultMap" type="com.cftech.shop.qrcode.model.WxQrcode">
        <id column="id" property="id"/>
        <result column="org_id" property="orgId"/>
        <result column="number" property="number"/>
        <result column="scene_id" property="sceneId"/>
        <result column="type" property="type"/>
        <result column="ticket" property="ticket"/>
        <result column="bind_id" property="bindId"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </resultMap>

    <resultMap id="resultMapVO" type="com.cftech.shop.qrcode.model.vo.WxQrcodeVO">
        <id column="id" property="id"/>
        <result column="store_name" property="storeName"/>
        <result column="emp_name" property="empName"/>
        <result column="scene_id" property="sceneId"/>
        <result column="type" property="type"/>
        <result column="ticket" property="ticket"/>
        <result column="create_time" property="createTime"/>
        <result column="description" property="description"/>
        <result column="car_name" property="carName"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
       id,
       org_id,
       number,
       scene_id,
       type,
       ticket,
       bind_id,
       accounts_id,
       del_flag,
       status,
       create_time,
       update_time,
       description,
       create_by,
       update_by
    </sql>

    <sql id="sqlColumnsT">
      t.id,
      t.org_id,
      t.number,
      t.scene_id,
      t.type,
      t.ticket,
      t.bind_id,
      t.accounts_id,
      t.del_flag,
      t.status,
      t.create_time,
      t.update_time,
      t.description,
      t.create_by,
      t.update_by
    </sql>

    <insert id="save" parameterType="com.cftech.shop.qrcode.model.WxQrcode" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_shop_wxqrcode
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{orgId, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{sceneId, jdbcType=VARCHAR},
        #{type, jdbcType=VARCHAR},
        #{ticket, jdbcType=VARCHAR},
        #{bindId, jdbcType=BIGINT},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_shop_wxqrcode t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM    t_shop_wxqrcode t
        LEFT JOIN t_qyuser q ON q.id = t.bind_id
        LEFT JOIN t_orgunit o ON o.id = t.bind_id AND t.type='3'
        <include refid="sqlWhere"/>
        <if test="orgId > 0">
            and instr(o.parent_ids,',${orgId},')>0
        </if>

    </select>

    <select id="fetchSearchByPageVO" parameterType="java.util.Map" resultMap="resultMapVO">
        SELECT
        t.id,
        t.ticket,
        t.scene_id sceneId,
        t.type,
        t.create_time createTime,
        t.description,
        q.`name`,
        o.org_name,
        CASE
        WHEN t.type = '1'  OR t.type = '2'  OR t.type = '3' THEN
        q.`name`
        WHEN t.type = '4' THEN
        o.org_name ELSE ''
        END qrcodeNames
        FROM
        t_shop_wxqrcode t
        LEFT JOIN t_qyuser q ON q.id = t.bind_id
        LEFT JOIN t_orgunit o ON o.id = t.bind_id AND t.type = '4'
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumnsT"/>
        FROM t_shop_wxqrcode t
        left join t_orgunit o on o.id = t.org_id
        <include refid="sqlWhere"/>
        <if test="orgId > 0">
            and instr(o.parent_ids,',${orgId},')>0
        </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <update id="update" parameterType="com.cftech.shop.qrcode.model.WxQrcode">
        update t_shop_wxqrcode
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="orgId != null">
                org_id = #{orgId, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="sceneId != null">
                scene_id = #{sceneId, jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type, jdbcType=VARCHAR},
            </if>
            <if test="ticket != null">
                ticket = #{ticket, jdbcType=VARCHAR},
            </if>
            <if test="bindId != null">
                bind_id = #{bindId, jdbcType=BIGINT},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>update_time = now()
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update t_shop_wxqrcode set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="updateBindId" parameterType="com.cftech.shop.qrcode.model.WxQrcode">
        update t_shop_wxqrcode set bind_id = #{bindId, jdbcType=BIGINT} where id=#{id,jdbcType=BIGINT}
    </update>

    <select id="fetchSingleSearchInfo" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_shop_wxqrcode
        <include refid="sqlWhere"/>
         limit 0,1
    </select>

    <select id="fromUser" resultType="com.cftech.shop.qrcode.model.QyUser">
        SELECT t.id, t.name FROM t_qyuser t
        LEFT JOIN t_orgunit o ON t.org_id = o.id
        where t.user_type = #{type}
        <if test=" allergy !=null and allergy != '' "> and o.id = #{allergy} </if>
        <if test=" name !=null and name != '' "> and name like '%${name}%'</if>
        ORDER BY t.create_time
        <if test="pageSize>0">limit #{page}, #{pageSize}</if>
    </select>

    <select id="countUser" resultType="java.lang.Integer">
        SELECT count(1) FROM t_qyuser t
        LEFT JOIN t_orgunit o ON t.org_id = o.id
        where t.user_type = #{type}
        <if test=" allergy !=null and allergy != '' "> and o.id = #{allergy} </if>
        <if test=" name !=null and name != '' "> and name like '%${name}%'</if>
        ORDER BY t.create_time
    </select>

    <select id="fromOrg" resultType="com.cftech.shop.qrcode.model.Orgunit">
        select id, org_name name from t_orgunit
        where FIND_IN_SET((SELECT id FROM t_orgunit WHERE org_name = #{outSide}), parent_ids) != 0 and del_flag = 0
        <if test=" name !=null and name != '' ">AND org_name like '%${name}%' </if>
        ORDER BY LENGTH(parent_ids) ASC, create_time DESC
        <if test="pageSize>0">limit #{page}, #{pageSize}</if>
    </select>

    <select id="countOrg" resultType="java.lang.Integer">
        select count(1) from t_orgunit where FIND_IN_SET((SELECT id FROM t_orgunit WHERE org_name = #{outSide}), parent_ids) != 0 and del_flag = 0
        <if test=" name !=null and name != '' "> AND org_name like '%${name}%' </if>
    </select>
</mapper>