<?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.training.mapper.TWorkcertificateCbpsMapper">

    <resultMap type="TWorkcertificateCbps" id="TWorkcertificateCbpsResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="name"    column="name"    />
        <result property="employeeid"    column="employeeid"    />
        <result property="classes"    column="classes"    />
        <result property="container"    column="container"    />
        <result property="containerDate"    column="container_date"    />
        <result property="containerLifespan"    column="container_lifespan"    />
        <result property="pipe"    column="pipe"    />
        <result property="pipeDate"    column="pipe_date"    />
        <result property="pipeLifespan"    column="pipe_lifespan"    />
        <result property="worker"    column="worker"    />
        <result property="workerDate"    column="worker_date"    />
        <result property="workerLifespan"    column="worker_lifespan"    />
        <result property="foreman"    column="foreman"    />
        <result property="foremanDate"    column="foreman_date"    />
        <result property="foremanLifespan"    column="foreman_lifespan"    />
        <result property="ac"    column="ac"    />
        <result property="acDate"    column="ac_date"    />
        <result property="acLifespan"    column="ac_lifespan"    />
        <result property="firefighter"    column="firefighter"    />
        <result property="firefighterDate"    column="firefighter_date"    />
        <result property="firefighterLifespan"    column="firefighter_lifespan"    />
        <result property="safety"    column="safety"    />
        <result property="safetyDate"    column="safety_date"    />
        <result property="safetyLifespan"    column="safety_lifespan"    />
        <result property="tds"    column="tds"    />
        <result property="tdsDate"    column="tds_date"    />
        <result property="tdsLifespan"    column="tds_lifespan"    />
        <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" />
    </resultMap>

    <sql id="selectTWorkcertificateCbpsVo">
        select d.id, d.plant_code, d.name, d.employeeid, d.classes, d.container, d.container_date, d.container_lifespan, d.pipe, d.pipe_date, d.pipe_lifespan, d.worker, d.worker_date, d.worker_lifespan, d.foreman, d.foreman_date, d.foreman_lifespan, d.ac, d.ac_date, d.ac_lifespan, d.firefighter, d.firefighter_date, d.firefighter_lifespan, d.safety, d.safety_date, d.safety_lifespan, d.tds, d.tds_date, d.tds_lifespan, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id ,s.dept_name from t_workcertificate_cbps d
      left join sys_dept s on s.dept_id = d.dept_id

    </sql>

    <select id="selectTWorkcertificateCbpsList" parameterType="TWorkcertificateCbps" resultMap="TWorkcertificateCbpsResult">
        <include refid="selectTWorkcertificateCbpsVo"/>
        LEFT JOIN t_staffmgr t on d.employeeid = t.staffid and t.del_flag = 0
        LEFT JOIN SYS_DICT_DATA sdd on t.ACTUALPOST = sdd.DICT_VALUE and sdd.DICT_TYPE = 'ACTUALPOST'
        LEFT JOIN SYS_DICT_DATA sdd2 on d.classes = sdd2.DICT_VALUE and sdd2.DICT_TYPE = 'TEAM_DIVIDE'
        <where>
            <if test="plantCode != null  and plantCode != ''"> and plant_code = #{plantCode}</if>
            <if test="employeeid != null  and employeeid != ''"> and employeeid = #{employeeid}</if>
            <if test="classes != null  and classes != ''"> and classes = #{classes}</if>
            <if test="container != null  and container != ''"> and container = #{container}</if>
            <if test="containerDate != null "> and container_date = #{containerDate}</if>
            <if test="containerLifespan != null "> and container_lifespan = #{containerLifespan}</if>
            <if test="pipe != null  and pipe != ''"> and pipe = #{pipe}</if>
            <if test="pipeDate != null "> and pipe_date = #{pipeDate}</if>
            <if test="pipeLifespan != null "> and pipe_lifespan = #{pipeLifespan}</if>
            <if test="worker != null  and worker != ''"> and worker = #{worker}</if>
            <if test="workerDate != null "> and worker_date = #{workerDate}</if>
            <if test="workerLifespan != null "> and worker_lifespan = #{workerLifespan}</if>
            <if test="foreman != null  and foreman != ''"> and foreman = #{foreman}</if>
            <if test="foremanDate != null "> and foreman_date = #{foremanDate}</if>
            <if test="foremanLifespan != null "> and foreman_lifespan = #{foremanLifespan}</if>
            <if test="ac != null  and ac != ''"> and ac = #{ac}</if>
            <if test="acDate != null "> and ac_date = #{acDate}</if>
            <if test="acLifespan != null "> and ac_lifespan = #{acLifespan}</if>
            <if test="firefighter != null  and firefighter != ''"> and firefighter = #{firefighter}</if>
            <if test="firefighterDate != null "> and firefighter_date = #{firefighterDate}</if>
            <if test="firefighterLifespan != null "> and firefighter_lifespan = #{firefighterLifespan}</if>
            <if test="safety != null  and safety != ''"> and safety = #{safety}</if>
            <if test="safetyDate != null "> and safety_date = #{safetyDate}</if>
            <if test="safetyLifespan != null "> and safety_lifespan = #{safetyLifespan}</if>
            <if test="tds != null  and tds != ''"> and tds = #{tds}</if>
            <if test="tdsDate != null "> and tds_date = #{tdsDate}</if>
            <if test="tdsLifespan != null "> and tds_lifespan = #{tdsLifespan}</if>
            <if test="createrCode != null  and createrCode != ''"> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null  and updaterCode != ''"> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by sdd2.DICT_SORT ,sdd.DICT_SORT
    </select>

    <select id="selectTWorkcertificateCbpsById" parameterType="Long" resultMap="TWorkcertificateCbpsResult">
        <include refid="selectTWorkcertificateCbpsVo"/>
        where id = #{id}
    </select>

    <insert id="insertTWorkcertificateCbps" parameterType="TWorkcertificateCbps">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_workcertificate_cbps.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_workcertificate_cbps
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="name != null">name,</if>
            <if test="employeeid != null">employeeid,</if>
            <if test="classes != null">classes,</if>
            <if test="container != null">container,</if>
            <if test="containerDate != null">container_date,</if>
            <if test="containerLifespan != null">container_lifespan,</if>
            <if test="pipe != null">pipe,</if>
            <if test="pipeDate != null">pipe_date,</if>
            <if test="pipeLifespan != null">pipe_lifespan,</if>
            <if test="worker != null">worker,</if>
            <if test="workerDate != null">worker_date,</if>
            <if test="workerLifespan != null">worker_lifespan,</if>
            <if test="foreman != null">foreman,</if>
            <if test="foremanDate != null">foreman_date,</if>
            <if test="foremanLifespan != null">foreman_lifespan,</if>
            <if test="ac != null">ac,</if>
            <if test="acDate != null">ac_date,</if>
            <if test="acLifespan != null">ac_lifespan,</if>
            <if test="firefighter != null">firefighter,</if>
            <if test="firefighterDate != null">firefighter_date,</if>
            <if test="firefighterLifespan != null">firefighter_lifespan,</if>
            <if test="safety != null">safety,</if>
            <if test="safetyDate != null">safety_date,</if>
            <if test="safetyLifespan != null">safety_lifespan,</if>
            <if test="tds != null">tds,</if>
            <if test="tdsDate != null">tds_date,</if>
            <if test="tdsLifespan != null">tds_lifespan,</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="name != null">#{name},</if>
            <if test="employeeid != null">#{employeeid},</if>
            <if test="classes != null">#{classes},</if>
            <if test="container != null">#{container},</if>
            <if test="containerDate != null">#{containerDate},</if>
            <if test="containerLifespan != null">#{containerLifespan},</if>
            <if test="pipe != null">#{pipe},</if>
            <if test="pipeDate != null">#{pipeDate},</if>
            <if test="pipeLifespan != null">#{pipeLifespan},</if>
            <if test="worker != null">#{worker},</if>
            <if test="workerDate != null">#{workerDate},</if>
            <if test="workerLifespan != null">#{workerLifespan},</if>
            <if test="foreman != null">#{foreman},</if>
            <if test="foremanDate != null">#{foremanDate},</if>
            <if test="foremanLifespan != null">#{foremanLifespan},</if>
            <if test="ac != null">#{ac},</if>
            <if test="acDate != null">#{acDate},</if>
            <if test="acLifespan != null">#{acLifespan},</if>
            <if test="firefighter != null">#{firefighter},</if>
            <if test="firefighterDate != null">#{firefighterDate},</if>
            <if test="firefighterLifespan != null">#{firefighterLifespan},</if>
            <if test="safety != null">#{safety},</if>
            <if test="safetyDate != null">#{safetyDate},</if>
            <if test="safetyLifespan != null">#{safetyLifespan},</if>
            <if test="tds != null">#{tds},</if>
            <if test="tdsDate != null">#{tdsDate},</if>
            <if test="tdsLifespan != null">#{tdsLifespan},</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="updateTWorkcertificateCbps" parameterType="TWorkcertificateCbps">
        update t_workcertificate_cbps
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="name != null">name = #{name},</if>
            <if test="employeeid != null">employeeid = #{employeeid},</if>
            <if test="classes != null">classes = #{classes},</if>
            <if test="container != null">container = #{container},</if>
            <if test="containerDate != null">container_date = #{containerDate},</if>
            <if test="containerDate == null">container_date = NULL,</if>
            <if test="containerLifespan != null">container_lifespan = #{containerLifespan},</if>
            <if test="containerLifespan == null">container_lifespan = NULL,</if>
            <if test="pipe != null">pipe = #{pipe},</if>
            <if test="pipeDate != null">pipe_date = #{pipeDate},</if>
            <if test="pipeDate == null">pipe_date = NULL,</if>
            <if test="pipeLifespan != null">pipe_lifespan = #{pipeLifespan},</if>
            <if test="pipeLifespan == null">pipe_lifespan = NULL,</if>
            <if test="worker != null">worker = #{worker},</if>
            <if test="workerDate != null">worker_date = #{workerDate},</if>
            <if test="workerDate == null">worker_date = NULL,</if>
            <if test="workerLifespan != null">worker_lifespan = #{workerLifespan},</if>
            <if test="workerLifespan == null">worker_lifespan = NULL,</if>
            <if test="foreman != null">foreman = #{foreman},</if>
            <if test="foremanDate != null">foreman_date = #{foremanDate},</if>
            <if test="foremanDate == null">foreman_date = NULL,</if>
            <if test="foremanLifespan != null">foreman_lifespan = #{foremanLifespan},</if>
            <if test="foremanLifespan == null">foreman_lifespan = NULL,</if>
            <if test="ac != null">ac = #{ac},</if>
            <if test="acDate != null">ac_date = #{acDate},</if>
            <if test="acDate == null">ac_date = NULL,</if>
            <if test="acLifespan != null">ac_lifespan = #{acLifespan},</if>
            <if test="acLifespan == null">ac_lifespan = NULL,</if>
            <if test="firefighter != null">firefighter = #{firefighter},</if>
            <if test="firefighterDate != null">firefighter_date = #{firefighterDate},</if>
            <if test="firefighterDate == null">firefighter_date = NULL,</if>
            <if test="firefighterLifespan != null">firefighter_lifespan = #{firefighterLifespan},</if>
            <if test="firefighterLifespan == null">firefighter_lifespan = NULL,</if>
            <if test="safety != null">safety = #{safety},</if>
            <if test="safetyDate != null">safety_date = #{safetyDate},</if>
            <if test="safetyDate == null">safety_date = NULL,</if>
            <if test="safetyLifespan != null">safety_lifespan = #{safetyLifespan},</if>
            <if test="safetyLifespan == null">safety_lifespan = NULL,</if>
            <if test="tds != null">tds = #{tds},</if>
            <if test="tdsDate != null">tds_date = #{tdsDate},</if>
            <if test="tdsDate == null">tds_date = NULL,</if>
            <if test="tdsLifespan != null">tds_lifespan = #{tdsLifespan},</if>
            <if test="tdsLifespan == null">tds_lifespan = NULL,</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="updateTWorkcertificateCbpsByStaffId" parameterType="TWorkcertificateCbps">
        update t_workcertificate_cbps
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="name != null">name = #{name},</if>
            <if test="classes != null">classes = #{classes},</if>
            <if test="container != null">container = #{container},</if>
            <if test="containerDate != null">container_date = #{containerDate},</if>
            <if test="containerDate == null">container_date = NULL,</if>
            <if test="containerLifespan != null">container_lifespan = #{containerLifespan},</if>
            <if test="containerLifespan == null">container_lifespan = NULL,</if>
            <if test="pipe != null">pipe = #{pipe},</if>
            <if test="pipeDate != null">pipe_date = #{pipeDate},</if>
            <if test="pipeDate == null">pipe_date = NULL,</if>
            <if test="pipeLifespan != null">pipe_lifespan = #{pipeLifespan},</if>
            <if test="pipeLifespan == null">pipe_lifespan = NULL,</if>
            <if test="worker != null">worker = #{worker},</if>
            <if test="workerDate != null">worker_date = #{workerDate},</if>
            <if test="workerDate == null">worker_date = NULL,</if>
            <if test="workerLifespan != null">worker_lifespan = #{workerLifespan},</if>
            <if test="workerLifespan == null">worker_lifespan = NULL,</if>
            <if test="foreman != null">foreman = #{foreman},</if>
            <if test="foremanDate != null">foreman_date = #{foremanDate},</if>
            <if test="foremanDate == null">foreman_date = NULL,</if>
            <if test="foremanLifespan != null">foreman_lifespan = #{foremanLifespan},</if>
            <if test="foremanLifespan == null">foreman_lifespan = NULL,</if>
            <if test="ac != null">ac = #{ac},</if>
            <if test="acDate != null">ac_date = #{acDate},</if>
            <if test="acDate == null">ac_date = NULL,</if>
            <if test="acLifespan != null">ac_lifespan = #{acLifespan},</if>
            <if test="acLifespan == null">ac_lifespan = NULL,</if>
            <if test="firefighter != null">firefighter = #{firefighter},</if>
            <if test="firefighterDate != null">firefighter_date = #{firefighterDate},</if>
            <if test="firefighterDate == null">firefighter_date = NULL,</if>
            <if test="firefighterLifespan != null">firefighter_lifespan = #{firefighterLifespan},</if>
            <if test="firefighterLifespan == null">firefighter_lifespan = NULL,</if>
            <if test="safety != null">safety = #{safety},</if>
            <if test="safetyDate != null">safety_date = #{safetyDate},</if>
            <if test="safetyDate == null">safety_date = NULL,</if>
            <if test="safetyLifespan != null">safety_lifespan = #{safetyLifespan},</if>
            <if test="safetyLifespan == null">safety_lifespan = NULL,</if>
            <if test="tds != null">tds = #{tds},</if>
            <if test="tdsDate != null">tds_date = #{tdsDate},</if>
            <if test="tdsDate == null">tds_date = NULL,</if>
            <if test="tdsLifespan != null">tds_lifespan = #{tdsLifespan},</if>
            <if test="tdsLifespan == null">tds_lifespan = NULL,</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 employeeid = #{employeeid}
    </update>


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

    <update id="deleteTWorkcertificateCbpsByStaffId" parameterType="String">
        update t_workcertificate_cbps set del_flag = 2 where EMPLOYEEID = #{staffId}
    </update>



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


    <select id="queryNeedInsertIds" resultType="String">
        SELECT a.STAFFID FROM "T_STAFFMGR" a where DEPT_ID = 10012 and a.del_flag = 0 and a.unit = 30
            minus
        SELECT b.EMPLOYEEID from T_WORKCERTIFICATE_CBPS b where b.del_flag = 0
    </select>

    <select id="queryNeedDeleteIds" resultType="String">
        SELECT b.EMPLOYEEID from T_WORKCERTIFICATE_CBPS b where b.del_flag = 0
            minus
        SELECT a.STAFFID FROM "T_STAFFMGR" a where DEPT_ID = 10012 and a.del_flag = 0 and a.unit = 30
    </select>


    <select id="selectDateNotNull" resultType="map">
        SELECT
            COUNT(CASE WHEN container_date IS NOT NULL THEN 1 END) AS container,
            COUNT(CASE WHEN pipe_date IS NOT NULL THEN 1 END) AS pipe,
            COUNT(CASE WHEN worker_date IS NOT NULL THEN 1 END) AS worker,
            COUNT(CASE WHEN foreman_date IS NOT NULL THEN 1 END) AS foreman,
            COUNT(CASE WHEN ac_date IS NOT NULL THEN 1 END) AS ac,
            COUNT(CASE WHEN firefighter_date IS NOT NULL THEN 1 END) AS firefighter,
            COUNT(CASE WHEN safety_date IS NOT NULL THEN 1 END) AS safety,
            COUNT(CASE WHEN tds_date IS NOT NULL THEN 1 END) AS tds
        FROM
            t_workcertificate_cbps
    </select>

    <select id="selectDateWarn" resultType="map">
        SELECT
        COUNT(CASE WHEN container_LIFESPAN IS NOT NULL AND container_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS container,
        COUNT(CASE WHEN pipe_LIFESPAN IS NOT NULL AND pipe_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS pipe,
        COUNT(CASE WHEN worker_LIFESPAN IS NOT NULL AND worker_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS worker,
        COUNT(CASE WHEN foreman_LIFESPAN IS NOT NULL AND foreman_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS foreman,
        COUNT(CASE WHEN ac_LIFESPAN IS NOT NULL AND ac_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS ac,
        COUNT(CASE WHEN firefighter_LIFESPAN IS NOT NULL AND firefighter_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS firefighter,
        COUNT(CASE WHEN safety_LIFESPAN IS NOT NULL AND safety_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS safety,
        COUNT(CASE WHEN tds_LIFESPAN IS NOT NULL AND tds_LIFESPAN <![CDATA[ <= ]]> sysdate + 30 THEN 1 END) AS tds
        FROM
        t_workcertificate_cbps
    </select>




</mapper>
