<?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.production.mapper.TAccidentMapper">

    <resultMap type="TAccident" id="TAccidentResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="area"    column="area"    />
        <result property="areadate"    column="areadate"    />
        <result property="content"    column="content"    />
        <result property="team"    column="team"    />
        <result property="staffid"    column="staffid"    />
        <result property="detector"    column="name"    />
        <result property="dealer"    column="dealer"    />
        <result property="status"    column="status"    />
        <result property="areaowner"    column="areaowner"    />
        <result property="confirm"    column="confirm"    />
        <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="accidentytpe"    column="accidentytpe"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="startDate"    column="areadate"    />
        <result property="endDate"    column="areadate"    />
    </resultMap>

    <sql id="selectTAccidentVo">
        select d.id, d.plant_code, d.area, d.areadate, d.content, d.team, d.staffid, d.dealer, d.status, d.areaowner, d.confirm, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.accidentytpe, d.dept_id , s.dept_name, t.name from t_accident d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_staffmgr t on t.staffid = d.staffid
    </sql>

    <select id="selectTAccidentList" parameterType="TAccident" resultMap="TAccidentResult">
        <include refid="selectTAccidentVo"/>
        <where>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="areas != null  and areas != ''">
                and
                d.area in
                <foreach collection="areas.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="areadate != null "> and areadate = #{areadate}</if>
            <if test="team != null  and team != ''"> and d.team = #{team}</if>
            <if test="teams != null  and teams != ''">
                and
                d.team in
                <foreach collection="teams.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="staffid != null  and staffid != ''"> and d.staffid = #{staffid}</if>
            <if test="status != null  and status != ''"> and d.status like concat(concat('%', #{status}), '%')</if>
            <if test="confirm != null  and confirm != ''"> and confirm = #{confirm}</if>
            <if test="accidentytpe != null  and accidentytpe != ''"> and accidentytpe = #{accidentytpe}</if>
            <if test="startDate != null ">and areadate <![CDATA[>=]]> #{startDate}</if>
            <if test="endDate != null">and areadate <![CDATA[<=]]>#{endDate}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.areadate desc
    </select>

    <select id="selectTAccidentById" parameterType="Long" resultMap="TAccidentResult">
        <include refid="selectTAccidentVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTAccident" parameterType="TAccident">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_accident.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_accident
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="area != null">area,</if>
            <if test="areadate != null">areadate,</if>
            <if test="content != null">content,</if>
            <if test="team != null">team,</if>
            <if test="staffid != null">staffid,</if>
            <if test="dealer != null">dealer,</if>
            <if test="status != null">status,</if>
            <if test="areaowner != null">areaowner,</if>
            <if test="confirm != null">confirm,</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="accidentytpe != null">accidentytpe,</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="area != null">#{area},</if>
            <if test="areadate != null">#{areadate},</if>
            <if test="content != null">#{content},</if>
            <if test="team != null">#{team},</if>
            <if test="staffid != null">#{staffid},</if>
            <if test="dealer != null">#{dealer},</if>
            <if test="status != null">#{status},</if>
            <if test="areaowner != null">#{areaowner},</if>
            <if test="confirm != null">#{confirm},</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="accidentytpe != null">#{accidentytpe},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateTAccident" parameterType="TAccident">
        update t_accident
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="area != null">area = #{area},</if>
            <if test="areadate != null">areadate = #{areadate},</if>
            <if test="content != null">content = #{content},</if>
            <if test="team != null">team = #{team},</if>
            <if test="staffid != null">staffid = #{staffid},</if>
            <if test="dealer != null">dealer = #{dealer},</if>
            <if test="status != null">status = #{status},</if>
            <if test="areaowner != null">areaowner = #{areaowner},</if>
            <if test="confirm != null">confirm = #{confirm},</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="accidentytpe != null">accidentytpe = #{accidentytpe},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <select id="selectHomeData" parameterType="String" resultType="int">
        select count(0) from t_accident t where to_char(t.AREADATE,'yyyy-MM') like #{month} and del_flag = 0
    </select>

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

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

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

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

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