<?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.background.mapper.IProMapper">
    <!--新增/修改 工程-->
    <insert id="addOrUpdatePro">
        <if test="type == 1">
            INSERT INTO tb_project
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="proId != null and proId != ''">pro_id,</if>
                <if test="userId != null">user_id,</if>
                <if test="signCode != null and signCode!=''">sign_code,</if>
                <if test="bidCode != null and bidCode!=''">bid_code,</if>
                <if test="proCode != null and proCode!=''">pro_code,</if>
                <if test="proName != null and proName!=''">pro_name,</if>
                <if test="proCost != null and proCost!=''">pro_cost,</if>
                <if test="sgUnit != null and sgUnit!=''">sg_unit,</if>
                <if test="jlUnit != null and jlUnit!=''">jl_unit,</if>
                <if test="proType != null and proType!=''">pro_type,</if>
                <if test="proScale != null and proScale!=''">pro_scale,</if>
                <if test="manager != null and manager!=''">manager,</if>
                <if test="proBrief != null and proBrief!=''">pro_brief,</if>
                <if test="nowGx != null and nowGx!=''">now_gx,</if>
                <if test="status != null and status!=''">status,</if>
                <if test="planStartTime != null and planStartTime!=''">plan_start_time,</if>
                <if test="planEndTime != null and planEndTime!=''">plan_end_time,</if>
                <if test="startTime != null and startTime!=''">start_time,</if>
                <if test="endTime != null and endTime!=''">end_time,</if>
                <if test="org != null and org!=''">org,</if>
                del_flag,
                <if test="bidType != null and bidType!=''">bid_type,</if>
                <if test="sgManage != null and sgManage!=''">sg_manage,</if>
                <if test="jlManage != null and jlManage!=''">jl_manage,</if>
                <if test="subType != null and subType!=''">sub_type,</if>
                <if test="tcDate != null and tcDate!=''">tc_date,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="proId != null and proId != ''">#{proId},</if>
                <if test="userId != null">#{userId},</if>
                <if test="signCode != null and signCode!=''">#{signCode},</if>
                <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
                <if test="proCode != null and proCode!=''">#{proCode},</if>
                <if test="proName != null and proName != ''">#{proName},</if>
                <if test="proCost != null and proCost!=''">#{proCost},</if>
                <if test="sgUnit != null and sgUnit!=''">#{sgUnit},</if>
                <if test="jlUnit != null and jlUnit!=''">#{jlUnit},</if>
                <if test="proType != null and proType!=''">#{proType},</if>
                <if test="proScale != null and proScale != ''">#{proScale},</if>
                <if test="manager != null and manager!=''">#{manager},</if>
                <if test="proBrief != null and proBrief!=''">#{proBrief},</if>
                <if test="nowGx != null and nowGx!=''">#{nowGx},</if>
                <if test="status != null and status!=''">#{status},</if>
                <if test="planStartTime != null and planStartTime != ''">#{planStartTime},</if>
                <if test="planEndTime != null and planEndTime!=''">#{planEndTime},</if>
                <if test="startTime != null and startTime!=''">#{startTime},</if>
                <if test="endTime != null and endTime!=''">#{endTime},</if>
                <if test="org != null and org!=''">#{org},</if>
                0,
                <if test="bidType != null and bidType!=''">#{bidType},</if>
                <if test="sgManage != null and sgManage!=''">#{sgManage},</if>
                <if test="jlManage != null and jlManage!=''">#{jlManage},</if>
                <if test="subType != null and subType!=''">#{subType},</if>
                <if test="tcDate != null and tcDate!=''">#{tcDate},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_project
            <set>
                <if test="userId != null">user_id = #{userId},</if>
                <if test="signCode != null and signCode != ''">sign_code = #{signCode},</if>
                <if test="bidCode != null and bidCode != ''">bid_code = #{bidCode},</if>
                <if test="proCode != null and proCode != ''">pro_code = #{proCode},</if>
                <if test="proName != null and proName != ''">pro_name = #{proName},</if>
                <if test="proCost != null and proCost != ''">pro_cost = #{proCost},</if>
                <if test="sgUnit != null and sgUnit != ''">sg_unit = #{sgUnit},</if>
                <if test="jlUnit != null and jlUnit != ''">jl_unit = #{jlUnit},</if>
                <if test="proType != null and proType != ''">pro_type = #{proType},</if>
                <if test="proScale != null and proScale != ''">pro_scale = #{proScale},</if>
                <if test="manager != null and manager != ''">manager = #{manager},</if>
                <if test="nowGx != null and nowGx != ''">now_gx = #{nowGx},</if>
                <if test="status != null and status != ''">status = #{status},</if>
                <if test="planStartTime != null and planStartTime != ''">plan_start_time = #{planStartTime},</if>
                <if test="planEndTime != null and planEndTime != ''">plan_end_time = #{planEndTime},</if>
                <if test="startTime != null and startTime != ''">start_time = #{startTime},</if>
                <if test="endTime != null and endTime != ''">end_time = #{endTime},</if>
                <if test="org != null and org != ''">org = #{org},</if>
                <if test="bidType != null and bidType!=''">bid_type = #{bidType},</if>
                <if test="sgManage != null and sgManage!=''">sg_manage = #{sgManage},</if>
                <if test="jlManage != null and jlManage!=''">jl_manage = #{jlManage},</if>
                <if test="subType != null and subType!=''">sub_type = #{subType},</if>
                <if test="tcDate != null and tcDate!=''">tc_date = #{tcDate},</if>
            </set>
            WHERE pro_id = #{proId}
        </if>
    </insert>
    <!--保存文件资源-->
    <insert id="addFiles">
        INSERT INTO tb_resource_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resourceId != null and resourceId != ''">id,</if>
            <if test="fileType != null">file_type,</if>
            <if test="fileId != null and fileId!=''">file_id,</if>
            <if test="fileSuffix != null and fileSuffix!=''">file_suffix,</if>
            <if test="fileName != null and fileName!=''">file_name,</if>
            <if test="sourceId != null and sourceId!=''">source_id,</if>
            <if test="sourceType != null and sourceType!=''">source_type,</if>
            del_flag,
            <if test="updateTime != null and updateTime!=''">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="resourceId != null and resourceId != ''">#{resourceId},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="fileId != null and fileId!=''">#{fileId},</if>
            <if test="fileSuffix != null and fileSuffix!=''">#{fileSuffix},</if>
            <if test="fileName != null and fileName!=''">#{fileName},</if>
            <if test="sourceId != null and sourceId!=''">#{sourceId},</if>
            <if test="sourceType != null and sourceType!=''">#{sourceType},</if>
            0,
            <if test="updateTime != null and updateTime!=''">#{updateTime},</if>
        </trim>
    </insert>
    <!--新增/修改 工序计划-->
    <insert id="addOrUpdateGxPlan">
        <if test="type == 1">
            INSERT INTO tb_gx_plan
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="planId != null and planId != ''">plan_id,</if>
                <if test="bidCode != null and bidCode!=''">bid_code,</if>
                <if test="gxId != null and gxId!=''">gx_id,</if>
                <if test="gxWeight != null and gxWeight!=''">gx_weight,</if>
                <if test="planStartTime != null and planStartTime!=''">plan_start_time,</if>
                <if test="planEndTime != null and planEndTime!=''">plan_end_time,</if>
                <if test="startTime != null and startTime!=''">start_time,</if>
                <if test="endTime != null and endTime!=''">end_time,</if>
                <if test="delaReason != null and delaReason!=''">dela_reason,</if>
                del_flag,
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="planId != null and planId != ''">#{planId},</if>
                <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
                <if test="gxId != null and gxId!=''">#{gxId},</if>
                <if test="gxWeight != null and gxWeight!=''">#{gxWeight},</if>
                <if test="planStartTime != null and planStartTime != ''">#{planStartTime},</if>
                <if test="planEndTime != null and planEndTime!=''">#{planEndTime},</if>
                <if test="startTime != null and startTime!=''">#{startTime},</if>
                <if test="endTime != null and endTime!=''">#{endTime},</if>
                <if test="delaReason != null and delaReason!=''">#{delaReason},</if>
                0,
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_gx_plan
            <set>
                <if test="gxId != null and gxId != ''">gx_id = #{gxId},</if>
                <if test="gxWeight != null and gxWeight != ''">gx_weight = #{gxWeight},</if>
                <if test="planStartTime != null and planStartTime != ''">plan_start_time = #{planStartTime},</if>
                <if test="planEndTime != null and planEndTime != ''">plan_end_time = #{planEndTime},</if>
                <if test="startTime != null and startTime != ''">start_time = #{startTime},</if>
                <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>
    </insert>
    <!--工程完工-->
    <update id="editProStatus">
        UPDATE tb_project
        <set>
            <if test="startTime != null and startTime!=''">start_time = #{startTime},</if>
            <if test="endTime != null and endTime!=''">end_time = #{endTime},</if>
            <if test="status != null and status != ''">status = '完工',</if>
        </set>
        WHERE pro_id = #{proId}
    </update>
    <!--删除工程-->
    <delete id="delPro">
        UPDATE tb_project SET del_flag = 1 WHERE pro_id = #{proId}
    </delete>
    <!--删除工程工序计划-->
    <delete id="delProGxPlan">
        # DELETE FROM tb_gx_plan WHERE
    </delete>
    <!--删除文件-->
    <delete id="delFile">
        DELETE FROM tb_resource_file WHERE file_id = #{fileId}
    </delete>
    <!--删除工序计划-->
    <delete id="delGxPlanById">
        DELETE FROM tb_gx_plan WHERE plan_id = #{planId}
    </delete>

    <!--获取工程列表-->
    <select id="getProLists" resultType="com.securitycontrol.entity.system.base.vo.ProVo">
        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
                )
            </if>
        </where>
    </select>
    <!--工程图片/平面图数量-->
    <select id="getProFiles" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_resource_file WHERE source_id = #{proId} AND file_type = 1 AND source_type = '工程图片'
        UNION ALL
        SELECT COUNT(*) FROM tb_resource_file WHERE source_id = #{proId} AND file_type = 1 AND source_type = '工程平面图'
    </select>
    <!--工程详情-->
    <select id="getProById" resultType="com.securitycontrol.entity.system.base.vo.ProVo">
        SELECT tp.pro_id AS proId,
        tp.sign_code AS signCode,
        tp.bid_code AS bidCode,
        tp.pro_code AS proCode,
        tp.pro_name AS proName,
        tp.pro_cost AS proCost,
        tp.sg_unit AS sgUnit,
        tp.jl_unit AS jlUnit,
        tp.pro_type AS proType,
        tp.pro_scale AS proScale,
        tp.manager AS manager,
        tp.pro_brief AS proBrief,
        tp.now_gx AS nowGx,
        tp.status,
        tp.plan_start_time AS planStartTime,
        tp.plan_end_time AS planEndTime,
        tp.start_time AS startTime,
        tp.end_time AS endTime,
        tp.org,
        tp.bid_type AS bidType,
        tp.sg_manage AS sgManage,
        tp.jl_manage AS jlManage,
        tp.sub_type AS subType,
        tp.tc_date AS tcDate
        FROM tb_project tp
        WHERE pro_id = #{proId}
    </select>
    <!--获取工程图片/平面图-->
    <select id="getFiles" resultType="com.securitycontrol.entity.system.vo.ResourceFileVo">
        SELECT id AS resourceId,
        file_id AS fileId,
        CASE source_type WHEN '工程图片' THEN '1' WHEN '工程平面图' THEN '2' END AS sourceType
        FROM tb_resource_file WHERE source_id = #{proId} AND del_flag = 0
    </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}
        <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="getGxPlanById" resultType="com.securitycontrol.entity.system.base.vo.GxPlanVo">
        SELECT tgp.plan_id AS planId,
        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,
        tgp.gx_id AS gxId
        FROM tb_gx_plan tgp
        WHERE tgp.bid_code = #{bidCode} AND tgp.plan_id =#{planId}
    </select>
    <!--工程是否包含工序计划-->
    <select id="isHasGxPlan" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_gx_plan WHERE bid_code = #{bidCode}
    </select>
    <!--当前工序计划是否存在进度填报数据-->
    <select id="hasGxProgress" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_project_progress WHERE plan_id = #{planId}
    </select>
    <select id="proIsExist" resultType="java.lang.Integer">
        <if test="proId == null or proId == ''">
            SELECT COUNT(*) FROM tb_project tp WHERE bid_code = #{bidCode} AND del_flag = 0
        </if>
        <if test="proId != null and proId != ''">
            SELECT COUNT(*) FROM tb_project tp WHERE bid_code = #{bidCode} AND pro_id != #{proId} AND del_flag = 0
        </if>
    </select>
    <!--获取工序计划作业权重占比-->
    <select id="getGxWeight" resultType="java.util.Map">
        SELECT plan_id AS planId,
               gx_weight AS gxWeight
        FROM tb_gx_plan
        WHERE bid_code = #{bidCode}
    </select>
    <!--获取工程状态-->
    <select id="getProStatus" resultType="java.util.Map">
        SELECT sd2.dict_code AS id,
               sd2.dict_name AS name
        FROM sys_dict sd
        LEFT JOIN sys_dict sd2 ON sd.dict_code = sd2.p_code AND sd2.del_flag = 0
        WHERE sd.dict_code = 1200 AND sd.del_flag = 0
        ORDER BY sd2.dict_sort
    </select>
    <!--添加工序计划时杆塔是否被重复添加-->
    <select id="getTowerIsExist" resultType="java.lang.Integer">
        <if test="planId == null or planId == ''">
            SELECT COUNT(*) FROM tb_gx_plan WHERE gx_id = #{gxId} AND bid_code = #{bidCode} AND del_flag = 0
        </if>
        <if test="planId != null and planId != ''">
            SELECT COUNT(*) FROM tb_gx_plan WHERE gx_id = #{gxId} AND bid_code = #{bidCode} AND plan_id != #{planId} AND del_flag = 0
        </if>

    </select>
</mapper>