<?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.picking.activiti.mapper.PickIngMapper">

    <sql id="classColumns">
        a.id AS "id",
		a.md_img AS "mdImg",
		a.md_big_class AS "mdBigClass",
		a.md_the_class AS "mdTheClass",
		a.md_small_class AS "mdSmallClass",
		a.md_unit AS "mdUnit"
    </sql>


    <select id="get" resultType="com.jeeplus.modules.sg.picking.activiti.entity.PickIng">
        SELECT
        <include refid="classColumns"/>,
        FROM md_module_block a
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="com.jeeplus.modules.sg.picking.activiti.entity.PickIng">
        SELECT
        <include refid="classColumns"/>
        FROM md_module_block a
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
        </choose>
    </select>

    <select id="findAllList" resultType="com.jeeplus.modules.sg.picking.activiti.entity.PickIng">
        SELECT
        <include refid="classColumns"/>
        FROM md_module_block a where md_version = (select id from md_module_block_version where versionName =
        #{version}) order by md_big_sort,md_the_sort,md_small_sort
    </select>

    <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>


    <!--物理删除-->
    <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="allVersion" resultType="string">
        select versionName
        from md_module_block_version
        order by create_date desc;
    </select>

    <update id="updateSelectNumber">
        update md_acquisition_process
        set select_number = #{selectNumber}
        where proc_ins_id = #{procInsId};
    </update>

    <select id="findSelectNumber" resultType="string">
        select select_number
        from md_acquisition_process
        where proc_ins_id = #{procInsId};
    </select>

    <select id="selectVersion" resultType="string">
        select version
        from md_acquisition_process
        where proc_ins_id = #{procInsId};
    </select>

    <select id="projectNumber" resultType="com.jeeplus.modules.sg.picking.activiti.entity.ProjectNumber">
        select a.project_Name, a.the_number, b.canEnter
        from md_acquisition_process a
                 LEFT JOIN xm_material_project b
                           ON a.reserveProjectName = b.reserveProjectName
        where a.proc_ins_id = #{procInsId};
    </select>

    <update id="updateSelectVersion">
        UPDATE sys_dict_value
        set `value` = #{version}
        where id = '1010101010'
    </update>

    <delete id="delAllTem">
        TRUNCATE md_temporary_list
    </delete>

    <insert id="processList">
        insert into md_process_list (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        procInsId,
        materialCode,
        extensionDescription,
        materialDescription,
        mdUnits,
        mdTotalAmount,
        mdTotalAmountAfter,
        mdSingleWeight,
        mdTotalWeight,
        mdTheParty,
        mdText
        )values
        <foreach collection="showList" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.delFlag},
            #{item.remarks},
            #{item.procInsId},
            #{item.materialCode},
            #{item.extensionDescription},
            #{item.materialDescription},
            #{item.mdUnits},
            #{item.mdTotalAmount},
            #{item.mdTotalAmountAfter},
            #{item.mdSingleWeight},
            #{item.mdTotalWeight},
            #{item.mdTheParty},
            #{item.mdText}
            )
        </foreach>
    </insert>

    <select id="findErpType" resultType="com.jeeplus.modules.sg.picking.activiti.entity.Acquisition">
        select a.id,a.proc_ins_id,b.projectNature as proType,a.reserveProjectName,a.canEnter
        from md_acquisition_process as a
        LEFT JOIN xm_project_basicinformation as b
        on a.reserveProjectName = b.reserveProjectName where a.id
        in(
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
        )
    </select>

    <select id="findErpAll" resultType="com.jeeplus.modules.sg.picking.activiti.entity.Acquisition">
        select a.id,a.proc_ins_id,b.projectNature as proType,a.reserveProjectName,a.canEnter
        from md_acquisition_process as a
        LEFT JOIN xm_project_basicinformation as b
        on a.reserveProjectName = b.reserveProjectName
        where a.reserveProjectName
        in(
            select reserveProjectName from md_acquisition_process where id in(
                <foreach collection="list" item="item" separator=",">
                    #{item}
                </foreach>
            )
        )
    </select>

    <select id="exportList" resultType="com.jeeplus.modules.sg.picking.activiti.entity.CapitalExport">
        select
        a.materialCode as sapMaterialCode,
        a.extensionDescription as deviceSerialNumber,
        a.materialDescription as nameOfBuilding,
        a.mdUnits as company,
        c.projectName as projectName,
        c.projectId as  projectWbsNumber,
        d.referencePrice as estimatedPrice
        from md_process_list as a
        LEFT JOIN md_acquisition_process as b
        on a.procInsId = b.proc_ins_id
        LEFT JOIN xm_material_project as c
        on b.reserveProjectName = c.reserveProjectName
        LEFT JOIN xm_material_provided as d
        on a.materialCode = d.materialCode
        where b.id in(
        <foreach collection="list" item="item" separator=",">
            #{item.id}
        </foreach>
        )
        <if test="projectName != null and projectName != ''">
            AND c.projectName LIKE concat('%',#{projectName},'%')
        </if>
    </select>

    <update id="updateErpAfter">
        update md_acquisition_process set exportAfter = '是',exportTime = now() where id in (
        <foreach collection="list" item="item" separator=",">
            #{item.id}
        </foreach>
        );
    </update>
    <update id="updateEnter">
        update xm_material_project
        set canEnter = '1'
        where reserveProjectName
            in (
            select a.*
            from (
               select reserveProjectName
               from md_acquisition_process
               where proc_ins_id = #{procInsId}) a);
    </update>
    <update id="updateListEnter">
        UPDATE md_acquisition_process
        SET canEnter = 1
        WHERE id in (
            select a.* from (
                SELECT id FROM md_acquisition_process
                WHERE reserveProjectName =(
                      select reserveProjectName
                      from md_acquisition_process
                      where proc_ins_id = #{procInsId}
                      )
                AND the_number &lt;= (
                    SELECT the_number
                    FROM md_acquisition_process
                    WHERE proc_ins_id = #{procInsId})
            )a
        )
    </update>
    <update id="updateEnterList">
        <foreach collection="list" item="item" index="index" separator=";">
            update md_acquisition_process set canEnter = '2' where id = #{item.id}
        </foreach>
    </update>

    <update id="updateProjectEnter">
        <foreach collection="list" item="item" index="index" separator=";">
            update xm_material_project set canEnter = '2',acquisitionStatus ='2' where reserveProjectName = #{item}
        </foreach>
    </update>

    <select id="findExportBlock" resultType="com.jeeplus.modules.sg.picking.activiti.entity.ExportBlock">
        select materialCode,
               materialDescription as projectName,
               extensionDescription,
               mdUnits,
               mdTotalAmount,
               mdSingleWeight,
               mdTotalWeight,
               mdTheParty,
               mdText
        from md_process_list
        where procInsId = #{procInsId}
        order by materialCode, materialDescription;
    </select>

    <delete id="truncateTemporary">
        truncate table md_temporary_list
    </delete>

    <insert id="processTemList">
        insert into md_temporary_list (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        procInsId,
        materialCode,
        extensionDescription,
        materialDescription,
        mdUnits,
        mdTotalAmount,
        mdTotalAmountAfter,
        mdSingleWeight,
        mdTotalWeight,
        mdTheParty,
        mdText
        )values
        <foreach collection="showList" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.delFlag},
            #{item.remarks},
            #{item.procInsId},
            #{item.materialCode},
            #{item.extensionDescription},
            #{item.materialDescription},
            #{item.mdUnits},
            #{item.mdTotalAmount},
            #{item.mdTotalAmountAfter},
            #{item.mdSingleWeight},
            #{item.mdTotalWeight},
            #{item.mdTheParty},
            #{item.mdText}
            )
        </foreach>
    </insert>

    <insert id="processTemReportList">
        insert into md_temporary_report_list (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        procInsId,
        materialCode,
        extensionDescription,
        materialDescription,
        mdUnits,
        mdTotalAmount,
        mdTotalAmountAfter,
        mdSingleWeight,
        mdTotalWeight,
        mdTheParty,
        mdText,
        reserveName
        )values
        <foreach collection="showList" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.delFlag},
            #{item.remarks},
            #{item.procInsId},
            #{item.materialCode},
            #{item.extensionDescription},
            #{item.materialDescription},
            #{item.mdUnits},
            #{item.mdTotalAmount},
            #{item.mdTotalAmountAfter},
            #{item.mdSingleWeight},
            #{item.mdTotalWeight},
            #{item.mdTheParty},
            #{item.mdText},
            #{item.reserveName}
            )
        </foreach>
    </insert>

    <select id="findAllTem" resultType="com.jeeplus.modules.sg.picking.activiti.entity.ShowList">
        select *
        from md_temporary_list
        where procInsId = #{procInsId};
    </select>

    <sql id="materialReportColumns">
        a.id AS "id",
		a.procInsId AS "procInsId",
		a.reserveName AS "reserveName",
		a.materialCode AS "materialCode",
		a.extensionDescription AS "extensionDescription",
		a.materialDescription AS "materialDescription",
		a.mdUnits AS "mdUnits",
		a.mdTotalAmount AS "mdTotalAmount",
		a.mdTotalAmountAfter AS "mdTotalAmountAfter",
		a.mdSingleWeight AS "mdSingleWeight",
		a.mdTotalWeight AS "mdTotalWeight",
		a.mdTheParty AS "mdTheParty",
		a.mdText AS "mdText"
    </sql>

    <select id="findProcInsIdMaterialReport" resultType="com.jeeplus.modules.sg.picking.activiti.entity.ShowList">
        select
        <include refid="materialReportColumns"/>
        from md_temporary_report_list a where a.procInsId = #{procInsId}
    </select>

    <delete id="deleteProcInsIdMaterialReport" parameterType="string">
        delete
        from md_temporary_report_list
        where procInsId = #{procInsId}
    </delete>

    <delete id="deleteProcInsIdPickingApplication">
        delete
        from md_picking_application
        where procInsId = #{procInsId}
    </delete>
</mapper>