<?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.yanzhu.jh.trouble.mapper.SmzSspAuditinfoMapper">
    
    <resultMap type="SmzSspAuditinfo" id="SmzSspAuditinfoResult">
        <result property="id"    column="id"    />
        <result property="mainId"    column="mainId"    />
        <result property="processState"    column="processState"    />
        <result property="opinion"    column="opinion"    />
        <result property="isDel"    column="isDel"    />
        <result property="createUser"    column="createUser"    />
        <result property="createUserName"    column="createUserName"    />
        <result property="createTime"    column="createTime"    />
        <result property="fileUrls"    column="fileUrls"    />
        <result property="fileUrls"    column="fileUrls"    />
        <result property="process"    column="process"    />
    </resultMap>

    <sql id="selectSmzSspAuditinfoVo">
        select ssa.id, ssa.mainId, ssa.processState, ssa.opinion, ssa.isDel, ssa.createUser, ssa.createTime, group_concat(ssf.fileUrl) as fileUrls, u.nick_name as createUserName,ssd.dict_label as process from smz_ssp_auditinfo ssa
        left join smz_ssp_fileinfo ssf on ssf.auditId = ssa.id
        left join sys_user u on u.phonenumber = ssa.createUser
        left join sys_dict_data ssd on ssd.dict_type = 'smz_ssp_auditinfo' and ssd.dict_value = ssa.processState
    </sql>

    <select id="selectSmzSspAuditinfoList" parameterType="SmzSspAuditinfo" resultMap="SmzSspAuditinfoResult">
        <include refid="selectSmzSspAuditinfoVo"/>
        <where>  
            <if test="mainId != null "> and ssa.mainId = #{mainId}</if>
            <if test="processState != null  and processState != ''"> and ssa.processState = #{processState}</if>
            <if test="opinion != null  and opinion != ''"> and ssa.opinion = #{opinion}</if>
            <if test="isDel != null "> and ssa.isDel = #{isDel}</if>
            <if test="createUser != null  and createUser != ''"> and ssa.createUser = #{createUser}</if>
            <if test="createTime != null "> and ssa.createTime = #{createTime}</if>
            and ssa.isDel = 0
        </where>
        group by ssa.id
        order by ssa.createTime desc
    </select>

    <select id="selectLastSmzSspAuditinfoByMainId" parameterType="Long" resultMap="SmzSspAuditinfoResult">
        select ssa.id, ssa.mainId, ssa.processState, ssa.opinion, ssa.isDel, ssa.createUser, ssa.createTime, group_concat(ssf.fileUrl) as fileUrls, u.nick_name as createUserName,ssd.dict_label as process from smz_ssp_auditinfo ssa
        left join smz_ssp_fileinfo ssf on ssf.auditId = ssa.id
        left join sys_user u on u.phonenumber = ssa.createUser
        left join sys_dict_data ssd on ssd.dict_type = 'smz_ssp_auditinfo' and ssd.dict_value = ssa.processState
        where ssa.mainId = #{mainId} and ssa.processState in (0,2)
        GROUP BY ssa.id
        order by ssa.createTime desc
        limit 0,1
    </select>

    <select id="selectSmzSspAuditinfoById" parameterType="Long" resultMap="SmzSspAuditinfoResult">
        <include refid="selectSmzSspAuditinfoVo"/>
        where ssa.id = #{id}
    </select>
        
    <insert id="insertSmzSspAuditinfo" parameterType="SmzSspAuditinfo" useGeneratedKeys="true" keyProperty="id">
        insert into smz_ssp_auditinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mainId != null">mainId,</if>
            <if test="processState != null">processState,</if>
            <if test="opinion != null">opinion,</if>
            <if test="isDel != null">isDel,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mainId != null">#{mainId},</if>
            <if test="processState != null">#{processState},</if>
            <if test="opinion != null">#{opinion},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateSmzSspAuditinfo" parameterType="SmzSspAuditinfo">
        update smz_ssp_auditinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="mainId != null">mainId = #{mainId},</if>
            <if test="processState != null">processState = #{processState},</if>
            <if test="opinion != null">opinion = #{opinion},</if>
            <if test="isDel != null">isDel = #{isDel},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSmzSspAuditinfoById" parameterType="Long">
        delete from smz_ssp_auditinfo where id = #{id}
    </delete>

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

    <select id="selectAuditinfoByProblemmodifyId" parameterType="Long" resultType="map">
        select ssa.*,group_concat(ssf.fileUrl) as fileUrls from smz_ssp_problemmodify ssp

        where ssp.id = 28
        group by ssa.id
        order by ssa.createTime desc
    </select>

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

    <delete id="deleteSmzSspFileinfoByAuditId" parameterType="Long"> delete from smz_ssp_fileinfo where auditId = #{auditId} </delete>

    <insert id="batchSmzSspFileinfo">
        insert into smz_ssp_fileinfo( id, auditId, fileType, fileUrl, isDel, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=","> ( #{item.id}, #{item.auditId}, #{item.fileType}, #{item.fileUrl}, #{item.isDel}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime}) </foreach>
    </insert>

</mapper>