<?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.audit.material.mapper.MaterialMapper">

    <sql id="materialColumns">
		id,
		project_id,
		infor_id,
		material_name,
		outbound_amount,
		authorized_amount,
		difference,
		outbound_number,
		authorized_number,
		price,
		unit,
		material_small,
		material_mid,
		material_big,
		isBack,
		isBackNo,
		afterNumber,
		status,
		info
	</sql>

    <select id="findByMaterialId" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        SELECT
        <include refid="materialColumns"/>
        FROM sj_material_information
        where project_Id = #{projectId} and infor_id = #{inforId} and material_name = #{materialName}
    </select>

    <update id="updateInfo" parameterType="com.jeeplus.modules.sg.audit.material.entity.Material">
		update sj_material_information set status = #{status},info = #{info},isback = "是",isBackNo=0,afterNumber=#{afterNumber},afterAuthor=#{afterAuthor} where id = #{id};
	</update>
    <select id="findList" resultType="Material">
        SELECT
        <include refid="materialColumns"/>
        FROM sj_material_information
        <where>
            1 = 1
            <if test="projectId != null and projectId != ''">
                AND project_id = #{projectId}
            </if>
            <if test="id != null and id!=''">
                AND id=#{id}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY update_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultType="Material">
        SELECT
        <include refid="materialColumns"/>
        FROM sj_material_information
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>
    <select id="get" resultType="Material">
		select id,status,info from sj_material_information where id = #{id}
	</select>
    <select id="projectNeed" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        select project_id  from sj_material_information where project_id
        in(
            <foreach collection="list" index="index" item="material" separator=",">
                #{material.projectId}
            </foreach>
        )
        GROUP BY project_id HAVING SUM(difference) = 0
    </select>
    
    <insert id="saveMater">
        replace into sj_material_information(
        id,
        project_id,
        infor_id,
        material_name,
        outbound_amount,
        authorized_amount,
        difference,
        outbound_number,
        authorized_number,
        price,
        unit,
        material_small,
        material_mid,
        material_big,
        isBack,
        isBackNo,
        status,
        info,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        afterNumber,
        afterAuthor
        ) VALUES
        <foreach collection="list" item="material" index="index" separator=",">
            (
            #{material.id},
            #{material.projectId},
            #{material.inforId},
            #{material.materialName},
            #{material.outboundAmount},
            #{material.authorizedAmount},
            #{material.difference},
            #{material.outboundNumber},
            #{material.authorizedNumber},
            #{material.price},
            #{material.unit},
            #{material.materialSmall},
            #{material.materialMid},
            #{material.materialBig},
            #{material.isBack},
            #{material.isBackNo},
            #{material.status},
            #{material.info},
            #{material.createBy.id},
            #{material.createDate},
            #{material.updateBy.id},
            #{material.updateDate},
            #{material.delFlag},
            #{material.afterNumber},
            #{material.afterAuthor}
            )
        </foreach>
    </insert>

    <insert id="insert">
		INSERT INTO sj_material_information(
			id,
			project_id,
			infor_id,
			material_name,
			outbound_amount,
			authorized_amount,
			difference,
			outbound_number,
			authorized_number,
			price,
			unit,
			material_small,
			material_mid,
			material_big,
			isBack,
			status,
			info,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag
		) VALUES (
			#{id},
			#{projectId},
			#{inforId},
			#{materialName},
			#{outboundAmount},
			#{authorizedAmount},
			#{difference},
			#{outboundNumber},
			#{authorizedNumber},
			#{price},
			#{unit},
			#{materialSmall},
			#{materialMid},
			#{materialBig},
			#{isBack},
			#{status},
			#{info},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag}
		)on duplicate key update
			id = #{id},
			project_Id = #{projectId},
			infor_id = #{inforId},
			material_name = #{materialName},
			outbound_amount = #{outboundAmount},
			authorized_amount = #{authorizedAmount},
			difference = #{difference},
			outbound_number=#{outboundNumber},
			authorized_number = #{authorizedNumber},
			price = #{price},
			unit = #{unit},
			material_small = #{materialSmall},
			material_mid = #{materialMid},
			material_big = #{materialBig},
			isBack = #{isBack},
			status = #{status},
			info = #{info},
			create_by = #{createBy.id},
			create_date = #{createDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			del_flag = #{delFlag}
	</insert>

