<?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.SurProjectMaterialSealMapper">
    
    <resultMap type="SurProjectMaterialSeal" id="SurProjectMaterialSealResult">
        <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="materialName"    column="material_name"    />
        <result property="usePosition"    column="use_position"    />
        <result property="contractBrand"    column="contract_brand"    />
        <result property="useBrand"    column="use_brand"    />
        <result property="sealDate"    column="seal_date"    />
        <result property="signFiles"    column="sign_files"    />
        <result property="alterationFiles"    column="alteration_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="materialResult"    column="material_result"    />
        <result property="witnessUser"    column="witness_user"    />
        <result property="witnessUserName"    column="witness_user_name"    />
        <result property="approveStatus"    column="approve_status"    />
    </resultMap>

    <sql id="selectSurProjectMaterialSealVo">
        select spme.id, spme.project_id, spme.dept_id, spme.main_image, spme.image_urls, spme.material_name, spme.use_position, spme.contract_brand,
               spme.use_brand, spme.seal_date, spme.sign_files, spme.alteration_files, spme.is_del, spme.create_by, spme.create_time, spme.update_by, spme.update_time,
               spme.remark, spme.witness_user, spme.witness_user_name, spme.approve_status, spme.material_result, sp.projectName, sd.dept_name
        from sur_project_material_seal spme
        left join sur_project sp on spme.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spme.dept_id
    </sql>

    <select id="selectSurProjectMaterialSealList" parameterType="SurProjectMaterialSeal" resultMap="SurProjectMaterialSealResult">
        <include refid="selectSurProjectMaterialSealVo"/>
        <where>
            and spme.is_del='0'
            <if test="projectId != null "> and spme.project_id = #{projectId}</if>
            <if test="deptId != null "> and (spme.dept_id = #{deptId} or spme.witness_user like concat('%', #{nowUserName}, '%'))</if>
            <if test="imageUrls != null  and imageUrls != ''"> and spme.image_urls = #{imageUrls}</if>
            <if test="materialName != null  and materialName != ''"> and spme.material_name like concat('%', #{materialName}, '%')</if>
            <if test="usePosition != null  and usePosition != ''"> and spme.use_position like concat('%', #{usePosition}, '%')</if>
            <if test="contractBrand != null  and contractBrand != ''"> and spme.contract_brand like concat('%', #{contractBrand}, '%')</if>
            <if test="useBrand != null  and useBrand != ''"> and spme.use_brand like concat('%', #{useBrand}, '%')</if>
            <if test="params.beginSealDate != null and params.beginSealDate != '' and params.endSealDate != null and params.endSealDate != ''"> and spme.seal_date between #{params.beginSealDate} and #{params.endSealDate}</if>
            <if test="isDel != null  and isDel != ''"> and spme.is_del = #{isDel}</if>
            <if test="materialResult != null  and materialResult != ''"> and spme.material_result = #{materialResult}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and spme.approve_status = #{approveStatus}</if>
            <if test="witnessUser != null  and witnessUser != ''"> and (spme.witness_user like concat('%', #{witnessUser}, '%') or spme.witness_user_name like concat('%', #{witnessUser}, '%'))</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 spme.witness_user like concat('%', #{nowUserName}, '%')
                )
            </if>
            <if test='activeName == "jxz"'> and spme.approve_status != '4'</if>
            <if test='activeName == "ywc"'> and spme.approve_status = '4'</if>
        </where>
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        SELECT
            use_position AS type,
            count(1) total
        FROM
            sur_project_material_seal
        where is_del=0
            and project_id=#{projectId}
        GROUP BY
            use_position
    </select>
    
    <select id="selectSurProjectMaterialSealById" parameterType="Long" resultMap="SurProjectMaterialSealResult">
        <include refid="selectSurProjectMaterialSealVo"/>
        where spme.id = #{id}
    </select>
        
    <insert id="insertSurProjectMaterialSeal" parameterType="SurProjectMaterialSeal" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_material_seal
        <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="materialName != null">material_name,</if>
            <if test="usePosition != null">use_position,</if>
            <if test="contractBrand != null">contract_brand,</if>
            <if test="useBrand != null">use_brand,</if>
            <if test="sealDate != null">seal_date,</if>
            <if test="signFiles != null">sign_files,</if>
            <if test="alterationFiles != null">alteration_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="materialResult != null">material_result,</if>
            <if test="witnessUser != null">witness_user,</if>
            <if test="witnessUserName != null">witness_user_name,</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="materialName != null">#{materialName},</if>
            <if test="usePosition != null">#{usePosition},</if>
            <if test="contractBrand != null">#{contractBrand},</if>
            <if test="useBrand != null">#{useBrand},</if>
            <if test="sealDate != null">#{sealDate},</if>
            <if test="signFiles != null">#{signFiles},</if>
            <if test="alterationFiles != null">#{alterationFiles},</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="materialResult != null">#{materialResult},</if>
            <if test="witnessUser != null">#{witnessUser},</if>
            <if test="witnessUserName != null">#{witnessUserName},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
         </trim>
    </insert>

    <update id="updateSurProjectMaterialSeal" parameterType="SurProjectMaterialSeal">
        update sur_project_material_seal
        <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="materialName != null">material_name = #{materialName},</if>
            <if test="usePosition != null">use_position = #{usePosition},</if>
            <if test="contractBrand != null">contract_brand = #{contractBrand},</if>
            <if test="useBrand != null">use_brand = #{useBrand},</if>
            <if test="sealDate != null">seal_date = #{sealDate},</if>
            <if test="signFiles != null">sign_files = #{signFiles},</if>
            <if test="alterationFiles != null">alteration_files = #{alterationFiles},</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="materialResult != null">material_result = #{materialResult},</if>
            <if test="witnessUser != null">witness_user = #{witnessUser},</if>
            <if test="witnessUserName != null">witness_user_name = #{witnessUserName},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectMaterialSealById" parameterType="Long">
        delete from sur_project_material_seal where id = #{id}
    </delete>

    <delete id="deleteSurProjectMaterialSealByIds" parameterType="String">
        delete from sur_project_material_seal where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectTop20" parameterType="SurProjectMaterialSeal" resultMap="SurProjectMaterialSealResult">
    select spme.id, spme.project_id, spme.dept_id, spme.main_image, spme.image_urls, spme.material_name, spme.use_position, spme.contract_brand, spme.use_brand, spme.seal_date, spme.sign_files, spme.alteration_files, spme.is_del, spme.create_by, spme.create_time, spme.update_by, spme.update_time, spme.remark, spme.projectName,
           spme.dept_name
    from (
        select spme.*,sp.projectName, sd.dept_name from sur_project_material_seal spme
        left join sur_project sp on spme.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spme.dept_id
        where spme.is_del=0
            <if test="deptId != null and deptId > 0"> and sp.deptId = #{deptId} </if>
            <if test="projectId != null and projectId > 0">and sp.id = #{projectId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and sp.id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        ) spme
    order by spme.create_time desc
    LIMIT 0,20
    </select>
    <select id="groupTop12Month"  parameterType="SurProjectMaterialSeal" resultMap="SurProjectMaterialSealResult">
        select year(seal_date) project_id,month(seal_date) dept_id,count(1) id

        from sur_project_material_seal where is_del=0
        <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>
        and seal_date>=DATE_SUB(CURDATE(),interval 12 month)
        group by year(seal_date),month(seal_date)
        order by year(seal_date) desc,month(seal_date) desc
    </select>
    <select id="selectTop12Month"   parameterType="SurProjectMaterialSeal" resultMap="SurProjectMaterialSealResult">
        select *
        from (
        select spme.*,sp.projectName, sd.dept_name from sur_project_material_seal spme
        left join sur_project sp on spme.project_id = sp.id
        left join sys_dept sd on sd.dept_id = spme.dept_id
        where spme.is_del=0
        and spme.seal_date>=DATE_SUB(CURDATE(),interval 12 month)
        <if test="deptId != null and deptId > 0"> and sp.deptId = #{deptId} </if>
        <if test="projectId != null and projectId > 0">and sp.id = #{projectId}</if>
        <if test="prjIds !=null and prjIds.size()>0">
            and sp.id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by spme.seal_date desc
        ) spme
    </select>
</mapper>