<?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.plant.mapper.TStaffmgrMapper">

    <resultMap type="TStaffmgr" id="TStaffmgrResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="staffid"    column="staffid"    />
        <result property="name"    column="name"    />
        <result property="sex"    column="sex"    />
        <result property="birthday"    column="birthday"    />
        <result property="unit"    column="unit"    />
        <result property="team"    column="team"    />
        <result property="actualpost"    column="actualpost"    />
        <result property="contact"    column="contact"    />
        <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="deptName" column="dept_name" />
        <result property="education"    column="education"    />
        <result property="enAbility"    column="en_ability"    />
        <result property="mail"    column="mail"    />
        <result property="skillScore"    column="skill_score"    />
        <result property="accidentNum"    column="accident_num"    />
        <result property="photo"    column="photo"    />
        <result property="pId"    column="p_id"    />
        <result property="specialDuty"    column="special_duty"    />
        <result property="leftDate"    column="left_date"    />
        <result property="idCard"    column="id_card"    />
        <result property="address"    column="address"    />
        <result property="speciality"    column="speciality"    />
    </resultMap>

    <sql id="selectTStaffmgrVo">
         select d.id_card,d.address,d.speciality,d.id, d.plant_code,d.left_date, d.staffid, d.name, d.photo, d.sex, d.unit, d.team, d.actualpost, d.contact, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks, d.education, d.en_ability, d.accident_num, d.mail, d.skill_score, d.p_id , d.special_duty, s.dept_name from t_staffmgr d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <!-- 查询导师下拉列表 -->
    <select id="selectMentorList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        select d.id, s2.DICT_LABEL,d.plant_code, d.staffid, d.name, d.photo, d.sex,  d.unit,
               d.team, d.actualpost, d.contact, d.del_flag, d.creater_code, d.createdate, d.updater_code,
               d.updatedate, d.dept_id, d.remarks, d.education, d.en_ability, d.accident_num, d.mail,
               d.skill_score, d.p_id , d.special_duty, s.dept_name

        from t_staffmgr d
        left join sys_dept s on s.dept_id = d.dept_id
        Left join SYS_DICT_DATA s0 on d.UNIT = s0.DICT_VALUE and s0.DICT_TYPE ='STAFF_UNIT'
        LEFT JOIN SYS_DICT_DATA s2 on d.ACTUALPOST = s2.DICT_VALUE and s2.DICT_TYPE = 'ACTUALPOST'
        LEFT JOIN SYS_DICT_DATA s1 on d.team = s1.DICT_VALUE and s1.DICT_TYPE = 'TEAM_DIVIDE'

        where
        (s2.DICT_LABEL like '%主管%'
        or s2.DICT_LABEL like '%经理%'
        or s2.DICT_LABEL like '%工程师%')
        and d.DEL_FLAG!=9
    </select>

    <select id="selectTStaffmgrList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        Left join SYS_DICT_DATA s0 on d.UNIT = s0.DICT_VALUE and s0.DICT_TYPE ='STAFF_UNIT'
        LEFT JOIN SYS_DICT_DATA s2 on d.ACTUALPOST = s2.DICT_VALUE and s2.DICT_TYPE = 'ACTUALPOST'
        LEFT JOIN SYS_DICT_DATA s1 on d.team = s1.DICT_VALUE and s1.DICT_TYPE = 'TEAM_DIVIDE'
        <where>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="specialDuty != null  and specialDuty != ''"> and special_duty like concat(concat('%', #{specialDuty}), '%')</if>
            <if test="units != null  and units != ''">
                and
                unit in
                <foreach collection="units.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
             </if>
            <if test="team != null  and team != ''"> and team = #{team}</if>
            <if test="teams != null  and teams != ''">
                and
                team in
                <foreach collection="teams.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
             </if>
            <if test="actualpost != null  and actualpost != ''"> and actualpost = #{actualpost}</if>
            <if test="actualposts != null  and actualposts != ''">
                and
                actualpost in
                <foreach collection="actualposts.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>

            </if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="educations != null  and educations != ''">
                and
                education in
                <foreach collection="educations.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
             </if>
            <if test="enAbility != null  and enAbility != ''"> and en_ability = #{enAbility}</if>
            <if test="enAbilitys != null  and enAbilitys != ''">
                and
                en_ability in
                <foreach collection="enAbilitys.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="haveEmail != null  and haveEmail != ''"> and mail IS NOT NULL</if>
            <if test="skillevaluation != null"> and team = 10
                or team = 12
                or team = 14
                or team = 16
            </if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        ORDER BY s0.DICT_SORT, s1.DICT_SORT,s2.DICT_SORT,d.staffid
    </select>


    <select id="selectTStaffmgrListAll" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        Left join SYS_DICT_DATA s0 on d.UNIT = s0.DICT_VALUE and s0.DICT_TYPE ='STAFF_UNIT'
        LEFT JOIN SYS_DICT_DATA s2 on d.ACTUALPOST = s2.DICT_VALUE and s2.DICT_TYPE = 'ACTUALPOST'
        LEFT JOIN SYS_DICT_DATA s1 on d.team = s1.DICT_VALUE and s1.DICT_TYPE = 'TEAM_DIVIDE'
        <where>
            and d.del_flag in (0 , 9)
        </where>
        ORDER BY s0.DICT_SORT, s1.DICT_SORT ,s2.DICT_SORT
    </select>

    <select id="selectRecordList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        LEFT JOIN SYS_DICT_DATA s11 on d.ACTUALPOST = s11.DICT_VALUE and s11.DICT_TYPE = 'ACTUALPOST'
        <where>
            <if test="units != null  and units != ''">
                and
                unit in
                <foreach collection="units.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        ORDER BY s11.DICT_SORT, d.id
    </select>

    <select id="selectList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        <where>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTaskList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        <where>
            and d.del_flag = 0
        </where>
    </select>


    <select id="selectLeftTStaffmgrList" parameterType="TStaffmgr" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        <where>
            and d.del_flag = 9
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTStaffmgrById" parameterType="Long" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        where id = #{id}
    </select>

    <select id="selectTStaffmgrByStaffId" parameterType="String" resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        where staffid = #{staffid}
    </select>

    <insert id="insertTStaffmgr" parameterType="TStaffmgr" useGeneratedKeys="true" keyProperty="id">

        insert into t_staffmgr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="staffid != null and staffid != ''">staffid,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="sex != null and sex != ''">sex,</if>
            <if test="birthday != null">birthday,</if>
            <if test="unit != null">unit,</if>
            <if test="team != null">team,</if>
            <if test="actualpost != null">actualpost,</if>
            <if test="contact != null">contact,</if>
            <if test="education != null">education,</if>
            <if test="enAbility != null">en_ability,</if>
            <if test="mail != null">mail,</if>
            <if test="accidentNum != null">accident_num,</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="skillScore != null">skill_score,</if>
            <if test="photo != null">photo,</if>
            <if test="pId != null">p_id,</if>
            <if test="specialDuty != null">special_duty,</if>
            <if test="idCard != null">id_card,</if>
            <if test="address != null">address,</if>
            <if test="speciality != null">speciality,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="staffid != null and staffid != ''">#{staffid},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="sex != null and sex != ''">#{sex},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="unit != null">#{unit},</if>
            <if test="team != null">#{team},</if>
            <if test="actualpost != null">#{actualpost},</if>
            <if test="contact != null">#{contact},</if>
            <if test="education != null">#{education},</if>
            <if test="enAbility != null">#{enAbility},</if>
            <if test="mail != null">#{mail},</if>
            <if test="accidentNum != null">#{accidentNum},</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="skillScore != null">#{skillScore},</if>
            <if test="photo != null">#{photo},</if>
            <if test="pId != null">#{pId},</if>
            <if test="specialDuty != null">#{specialDuty},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="address != null">#{address},</if>
            <if test="speciality != null">#{speciality},</if>
        </trim>
    </insert>

    <update id="updateTStaffmgr" parameterType="TStaffmgr">
        update t_staffmgr
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="staffid != null and staffid != ''">staffid = #{staffid},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sex != null and sex != ''">sex = #{sex},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="team != null">team = #{team},</if>
            <if test="actualpost != null">actualpost = #{actualpost},</if>
            <if test="contact != null">contact = #{contact},</if>
            <if test="education != null">education = #{education},</if>
            <if test="enAbility != null">en_ability = #{enAbility},</if>
            <if test="accidentNum != null">accident_num = #{accidentNum},</if>
            <if test="mail != null">mail = #{mail},</if>
            <if test="skillScore != null">skill_score = #{skillScore},</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="photo != null">photo = #{photo},</if>
            <if test="pId != null">p_id = #{pId},</if>
            <if test="specialDuty != null">special_duty = #{specialDuty},</if>
            <if test="leftDate != null">left_date = #{leftDate},</if>
            <if test="idCard != null">id_card=#{idCard},</if>
            <if test="address != null">address=#{address},</if>
            <if test="speciality != null">speciality=#{speciality},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="deleteLeftTStaffmgrByIds" parameterType="Long">
        update t_staffmgr set del_flag = 9 , LEFT_DATE = now()  where id = #{id}
    </update>

    <update id="reLeftTStaffmgrByIds" parameterType="Long">
        update t_staffmgr set del_flag = 0 , LEFT_DATE = null where id = #{id}
    </update>
    <update id="deleteTStaffmgrByIds" parameterType="String">
        update t_staffmgr set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectEduData" resultType="com.ruoyi.project.common.DataEntity">
            SELECT count(1) as dataNum,d.EDUCATION as dataName FROM T_STAFFMGR d
        <where>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="team != null  and team != ''"> and team = #{team}</if>
            <if test="actualpost != null  and actualpost != ''"> and actualpost = #{actualpost}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="enAbility != null  and enAbility != ''"> and en_ability = #{enAbility}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY EDUCATION
        order by dataName
    </select>

    <select id="selectEngData" resultType="com.ruoyi.project.common.DataEntity">
        SELECT count(1) as dataNum,d.en_ability as dataName FROM T_STAFFMGR d
        <where>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="team != null  and team != ''"> and team = #{team}</if>
            <if test="actualpost != null  and actualpost != ''"> and actualpost = #{actualpost}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="enAbility != null  and enAbility != ''"> and en_ability = #{enAbility}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY en_ability
        order by dataName
    </select>

    <select id="selectTeamData" resultType="com.ruoyi.project.common.DataEntity">
        SELECT count(1) as dataNum,d.team as dataName FROM T_STAFFMGR d
        <where>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="team != null  and team != ''"> and team = #{team}</if>
            <if test="actualpost != null  and actualpost != ''"> and actualpost = #{actualpost}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="enAbility != null  and enAbility != ''"> and en_ability = #{enAbility}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        GROUP BY team
        order by dataName
    </select>


    <select id="selectTMentorStaffmgrByPost"  resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
            LEFT JOIN SYS_DICT_DATA s11 on d.ACTUALPOST = s11.DICT_VALUE and s11.DICT_TYPE = 'ACTUALPOST'
        where
            d.dept_id = #{deptId}
            and d.del_flag = 0
            and (s11.DICT_LABEL like '%主管%'
            or s11.DICT_LABEL like '%经理%'
            or s11.DICT_LABEL like '%工程师%'
            )
    </select>

    <select id="selectTStaffmgrByPost"  resultMap="TStaffmgrResult">
        <include refid="selectTStaffmgrVo"/>
        LEFT JOIN SYS_DICT_DATA s11 on d.ACTUALPOST = s11.DICT_VALUE and s11.DICT_TYPE = 'ACTUALPOST'
        where
        d.dept_id = #{deptId}
        and d.del_flag = 0
        and (s11.DICT_LABEL like '%主操%'
        or s11.DICT_LABEL like '%班长%'
        or s11.DICT_LABEL like '%工长%'
        )
    </select>

    <select id="selectStaffByTeam"  resultMap="TStaffmgrResult">
        select  d.name,s11.DICT_LABEL team from t_staffmgr d
        LEFT JOIN SYS_DICT_DATA s11 on d.team = s11.DICT_VALUE and s11.DICT_TYPE = 'TEAM_DIVIDE'
        <where>
            <if test="team != null  and team != ''"> and team = #{team}</if>
        </where>
        and d.del_flag = 0
    </select>
</mapper>
