<?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.sems.mapper.TSpecdevYlrqMapper">

    <resultMap type="TSpecdevYlrq" id="TSpecdevYlrqResult">
        <result property="id" column="id"/>
        <result property="plantCode" column="plant_code"/>
        <result property="unit" column="unit"/>
        <result property="devname" column="devname"/>
        <result property="devno" column="devno"/>
        <result property="submitdate" column="submitdate"/>
        <result property="status" column="status"/>
        <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="approveStatus" column="approve_status"/>
        <result property="regno" column="regno"/>
        <result property="useno" column="useno"/>
        <result property="warnDate" column="warn_date"/>
        <result property="warnCycle" column="warn_cycle"/>
        <result property="nextWarnDate" column="next_warn_date"/>
        <result property="warnFlag" column="warn_flag"/>
        <result property="plantMaint" column="plant_maint"/>
        <result property="engineer" column="engineer"/>
        <result property="vesselType" column="vessel_type"/>
        <result property="isKey" column="is_key"/>
        <result property="createUnit" column="create_unit"/>
        <result property="nation" column="nation"/>
        <result property="outSize" column="out_size"/>
        <result property="material" column="material"/>
        <result property="medium" column="medium"/>
        <result property="desPressure" column="des_pressure"/>
        <result property="desTemp" column="des_temp"/>
        <result property="optPressure" column="opt_pressure"/>
        <result property="optTemp" column="opt_temp"/>
        <result property="checkUnit" column="check_unit"/>
        <result property="reportNo" column="report_no"/>
        <result property="isDanger" column="is_danger"/>
        <result property="isOverlife" column="is_overlife"/>
        <result property="safeClass" column="safe_class"/>
        <result property="approveTime" column="approve_time"/>
        <result property="changeTime" column="change_time"/>
        <result property="desYear" column="des_year"/>
        <result property="yearWarnDate" column="year_warn_date"/>
        <result property="checkConclusion" column="check_conclusion"/>
        <result property="yearNextWarnDate" column="year_next_warn_date"/>
        <result property="yearReportNo" column="year_report_no"/>
        <result property="hiFlag" column="hiFlag"/>
        <result property="deptName" column="dept_name"/>
        <result property="isRepeat" column="is_repeat"/>
        <result property="factoryNo" column="factory_no"/>
        <result property="devEnname" column="dev_enname"/>
        <result property="pidNo" column="pid_no"/>
        <result property="reliefValve" column="relief_valve"/>
        <result property="factoryInfo" column="factory_info"/>
        <result property="designUnit" column="design_unit"/>
        <result property="useCode" column="use_code"/>
        <result property="pressureGrade" column="pressure_grade"/>
        <result property="designDead" column="design_dead"/>
    </resultMap>

    <sql id="selectTSpecdevYlrqVo">
        select d.id,
               d.plant_code,
               d.unit,
               d.devname,
               d.factory_no,
               d.devno,
               d.submitdate,
               d.status,
               d.del_flag,
               d.creater_code,
               d.createdate,
               d.updater_code,
               d.updatedate,
               d.dept_id,
               d.remarks,
               d.approve_status,
               d.regno,
               d.useno,
               d.warn_date,
               d.warn_cycle,
               d.next_warn_date,
               d.warn_flag,
               d.plant_maint,
               d.engineer,
               d.vessel_type,
               d.is_key,
               d.create_unit,
               d.nation,
               d.out_size,
               d.material,
               d.medium,
               d.des_pressure,
               d.des_temp,
               d.opt_pressure,
               d.opt_temp,
               d.check_unit,
               d.report_no,
               d.is_danger,
               d.is_overlife,
               d.safe_class,
               d.approve_time,
               d.change_time,
               d.des_year,
               d.year_warn_date,
               d.check_conclusion,
               d.year_next_warn_date,
               d.year_report_no,
               s.dept_name,
               d.is_repeat,
               d.dev_enname,
               d.pid_no,
               d.relief_valve,
               d.factory_info,
               d.design_unit,
               d.use_code,
               d.pressure_grade,
               d.design_dead
        from t_specdev_ylrq d
                 left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTSpecdevYlrqByIds" parameterType="Long" resultMap="TSpecdevYlrqResult">
        <include refid="selectTSpecdevYlrqVo"/>
        where id in
        <foreach collection="ids" open="(" close=")" item="id" index="index">
            <if test="index != 0">
                <choose>
                    <when test="index % 1000 == 999">) OR id in (</when>
                    <otherwise>,</otherwise>
                </choose>
            </if>
            #{id}
        </foreach>
    </select>

    <select id="selectTSpecdevYlrqList" parameterType="TSpecdevYlrq" resultMap="TSpecdevYlrqResult">
        <include refid="selectTSpecdevYlrqVo"/>
        <where>
            <if test="plantCode != null  and plantCode != ''">and plant_code = #{plantCode}</if>
            <if test="unit != null  and unit != ''">and unit = #{unit}</if>
            <if test="devname != null  and devname != ''">and devname like concat(concat('%', #{devname}), '%')</if>
            <if test="devno != null  and devno != ''">and devno = #{devno}</if>
            <if test="status != null ">and d.status = #{status}</if>
            <if test="approveStatus != null ">and approve_status = #{approveStatus}</if>
            <if test="warnFlag != null ">and warn_Flag = #{warnFlag}</if>
            <if test="fuzzy != null ">and (plant_code like concat('%',#{fuzzy},'%')
                or unit like concat('%',#{fuzzy},'%')
                or devname like concat('%',#{fuzzy},'%')
                or factory_no like concat('%',#{fuzzy},'%')
                or devno like concat('%',#{fuzzy},'%')
                or createdate like concat('%',#{fuzzy},'%')
                or remarks like concat('%',#{fuzzy},'%')
                or regno like concat('%',#{fuzzy},'%')
                or useno like concat('%',#{fuzzy},'%')
                or warn_date like concat('%',#{fuzzy},'%')
                or warn_cycle like concat('%',#{fuzzy},'%')
                or next_warn_date like concat('%',#{fuzzy},'%')
                or plant_maint like concat('%',#{fuzzy},'%')
                or engineer like concat('%',#{fuzzy},'%')
                or vessel_type like concat('%',#{fuzzy},'%')
                or create_unit like concat('%',#{fuzzy},'%')
                or nation like concat('%',#{fuzzy},'%')
                or out_size like concat('%',#{fuzzy},'%')
                or material like concat('%',#{fuzzy},'%')
                or `medium` like concat('%',#{fuzzy},'%')
                or des_pressure like concat('%',#{fuzzy},'%')
                or des_temp like concat('%',#{fuzzy},'%')
                or opt_pressure like concat('%',#{fuzzy},'%')
                or opt_temp like concat('%',#{fuzzy},'%')
                or check_unit like concat('%',#{fuzzy},'%')
                or report_no like concat('%',#{fuzzy},'%')
                or is_danger like concat('%',#{fuzzy},'%')
                or is_overlife like concat('%',#{fuzzy},'%')
                or safe_class like concat('%',#{fuzzy},'%')
                or des_year like concat('%',#{fuzzy},'%')
                or year_warn_date like concat('%',#{fuzzy},'%')
                or check_conclusion like concat('%',#{fuzzy},'%')
                or year_next_warn_date like concat('%',#{fuzzy},'%')
                or year_report_no like concat('%',#{fuzzy},'%')
                or dev_enname like concat('%',#{fuzzy},'%')
                or pid_no like concat('%',#{fuzzy},'%')
                or relief_valve like concat('%',#{fuzzy},'%')
                or factory_info like concat('%',#{fuzzy},'%')
                or design_unit like concat('%',#{fuzzy},'%')
                or use_code like concat('%',#{fuzzy},'%')
                or pressure_grade like concat('%',#{fuzzy},'%')
                or design_dead like concat('%',#{fuzzy},'%') )</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
        order by d.is_repeat desc, d.plant_code, d.unit, d.id
    </select>

    <select id="selectTSpecdevYlrqListByYear" parameterType="TSpecdevYlrq" resultMap="TSpecdevYlrqResult">
        <include refid="selectTSpecdevYlrqVo"/>
        <where>
            <if test="status != null ">and d.status = #{status}</if>
            <if test="checkYear != null ">and date_format(NEXT_WARN_DATE,'%Y') = #{checkYear}</if>
            <if test="plantIds != null  and plantIds != ''">
                and
                d.plant_code in
                <foreach collection="plantIds.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <!--<if test="unitIds != null  and unitIds != ''">
                and
                (
                d.unit in
                <foreach collection="unitIds.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
                )
            </if>-->
            and d.del_flag = 0
        </where>
    </select>


    <select id="selectTSpecdevYlrqById" parameterType="Long" resultMap="TSpecdevYlrqResult">
        <include refid="selectTSpecdevYlrqVo"/>
        where id = #{id}
    </select>

    <insert id="insertTSpecdevYlrq" parameterType="TSpecdevYlrq">

        insert into t_specdev_ylrq
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="unit != null">unit,</if>
            <if test="devname != null">devname,</if>
            <if test="factoryNo != null">factory_no,</if>
            <if test="devno != null">devno,</if>
            <if test="submitdate != null">submitdate,</if>
            <if test="status != null">status,</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="approveStatus != null">approve_status,</if>
            <if test="regno != null">regno,</if>
            <if test="useno != null">useno,</if>
            <if test="warnDate != null">warn_date,</if>
            <if test="warnCycle != null">warn_cycle,</if>
            <if test="nextWarnDate != null">next_warn_date,</if>
            <if test="warnFlag != null">warn_flag,</if>
            <if test="plantMaint != null">plant_maint,</if>
            <if test="engineer != null">engineer,</if>
            <if test="vesselType != null">vessel_type,</if>
            <if test="isKey != null">is_key,</if>
            <if test="createUnit != null">create_unit,</if>
            <if test="nation != null">nation,</if>
            <if test="outSize != null">out_size,</if>
            <if test="material != null">material,</if>
            <if test="medium != null">medium,</if>
            <if test="desPressure != null">des_pressure,</if>
            <if test="desTemp != null">des_temp,</if>
            <if test="optPressure != null">opt_pressure,</if>
            <if test="optTemp != null">opt_temp,</if>
            <if test="checkUnit != null">check_unit,</if>
            <if test="reportNo != null">report_no,</if>
            <if test="isDanger != null">is_danger,</if>
            <if test="isOverlife != null">is_overlife,</if>
            <if test="safeClass != null">safe_class,</if>
            <if test="approveTime != null">approve_time,</if>
            <if test="changeTime != null">change_time,</if>
            <if test="desYear != null">des_year,</if>
            <if test="yearWarnDate != null">year_warn_date,</if>
            <if test="checkConclusion != null">check_conclusion,</if>
            <if test="yearNextWarnDate != null">year_next_warn_date,</if>
            <if test="yearReportNo != null">year_report_no,</if>
            <if test="devEnname != null">dev_enname,</if>
            <if test="pidNo != null">pid_no,</if>
            <if test="reliefValve != null">relief_valve,</if>
            <if test="factoryInfo != null">factory_info,</if>
            <if test="designUnit != null">design_unit,</if>
            <if test="useCode != null">use_code,</if>
            <if test="pressureGrade != null">pressure_grade,</if>
            <if test="designDead != null">design_dead,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="unit != null">#{unit},</if>
            <if test="devname != null">#{devname},</if>
            <if test="factoryNo != null">#{factoryNo},</if>
            <if test="devno != null">#{devno},</if>
            <if test="submitdate != null">#{submitdate},</if>
            <if test="status != null">#{status},</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="approveStatus != null">#{approveStatus},</if>
            <if test="regno != null">#{regno},</if>
            <if test="useno != null">#{useno},</if>
            <if test="warnDate != null">#{warnDate},</if>
            <if test="warnCycle != null">#{warnCycle},</if>
            <if test="nextWarnDate != null">#{nextWarnDate},</if>
            <if test="warnFlag != null">#{warnFlag},</if>
            <if test="plantMaint != null">#{plantMaint},</if>
            <if test="engineer != null">#{engineer},</if>
            <if test="vesselType != null">#{vesselType},</if>
            <if test="isKey != null">#{isKey},</if>
            <if test="createUnit != null">#{createUnit},</if>
            <if test="nation != null">#{nation},</if>
            <if test="outSize != null">#{outSize},</if>
            <if test="material != null">#{material},</if>
            <if test="medium != null">#{medium},</if>
            <if test="desPressure != null">#{desPressure},</if>
            <if test="desTemp != null">#{desTemp},</if>
            <if test="optPressure != null">#{optPressure},</if>
            <if test="optTemp != null">#{optTemp},</if>
            <if test="checkUnit != null">#{checkUnit},</if>
            <if test="reportNo != null">#{reportNo},</if>
            <if test="isDanger != null">#{isDanger},</if>
            <if test="isOverlife != null">#{isOverlife},</if>
            <if test="safeClass != null">#{safeClass},</if>
            <if test="approveTime != null">#{approveTime},</if>
            <if test="changeTime != null">#{changeTime},</if>
            <if test="desYear != null">#{desYear},</if>
            <if test="yearWarnDate != null">#{yearWarnDate},</if>
            <if test="checkConclusion != null">#{checkConclusion},</if>
            <if test="yearNextWarnDate != null">#{yearNextWarnDate},</if>
            <if test="yearReportNo != null">#{yearReportNo},</if>
            <if test="devEnname != null">#{devEnname},</if>
            <if test="pidNo != null">#{pidNo},</if>
            <if test="reliefValve != null">#{reliefValve},</if>
            <if test="factoryInfo != null">#{factoryInfo},</if>
            <if test="designUnit != null">#{designUnit},</if>
            <if test="useCode != null">#{useCode},</if>
            <if test="pressureGrade != null">#{pressureGrade},</if>
            <if test="designDead != null">#{designDead},</if>
        </trim>
    </insert>

    <update id="updateTSpecdevYlrq" parameterType="TSpecdevYlrq">
        update t_specdev_ylrq
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="devname != null">devname = #{devname},</if>
            <if test="factoryNo != null">factory_no = #{factoryNo},</if>
            <if test="devno != null">devno = #{devno},</if>
            <if test="submitdate != null">submitdate = #{submitdate},</if>
            <if test="status != null">status = #{status},</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="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="regno != null">regno = #{regno},</if>
            <if test="useno != null">useno = #{useno},</if>
            <if test="warnDate != null">warn_date = #{warnDate},</if>
            <if test="warnCycle != null">warn_cycle = #{warnCycle},</if>
            <if test="nextWarnDate != null">next_warn_date = #{nextWarnDate},</if>
            <if test="warnFlag != null">warn_flag = #{warnFlag},</if>
            <if test="plantMaint != null">plant_maint = #{plantMaint},</if>
            <if test="engineer != null">engineer = #{engineer},</if>
            <if test="vesselType != null">vessel_type = #{vesselType},</if>
            <if test="isKey != null">is_key = #{isKey},</if>
            <if test="createUnit != null">create_unit = #{createUnit},</if>
            <if test="nation != null">nation = #{nation},</if>
            <if test="outSize != null">out_size = #{outSize},</if>
            <if test="material != null">material = #{material},</if>
            <if test="medium != null">medium = #{medium},</if>
            <if test="desPressure != null">des_pressure = #{desPressure},</if>
            <if test="desTemp != null">des_temp = #{desTemp},</if>
            <if test="optPressure != null">opt_pressure = #{optPressure},</if>
            <if test="optTemp != null">opt_temp = #{optTemp},</if>
            <if test="checkUnit != null">check_unit = #{checkUnit},</if>
            <if test="reportNo != null">report_no = #{reportNo},</if>
            <if test="isDanger != null">is_danger = #{isDanger},</if>
            <if test="isOverlife != null">is_overlife = #{isOverlife},</if>
            <if test="safeClass != null">safe_class = #{safeClass},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="changeTime != null">change_time = #{changeTime},</if>
            <if test="desYear != null">des_year = #{desYear},</if>
            <if test="yearWarnDate != null">year_warn_date = #{yearWarnDate},</if>
            <if test="checkConclusion != null">check_conclusion = #{checkConclusion},</if>
            <if test="yearNextWarnDate != null">year_next_warn_date = #{yearNextWarnDate},</if>
            <if test="yearReportNo != null">year_report_no = #{yearReportNo},</if>
            <if test="isRepeat != null">is_repeat = #{isRepeat},</if>
            <if test="devEnname != null">dev_enname = #{devEnname},</if>
            <if test="pidNo != null">pid_no = #{pidNo},</if>
            <if test="reliefValve != null">relief_valve = #{reliefValve},</if>
            <if test="factoryInfo != null">factory_info = #{factoryInfo},</if>
            <if test="designUnit != null">design_unit = #{designUnit},</if>
            <if test="useCode != null">use_code = #{useCode},</if>
            <if test="pressureGrade != null">pressure_grade = #{pressureGrade},</if>
            <if test="designDead != null">design_dead = #{designDead},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <update id="duplicateTSpecdevYlrq" >
        UPDATE  T_SPECDEV_YLRQ set DEL_FLAG = 2 where id  in  (select b.id from   ( select id from t_specdev_YLRQ a where (a.DEVNO,a.plant_code) in(select DEVNO,plant_code from t_specdev_YLRQ group by DEVNO,plant_code having count(*) > 1)
                                                                                                                  and id not in (select min(id) from t_specdev_YLRQ group by DEVNO,plant_code having count(*)>1)   and a.DEL_FLAG = 0
                                                                                                                  and a.APPROVE_STATUS = 0 ) AS  b )
    </update>


</mapper>
