<?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.SurProjectCheckDetectionMapper">

    <resultMap type="SurProjectCheckDetection" id="SurProjectCheckDetectionResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="projectName"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="checkType"    column="check_type"    />
        <result property="checkTypeName"    column="check_type_name"    />
        <result property="materialName"    column="material_name"    />
        <result property="usePosition"    column="use_position"    />
        <result property="sampleNum"    column="sample_num"    />
        <result property="qualifiedFlag"    column="qualified_flag"    />
        <result property="represenNum"    column="represen_num"    />
        <result property="sampleUser"    column="sample_user"    />
        <result property="sampleUserName"    column="sample_user_name"    />
        <result property="witnessUser"    column="witness_user"    />
        <result property="witnessUserName"    column="witness_user_name"    />
        <result property="laboratoryName"    column="laboratory_name"    />
        <result property="checkTime"    column="check_time"    />
        <result property="checkState"    column="check_state"    />
        <result property="detectionResult"    column="detection_result"    />
        <result property="detectionFile"    column="detection_file"    />
        <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="createByName" column="create_by_name"/>
        <result property="updateByName" column="update_by_name"/>
        <result property="approveStatus" column="approve_status"/>
    </resultMap>

    <sql id="selectSurProjectCheckDetectionVo">
        SELECT spcd.id, spcd.project_id, sp.projectName, spcd.dept_id, sd.dept_name, spcd.check_type, sdd.dict_label AS check_type_name, spcd.material_name, spcd.use_position, spcd.sample_num, spcd.qualified_flag, spcd.represen_num, spcd.sample_user, spcd.sample_user_name, spcd.witness_user, spcd.witness_user_name, spcd.laboratory_name, spcd.check_time, spcd.check_state, spcd.detection_result, spcd.detection_file, spcd.create_by, spcd.create_time, spcd.is_del, spcd.update_by, spcd.update_time, spcd.remark, spcd.approve_status, u1.nick_name create_by_name,u2.nick_name update_by_name
        FROM sur_project_check_detection spcd
         LEFT JOIN sur_project sp ON spcd.project_id = sp.id
         LEFT JOIN sys_dept sd ON sd.dept_id = spcd.dept_id
         LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'check_detection_check_type' AND sdd.dict_value = spcd.check_type
         LEFT JOIN sys_user u1 ON spcd.create_by=u1.phonenumber
         LEFT JOIN sys_user u2 ON spcd.update_by=u2.phonenumber
    </sql>

    <select id="selectSurProjectCheckDetectionList" parameterType="SurProjectCheckDetection" resultMap="SurProjectCheckDetectionResult">
        <include refid="selectSurProjectCheckDetectionVo"/>
        <where>
            and spcd.is_del='0'
            <if test="projectId != null "> and spcd.project_id = #{projectId}</if>
            <if test="deptId != null "> and (spcd.dept_id = #{deptId} or (spcd.witness_user like concat('%', #{nowUserName}, '%') and spcd.approve_status is not null))</if>
            <if test="checkType != null  and checkType != ''"> and spcd.check_type = #{checkType}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and spcd.approve_status = #{approveStatus}</if>
            <if test="materialName != null  and materialName != ''"> and spcd.material_name like concat('%', #{materialName}, '%')</if>
            <if test="usePosition != null  and usePosition != ''"> and spcd.use_position like concat('%', #{usePosition}, '%')</if>
            <if test="sampleNum != null  and sampleNum != ''"> and spcd.sample_num = #{sampleNum}</if>
            <if test="isDel != null  and isDel != ''"> and spcd.is_del = #{isDel}</if>
            <if test="sampleUserName != null  and sampleUserName != ''"> and (spcd.sample_user like concat('%', #{sampleUserName}, '%') or spcd.sample_user_name like concat('%', #{sampleUserName}, '%'))</if>
            <if test="witnessUserName != null  and witnessUserName != ''"> and (spcd.witness_user like concat('%', #{witnessUserName}, '%') or spcd.witness_user_name like concat('%', #{witnessUserName}, '%'))</if>
            <if test="laboratoryName != null  and laboratoryName != ''"> and spcd.laboratory_name like concat('%', #{laboratoryName}, '%')</if>
            <if test="params.beginCheckTime != null and params.beginCheckTime != '' and params.endCheckTime != null and params.endCheckTime != ''"> and spcd.check_time between #{params.beginCheckTime} and #{params.endCheckTime}</if>
            <if test="checkState != null  and checkState != ''"> and spcd.check_state = #{checkState}</if>
            <if test="detectionResult != null  and detectionResult != ''"> and spcd.detection_result = #{detectionResult}</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 (spcd.witness_user like concat('%', #{nowUserName}, '%') and spcd.approve_status is not null)
                )
            </if>
            <if test='activeName == "jxz"'> and (spcd.approve_status != '4' or spcd.approve_status is null)</if>
            <if test='activeName == "ywc"'> and spcd.approve_status = '4'</if>
        </where>
        order by spcd.approve_status, spcd.check_time desc
    </select>

    <select id="queryGroupCount" parameterType="SurProjectCheckDetection" resultType="map">
        select spcd.check_state, count(1) as total from sur_project_check_detection spcd
        left join sur_project sp on spcd.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spcd.dept_id
        <where>
            and spcd.is_del='0'
            <if test="projectId != null "> and spcd.project_id = #{projectId}</if>
            <if test="deptId != null "> and spcd.dept_id = #{deptId}</if>
            <if test="checkType != null  and checkType != ''"> and spcd.check_type = #{checkType}</if>
            <if test="materialName != null  and materialName != ''"> and spcd.material_name like concat('%', #{materialName}, '%')</if>
            <if test="usePosition != null  and usePosition != ''"> and spcd.use_position like concat('%', #{usePosition}, '%')</if>
            <if test="sampleNum != null  and sampleNum != ''"> and spcd.sample_num = #{sampleNum}</if>
            <if test="isDel != null  and isDel != ''"> and spcd.is_del = #{isDel}</if>
            <if test="sampleUserName != null  and sampleUserName != ''"> and spcd.sample_user_name like concat('%', #{sampleUserName}, '%')</if>
            <if test="witnessUserName != null  and witnessUserName != ''"> and spcd.witness_user_name like concat('%', #{witnessUserName}, '%')</if>
            <if test="laboratoryName != null  and laboratoryName != ''"> and spcd.laboratory_name like concat('%', #{laboratoryName}, '%')</if>
            <if test="params.beginCheckTime != null and params.beginCheckTime != '' and params.endCheckTime != null and params.endCheckTime != ''"> and spcd.check_time between #{params.beginCheckTime} and #{params.endCheckTime}</if>
            <if test="checkState != null  and checkState != ''"> and spcd.check_state = #{checkState}</if>
            <if test="detectionResult != null  and detectionResult != ''"> and spcd.detection_result = #{detectionResult}</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 spcd.witness_user like concat('%', #{nowUserName}, '%')
                )
            </if>
            GROUP BY check_state
        </where>
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        select spcd.check_type as checkType,sdd.dict_label as checkTypeName, count(1) as total,checks.checkTotal,pass.passTotal from sur_project_check_detection spcd
        left join sys_dict_data sdd on sdd.dict_type='check_detection_check_type' and sdd.dict_value = spcd.check_type
        left join (select cd1.check_type as checks,count(1) as checkTotal from sur_project_check_detection cd1 where cd1.project_id=#{projectId} and cd1.check_state = '2' and cd1.is_del=0 GROUP BY cd1.check_type)checks on checks.checks=spcd.check_type
        left join (select cd2.check_type as pass,count(1) as passTotal from sur_project_check_detection cd2 where cd2.project_id=#{projectId} and cd2.detection_result = '1' and cd2.is_del=0 GROUP BY cd2.check_type)pass on pass.pass=spcd.check_type
        where spcd.project_id=#{projectId} and spcd.is_del=0
        GROUP BY spcd.check_type
    </select>

    <select id="selectSurProjectCheckDetectionById" parameterType="Long" resultMap="SurProjectCheckDetectionResult">
        <include refid="selectSurProjectCheckDetectionVo"/>
        where spcd.id = #{id}
    </select>

    <insert id="insertSurProjectCheckDetection" parameterType="SurProjectCheckDetection" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_check_detection
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="checkType != null">check_type,</if>
            <if test="materialName != null">material_name,</if>
            <if test="usePosition != null">use_position,</if>
            <if test="sampleNum != null">sample_num,</if>
            <if test="qualifiedFlag != null">qualified_flag,</if>
            <if test="represenNum != null">represen_num,</if>
            <if test="sampleUser != null">sample_user,</if>
            <if test="sampleUserName != null">sample_user_name,</if>
            <if test="witnessUser != null">witness_user,</if>
            <if test="witnessUserName != null">witness_user_name,</if>
            <if test="laboratoryName != null">laboratory_name,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="checkState != null">check_state,</if>
            <if test="detectionResult != null">detection_result,</if>
            <if test="detectionFile != null">detection_file,</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="checkType != null">#{checkType},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="usePosition != null">#{usePosition},</if>
            <if test="sampleNum != null">#{sampleNum},</if>
            <if test="qualifiedFlag != null">#{qualifiedFlag},</if>
            <if test="represenNum != null">#{represenNum},</if>
            <if test="sampleUser != null">#{sampleUser},</if>
            <if test="sampleUserName != null">#{sampleUserName},</if>
            <if test="witnessUser != null">#{witnessUser},</if>
            <if test="witnessUserName != null">#{witnessUserName},</if>
            <if test="laboratoryName != null">#{laboratoryName},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="checkState != null">#{checkState},</if>
            <if test="detectionResult != null">#{detectionResult},</if>
            <if test="detectionFile != null">#{detectionFile},</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="updateSurProjectCheckDetection" parameterType="SurProjectCheckDetection">
        update sur_project_check_detection
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="checkType != null">check_type = #{checkType},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="usePosition != null">use_position = #{usePosition},</if>
            <if test="sampleNum != null">sample_num = #{sampleNum},</if>
            <if test="qualifiedFlag != null">qualified_flag = #{qualifiedFlag},</if>
            <if test="represenNum != null">represen_num = #{represenNum},</if>
            <if test="sampleUser != null">sample_user = #{sampleUser},</if>
            <if test="sampleUserName != null">sample_user_name = #{sampleUserName},</if>
            <if test="witnessUser != null">witness_user = #{witnessUser},</if>
            <if test="witnessUserName != null">witness_user_name = #{witnessUserName},</if>
            <if test="laboratoryName != null">laboratory_name = #{laboratoryName},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="checkState != null">check_state = #{checkState},</if>
            <if test="detectionResult != null">detection_result = #{detectionResult},</if>
            <if test="detectionFile != null">detection_file = #{detectionFile},</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="deleteSurProjectCheckDetectionById" parameterType="Long">
        delete from sur_project_check_detection where id = #{id}
    </delete>

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

    <select id="groupByCheckType" parameterType="SurProjectCheckDetection" resultMap="SurProjectCheckDetectionResult">
        SELECT 1 id,COUNT(1) project_id,'送检数' check_type
        FROM  sur_project_check_detection WHERE is_del='0'
        <if test='checkType !=null and checkType !="0"'>
            and check_type= #{checkType}
        </if>
        <if test="deptId !=null and deptId > 0 ">
           and project_id IN (SELECT id FROM sur_project WHERE deptid=#{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="projectId !=null and projectId > 0 ">
          and  project_id=#{projectId}
        </if>

        UNION

        SELECT 2 id,COUNT(1) project_id,'待检数' check_type
        FROM  sur_project_check_detection WHERE check_state='1'
        and is_del='0'
        <if test='checkType !=null and checkType !="0"'>
            and check_type= #{checkType}
        </if>
        <if test="deptId !=null and deptId > 0 ">
            and project_id IN (SELECT id FROM sur_project WHERE deptid=#{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="projectId !=null and projectId > 0 ">
            and  project_id=#{projectId}
        </if>

        UNION

        SELECT 3 id,COUNT(1) project_id,'合格数' check_type
        FROM  sur_project_check_detection WHERE detection_result='1'
        and is_del='0'
        <if test='checkType !=null and checkType !="0"'>
            and check_type= #{checkType}
        </if>
        <if test="deptId !=null and deptId > 0 ">
            and project_id IN (SELECT id FROM sur_project WHERE deptid=#{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="projectId !=null and projectId > 0 ">
            and  project_id=#{projectId}
        </if>

    </select>

    <select id="getList" parameterType="SurProjectCheckDetection" resultMap="SurProjectCheckDetectionResult">
        <include refid="selectSurProjectCheckDetectionVo"/>
        WHERE spcd.is_del=0
       <if test="checkType!=null and checkType>0">AND spcd.check_type= #{checkType}</if>
        <if test="deptId !=null and deptId > 0 ">
            and spcd.project_id IN (SELECT id FROM sur_project WHERE deptid=#{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and spcd.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="projectId !=null and projectId > 0 ">
            and  spcd.project_id=#{projectId}
        </if>
    </select>
</mapper>