<?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.cos.uploader.dao.MaterialUploadMapper">

    <resultMap id="resultMap" type="com.cftech.cos.uploader.model.Material">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="url" property="url"/>
        <result column="upload_type" property="uploadType"/>
        <result column="key" property="key"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="cos_id" property="cosId"/>
    </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,
      title,
      url,
      upload_type,
      `key`,
      create_by,
      create_time,
      update_by,
      update_time,
      del_flag,
      cos_id,
      categoryid
    </sql>


    <insert id="save" parameterType="com.cftech.cos.uploader.model.Material" useGeneratedKeys="true">
        insert into wx_material
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{title, jdbcType=VARCHAR},
        #{url, jdbcType=VARCHAR},
        #{uploadType, jdbcType=VARCHAR},
        #{key, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        now(),
        #{updateBy, jdbcType=BIGINT},
        now(),
        #{delFlag, jdbcType=VARCHAR},
        #{cosId, jdbcType=BIGINT},
        #{categoryid,jdbcType=BIGINT}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_material
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_material
        <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="countVo" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_material t
        INNER JOIN wx_material_category c on c.id=t.categoryid
        <include refid="sqlWhere"/>
        <if test="pId > 0">
            and instr(c.parent_ids,',${pId},')>0
        </if>
    </select>

    <select id="fetchSearchByPageVo" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        t.*
        FROM wx_material t
        LEFT JOIN wx_material_category c on c.id=t.categoryid
        <include refid="sqlWhere"/>
        <if test="pId > 0">
            and instr(c.parent_ids,',${pId},')>0
        </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="fetchSeaechByNumber" parameterType="java.util.Map" resultType="java.util.Map">
        select substring_index(title,'-', 1) prefix,substring_index(title,'-', -1) number from wx_material w
where substring_index(title,'-', 1)=#{titlePrefix} and del_flag=0
order by substring_index(title,'-', -1) desc
limit 0,1
    </select>

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