<select id="findByProIn" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        SELECT
        <include refid="materialColumns"/>
        FROM sj_material_information
        where project_Id = #{projectId} and infor_id = #{inforId}
    </select>

    <select id="findInformation"  resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        select project_id,SUM(isBackNo) as isBackNo
        from sj_material_information
        where project_id
        in (
        <foreach collection="list" item="item" index="index" separator=",">
            #{item.projectId}
        </foreach>
        )
        GROUP BY project_id
    </select>

    <select id="findNeedInfor" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
        select * from sj_project_information where project_id = #{projectId}
    </select>

    <update id="updateNeed" parameterType="com.jeeplus.modules.sg.audit.information.entity.Information">
        update sj_project_information
        <trim prefix="set" suffixOverrides=",">
                secondFinishDate=#{secondFinishDate},
            <if test="firstFinish!=null and firstFinish != ''">
                firstFinish=#{firstFinish},
            </if>
            <if test="firstFinishDate!=null and firstFinishDate != ''">
                firstFinishDate=#{firstFinishDate},
            </if>
            <if test="secondFinish!=null and secondFinish != ''">
                secondFinish=#{secondFinish},
            </if>
        </trim>
        where project_id=#{projectId}
    </update>

    <update id="updateStatus" parameterType="com.jeeplus.modules.sg.audit.material.entity.Material">
        <foreach collection="list" item="item" index="index" separator=";">
        update sj_material_information
            <trim prefix="set" suffixOverrides=",">
                <if test="item.isBackNo!=null">
                     isBackNo=0,
                </if>
                <if test="item.isBack!=null">
                    isBack='是',
                </if>
                <if test="item.status!=null and item.status != ''">
                    status=#{item.status},
                </if>
                <if test="item.info!=null and item.info != ''">
                    info=#{item.info},
                </if>
                <if test="item.afterNumber!=null">
                    afterNumber=#{item.afterNumber},
                </if>
                <if test="item.afterAuthor!=null">
                    afterAuthor=#{item.afterAuthor},
                </if>
            </trim>
            where project_id=#{item.projectId} and infor_id=#{item.inforId}
        </foreach>
    </update>

    <select id="findIsBack" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        SELECT project_id from sj_material_information where project_id in(
        <foreach collection="list" item="item" separator=",">
            #{item.projectId}
        </foreach>
        )
        GROUP BY project_id HAVING SUM(isBackNo) = 0
    </select>
    
    <select id="findProId" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        SELECT project_id from sj_material_information where (status is not null or status != '')
         and project_id in(
         <foreach collection="list" item="item" separator=",">
             #{item.projectId}
         </foreach>
         )
         GROUP BY project_id
    </select>
    
    <select id="proIdName" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
        select project_id,project_name from sj_project_information where project_id in
        (
            <foreach collection="list" item="item" separator=",">
                #{item.projectId}
            </foreach>
        )
    </select>
    
    <select id="findMateial" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        select infor_id,material_name,outbound_number,authorized_number,status,afterNumber,afterAuthor
        from sj_material_information
        where (status is not null
        or status != '')
        and project_id=#{projectId}
    </select>
    
    
    <select id="findStatus" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        select * from sj_material_information where status is not null
    </select>

    <select id="findNeedBack" resultType="string">
        select isBack from sj_material_information where id = #{id};
    </select>

    <select id="idFormId" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
        select * from sj_material_information where id =#{id};
    </select>
</mapper>