<?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 = #{pprocInsId};
	</select>

	<select id="projectNumber" resultType="com.jeeplus.modules.sg.picking.activiti.entity.ProjectNumber">
		select require_name,the_number from md_acquisition_process as a,xm_project_details as b where a.project_id=b.id and proc_ins_id=#{procInsId}
	</select>

	<update id="updateSelectVersion">
		UPDATE sys_dict_value set `value` = #{version} where id = '1010101010'
	</update>
	
	<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.pro_type
		from md_acquisition_process as a
		LEFT JOIN xm_project_details as b
		on a.project_id = b.id where a.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.require_name as projectName,
		c.project_number 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_project_details as c
			on b.project_id = c.id
		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="projectId != null and projectId != ''">
			AND c.require_name LIKE concat('%',#{projectId},'%')
		</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>

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

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