<?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_orgunit o on o.id = t.org_id
        <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 id, s.name store_name, u.name emp_name,c.name car_name, t.scene_id scene_id,
        t.type type, t.ticket ticket, t.create_time create_time, t.description description
        FROM t_shop_wxqrcode t
        left join t_dc_store s on s.id = t.bind_id and t.type='0'
        left join t_qyuser u on u.id = t.bind_id and t.type='1'
        LEFT JOIN t_fours_store_car sc on sc.id=t.bind_id and t.type='2'
        LEFT JOIN t_fours_base_cars c on c.id=sc.carid
        <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>


</mapper>