<?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.SurProjectDeptWroksMapper">
    
    <resultMap type="SurProjectDeptWroks" id="SurProjectDeptWroksResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <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="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"/>
    </resultMap>

    <sql id="selectSurProjectDeptWroksVo">
        select dw.id, dw.project_id, dw.dept_id, d.dept_name, dw.service_personnel, dw.supervisor_personnel, dw.contractor_personnel, dw.create_by, dw.create_time, dw.update_by, dw.update_time, dw.remark from sur_project_dept_wroks dw
        left join sys_dept d on d.dept_id=dw.dept_id
    </sql>

    <select id="selectSurProjectDeptWroksList" parameterType="SurProjectDeptWroks" resultMap="SurProjectDeptWroksResult">
        <include refid="selectSurProjectDeptWroksVo"/>
        <where>
            <if test="projectId != null "> and dw.project_id = #{projectId}</if>
            <if test="deptId != null "> and dw.dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="selectSurProjectDeptWroksListv2" parameterType="SurProjectDeptWroks" resultMap="SurProjectDeptWroksResult">
        SELECT
            spdw.id,
            sp.id AS project_id,
            spui.unitId AS dept_id,
            spui.unitName AS dept_name,
            IFNULL(spdw.service_personnel, 0) AS service_personnel,
            IFNULL(
            spdw.supervisor_personnel,
            0
            ) AS supervisor_personnel,
            IFNULL(
            spdw.contractor_personnel,
            0
            ) AS contractor_personnel
        FROM
            sur_project sp
            LEFT JOIN sur_project_unit_info spui ON spui.projectId = sp.id and spui.unitType = 2
            LEFT JOIN sur_project_dept_wroks spdw ON spui.unitId = spdw.dept_id
            AND spdw.project_id = sp.id
        <where>
            <if test="projectId != null "> and sp.id = #{projectId}</if>
            <if test="nowDept != null  and nowDept != ''"> and spui.unitId = #{nowDept}</if>
        </where>
    </select>

    <select id="findSumByProjectId" parameterType="Long" resultType="map">
        SELECT
            SUM(spdw.contractor_personnel) as contractorPersonnel,
            SUM(spdw.service_personnel) as servicePersonnel,
            SUM(spdw.supervisor_personnel) as supervisorPersonnel
        FROM
            sur_project_dept_wroks spdw
        WHERE
            spdw.is_del=0
            AND spdw.project_id = #{projectId}
        GROUP BY
            spdw.project_id
    </select>

    <select id="selectBgscreenDeptWroksList" parameterType="SurProjectDeptWroks" resultMap="SurProjectDeptWroksResult">
        SELECT
        <if test="projectId != null ">spdw.project_id,</if>
        <if test="deptId != null and projectId == null ">sp.deptId as dept_id,</if>
        SUM(spdw.service_personnel) AS service_personnel,
        SUM(spdw.supervisor_personnel) AS supervisor_personnel,
        SUM(spdw.contractor_personnel) AS contractor_personnel
        FROM
        sur_project_dept_wroks spdw
        left join sur_project sp on spdw.project_id=sp.id
        <where>
            and spdw.is_del=0
            <if test="projectId != null  and projectId>0"> and spdw.project_id = #{projectId}</if>
            <if test="deptId != null  and deptId>0"> and sp.deptId = #{deptId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and spdw.project_id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        <if test="projectId != null "> group by spdw.project_id</if>
        <if test="deptId != null and projectId == null"> group by sp.deptId</if>
    </select>
    
    <select id="selectSurProjectDeptWroksById" parameterType="Long" resultMap="SurProjectDeptWroksResult">
        <include refid="selectSurProjectDeptWroksVo"/>
        where dw.id = #{id}
    </select>
        
    <insert id="insertSurProjectDeptWroks" parameterType="SurProjectDeptWroks" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_dept_wroks
        <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="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="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="updateSurProjectDeptWroks" parameterType="SurProjectDeptWroks">
        update sur_project_dept_wroks
        <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="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>

    <delete id="deleteSurProjectDeptWroksById" parameterType="Long">
        delete from sur_project_dept_wroks where id = #{id}
    </delete>

    <delete id="deleteSurProjectDeptWroksByParam" parameterType="SurProjectDeptWroks">
        delete from sur_project_dept_wroks where project_id = #{projectId} and dept_id = #{deptId}
    </delete>

    <delete id="deleteSurProjectDeptWroksByIds" parameterType="String">
        delete from sur_project_dept_wroks where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updateProjectDeptWorks1" parameterType="SurProjectDeptWroks">
        UPDATE sur_project sp
        SET sp.servicePersonnel = sp.servicePersonnel+#{servicePersonnel},
            sp.supervisorPersonnel = sp.supervisorPersonnel+#{supervisorPersonnel},
            sp.generalContractor = sp.generalContractor+#{contractorPersonnel}
        WHERE
        id = #{projectId}
    </update>

    <update id="updateProjectDeptWorks2" parameterType="SurProjectDeptWroks">
        UPDATE sur_project sp
        SET sp.servicePersonnel = sp.servicePersonnel-#{oldServicePersonnel}+#{servicePersonnel},
        sp.supervisorPersonnel = sp.supervisorPersonnel-#{oldSupervisorPersonnel}+#{supervisorPersonnel},
        sp.generalContractor = sp.generalContractor-#{oldContractorPersonnel}+#{contractorPersonnel}
        WHERE
        id = #{projectId}
    </update>
    <select id="selectList" parameterType="SurProjectDeptWroks" resultMap="SurProjectDeptWroksResult">
        SELECT spdw.*,prj.projectName,dept.dept_name FROM
            sur_project_dept_wroks spdw
                LEFT JOIN sur_project prj ON spdw.project_id=prj.id
                LEFT JOIN sys_dept dept ON spdw.dept_id=dept.dept_id
        WHERE spdw.is_del=0
        <if test="projectId != null  and projectId>0"> and spdw.project_id = #{projectId}</if>
        <if test="deptId != null  and deptId>0"> and spdw.dept_id = #{deptId}</if>
        <if test="prjIds !=null and prjIds.size()>0">
            and spdw.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY prj.projectSort
    </select>
</mapper>