<?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.yanzhu.jh.project.mapper.SurProjectWorkSpecialMapper">
    
    <resultMap type="SurProjectWorkSpecial" id="SurProjectWorkSpecialResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="specialType"    column="special_type"    />
        <result property="specialTypeName"    column="special_type_name"    />
        <result property="name"    column="name"    />
        <result property="sex"    column="sex"    />
        <result property="age"    column="age"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="idNumber"    column="id_number"    />
        <result property="isCredential"    column="is_credential"    />
        <result property="credentialNumber"    column="credential_number"    />
        <result property="credentialExpirationTime"    column="credential_expiration_time"    />
        <result property="credentialFile"    column="credential_file"    />
        <result property="credentialType"    column="credential_type"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="projectName"    column="projectName"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectSurProjectWorkSpecialVo">
        select spwp.id, spwp.project_id, spwp.dept_id, spwp.special_type, sdd.dict_label as special_type_name, spwp.name, spwp.sex, spwp.age, spwp.phone_number, spwp.id_number, spwp.is_credential, spwp.credential_number, spwp.credential_expiration_time, spwp.credential_file, spwp.credential_type, spwp.is_del, spwp.create_by, spwp.create_time, spwp.update_by, spwp.update_time, spwp.remark, sp.projectName, sd.dept_name from sur_project_work_special spwp
        left join sur_project sp on sp.id = spwp.project_id
        left join sys_dept sd on sd.dept_id = spwp.dept_id
        left join sys_dict_data sdd on sdd.dict_type = 'project_special_type' and sdd.dict_value = spwp.special_type
    </sql>

    <select id="selectSurProjectWorkSpecialList" parameterType="SurProjectWorkSpecial" resultMap="SurProjectWorkSpecialResult">
        select spwp.id, spwp.project_id, spwp.dept_id, spwp.special_type, sdd.dict_label as special_type_name, spwp.name, spwp.sex, spwp.age, spwp.phone_number, spwp.id_number, spwp.is_credential, spwp.credential_number, spwp.credential_expiration_time, spwp.credential_file, spwp.credential_type, spwp.is_del, spwp.create_by, spwp.create_time, spwp.update_by, spwp.update_time, spwp.remark, sp.projectName, sd.dept_name from sur_project_work_special spwp
        left join sur_project sp on sp.id = spwp.project_id
        left join sys_dept sd on sd.dept_id = spwp.dept_id
        left join sys_dict_data sdd on sdd.dict_type = 'project_special_type' and sdd.dict_value = spwp.special_type
        <where>
            and spwp.is_del=0
            <if test="projectId != null "> and spwp.project_id = #{projectId}</if>
            <if test="deptId != null "> and spwp.dept_id = #{deptId}</if>
            <if test="name != null  and name != ''"> and spwp.name like concat('%', #{name}, '%')</if>
            <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="isCredential != null  and isCredential != ''"> and spwp.is_credential = #{isCredential}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and spwp.phone_number like concat('%', #{phoneNumber}, '%')</if>
            <if test="credentialNumber != null  and credentialNumber != ''"> and spwp.credential_number like concat('%', #{credentialNumber}, '%')</if>
            <if test="params.beginCredentialExpirationTime != null and params.beginCredentialExpirationTime != '' and params.endCredentialExpirationTime != null and params.endCredentialExpirationTime != ''"> and spwp.credential_expiration_time between #{params.beginCredentialExpirationTime} and #{params.endCredentialExpirationTime}</if>
            <if test="isDel != null  and isDel != ''"> and spwp.is_del = #{isDel}</if>
            <if test="credentialType != null  and credentialType != ''"> and spwp.credential_type = #{credentialType}</if>
            <if test="specialType != null  and credentialType != ''"> and spwp.special_type = #{specialType}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and sp.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and sp.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
            <if test='activeName == "wgq"'> and date(spwp.credential_expiration_time) <![CDATA[ > ]]> date(NOW())</if>
            <if test='activeName == "ygq"'> and date(spwp.credential_expiration_time) <![CDATA[ <= ]]> date(NOW())</if>
        </where>
    </select>

    <select id="selectSurProjectWorkSpecialExpired" parameterType="SurProjectWorkSpecial" resultType="integer">
        select count(1) as total from sur_project_work_special spwp
        left join sur_project sp on sp.id = spwp.project_id
        <where>
            and spwp.is_del=0
            and date(spwp.credential_expiration_time) <![CDATA[ <= ]]> date(NOW())
            <if test="projectId != null "> and spwp.project_id = #{projectId}</if>
            <if test="deptId != null "> and spwp.dept_id = #{deptId}</if>
            <if test="name != null  and name != ''"> and spwp.name like concat('%', #{name}, '%')</if>
            <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="isCredential != null  and isCredential != ''"> and spwp.is_credential = #{isCredential}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and spwp.phone_number like concat('%', #{phoneNumber}, '%')</if>
            <if test="credentialNumber != null  and credentialNumber != ''"> and spwp.credential_number like concat('%', #{credentialNumber}, '%')</if>
            <if test="params.beginCredentialExpirationTime != null and params.beginCredentialExpirationTime != '' and params.endCredentialExpirationTime != null and params.endCredentialExpirationTime != ''"> and spwp.credential_expiration_time between #{params.beginCredentialExpirationTime} and #{params.endCredentialExpirationTime}</if>
            <if test="isDel != null  and isDel != ''"> and spwp.is_del = #{isDel}</if>
            <if test="credentialType != null  and credentialType != ''"> and spwp.credential_type = #{credentialType}</if>
            <if test="specialType != null  and credentialType != ''"> and spwp.special_type = #{specialType}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and sp.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and sp.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
        </where>
    </select>

    <select id="selectSurProjectWorkSpecialNotExpired" parameterType="SurProjectWorkSpecial" resultType="integer">
        select count(1) as total from sur_project_work_special spwp
        left join sur_project sp on sp.id = spwp.project_id
        <where>
            and spwp.is_del=0
            and date(spwp.credential_expiration_time) <![CDATA[ > ]]> date(NOW())
            <if test="projectId != null "> and spwp.project_id = #{projectId}</if>
            <if test="deptId != null "> and spwp.dept_id = #{deptId}</if>
            <if test="name != null  and name != ''"> and spwp.name like concat('%', #{name}, '%')</if>
            <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="isCredential != null  and isCredential != ''"> and spwp.is_credential = #{isCredential}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and spwp.phone_number like concat('%', #{phoneNumber}, '%')</if>
            <if test="credentialNumber != null  and credentialNumber != ''"> and spwp.credential_number like concat('%', #{credentialNumber}, '%')</if>
            <if test="params.beginCredentialExpirationTime != null and params.beginCredentialExpirationTime != '' and params.endCredentialExpirationTime != null and params.endCredentialExpirationTime != ''"> and spwp.credential_expiration_time between #{params.beginCredentialExpirationTime} and #{params.endCredentialExpirationTime}</if>
            <if test="isDel != null  and isDel != ''"> and spwp.is_del = #{isDel}</if>
            <if test="credentialType != null  and credentialType != ''"> and spwp.credential_type = #{credentialType}</if>
            <if test="specialType != null  and credentialType != ''"> and spwp.special_type = #{specialType}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and sp.id in (select DISTINCT spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and sp.id in (select DISTINCT spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
        </where>
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        SELECT
            spp.special_type AS type,
            sdd.remark AS typeName,
            count(1) AS total
        FROM
            sur_project_work_special spp
            LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'project_special_type'
            AND sdd.dict_value = spp.special_type
        WHERE
            spp.is_del = 0
            AND spp.project_id = #{projectId}
        GROUP BY
            spp.special_type
        ORDER By
            sdd.dict_sort
    </select>

    <!--查询项目特种人员统计列表-->
    <select id="selectBgscreenWorkSpecialList" parameterType="SurProjectWorkSpecial" resultType="map">
        SELECT
            spws.project_id as projectId,
            sp.projectName,
            count(1) AS total,
            IFNULL(expired.expired, 0) AS expired,
            IFNULL(void.void, 0) AS void
        FROM
            sur_project_work_special spws
        LEFT JOIN sur_project sp ON sp.id = spws.project_id
        LEFT JOIN (
            SELECT
                s1.project_id AS pid,
                count(1) AS expired
            FROM
                sur_project_work_special s1
                left join sur_project sp1 on sp1.id = s1.project_id
            WHERE
                s1.is_del = 0
                <if test="nowDept != null and nowDept != ''"> and sp1.deptId = #{nowDept}</if>
                <if test="projectId != null "> and s1.project_id = #{projectId}</if>
                <if test="prjIds !=null and prjIds.size()>0">
                    and s1.project_id in
                    <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>

        AND s1.credential_expiration_time > NOW()
                AND DATE_SUB(
                    s1.credential_expiration_time,
                    INTERVAL 1 MONTH
                    ) <![CDATA[ <= ]]> NOW()
            GROUP BY
                s1.project_id
        ) expired ON expired.pid = spws.project_id
        LEFT JOIN (
            SELECT
                s2.project_id AS pid,
                count(1) AS void
            FROM
                sur_project_work_special s2
                left join sur_project sp2 on sp2.id = s2.project_id
            WHERE
                s2.is_del = 0
                <if test="nowDept != null and nowDept != ''"> and sp2.deptId = #{nowDept}</if>
                <if test="projectId != null "> and s2.project_id = #{projectId}</if>
                <if test="prjIds !=null and prjIds.size()>0">
                    and  s2.project_id in
                    <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                AND s2.credential_expiration_time <![CDATA[ < ]]> NOW()
            GROUP BY
                s2.project_id
        ) void ON void.pid = spws.project_id
        WHERE
            spws.is_del = 0
            <if test="nowDept != null and nowDept != ''"> and sp.deptId = #{nowDept}</if>
            <if test="projectId != null "> and spws.project_id = #{projectId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and  spws.project_id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        GROUP BY
            spws.project_id
        ORDER BY
            sp.projectSort ASC
    </select>
    
    <select id="selectSurProjectWorkSpecialById" parameterType="Long" resultMap="SurProjectWorkSpecialResult">
        <include refid="selectSurProjectWorkSpecialVo"/>
        where spwp.id = #{id}
    </select>
        
    <insert id="insertSurProjectWorkSpecial" parameterType="SurProjectWorkSpecial" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_work_special
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="specialType != null">special_type,</if>
            <if test="name != null">name,</if>
            <if test="sex != null">sex,</if>
            <if test="age != null">age,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="idNumber != null">id_number,</if>
            <if test="isCredential != null">is_credential,</if>
            <if test="credentialNumber != null">credential_number,</if>
            <if test="credentialExpirationTime != null">credential_expiration_time,</if>
            <if test="credentialFile != null">credential_file,</if>
            <if test="credentialType != null">credential_type,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="specialType != null">#{specialType},</if>
            <if test="name != null">#{name},</if>
            <if test="sex != null">#{sex},</if>
            <if test="age != null">#{age},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="idNumber != null">#{idNumber},</if>
            <if test="isCredential != null">#{isCredential},</if>
            <if test="credentialNumber != null">#{credentialNumber},</if>
            <if test="credentialExpirationTime != null">#{credentialExpirationTime},</if>
            <if test="credentialFile != null">#{credentialFile},</if>
            <if test="credentialType != null">#{credentialType},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectWorkSpecial" parameterType="SurProjectWorkSpecial">
        update sur_project_work_special
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="specialType != null">special_type = #{specialType},</if>
            <if test="name != null">name = #{name},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="age != null">age = #{age},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="idNumber != null">id_number = #{idNumber},</if>
            <if test="isCredential != null">is_credential = #{isCredential},</if>
            <if test="credentialNumber != null">credential_number = #{credentialNumber},</if>
            <if test="credentialExpirationTime != null">credential_expiration_time = #{credentialExpirationTime},</if>
            <if test="credentialFile != null">credential_file = #{credentialFile},</if>
            <if test="credentialType != null">credential_type = #{credentialType},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSurProjectWorkSpecialById" parameterType="Long">
        update sur_project_work_special set is_del=1 where id = #{id}
    </update>

    <update id="deleteSurProjectWorkSpecialByIds" parameterType="String">
        update sur_project_work_special set is_del=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>