<?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.jeeplus.modules.sg.settActivi.mapper.SettActiviMapper">

    <insert id="insert">
        INSERT INTO md_acquisition_process
            (id,
           proc_ins_id,
           project_id,
           the_number,
           version,
           select_number,
           create_by,
           create_date,
           update_by,
           update_date,
           remarks,
           del_flag)
        VALUES (#{id},
                #{procInsId},
                #{projectId},
                #{theNumber},
                #{version},
                #{selectNumber},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag})
    </insert>

    <update id="update">
        UPDATE md_acquisition_process
        SET proc_ins_id   = #{procInsId},
            project_id    = #{projectId},
            the_number    = #{theNumber},
            version       = #{version},
            select_number = #{selectNumber},
            update_by     = #{updateBy.id},
            update_date   = #{updateDate},
            remarks       = #{remarks}
        WHERE id = #{id}
    </update>

    <select id="get" resultType="SettActivi">
        SELECT id,
               create_by,
               create_date,
               update_by,
               update_date,
               del_flag,
               remarks,
               projectId,
               projectName,
               proc_ins_id,
               reserveProjectName
        from xm_settlement_process
        WHERE id = #{id};

    </select>

    <!--物理删除-->
    <update id="delete">
        DELETE
        FROM md_module_block
        WHERE id = #{id}
    </update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE md_module_block
        SET del_flag = #{DEL_FLAG_DELETE}
        WHERE id = #{id}
    </update>

    <select id="findByIdList" resultType="com.jeeplus.modules.sg.managementcenter.project.entity.Project1">
        select project_id,project_number,require_name,construction_unit_manage from xm_project_details where id in
        (
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
        )
    </select>

    <insert id="insertSettList">
        insert into xm_settlement_list(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        projectId,
        requireName,
        procInsId,
        materialEncoding,
        materialName,
        company,
        unitPrice,
        totalPrice,
        outgoingQuantity,
        usedQuantity,
        reviewQuantity,
        settMark,
        overUnder
        )VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.projectId},
            #{item.requireName},
            #{item.procInsId},
            #{item.materialEncoding},
            #{item.materialName},
            #{item.company},
            #{item.unitPrice},
            #{item.totalPrice},
            #{item.outgoingQuantity},
            0,
            0,
            '',
            0
            )
        </foreach>
    </insert>

    <insert id="insertSettActivi">
        replace into xm_settlement_process(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        projectId,
        projectName,
       reserveProjectName
        )VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.delFlag},
            #{item.remarks},
            #{item.projectId},
            #{item.projectName},
            #{item.reserveProjectName}
            )
        </foreach>
    </insert>
    <select id="findAllSett" resultType="com.jeeplus.modules.sg.settActivi.entity.SettActivi">
        select a.id,a.projectId,a.reserveProjectName,a.proc_ins_id,b.construction_unit_manage from xm_settlement_process a
        left join xm_project_details b
        on a.projectId = b.project_id
        where a.reserveProjectName in
        (
        <foreach collection="list" item="item" separator=",">
            #{item.reserveProjectName}
        </foreach>
        )
    </select>

    <update id="updateSetStatus">
        update xm_project_details set sett_status = '1' where project_id in
        (
        <foreach collection="list" item="item" separator=",">
            #{item.projectId}
        </foreach>
        )
    </update>

    <update id="finSetStatus">
        update xm_project_details
        set sett_status = '2'
        where project_id = (
            select projectId from xm_settlement_process where proc_ins_id = #{procInsId}
        );
    </update>

    <select id="findOutgoingQuantity" resultType="java.lang.String">
        select outgoingQuantity
        from xm_settlement_list
        where id = #{id};
    </select>

    <select id="findUseQuantityData" resultType="com.jeeplus.modules.sg.settActivi.entity.SettList">
        select id,
               projectId,
               requireName,
               materialEncoding,
               materialName,
               company,
               unitPrice,
               totalPrice,
               outgoingQuantity,
               usedQuantity,
               reviewQuantity,
               settMark,
               overUnder
        from xm_settlement_list
        where procInsId = #{procInsId}
    </select>

    <update id="updateUsed">
        update xm_settlement_list
        set usedQuantity = #{usedQuantity}
        where id = #{id};
    </update>

    <update id="updateReviewQuantity">
        update xm_settlement_list
        set reviewQuantity = #{reviewQuantity},
            overUnder      = #{overUnder}
        where id = #{id};
    </update>

    <update id="updateSettMark">
        update xm_settlement_list
        set settMark = #{settMark}
        where id = #{id};
    </update>

    <select id="exportFind" resultType="com.jeeplus.modules.sg.settActivi.entity.SettActivi">
        select a.reserveProjectName, b.project_id, b.construction_unit_manage
        from xm_settlement_process a
                 left join xm_project_details b
                           on a.projectId = b.project_id
        where a.proc_ins_id = #{procInsId}
    </select>

    <insert id="insertSett">
        replace into xm_settlement_process(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        projectId,
        projectName,
        reserveProjectName
        )VALUES
            (
            #{id},
            #{createBy.id},
            #{createDate},
            #{updateBy.id},
            #{updateDate},
            #{delFlag},
            #{remarks},
            #{projectId},
            #{projectName},
            #{reserveProjectName}
            )
    </insert>
</mapper>