<?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.base.appmenu.dao.AppmenuMapper">

    <resultMap id="resultMap" type="com.cftech.base.appmenu.model.Appmenu">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="icon" property="icon"/>
        <result column="color" property="color"/>
        <result column="number" property="number"/>
        <result column="sortno" property="sortno"/>
        <result column="position_ids" property="positionIds"/>
        <result column="position_names" property="positionNames"/>
        <result column="app" property="app"/>
        <result column="link" property="link"/>
    </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, name, accounts_id, status, create_time, update_time, description, del_flag, create_by, update_by, icon, color, number, sortno, position_ids, position_names, app, link
	</sql>

    <insert id="save" parameterType="com.cftech.base.appmenu.model.Appmenu" useGeneratedKeys="true" keyProperty="id">
		insert into t_appmenu
		(
		id, name, accounts_id, status, create_time, update_time, description, del_flag, create_by, update_by, icon, color, number, sortno, position_ids, position_names, app, link
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{name, jdbcType=VARCHAR},  #{accountsId, jdbcType=BIGINT},  #{status, jdbcType=VARCHAR},  
		now(),  now(),  #{description, jdbcType=VARCHAR},  #{delFlag, jdbcType=TINYINT},  
		#{createBy, jdbcType=BIGINT},  #{updateBy, jdbcType=BIGINT},  #{icon, jdbcType=VARCHAR},  #{color, jdbcType=VARCHAR},  
		#{number, jdbcType=VARCHAR},  #{sortno, jdbcType=INTEGER},  #{positionIds, jdbcType=VARCHAR},  #{positionNames, jdbcType=VARCHAR}, #{app, jdbcType=TINYINT},
		#{link, jdbcType=VARCHAR}
		)
	</insert>

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

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

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_appmenu t
        <include refid="sqlWhere"/>
        <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.base.appmenu.model.Appmenu">
        update t_appmenu
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="name != null">
                name = #{name, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time=now(),
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="icon != null">
                icon = #{icon, jdbcType=VARCHAR},
            </if>
            <if test="color != null">
                color = #{color, jdbcType=VARCHAR},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="sortno != null">
                sortno = #{sortno, jdbcType=INTEGER},
            </if>
            <if test="positionIds != null">
                position_ids = #{positionIds, jdbcType=VARCHAR},
            </if>
            <if test="positionNames != null">
                position_names = #{positionNames, jdbcType=VARCHAR},
            </if>
            <if test="app != null">
                app = #{app, jdbcType=TINYINT},
            </if>
            <if test="link != null">
                link = #{link, jdbcType=VARCHAR},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <!-- 根据微信账号和职位和微信应用的Id，查找满足条件的应用菜单信息 -->
    <select id="findByAccountsIdAndPositionIdAndApp" parameterType="java.util.Map"
            resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM
        t_appmenu
        WHERE
        instr(position_ids, '${positionId},') > 0
        AND accounts_id = #{accountsId}
        and app = #{app}
        and del_flag = 0
        order by sortno asc
    </select>
</mapper>