<?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.supply.particulars.mapper.SupplyParticularsMapper">

    <sql id="stockOutColumns">
        a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.stock_out_number AS "stockOutNumber",
        a.wbs_element AS "wbsElement",
        a.project_name AS "projectName",
        a.project_definition AS "projectDefinition",
        a.material_number AS "materialNumber",
        ml.material_description AS "materialDescription",
        a.batch AS "batch",
        ml.unit_of_measurement AS "unit",
        a.actual_quantity_issued AS "actualQuantityIssued",
        a.unit_price AS "unitPrice",
        a.money AS "money",
        a.posting_account_date AS "postingAccountDate",
        DATE_FORMAT(a.posting_account_date,'%Y-%m-%d') AS "postingAccountDateStr",
        a.supplier_name AS "supplierName",
        a.factory AS "factory",
        a.picking_number AS "pickingNumber",
        a.picking_create_by AS "pickingCreateBy",
        a.picking_department AS "pickingDepartment",
        a.reserved_number AS "reservedNumber",
        a.repertory_place AS "repertoryPlace",
        a.project_package_name AS "projectPackageName",
        a.stock_out_person AS "stockOutPerson",
        a.wbs_element_description AS "wbsElementDescription",
        a.project_second_classification AS "projectSecondClassification",
        a.project_thirdly_classification AS "projectThirdlyClassification",
        a.order_number AS "orderNumber",
        a.assess_type AS "assessType",
        a.stock_out_import_date AS "stockOutImportDate",
        DATE_FORMAT(a.stock_out_import_date,'%Y-%m-%d') AS "stockOutImportDateStr",
        a.accountant_certificate AS "accountantCertificate",
        a.reply_proof AS "replyProof",
        a.materials_type AS "materialsType"
    </sql>

    <sql id="judgementBillsColumns">
        a.project_name AS "projectName",
        a.project_definition AS "projectDefinition",
        ml.material_description AS "materialDescription",
        ml.unit_of_measurement AS "unit",
        sum(a.examine_count) AS "examineCount",
        a.material_number AS "materialNumber",
        a.submittals_id AS "submittalsId"
    </sql>

    <select id="getSupplyStockOutList" resultType="com.jeeplus.modules.supply.stockOut.entity.SupplyStockOut">
        select
        <include refid="stockOutColumns"/>
        from supply_stock_out a
        left join material_library ml on ml.material_code = a.material_number
        <where>
            <if test="projectDefinitionList.size>0">
                and a.project_definition in
                <foreach collection="projectDefinitionList" item="projectDefinition" index="index" open="(" separator="," close=")">
                    #{projectDefinition}
                </foreach>
            </if>
        </where>
        order by a.project_definition asc,a.actual_quantity_issued desc,a.posting_account_date asc
    </select>

    <select id="getSupplyJudgementBillsListList" resultType="com.jeeplus.modules.supply.judgementBills.entity.SupplyJudgementBills">
        select
        <include refid="judgementBillsColumns"/>
        from supply_judgement_bills a
        left join material_library ml on ml.material_code = a.material_number
        <where>
            <if test="projectDefinitionList.size>0">
                and a.project_definition in
                <foreach collection="projectDefinitionList" item="projectDefinition" index="index" open="(" separator="," close=")">
                    #{projectDefinition}
                </foreach>
            </if>
            <choose>
                <when test="null != submittalsId and '' != submittalsId">
                    and a.submittals_id = #{submittalsId}
                </when>
                <otherwise>
                    and (a.submittals_id is null or a.submittals_id = '')
                </otherwise>
            </choose>
        </where>
        group by a.project_definition,a.material_number
    </select>

    <select id="getProjectDefinitionListBySubmittalsId" resultType="java.lang.String">
        select project_definition from supply_judgement_bills where submittals_id = #{submittalsId}
    </select>

    <select id="getAvgUtilPriceOnStockOutByMaterialNumber" resultType="java.lang.String">
        select ifnull(round(avg(unit_price),3),'0.000') from supply_stock_out where material_number= #{materialNumber}
    </select>

</mapper>
