<?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.process.mapper.TMocMapper">

    <resultMap type="TMoc" id="TMocResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="mocNo"    column="moc_no"    />
        <result property="plantNumber"    column="plant_number"    />
        <result property="noticeLetter"    column="notice_letter"    />
        <result property="workLetter"    column="work_letter"    />
        <result property="cteNo"    column="cte_no"    />
        <result property="investCost"    column="invest_cost"    />
        <result property="category"    column="category"    />
        <result property="significance"    column="significance"    />
        <result property="classification"    column="classification"    />
        <result property="area"    column="area"    />
        <result property="dashControl"    column="dash_control"    />
        <result property="title"    column="title"    />
        <result property="owner"    column="owner"    />
        <result property="approveTime"    column="approve_time"    />
        <result property="estimateEndtime"    column="estimate_endtime"    />
        <result property="endtime"    column="endtime"    />
        <result property="trueState"    column="true_state"    />
        <result property="changeNature"    column="change_nature"    />
        <result property="overTime"    column="over_time"    />
        <result property="riskLevel"    column="risk_level"    />
        <result property="training"    column="training"    />
        <result property="pssr"    column="pssr"    />
        <result property="ehsCheck"    column="ehs_check"    />
        <result property="ehsDb"    column="ehs_db"    />
        <result property="pidMaster"    column="pid_master"    />
        <result property="pssrDb"    column="pssr_db"    />
        <result property="capex"    column="capex"    />
        <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="deptId"    column="dept_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="temporaryState"    column="temporary_state"    />
        <result property="sopUpdate"    column="sop_update"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTMocVo">
        select d.id, d.plant_code, d.moc_no, d.plant_number, d.notice_letter, d.work_letter, d.cte_no, d.invest_cost, d.category, d.significance, d.classification, d.area, d.dash_control, d.title, d.owner, d.approve_time, d.estimate_endtime, d.endtime, d.true_state, d.change_nature, d.over_time, d.risk_level, d.training, d.pssr, d.ehs_check, d.ehs_db, d.pid_master, d.pssr_db, d.capex, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks, d.temporary_state, d.sop_update ,s.dept_name from t_moc d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTMocList" parameterType="TMoc" resultMap="TMocResult">
        <include refid="selectTMocVo"/>
        <where>
            <if test="mocNo != null  and mocNo != ''"> and moc_no like concat(concat('%', #{mocNo}), '%')</if>
            <if test="plantNumber != null  and plantNumber != ''"> and plant_number like concat(concat('%', #{plantNumber}), '%')</if>
            <if test="noticeLetter != null  and noticeLetter != ''"> and notice_letter like concat(concat('%', #{noticeLetter}), '%')</if>
            <if test="workLetter != null  and workLetter != ''"> and work_letter like concat(concat('%', #{workLetter}), '%')</if>
            <if test="cteNo != null  and cteNo != ''"> and cte_no like concat(concat('%', #{cteNo}), '%')</if>
            <if test="investCost != null  and investCost != ''"> and invest_cost like concat(concat('%', #{investCost}), '%')</if>
            <if test="category != null  and category != ''"> and category like concat(concat('%', #{category}), '%')</if>
            <if test="significance != null  and significance != ''"> and significance like concat(concat('%', #{significance}), '%')</if>
            <if test="classification != null  and classification != ''"> and classification like concat(concat('%', #{classification}), '%')</if>
            <if test="area != null  and area != ''"> and area like concat(concat('%', #{area}), '%')</if>
            <if test="dashControl != null  and dashControl != ''"> and dash_control like concat(concat('%', #{dashControl}), '%')</if>
            <if test="title != null  and title != ''"> and title like concat(concat('%', #{title}), '%')</if>
            <if test="owner != null  and owner != ''"> and owner like concat(concat('%', #{owner}), '%')</if>
            <if test="approveTime != null "> and approve_time = #{approveTime}</if>
            <if test="estimateEndtime != null "> and estimate_endtime = #{estimateEndtime}</if>
            <if test="endtime != null "> and endtime = #{endtime}</if>
            <if test="trueState != null  and trueState != ''"> and true_state like concat(concat('%', #{trueState}), '%')</if>
            <if test="changeNature != null  and changeNature != ''"> and change_nature like concat(concat('%', #{changeNature}), '%')</if>
            <if test="overTime != null "> and over_time = #{overTime}</if>
            <if test="riskLevel != null  and riskLevel != ''"> and risk_level like concat(concat('%', #{riskLevel}), '%')</if>
            <if test="training != null  and training != ''"> and training like concat(concat('%', #{training}), '%')</if>
            <if test="pssr != null  and pssr != ''"> and pssr like concat(concat('%', #{pssr}), '%')</if>
            <if test="ehsCheck != null  and ehsCheck != ''"> and ehs_check like concat(concat('%', #{ehsCheck}), '%')</if>
            <if test="ehsDb != null  and ehsDb != ''"> and ehs_db like concat(concat('%', #{ehsDb}), '%')</if>
            <if test="pidMaster != null  and pidMaster != ''"> and pid_master like concat(concat('%', #{pidMaster}), '%')</if>
            <if test="pssrDb != null  and pssrDb != ''"> and pssr_db like concat(concat('%', #{pssrDb}), '%')</if>
            <if test="capex != null  and capex != ''"> and capex like concat(concat('%', #{capex}), '%')</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.id desc
    </select>

    <select id="selectTMocById" parameterType="Long" resultMap="TMocResult">
        <include refid="selectTMocVo"/>
        where id = #{id}
    </select>

    <insert id="insertTMoc" parameterType="TMoc">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_moc.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_moc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="mocNo != null">moc_no,</if>
            <if test="plantNumber != null">plant_number,</if>
            <if test="noticeLetter != null">notice_letter,</if>
            <if test="workLetter != null">work_letter,</if>
            <if test="cteNo != null">cte_no,</if>
            <if test="investCost != null">invest_cost,</if>
            <if test="category != null">category,</if>
            <if test="significance != null">significance,</if>
            <if test="classification != null">classification,</if>
            <if test="area != null">area,</if>
            <if test="dashControl != null">dash_control,</if>
            <if test="title != null">title,</if>
            <if test="owner != null">owner,</if>
            <if test="approveTime != null">approve_time,</if>
            <if test="estimateEndtime != null">estimate_endtime,</if>
            <if test="endtime != null">endtime,</if>
            <if test="trueState != null">true_state,</if>
            <if test="changeNature != null">change_nature,</if>
            <if test="overTime != null">over_time,</if>
            <if test="riskLevel != null">risk_level,</if>
            <if test="training != null">training,</if>
            <if test="pssr != null">pssr,</if>
            <if test="ehsCheck != null">ehs_check,</if>
            <if test="ehsDb != null">ehs_db,</if>
            <if test="pidMaster != null">pid_master,</if>
            <if test="pssrDb != null">pssr_db,</if>
            <if test="capex != null">capex,</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="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="temporaryState != null">temporary_state,</if>
            <if test="sopUpdate != null">sop_update,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="mocNo != null">#{mocNo},</if>
            <if test="plantNumber != null">#{plantNumber},</if>
            <if test="noticeLetter != null">#{noticeLetter},</if>
            <if test="workLetter != null">#{workLetter},</if>
            <if test="cteNo != null">#{cteNo},</if>
            <if test="investCost != null">#{investCost},</if>
            <if test="category != null">#{category},</if>
            <if test="significance != null">#{significance},</if>
            <if test="classification != null">#{classification},</if>
            <if test="area != null">#{area},</if>
            <if test="dashControl != null">#{dashControl},</if>
            <if test="title != null">#{title},</if>
            <if test="owner != null">#{owner},</if>
            <if test="approveTime != null">#{approveTime},</if>
            <if test="estimateEndtime != null">#{estimateEndtime},</if>
            <if test="endtime != null">#{endtime},</if>
            <if test="trueState != null">#{trueState},</if>
            <if test="changeNature != null">#{changeNature},</if>
            <if test="overTime != null">#{overTime},</if>
            <if test="riskLevel != null">#{riskLevel},</if>
            <if test="training != null">#{training},</if>
            <if test="pssr != null">#{pssr},</if>
            <if test="ehsCheck != null">#{ehsCheck},</if>
            <if test="ehsDb != null">#{ehsDb},</if>
            <if test="pidMaster != null">#{pidMaster},</if>
            <if test="pssrDb != null">#{pssrDb},</if>
            <if test="capex != null">#{capex},</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="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="temporaryState != null">#{temporaryState},</if>
            <if test="sopUpdate != null">#{sopUpdate},</if>
         </trim>
    </insert>

    <update id="updateTMoc" parameterType="TMoc">
        update t_moc
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="mocNo != null">moc_no = #{mocNo},</if>
            <if test="plantNumber != null">plant_number = #{plantNumber},</if>
            <if test="noticeLetter != null">notice_letter = #{noticeLetter},</if>
            <if test="workLetter != null">work_letter = #{workLetter},</if>
            <if test="cteNo != null">cte_no = #{cteNo},</if>
            <if test="investCost != null">invest_cost = #{investCost},</if>
            <if test="category != null">category = #{category},</if>
            <if test="significance != null">significance = #{significance},</if>
            <if test="classification != null">classification = #{classification},</if>
            <if test="area != null">area = #{area},</if>
            <if test="dashControl != null">dash_control = #{dashControl},</if>
            <if test="title != null">title = #{title},</if>
            <if test="owner != null">owner = #{owner},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="estimateEndtime != null">estimate_endtime = #{estimateEndtime},</if>
            <if test="endtime != null">endtime = #{endtime},</if>
            <if test="approveTime == null">approve_time = NULL,</if>
            <if test="estimateEndtime == null">estimate_endtime = NULL,</if>
            <if test="endtime == null">endtime = NULL,</if>
            <if test="trueState != null">true_state = #{trueState},</if>
            <if test="changeNature != null">change_nature = #{changeNature},</if>
            <if test="overTime != null">over_time = #{overTime},</if>
            <if test="overTime == null">over_time = NULL,</if>
            <if test="riskLevel != null">risk_level = #{riskLevel},</if>
            <if test="training != null">training = #{training},</if>
            <if test="pssr != null">pssr = #{pssr},</if>
            <if test="ehsCheck != null">ehs_check = #{ehsCheck},</if>
            <if test="ehsDb != null">ehs_db = #{ehsDb},</if>
            <if test="pidMaster != null">pid_master = #{pidMaster},</if>
            <if test="pssrDb != null">pssr_db = #{pssrDb},</if>
            <if test="capex != null">capex = #{capex},</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="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="temporaryState != null">temporary_state = #{temporaryState},</if>
            <if test="sopUpdate != null">sop_update = #{sopUpdate},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="deleteTMocByIds" parameterType="String">
        update t_moc 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.APPROVE_TIME,'YYYY') as dataName FROM t_moc d
        <where>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY TO_CHAR(d.APPROVE_TIME,'YYYY')
        order by dataName
    </select>

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

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

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

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

</mapper>
