<?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.project.reliability.mapper.TAbnormalreportMapper">

    <resultMap type="TAbnormalreport" id="TAbnormalreportResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="time"    column="time"    />
        <result property="team"    column="team"    />
        <result property="reporter"    column="reporter"    />
        <result property="reportno"    column="reportno"    />
        <result property="abnormalphen"    column="abnormalphen"    />
        <result property="treatprocess"    column="treatprocess"    />
        <result property="reason"    column="reason"    />
        <result property="measures"    column="measures"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
        <result property="startDate"    column="time"    />
        <result property="endDate"    column="time"    />
    </resultMap>

    <sql id="selectTAbnormalreportVo">
        select d.id, d.plant_code, d.time, d.team, d.reporter, d.reportno, d.abnormalphen, d.treatprocess, d.reason, d.measures, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id ,s.dept_name from t_abnormalreport d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTAbnormalreportList" parameterType="TAbnormalreport" resultMap="TAbnormalreportResult">
        <include refid="selectTAbnormalreportVo"/>
        <where>
            <if test="team != null  and team != ''"> and team = #{team}</if>
            <if test="reporter != null  and reporter != ''"> and reporter like concat(concat('%', #{reporter}), '%')</if>
            <if test="reportno != null  and reportno != ''"> and reportno like concat(concat('%', #{reportno}), '%')</if>
            <if test="startDate != null ">and time <![CDATA[>=]]> #{startDate}</if>
            <if test="endDate != null">and time <![CDATA[<=]]>#{endDate}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.time desc
    </select>

    <select id="selectTAbnormalreportById" parameterType="Long" resultMap="TAbnormalreportResult">
        <include refid="selectTAbnormalreportVo"/>
        where id = #{id}
    </select>

    <insert id="insertTAbnormalreport" parameterType="TAbnormalreport">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_abnormalreport.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_abnormalreport
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="time != null">time,</if>
            <if test="team != null">team,</if>
            <if test="reporter != null">reporter,</if>
            <if test="reportno != null">reportno,</if>
            <if test="abnormalphen != null">abnormalphen,</if>
            <if test="treatprocess != null">treatprocess,</if>
            <if test="reason != null">reason,</if>
            <if test="measures != null">measures,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="time != null">#{time},</if>
            <if test="team != null">#{team},</if>
            <if test="reporter != null">#{reporter},</if>
            <if test="reportno != null">#{reportno},</if>
            <if test="abnormalphen != null">#{abnormalphen},</if>
            <if test="treatprocess != null">#{treatprocess},</if>
            <if test="reason != null">#{reason},</if>
            <if test="measures != null">#{measures},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateTAbnormalreport" parameterType="TAbnormalreport">
        update t_abnormalreport
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="time != null">time = #{time},</if>
            <if test="team != null">team = #{team},</if>
            <if test="reporter != null">reporter = #{reporter},</if>
            <if test="reportno != null">reportno = #{reportno},</if>
            <if test="abnormalphen != null">abnormalphen = #{abnormalphen},</if>
            <if test="treatprocess != null">treatprocess = #{treatprocess},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="measures != null">measures = #{measures},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTAbnormalreportById" parameterType="Long">
        update t_abnormalreport set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTAbnormalreportByIds" parameterType="String">
        update t_abnormalreport set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectYearData" resultType="com.ruoyi.project.common.domain.DataEntity">
        SELECT count(1) as dataNum,TO_CHAR(d.time,'YYYY') as dataName FROM t_abnormalreport d
        <where>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY TO_CHAR(d.time,'YYYY')
        order by dataName
    </select>

    <select id="teamData" resultType="com.ruoyi.project.common.domain.DataEntity">
        SELECT count(1) as dataNum,d.team as dataName FROM t_abnormalreport d
        <where>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY d.team
        order by dataName
    </select>
</mapper>
