<?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.ruoyi.ss.storeApply.mapper.StoreApplyMapper">

    <resultMap type="StoreApplyVO" id="StoreApplyResult" autoMapping="true" >
        <result property="oldData" column="old_data" typeHandler="com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler"/>
        <result property="newData" column="new_data" typeHandler="com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler"/>
    </resultMap>

    <sql id="selectStoreApplyVo">
        select
            ssa.id,
            ssa.apply_type,
            ssa.status,
            ssa.store_id,
            ssa.create_time,
            ssa.user_id,
            ssa.verify_id,
            ssa.verify_time,
            ssa.verify_remark,
            ssa.old_data,
            ssa.new_data,
            ss.name as store_name,
            su.user_name as user_name,
            sys_u.user_name as verify_name
        from <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        ss_store_apply ssa
            left join sm_store ss on ss.store_id = ssa.store_id
            left join sm_user su on su.user_id = ssa.user_id
            left join sys_user sys_u on sys_u.user_id = ssa.verify_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and ssa.id = #{query.id}</if>
        <if test="query.applyType != null  and query.applyType != ''"> and ssa.apply_type = #{query.applyType}</if>
        <if test="query.status != null  and query.status != ''"> and ssa.`status` = #{query.status}</if>
        <if test="query.storeId != null "> and ssa.store_id = #{query.storeId}</if>
        <if test="query.userId != null "> and ssa.user_id = #{query.userId}</if>
        <if test="query.verifyId != null "> and ssa.verify_id = #{query.verifyId}</if>
        <if test="query.verifyRemark != null  and query.verifyRemark != ''"> and ssa.verify_remark like concat('%', #{query.verifyRemark}, '%')</if>
        <if test="query.storeName != null  and query.storeName != ''"> and ss.name like concat('%', #{query.storeName}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and su.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.verifyName != null  and query.verifyName != ''"> and sys_u.user_name like concat('%', #{query.verifyName}, '%')</if>
    </sql>

    <select id="selectStoreApplyList" parameterType="StoreApplyQuery" resultMap="StoreApplyResult">
        <include refid="selectStoreApplyVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectStoreApplyById" parameterType="Long" resultMap="StoreApplyResult">
        <include refid="selectStoreApplyVo"/>
        where id = #{id}
    </select>

    <select id="selectOne" resultMap="StoreApplyResult">
        <include refid="selectStoreApplyVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(ssa.id)
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertStoreApply" parameterType="StoreApply" useGeneratedKeys="true" keyProperty="id">
        insert into ss_store_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyType != null and applyType != ''">apply_type,</if>
            <if test="status != null">`status`,</if>
            <if test="storeId != null">store_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="verifyId != null">verify_id,</if>
            <if test="verifyTime != null">verify_time,</if>
            <if test="verifyRemark != null">verify_remark,</if>
            <if test="oldData != null">old_data,</if>
            <if test="newData != null and newData != ''">new_data,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyType != null and applyType != ''">#{applyType},</if>
            <if test="status != null">#{status},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="verifyId != null">#{verifyId},</if>
            <if test="verifyTime != null">#{verifyTime},</if>
            <if test="verifyRemark != null">#{verifyRemark},</if>
            <if test="oldData != null">#{oldData,typeHandler=com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler},</if>
            <if test="newData != null and newData != ''">#{newData,typeHandler=com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateStoreApply" parameterType="StoreApply">
        update ss_store_apply
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.applyType != null and data.applyType != ''">apply_type = #{data.applyType},</if>
        <if test="data.status != null">`status` = #{data.status},</if>
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.verifyId != null">verify_id = #{data.verifyId},</if>
        <if test="data.verifyTime != null">verify_time = #{data.verifyTime},</if>
        <if test="data.verifyRemark != null">verify_remark = #{data.verifyRemark},</if>
        <if test="data.oldData != null">old_data = #{data.oldData,typeHandler=com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler},</if>
        <if test="data.newData != null and data.newData != ''">new_data = #{data.newData,typeHandler=com.ruoyi.ss.storeApply.mapper.typehandler.StoreJsonTypeHandler},</if>
    </sql>

    <update id="updateByQuery">
        update ss_store_apply ssa
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <delete id="deleteStoreApplyById" parameterType="Long">
        delete from ss_store_apply where id = #{id}
    </delete>

    <delete id="deleteStoreApplyByIds" parameterType="String">
        delete from ss_store_apply where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
