<?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.SurProjectScheduleMapper">
    
    <resultMap type="SurProjectSchedule" id="SurProjectScheduleResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="projectName"    />
        <result property="image"    column="image"    />
        <result property="plannedNode"    column="planned_node"    />
        <result property="actualNode"    column="actual_node"    />
        <result property="purchaseProgress"    column="purchase_progress"    />
        <result property="designProgress"    column="design_progress"    />
        <result property="constructionProgress"    column="construction_progress"    />
        <result property="acceptanceProgress"    column="acceptance_progress"    />
        <result property="planForNextWeek"    column="plan_for_next_week"    />
        <result property="existingProblem"    column="existing_problem"    />
        <result property="problemProgress"    column="problem_progress"    />
        <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="mainImage"    column="mainImage"    />
        <result property="dataTemplate"    column="data_template"    />
        <result property="projectType"    column="projectType"    />
        <association property="surProject" javaType="SurProject">
            <result property="id"    column="project_id"    />
            <result property="projectName" column="projectName"/>
            <result property="projectSort" column="projectSort"/>
            <result property="projectSchedule" column="projectSchedule"/>
        </association>
    </resultMap>
    <sql id="selectSurProjectScheduleVo">
        SELECT  * from (
        SELECT s.*,p.projectType,p.projectName
        FROM sur_project_schedule AS s LEFT JOIN sur_project AS p ON s.project_id=p.id ) as a
    </sql>

    <select id="selectSurProjectScheduleList" parameterType="SurProjectSchedule" resultMap="SurProjectScheduleResult">
        SELECT  * from (
        SELECT s.*,substring_index(s.image,',',1) as mainImage,p.projectName
        FROM sur_project_schedule AS s LEFT JOIN sur_project AS p ON s.project_id=p.id
        <!--监理单位/总包公司/分包单位-->
        <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> left join sur_project_unit_info spui on spui.projectId = p.id</if>
        <!--普通用户查询项目人员-->
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> left join sur_project_userinfo spu on spu.project_id = p.id</if>
        where 1=1
        <!--子部门数据-->
        <if test='nowRole == "4"'> and p.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 spu.user_id = #{nowUser} and spu.is_del=0</if>
        <!-- 查询条件-项目部门 -->
        <if test="projectDeptId != null "> and p.deptId = #{projectDeptId}</if>
        ) as a
        <where>  
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="plannedNode != null  and plannedNode != ''"> and planned_node = #{plannedNode}</if>
            <if test="actualNode != null  and actualNode != ''"> and actual_node = #{actualNode}</if>
            <if test="purchaseProgress != null  and purchaseProgress != ''"> and purchase_progress = #{purchaseProgress}</if>
            <if test="designProgress != null  and designProgress != ''"> and design_progress = #{designProgress}</if>
            <if test="constructionProgress != null  and constructionProgress != ''"> and construction_progress = #{constructionProgress}</if>
            <if test="acceptanceProgress != null  and acceptanceProgress != ''"> and acceptance_progress = #{acceptanceProgress}</if>
            <if test="planForNextWeek != null  and planForNextWeek != ''"> and plan_for_next_week = #{planForNextWeek}</if>
            <if test="existingProblem != null  and existingProblem != ''"> and existing_problem = #{existingProblem}</if>
            <if test="problemProgress != null  and problemProgress != ''"> and problem_progress = #{problemProgress}</if>
            <if test="params.beginMarksTime != null and params.beginMarksTime != '' and params.endMarksTime != null and params.endMarksTime != ''"> and create_time between #{params.beginMarksTime} and #{params.endMarksTime}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="projectName != null and projectName != ''">
                AND projectName like concat('%', #{projectName}, '%')
            </if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSurProjectScheduleById" parameterType="Long" resultMap="SurProjectScheduleResult">
        <include refid="selectSurProjectScheduleVo"/>
        where id = #{id}
    </select>

    <select id="getProjectConstructionProgress" parameterType="Long" resultMap="SurProjectScheduleResult">
        <include refid="selectSurProjectScheduleVo"/>
        <where>
            <if test="projectId != null "> project_id = #{projectId}</if>
        </where>
          order by id desc  LIMIT 0,1 ;
    </select>

        
    <insert id="insertSurProjectSchedule" parameterType="SurProjectSchedule">
        insert into sur_project_schedule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="image != null">image,</if>
            <if test="plannedNode != null">planned_node,</if>
            <if test="actualNode != null">actual_node,</if>
            <if test="purchaseProgress != null">purchase_progress,</if>
            <if test="designProgress != null">design_progress,</if>
            <if test="constructionProgress != null">construction_progress,</if>
            <if test="acceptanceProgress != null">acceptance_progress,</if>
            <if test="planForNextWeek != null">plan_for_next_week,</if>
            <if test="existingProblem != null">existing_problem,</if>
            <if test="problemProgress != null">problem_progress,</if>
            <if test="dataTemplate != null">data_template,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="image != null">#{image},</if>
            <if test="plannedNode != null">#{plannedNode},</if>
            <if test="actualNode != null">#{actualNode},</if>
            <if test="purchaseProgress != null">#{purchaseProgress},</if>
            <if test="designProgress != null">#{designProgress},</if>
            <if test="constructionProgress != null">#{constructionProgress},</if>
            <if test="acceptanceProgress != null">#{acceptanceProgress},</if>
            <if test="planForNextWeek != null">#{planForNextWeek},</if>
            <if test="existingProblem != null">#{existingProblem},</if>
            <if test="problemProgress != null">#{problemProgress},</if>
            <if test="dataTemplate != null">#{dataTemplate},</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>
         </trim>
    </insert>

    <update id="updateSurProjectSchedule" parameterType="SurProjectSchedule">
        update sur_project_schedule
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="image != null">image = #{image},</if>
            <if test="plannedNode != null">planned_node = #{plannedNode},</if>
            <if test="actualNode != null">actual_node = #{actualNode},</if>
            <if test="purchaseProgress != null">purchase_progress = #{purchaseProgress},</if>
            <if test="designProgress != null">design_progress = #{designProgress},</if>
            <if test="constructionProgress != null">construction_progress = #{constructionProgress},</if>
            <if test="acceptanceProgress != null">acceptance_progress = #{acceptanceProgress},</if>
            <if test="planForNextWeek != null">plan_for_next_week = #{planForNextWeek},</if>
            <if test="existingProblem != null">existing_problem = #{existingProblem},</if>
            <if test="problemProgress != null">problem_progress = #{problemProgress},</if>
            <if test="dataTemplate != null">data_template = #{dataTemplate},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectScheduleById" parameterType="Long">
        delete from sur_project_schedule where id = #{id}
    </delete>

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

    <select id="selectByProjectType" parameterType="SurProjectScheduleWhere" resultMap="SurProjectScheduleResult">
        select * from vw_sur_project_schedule
        WHERE id IN (
            SELECT MAX(id) FROM sur_project_schedule
            WHERE project_id IN (
                SELECT id FROM sur_project WHERE
                isdel=0 AND projecttype=#{prjType}
                <if test="deptId>0">and deptId=#{deptId}</if>
                <if test="prjIds !=null and prjIds.size()>0">
                    and id in
                    <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                ) AND IF(is_del IS NULL ,0,is_del)=0
            GROUP BY project_id
        )
        order by projectSort
    </select>
    <select id="getConstructionProgressByDept" parameterType="SurProjectScheduleWhere" resultMap="SurProjectScheduleResult">
        select s.*,p.projectName from (
        select * from sur_project_schedule where id in (
            select max(id)
            from sur_project_schedule where
            IF(is_del IS NULL ,0,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 id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
            and ( (existing_problem is not null and existing_problem not like '暂无%' and existing_problem not like '无%' and length(trim(existing_problem))>0 )
            or (problem_progress IS NOT NULL AND problem_progress NOT LIKE '暂无%' AND problem_progress NOT LIKE '无%' AND LENGTH(TRIM(problem_progress))>0 ))
            group by project_id
        )
    ) s LEFT JOIN sur_project AS p ON s.project_id=p.id order by p.projectSort
    </select>
</mapper>