<?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.securitycontrol.system.base.mapper.IProScheduleMapper">
    <!--填报工序计划进度-->
    <insert id="addGxPlanProgress">
        INSERT INTO tb_project_progress
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="progressId != null and progressId != ''">id,</if>
            <if test="planId != null and planId!=''">plan_id,</if>
            <if test="gxProgress != null and gxProgress!=''">gx_progress,</if>
            <if test="nowGxId != null and nowGxId!=''">gx_id,</if>
            <if test="createTime != null and createTime!=''">create_time,</if>
            <if test="delaReason != null and delaReason!=''">dela_reason,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="progressId != null and progressId != ''">#{progressId},</if>
            <if test="planId != null and planId!=''">#{planId},</if>
            <if test="gxProgress != null and gxProgress!=''">#{gxProgress},</if>
            <if test="nowGxId != null and nowGxId!=''">#{nowGxId},</if>
            <if test="createTime != null and createTime!=''">#{createTime},</if>
            <if test="delaReason != null and delaReason!=''">#{delaReason},</if>
        </trim>
    </insert>

    <!--更新工序计划信息-->
    <update id="updateGxPlanData">
        <if test="type == 1">
            UPDATE tb_gx_plan SET start_time = #{startTime} WHERE plan_id = #{planId}
        </if>
        <if test="type == 2">
            UPDATE tb_gx_plan
            <set>
                <if test="endTime != null and endTime != ''">end_time = #{endTime},</if>
                <if test="delaReason != null and delaReason != ''">dela_reason = #{delaReason},</if>
            </set>
            WHERE plan_id = #{planId}
        </if>
    </update>
    <!--工程为线路时 更新杆塔当前工序-->
    <update id="updateGtData">
        UPDATE t_pro_gt SET now_gx = #{nowGxId} WHERE gt_id = #{gxId}
    </update>
    <!--更新工序计划进度-->
    <update id="updateGxPlan">
        UPDATE tb_gx_plan SET plan_progress = #{planProgress} WHERE plan_id = #{planId}
    </update>

    <!--获取工程进度列表-->
    <select id="getProScheduleLists" resultType="com.securitycontrol.entity.system.base.vo.ProScheduleVo">
        SELECT tp.pro_id AS proId,
        sb.city_name AS org,
        tp.pro_name AS proName,
        tp.pro_type AS proType,
        tp.pro_scale AS proScale,
        tp.jl_unit AS jlUnit,
        tp.sg_unit AS sgUnit,
        tp.manager AS manager,
        tp.plan_start_time AS planStartTime,
        tp.plan_end_time AS planEndTime,
        tp.start_time AS startTime,
        tp.end_time AS endTime,
        tp.pro_cost AS proCost,
        tp.pro_brief AS proBrief,
        sd.dict_name AS status,
        tp.bid_code AS bidCode,
        tp.sign_code AS signCode,
        tp.pro_code AS proCode
        FROM tb_project tp
        LEFT JOIN sys_build sb ON tp.org = sb.org_id
        LEFT JOIN sys_dict sd ON sd.dict_code = tp.status AND sd.del_flag = 0
        <where>
            tp.del_flag = 0
            <if test="keyWord !=null and keyWord!=''">
                AND (
                INSTR(tp.pro_name,#{keyWord}) > 0 OR
                INSTR(tp.jl_unit,#{keyWord}) > 0 OR
                INSTR(tp.sg_unit,#{keyWord}) > 0 OR
                INSTR(tp.manager,#{keyWord}) > 0 OR
                )
            </if>
        </where>
    </select>
    <!--获取工序计划列表-->
    <select id="getGxPlanLists" resultType="com.securitycontrol.entity.system.base.vo.GxPlanVo">
        SELECT tgp.plan_id AS planId,
        tgp.gx_weight AS gxWeight,
        tgp.plan_start_time AS planStartTime,
        tgp.plan_end_time AS planEndTime,
        tgp.start_time AS startTime,
        tgp.end_time AS endTime,
        tgp.dela_reason AS delaReason,
        tgp.bid_code AS bidCode,
        <if test="proType == '变电'">
            sd.dict_name AS gxName,
        </if>
        <if test="proType == '线路'">
            tpg.gt_name AS gxName,
        </if>
        tgp.gx_id AS gxId,
        tgp.plan_progress AS planProgress
        FROM tb_gx_plan tgp
        <if test="proType == '变电'">
            LEFT JOIN sys_dict sd ON sd.dict_code = tgp.gx_id
        </if>
        <if test="proType == '线路'">
            LEFT JOIN t_pro_gt tpg ON tgp.gx_id = tpg.gt_id
        </if>
        WHERE tgp.bid_code = #{bidCode} AND tgp.del_flag = 0
        <if test="proType == '变电' and keyWord!='' and keyWord!=null">
           AND INSTR(sd.dict_name,#{keyWord}) > 0
        </if>
        <if test="proType == '线路' and keyWord!='' and keyWord!=null">
           AND INSTR(tpg.gt_name,#{keyWord}) > 0
        </if>
    </select>
    <!--工序计划延期提醒-->
    <select id="getGxPlansIsDela" resultType="java.lang.String">
        SELECT plan_id FROM tb_gx_plan WHERE bid_code = #{bidCode} AND end_time IS NULL AND plan_end_time = CURRENT_DATE
    </select>
    <!--获取工序计划进度列表-->
    <select id="getGxPlanProgressLists"
            resultType="com.securitycontrol.entity.system.base.vo.GxPlanProgressVo">
        SELECT tpp.gx_progress AS gxProgress,
               tpp.create_time AS createTime,
               sd.dict_name AS currentGxName
        FROM tb_project_progress tpp
        LEFT JOIN tb_gx_plan tgp ON tpp.plan_id = tgp.plan_id AND tgp.del_flag = 0
        LEFT JOIN sys_dict sd on tpp.gx_id = sd.dict_code AND sd.del_flag = 0
        WHERE tpp.plan_id = #{planId}
        ORDER BY tpp.gx_id,tpp.create_time
    </select>
    <!--工序计划是否第一次填报-->
    <select id="isFirstFillingGxProgress" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_project_progress WHERE plan_id = #{planId}
    </select>
    <!--获取当前工序计划的填报进度-->
    <select id="getGxPlanProgress" resultType="java.lang.String">
        SELECT tpp.gx_progress AS gxProgress
        FROM tb_project_progress tpp
        WHERE plan_id = #{planId} AND gx_id = #{nowGxId}
        ORDER BY create_time DESC
        LIMIT 1
    </select>
    <!--查询该工序计划所有填报信息-->
    <select id="getProgressData" resultType="java.util.Map">
        SELECT tpp.plan_id AS planId,
               tpp.gx_progress AS gxProgress
        FROM tb_project_progress tpp
        INNER JOIN (SELECT MAX( create_time ) AS create_time FROM tb_project_progress WHERE plan_id = #{planId} GROUP BY gx_id) tpp2 ON tpp.create_time = tpp2.create_time
        WHERE plan_id = #{planId}
    </select>
</mapper>