<?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.mchApply.mapper.MchApplyMapper">

    <resultMap type="MchApplyVO" id="MchApplyResult" autoMapping="true">
    </resultMap>

    <sql id="selectMchApplyVo">
        select
            sma.apply_id,
            sma.user_id,
            sma.status,
            sma.name,
            sma.mobile,
            sma.content,
            sma.remark,
            sma.callback,
            sma.verify_by,
            sma.verify_time,
            sma.verify_remark,
            sma.create_time,
            sma.update_time,
            sma.deleted,
            su.user_name as user_name,
            sy_u.user_name as verify_name
        from ss_mch_apply sma
            left join sm_user su on su.user_id = sma.user_id
            left join sys_user sy_u on sy_u.user_id = sma.verify_by
    </sql>

    <sql id="searchCondition">
        <if test="query.userId != null "> and sma.user_id = #{query.userId}</if>
        <if test="query.status != null"> and sma.status = #{query.status}</if>
        <if test="query.name != null"> and sma.name like concat('%', #{query.name}, '%')</if>
        <if test="query.userName != null"> and su.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.mobile != null"> and sma.mobile like concat('%', #{query.mobile}, '%')</if>
        <if test="query.verifyBy != null "> and sma.verify_by = #{query.verifyBy}</if>
        <if test="query.verifyName != null "> and sy_u.user_name like concat('%', #{query.verifyName}, '%')</if>
        <if test="query.deleted == null">and sma.deleted = false</if>
        <if test="query.deleted != null">and sma.deleted = #{query.deleted}</if>
        <if test="query.applyId != null">and sma.apply_id = #{query.applyId}</if>
    </sql>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.status != null">`status` = #{data.status},</if>
        <if test="data.name != null">`name` = #{data.name},</if>
        <if test="data.mobile != null">mobile = #{data.mobile},</if>
        <if test="data.content != null">content = #{data.content},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.callback != null">callback = #{data.callback},</if>
        <if test="data.verifyBy != null">verify_by = #{data.verifyBy},</if>
        <if test="data.verifyTime != null">verify_time = #{data.verifyTime},</if>
        <if test="data.verifyRemark != null">verify_remark = #{data.verifyRemark},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
    </sql>

    <select id="selectMchApplyList" parameterType="MchApplyQuery" resultMap="MchApplyResult">
        <include refid="selectMchApplyVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by sma.create_time desc
    </select>

    <select id="selectMchApplyByApplyId" parameterType="Long" resultMap="MchApplyResult">
        <include refid="selectMchApplyVo"/>
        where sma.apply_id = #{applyId} and sma.deleted = false
    </select>

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

    <insert id="insertMchApply" parameterType="MchApply" useGeneratedKeys="true" keyProperty="applyId">
        insert into ss_mch_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="name != null and name != ''">`name`,</if>
            <if test="mobile != null and mobile != ''">mobile,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="remark != null">remark,</if>
            <if test="callback != null">callback,</if>
            <if test="verifyBy != null">verify_by,</if>
            <if test="verifyTime != null">verify_time,</if>
            <if test="verifyRemark != null">verify_remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="mobile != null and mobile != ''">#{mobile},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="remark != null">#{remark},</if>
            <if test="callback != null">#{callback},</if>
            <if test="verifyBy != null">#{verifyBy},</if>
            <if test="verifyTime != null">#{verifyTime},</if>
            <if test="verifyRemark != null">#{verifyRemark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateMchApply" parameterType="MchApply">
        update ss_mch_apply
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where apply_id = #{applyId}
    </update>

    <update id="updateWithCondition">
        update ss_mch_apply sma
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <delete id="deleteMchApplyByApplyId" parameterType="Long">
        delete from ss_mch_apply where apply_id = #{applyId}
    </delete>

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