<?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.frequency.worksshield.mapper.WorksShieldMapper">

    <resultMap type="com.ruoyi.frequency.worksshield.entity.WorksShield" id="WorksShieldResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="worksId"    column="works_id"    />
        <result property="remark"    column="remark"    />
        <association property="userInfo"    column="user_id" javaType="com.ruoyi.vo.UserInfo" resultMap="userInfo" />
    </resultMap>

    <resultMap id="userInfo" type="com.ruoyi.vo.UserInfo">
        <id     property="userId"    column="user_id"     />
        <result property="userType"  column="user_type"   />
        <result property="idNo"  column="id_no"   />
        <result property="nickname"  column="nickname"   />
        <result property="avatar" column="avatar"   />
        <result property="mobile" column="mobile"   />
    </resultMap>
    
    
    <sql id="selectWorksShieldVo">
        select
            t.id ,
            t.del_flag ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.user_type ,
            t.user_id ,
            t.works_id ,
            t.remark ,
            u.user_type,
            u.id_no,
            u.mobile,
            u.avatar,
            u.nickname
        from t_works_shield AS t
                LEFT JOIN t_user AS u ON t.user_id = u.id
    </sql>

    <select id="selectWorksShieldList" parameterType="com.ruoyi.frequency.worksshield.entity.WorksShield" resultMap="WorksShieldResult">
        <include refid="selectWorksShieldVo"/>
        <where> AND t.del_flag != 2
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="worksId != null "> and t.works_id = #{worksId}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectWorksShieldById" parameterType="Long" resultMap="WorksShieldResult">
        <include refid="selectWorksShieldVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertWorksShield" parameterType="com.ruoyi.frequency.worksshield.entity.WorksShield">
        insert into t_works_shield
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="worksId != null">works_id,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="worksId != null">#{worksId},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateWorksShield" parameterType="com.ruoyi.frequency.worksshield.entity.WorksShield">
        update t_works_shield
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="worksId != null">works_id = #{worksId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="worksShield" parameterType="com.ruoyi.frequency.worksshield.entity.WorksShield">
        insert into t_works_shield
        (id, create_by, create_time, update_by, update_time, user_type, user_id, works_id)
        values
            (#{req.id},#{req.updateBy},#{req.updateTime},#{req.updateBy},#{req.updateTime},#{userVo.userType}, #{userVo.userId}, #{req.worksId})
        ON DUPLICATE  KEY UPDATE
                              del_flag = IF( del_flag = 1, 3, 1 ),
                              update_time = #{req.updateTime},
                              update_by = #{req.updateBy};
    </update>
</mapper>
                                                                                                                        