<?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.SurProjectWorkAttendanceMapper">
    
    <resultMap type="SurProjectWorkAttendance" id="SurProjectWorkAttendanceResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="servicePersonnel"    column="service_personnel"    />
        <result property="supervisorPersonnel"    column="supervisor_personnel"    />
        <result property="contractorPersonnel"    column="contractor_personnel"    />
        <result property="workDate"    column="work_date"    />
        <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"    />
    </resultMap>

    <sql id="selectSurProjectWorkAttendanceVo">
        select id, project_id, dept_id, service_personnel, supervisor_personnel, contractor_personnel, work_date, is_del, create_by, create_time, update_by, update_time, remark from sur_project_work_attendance
    </sql>

    <select id="selectSurProjectWorkAttendanceList" parameterType="SurProjectWorkAttendance" resultMap="SurProjectWorkAttendanceResult">
        SELECT
            spwa.id,
            spwa.project_id,
            sp.projectName as project_name,
            spwa.dept_id,
            d.dept_name,
            IFNULL(spwa.service_personnel, 0) AS service_personnel,
            IFNULL(spwa.supervisor_personnel, 0) AS supervisor_personnel,
            IFNULL(spwa.contractor_personnel, 0) AS contractor_personnel,
            spwa.work_date,
            spwa.is_del,
            spwa.create_by,
            spwa.create_time,
            spwa.update_by,
            spwa.update_time,
            spwa.remark
        FROM
            sur_project_work_attendance spwa
            left join sur_project sp on sp.id=spwa.project_id
            left join sys_dept d on d.dept_id=spwa.dept_id
            <!--监理单位/总包公司/分包单位-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> left join sur_project_unit_info spui on spui.projectId = sp.id</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> left join sur_project_userinfo spu on spu.project_id = sp.id</if>
        <where>
            and spwa.is_del=0
            <if test="projectId != null "> and spwa.project_id = #{projectId}</if>
            <if test="projectName != null and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null "> and spwa.dept_id = #{deptId}</if>
            <if test="deptName != null and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="params.beginWorkDate != null and params.beginWorkDate != '' and params.endWorkDate != null and params.endWorkDate != ''"> and spwa.work_date between #{params.beginWorkDate} and #{params.endWorkDate}</if>
            <if test="workDate != null"> and date(spwa.work_date)=date(#{workDate})</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 spui.unitId = #{nowDept}</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and spu.user_id = #{nowUser} and spu.is_del=0</if>
        </where>
    </select>

    <select id="selectBgscreenWorkAttendanceList" parameterType="SurProjectWorkAttendance" resultMap="SurProjectWorkAttendanceResult">
        SELECT
        <if test="projectId != null ">spwa.project_id,</if>
        <if test="deptId != null and projectId == null ">spwa.dept_id,</if>
        SUM(spwa.service_personnel) AS service_personnel,
        SUM(spwa.supervisor_personnel) AS supervisor_personnel,
        SUM(spwa.contractor_personnel) AS contractor_personnel
        FROM
        sur_project_work_attendance spwa
        <where>
            and spwa.is_del=0
            <if test="projectId != null "> and spwa.project_id = #{projectId}</if>
            <if test="deptId != null "> and spwa.dept_id = #{deptId}</if>
            <if test="workDate != null"> and date(spwa.work_date)=date(#{workDate})</if>
        </where>
        <if test="projectId != null "> group by spwa.project_id</if>
        <if test="deptId != null and projectId == null"> group by spwa.dept_id</if>
    </select>
    
    <select id="selectSurProjectWorkAttendanceById" parameterType="Long" resultMap="SurProjectWorkAttendanceResult">
        <include refid="selectSurProjectWorkAttendanceVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectWorkAttendance" parameterType="SurProjectWorkAttendance" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_work_attendance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="servicePersonnel != null">service_personnel,</if>
            <if test="supervisorPersonnel != null">supervisor_personnel,</if>
            <if test="contractorPersonnel != null">contractor_personnel,</if>
            <if test="workDate != null">work_date,</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="servicePersonnel != null">#{servicePersonnel},</if>
            <if test="supervisorPersonnel != null">#{supervisorPersonnel},</if>
            <if test="contractorPersonnel != null">#{contractorPersonnel},</if>
            <if test="workDate != null">#{workDate},</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="updateSurProjectWorkAttendance" parameterType="SurProjectWorkAttendance">
        update sur_project_work_attendance
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="servicePersonnel != null">service_personnel = #{servicePersonnel},</if>
            <if test="supervisorPersonnel != null">supervisor_personnel = #{supervisorPersonnel},</if>
            <if test="contractorPersonnel != null">contractor_personnel = #{contractorPersonnel},</if>
            <if test="workDate != null">work_date = #{workDate},</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="deleteSurProjectWorkAttendanceById" parameterType="Long">
        update sur_project_work_attendance set is_del=1 where id = #{id}
    </update>

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

    <select id="selectByDate" parameterType="SurProjectWorkAttendance" resultMap="SurProjectWorkAttendanceResult">
        <include refid="selectSurProjectWorkAttendanceVo"/>
        <where>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="createTime != null"> and date(create_time)=date(#{createTime}) </if>
             and is_del = 0
        </where>
    </select>

</mapper>