<?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.SurProjectMeasureMapper">
    
    <resultMap type="SurProjectMeasure" id="SurProjectMeasureResult">
        <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="mainImage"    column="main_image"    />
        <result property="imageUrls"    column="image_urls"    />
        <result property="measureType"    column="measure_type"    />
        <result property="measurePosition"    column="measure_position"    />
        <result property="measureInfo"    column="measure_info"    />
        <result property="measureTime"    column="measure_time"    />
        <result property="measurePointPosition"    column="measure_point_position"    />
        <result property="measureResult"    column="measure_result"    />
        <result property="measureFiles"    column="measure_files"    />
        <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="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="measureTypeName"    column="measure_type_name"    />
        <result property="measureInfoName"    column="measure_info_name"    />
        <result property="createByName" column="create_by_name"/>
        <result property="updateByName" column="update_by_name"/>
        <result property="approveStatus" column="approve_status"/>
    </resultMap>

    <sql id="selectSurProjectMeasureVo">
        select spm.id, spm.project_id, spm.dept_id, spm.main_image, spm.image_urls, spm.measure_type, sdd1.dict_label as measure_type_name, spm.measure_position, spm.measure_info, sdd2.dict_label as measure_info_name, spm.measure_time, spm.measure_point_position, spm.measure_result, spm.measure_files, spm.quality_user, spm.quality_user_name, spm.supervise_user, spm.supervise_user_name, spm.is_del, spm.create_by, spm.create_time, spm.update_by, spm.update_time, spm.remark, sp.projectName, sd.dept_name, spm.approve_status from sur_project_measure spm
        left join sur_project sp on spm.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spm.dept_id
        left join sys_dict_data sdd1 on sdd1.dict_type = 'project_measure_type' and sdd1.dict_value = spm.measure_type
        left join sys_dict_data sdd2 on sdd2.dict_type = 'project_measure_info_type' and sdd2.dict_value = spm.measure_info
    </sql>

    <select id="selectSurProjectMeasureList" parameterType="SurProjectMeasure" resultMap="SurProjectMeasureResult">
        <include refid="selectSurProjectMeasureVo"/>
        <where>
            and spm.is_del='0'
            <if test="projectId != null "> and spm.project_id = #{projectId}</if>
            <if test="deptId != null ">
                and (spm.dept_id = #{deptId} or spm.supervise_user like concat('%', #{nowUserName}, '%'))
            </if>
            <if test="measureType != null  and measureType != ''"> and spm.measure_type = #{measureType}</if>
            <if test="measurePosition != null  and measurePosition != ''"> and spm.measure_position like concat('%', #{measurePosition}, '%')</if>
            <if test="measureInfo != null  and measureInfo != ''"> and spm.measure_info like concat('%', #{measureInfo}, '%')</if>
            <if test="params.beginMeasureTime != null and params.beginMeasureTime != '' and params.endMeasureTime != null and params.endMeasureTime != ''"> and spm.measure_time between #{params.beginMeasureTime} and #{params.endMeasureTime}</if>
            <if test="measurePointPosition != null  and measurePointPosition != ''"> and spm.measure_point_position like concat('%', #{measurePointPosition}, '%')</if>
            <if test="measureResult != null  and measureResult != ''"> and measure_result = #{measureResult}</if>
            <if test="qualityUser != null  and qualityUser != ''"> and (spm.quality_user like concat('%', #{qualityUser}, '%') or spm.quality_user_name like concat('%', #{qualityUser}, '%'))</if>
            <if test="superviseUser != null  and superviseUser != ''"> and (spm.supervise_user like concat('%', #{superviseUser}, '%') or spm.supervise_user_name like concat('%', #{superviseUser}, '%'))</if>
            <if test="isDel != null  and isDel != ''"> and spm.is_del like concat('%', #{isDel}, '%')</if>
            <if test="approveStatus != null  and approveStatus != ''"> and spm.approve_status = #{approveStatus}</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 spm.supervise_user like concat('%', #{nowUserName}, '%')
                )
            </if>
            <if test='activeName == "jxz"'> and spm.approve_status != '4'</if>
            <if test='activeName == "ywc"'> and spm.approve_status = '4'</if>
        </where>
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        select spm.measure_type as measureType, sdd.dict_label as measureTypeName,count(1) as total from sur_project_measure spm
        left join sys_dict_data sdd on sdd.dict_type = 'project_measure_type' and sdd.dict_value=spm.measure_type
        where spm.project_id=#{projectId}
        GROUP BY spm.measure_type
        ORDER BY sdd.dict_sort
    </select>
    
    <select id="selectSurProjectMeasureById" parameterType="Long" resultMap="SurProjectMeasureResult">
        <include refid="selectSurProjectMeasureVo"/>
        where spm.id = #{id}
    </select>
        
    <insert id="insertSurProjectMeasure" parameterType="SurProjectMeasure" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_measure
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="mainImage != null">main_image,</if>
            <if test="imageUrls != null">image_urls,</if>
            <if test="measureType != null">measure_type,</if>
            <if test="measurePosition != null">measure_position,</if>
            <if test="measureInfo != null">measure_info,</if>
            <if test="measureTime != null">measure_time,</if>
            <if test="measurePointPosition != null">measure_point_position,</if>
            <if test="measureResult != null">measure_result,</if>
            <if test="measureFiles != null">measure_files,</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="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="mainImage != null">#{mainImage},</if>
            <if test="imageUrls != null">#{imageUrls},</if>
            <if test="measureType != null">#{measureType},</if>
            <if test="measurePosition != null">#{measurePosition},</if>
            <if test="measureInfo != null">#{measureInfo},</if>
            <if test="measureTime != null">#{measureTime},</if>
            <if test="measurePointPosition != null">#{measurePointPosition},</if>
            <if test="measureResult != null">#{measureResult},</if>
            <if test="measureFiles != null">#{measureFiles},</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="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="updateSurProjectMeasure" parameterType="SurProjectMeasure">
        update sur_project_measure
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="mainImage != null">main_image = #{mainImage},</if>
            <if test="imageUrls != null">image_urls = #{imageUrls},</if>
            <if test="measureType != null">measure_type = #{measureType},</if>
            <if test="measurePosition != null">measure_position = #{measurePosition},</if>
            <if test="measureInfo != null">measure_info = #{measureInfo},</if>
            <if test="measureTime != null">measure_time = #{measureTime},</if>
            <if test="measurePointPosition != null">measure_point_position = #{measurePointPosition},</if>
            <if test="measureResult != null">measure_result = #{measureResult},</if>
            <if test="measureFiles != null">measure_files = #{measureFiles},</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="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="deleteSurProjectMeasureById" parameterType="Long">
        delete from sur_project_measure where id = #{id}
    </delete>

    <delete id="deleteSurProjectMeasureByIds" parameterType="String">
        delete from sur_project_measure where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="groupMeasureInfo" parameterType="SurProjectMeasure" resultMap="SurProjectMeasureResult">
        SELECT a.dict_label measure_info,IF(b.cnt IS NULL,0,b.cnt) id  FROM (
            SELECT  *  FROM sys_dict_data WHERE dict_type='project_measure_info_type') a
            LEFT JOIN
        (
            SELECT measure_info,COUNT(1) cnt
            FROM sur_project_measure
            WHERE IF(is_del IS NULL,0,is_del)=0
            <if test='measureType !=null and measureType !="0"'>
                and measure_type = #{measureType}
            </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>
            GROUP BY measure_info
        ) b
        ON a.dict_value=b.measure_info
    </select>

    <select id="getList"  parameterType="SurProjectMeasure" resultMap="SurProjectMeasureResult">
        SELECT spm.id, spm.project_id, spm.dept_id, spm.main_image, spm.image_urls, spm.measure_type, sdd1.dict_label AS measure_type_name, spm.measure_position, spm.measure_info, sdd2.dict_label AS measure_info_name, spm.measure_time, spm.measure_point_position, spm.measure_result, spm.measure_files, spm.quality_user, spm.quality_user_name, spm.supervise_user, spm.supervise_user_name, spm.is_del, spm.create_by, spm.create_time, spm.update_by, spm.update_time, spm.remark, spm.approve_status, sp.projectName, sd.dept_name ,u1.nick_name create_by_name,u2.nick_name update_by_name
        FROM sur_project_measure spm
         LEFT JOIN sur_project sp ON spm.project_id = sp.id
         LEFT JOIN sys_dept sd ON sd.dept_id = spm.dept_id
         LEFT JOIN sys_dict_data sdd1 ON sdd1.dict_type = 'project_measure_type' AND sdd1.dict_value = spm.measure_type
         LEFT JOIN sys_dict_data sdd2 ON sdd2.dict_type = 'project_measure_info_type' AND sdd2.dict_value = spm.measure_info
         LEFT JOIN sys_user u1 ON spm.create_by=u1.phonenumber
         LEFT JOIN sys_user u2 ON spm.update_by=u2.phonenumber

        WHERE IF(spm.is_del IS NULL,0,spm.is_del)=0
        <if test='measureType !=null and measureType !="0"'>
            and spm.measure_type = #{measureType}
        </if>
        <if test="deptId !=null and deptId >0">
            AND  spm.project_id IN (SELECT id FROM sur_project WHERE deptid=#{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and spm.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="projectId !=null and projectId >0">
            AND  spm.project_id=#{projectId}
        </if>
    </select>

</mapper>