<?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.SurProjectCheckingMapper">
    
    <resultMap type="SurProjectChecking" id="SurProjectCheckingResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="groupDeptId"    column="group_dept_id"    />
        <result property="checkType"    column="check_type"    />
        <result property="mainImage"    column="main_image"    />
        <result property="imageUrls"    column="image_urls"    />
        <result property="workingPositionType"    column="working_position_type"    />
        <result property="checkWorkingPosition"    column="check_working_position"    />
        <result property="checkingNum"    column="checking_num"    />
        <result property="checkResult"    column="check_result"    />
        <result property="intro"    column="intro"    />
        <result property="qualityUser"    column="quality_user"    />
        <result property="qualityUserName"    column="quality_user_name"    />
        <result property="superviseUser"    column="supervise_user"    />
        <result property="superviseUserName"    column="supervise_user_name"    />
        <result property="groupDeptUser"    column="group_dept_user"    />
        <result property="groupDeptUserName"    column="group_dept_user_name"    />
        <result property="checkingDate"    column="checking_date"    />
        <result property="checkingFiles"    column="checking_files"    />
        <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"    />
        <result property="approveStatus"    column="approve_status"    />
    </resultMap>

    <sql id="selectSurProjectCheckingVo">
        select spc.id, spc.project_id, spc.dept_id, spc.group_dept_id, spc.check_type, spc.main_image, spc.image_urls, spc.working_position_type, spc.check_working_position, spc.checking_num, spc.check_result, spc.intro, spc.quality_user, spc.quality_user_name, spc.supervise_user, spc.supervise_user_name, spc.group_dept_user, spc.group_dept_user_name, spc.checking_date, spc.checking_files, spc.is_del, spc.create_by, spc.create_time, spc.update_by, spc.update_time, spc.remark, spc.approve_status, sp.projectName, sd.dept_name from sur_project_checking spc
        left join sur_project sp on spc.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spc.dept_id
    </sql>

    <select id="selectSurProjectCheckingList" parameterType="SurProjectChecking" resultMap="SurProjectCheckingResult">
        select spc.id, spc.project_id, spc.dept_id, spc.group_dept_id, spc.check_type, spc.main_image, spc.image_urls, spc.working_position_type, spc.check_working_position, spc.checking_num, spc.check_result, spc.intro, spc.quality_user, spc.quality_user_name, spc.supervise_user, spc.supervise_user_name, spc.group_dept_user, spc.group_dept_user_name, spc.checking_date, spc.checking_files, spc.is_del, spc.create_by, spc.create_time, spc.update_by, spc.update_time, spc.remark, spc.approve_status, sp.projectName, sd.dept_name from sur_project_checking spc
        left join sur_project sp on spc.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spc.dept_id
        <where>
            and spc.is_del=0
            <if test="projectId != null "> and spc.project_id = #{projectId}</if>
            <if test="projectName != null and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null ">
                and (spc.dept_id = #{deptId} or spc.supervise_user like concat('%', #{nowUserName}, '%'))
            </if>
            <if test="deptName != null and deptName != ''"> and sd.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="groupDeptId != null "> and spc.group_dept_id = #{groupDeptId}</if>
            <if test="checkType != null  and checkType != ''"> and spc.check_type = #{checkType}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and spc.approve_status = #{approveStatus}</if>
            <if test="workingPositionType != null  and workingPositionType != ''"> and spc.working_position_type = #{workingPositionType}</if>
            <if test="checkWorkingPosition != null  and checkWorkingPosition != ''"> and spc.check_working_position like concat('%', #{checkWorkingPosition}, '%')</if>
            <if test="checkResult != null  and checkResult != ''"> and spc.check_result = #{checkResult}</if>
            <if test="qualityUser != null  and qualityUser != ''"> and (spc.quality_user like concat('%', #{qualityUser}, '%') or spc.quality_user_name like concat('%', #{qualityUser}, '%'))</if>
            <if test="qualityUserName != null  and qualityUserName != ''"> and spc.quality_user_name like concat('%', #{qualityUserName}, '%')</if>
            <if test="superviseUser != null  and superviseUser != ''"> and (spc.supervise_user like concat('%', #{superviseUser}, '%') or spc.supervise_user_name like concat('%', #{superviseUser}, '%'))</if>
            <if test="superviseUserName != null  and superviseUserName != ''"> and spc.supervise_user_name like concat('%', #{superviseUserName}, '%')</if>
            <if test="groupDeptUser != null  and groupDeptUser != ''"> and spc.group_dept_user like concat('%', #{groupDeptUser}, '%')</if>
            <if test="groupDeptUserName != null  and groupDeptUserName != ''"> and spc.group_dept_user_name like concat('%', #{groupDeptUserName}, '%')</if>
            <if test="params.beginCheckingDate != null and params.beginCheckingDate != '' and params.endCheckingDate != null and params.endCheckingDate != ''"> and spc.checking_date between #{params.beginCheckingDate} and #{params.endCheckingDate}</if>
            <if test="isDel != null "> and spc.is_del = #{isDel}</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 spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept} and spui.del_flag=0 )</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'>
                and (sp.id in (select spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)
                or spc.supervise_user like concat('%', #{nowUserName}, '%')
                )
            </if>
            <if test='activeName == "jxz"'> and spc.approve_status != '4'</if>
            <if test='activeName == "ywc"'> and spc.approve_status = '4'</if>
        </where>
        order by checking_date desc
    </select>

    <select id="selectBgscreenProjectCheckingList" parameterType="SurProjectChecking" resultMap="SurProjectCheckingResult">
        select spc.id, spc.project_id, spc.dept_id, spc.group_dept_id, spc.check_type, spc.main_image, spc.image_urls, spc.working_position_type, spc.check_working_position, spc.checking_num, spc.check_result, spc.intro, spc.quality_user, spc.quality_user_name, spc.supervise_user, spc.supervise_user_name, spc.group_dept_user, spc.group_dept_user_name, spc.checking_date, spc.checking_files, spc.is_del, spc.create_by, spc.create_time, spc.update_by, spc.update_time, spc.remark, spc.approve_status, sp.projectName, sd.dept_name from sur_project_checking spc
        left join sur_project sp on spc.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spc.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>
        <where>
            and spc.is_del=0
            <if test="projectId != null "> and spc.project_id = #{projectId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and spc.project_id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="projectName != null and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptId != null "> and spc.dept_id = #{deptId}</if>
            <if test="deptName != null and deptName != ''"> and sd.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="groupDeptId != null "> and spc.group_dept_id = #{groupDeptId}</if>
            <if test="checkType != null  and checkType != ''"> and spc.check_type = #{checkType}</if>
            <if test="workingPositionType != null  and workingPositionType != ''"> and spc.working_position_type = #{workingPositionType}</if>
            <if test="checkWorkingPosition != null  and checkWorkingPosition != ''"> and spc.check_working_position like concat('%', #{checkWorkingPosition}, '%')</if>
            <if test="checkResult != null  and checkResult != ''"> and spc.check_result = #{checkResult}</if>
            <if test="qualityUser != null  and qualityUser != ''"> and (spc.quality_user like concat('%', #{qualityUser}, '%') or spc.quality_user_name like concat('%', #{qualityUser}, '%'))</if>
            <if test="qualityUserName != null  and qualityUserName != ''"> and spc.quality_user_name like concat('%', #{qualityUserName}, '%')</if>
            <if test="superviseUser != null  and superviseUser != ''"> and (spc.supervise_user like concat('%', #{superviseUser}, '%') or spc.supervise_user_name like concat('%', #{superviseUser}, '%'))</if>
            <if test="superviseUserName != null  and superviseUserName != ''"> and spc.supervise_user_name like concat('%', #{superviseUserName}, '%')</if>
            <if test="groupDeptUser != null  and groupDeptUser != ''"> and spc.group_dept_user like concat('%', #{groupDeptUser}, '%')</if>
            <if test="groupDeptUserName != null  and groupDeptUserName != ''"> and spc.group_dept_user_name like concat('%', #{groupDeptUserName}, '%')</if>
            <if test="params.beginCheckingDate != null and params.beginCheckingDate != '' and params.endCheckingDate != null and params.endCheckingDate != ''"> and spc.checking_date between #{params.beginCheckingDate} and #{params.endCheckingDate}</if>
            <if test="isDel != null "> and spc.is_del = #{isDel}</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 spc.create_by = #{nowUserName}</if>
        </where>
        order by checking_date desc
        limit 0,10
    </select>

    <select id="selectBgscreenProjectCheckingView" parameterType="SurProjectChecking" resultType="map">
        select spc.check_result, count(spc.id) as total from sur_project_checking spc
        left join sur_project sp on spc.project_id = sp.id
        <where>
            and spc.is_del=0
            <if test="projectId != null "> and spc.project_id = #{projectId}</if>
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and spc.project_id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        group by check_result
    </select>

    <select id="selectSurProjectCheckingById" parameterType="Long" resultMap="SurProjectCheckingResult">
        select spc.id, spc.project_id, spc.dept_id, spc.group_dept_id, spc.check_type, spc.main_image, spc.image_urls, spc.working_position_type, spc.check_working_position, spc.checking_num, spc.check_result, spc.intro, spc.quality_user, spc.quality_user_name, spc.supervise_user, spc.supervise_user_name, spc.group_dept_user, spc.group_dept_user_name, spc.checking_date, spc.checking_files, spc.is_del, spc.create_by, spc.create_time, spc.update_by, spc.update_time, spc.remark, spc.approve_status, sp.projectName, sd.dept_name from sur_project_checking spc
        left join sur_project sp on spc.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spc.dept_id
        where spc.id = #{id}
    </select>

    <select id="findStatisticsByProjectId" parameterType="Long" resultType="map">
        SELECT
            spc.project_id,
            count(1) AS total,
            pass.pass
        FROM
            sur_project_checking spc
            LEFT JOIN (
                SELECT
                    pass.project_id,
                    count(1) AS pass
                FROM
                    sur_project_checking pass
                WHERE
                    pass.is_del = 0
                    AND pass.check_result = '1'
                    AND pass.project_id = #{projectId}
                GROUP BY
                    pass.project_id
            ) pass ON pass.project_id = spc.project_id
        WHERE
            is_del = 0
            AND spc.project_id = #{projectId}
        GROUP BY
            spc.project_id
    </select>
        
    <insert id="insertSurProjectChecking" parameterType="SurProjectChecking" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_checking
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="groupDeptId != null">group_dept_id,</if>
            <if test="checkType != null">check_type,</if>
            <if test="mainImage != null">main_image,</if>
            <if test="imageUrls != null">image_urls,</if>
            <if test="workingPositionType != null">working_position_type,</if>
            <if test="checkWorkingPosition != null">check_working_position,</if>
            <if test="checkingNum != null">checking_num,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="intro != null">intro,</if>
            <if test="qualityUser != null">quality_user,</if>
            <if test="qualityUserName != null">quality_user_name,</if>
            <if test="superviseUser != null">supervise_user,</if>
            <if test="superviseUserName != null">supervise_user_name,</if>
            <if test="groupDeptUser != null">group_dept_user,</if>
            <if test="groupDeptUserName != null">group_dept_user_name,</if>
            <if test="checkingDate != null">checking_date,</if>
            <if test="checkingFiles != null">checking_files,</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>
            <if test="approveStatus != null">approve_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="groupDeptId != null">#{groupDeptId},</if>
            <if test="checkType != null">#{checkType},</if>
            <if test="mainImage != null">#{mainImage},</if>
            <if test="imageUrls != null">#{imageUrls},</if>
            <if test="workingPositionType != null">#{workingPositionType},</if>
            <if test="checkWorkingPosition != null">#{checkWorkingPosition},</if>
            <if test="checkingNum != null">#{checkingNum},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="intro != null">#{intro},</if>
            <if test="qualityUser != null">#{qualityUser},</if>
            <if test="qualityUserName != null">#{qualityUserName},</if>
            <if test="superviseUser != null">#{superviseUser},</if>
            <if test="superviseUserName != null">#{superviseUserName},</if>
            <if test="groupDeptUser != null">#{groupDeptUser},</if>
            <if test="groupDeptUserName != null">#{groupDeptUserName},</if>
            <if test="checkingDate != null">#{checkingDate},</if>
            <if test="checkingFiles != null">#{checkingFiles},</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>
            <if test="approveStatus != null">#{approveStatus},</if>
         </trim>
    </insert>

    <update id="updateSurProjectChecking" parameterType="SurProjectChecking">
        update sur_project_checking
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="groupDeptId != null">group_dept_id = #{groupDeptId},</if>
            <if test="checkType != null">check_type = #{checkType},</if>
            <if test="mainImage != null">main_image = #{mainImage},</if>
            <if test="imageUrls != null">image_urls = #{imageUrls},</if>
            <if test="workingPositionType != null">working_position_type = #{workingPositionType},</if>
            <if test="checkWorkingPosition != null">check_working_position = #{checkWorkingPosition},</if>
            <if test="checkingNum != null">checking_num = #{checkingNum},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="intro != null">intro = #{intro},</if>
            <if test="qualityUser != null">quality_user = #{qualityUser},</if>
            <if test="qualityUserName != null">quality_user_name = #{qualityUserName},</if>
            <if test="superviseUser != null">supervise_user = #{superviseUser},</if>
            <if test="superviseUserName != null">supervise_user_name = #{superviseUserName},</if>
            <if test="groupDeptUser != null">group_dept_user = #{groupDeptUser},</if>
            <if test="groupDeptUserName != null">group_dept_user_name = #{groupDeptUserName},</if>
            <if test="checkingDate != null">checking_date = #{checkingDate},</if>
            <if test="checkingFiles != null">checking_files = #{checkingFiles},</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>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectCheckingById" parameterType="Long">
        delete from sur_project_checking where id = #{id}
    </delete>

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