<?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.zfire.mall.manager.commonmapper.engin.info.EnginInfoReserveMapper">

    <resultMap id="listResultMap" type="com.zfire.mall.manager.bean.engin.info.EnginInfoBean">
        <result column="engin_info_id" jdbcType="VARCHAR" property="enginInfoId" />
        <result column="admin_company_id" jdbcType="VARCHAR" property="adminCompanyId" />
        <result column="customer_id" jdbcType="VARCHAR" property="customerId" />
        <result column="customer_number" jdbcType="VARCHAR" property="customerNumber" />
        <result column="customer_name" jdbcType="VARCHAR" property="customerName" />
        <result column="engin_info_no" jdbcType="VARCHAR" property="enginInfoNo" />
        <result column="order_date" jdbcType="TIMESTAMP" property="orderDate" />
        <result column="main_id" jdbcType="VARCHAR" property="mainId" />
        <result column="main_name" jdbcType="VARCHAR" property="mainName" />
        <result column="project_name" jdbcType="VARCHAR" property="projectName" />
        <result column="project_type" jdbcType="VARCHAR" property="projectType" />
        <result column="project_note" jdbcType="VARCHAR" property="projectNote" />
        <result column="machine_type" jdbcType="VARCHAR" property="machineType" />
        <result column="use_unit" jdbcType="VARCHAR" property="useUnit" />
        <result column="install_address" jdbcType="VARCHAR" property="installAddress" />
        <result column="project_no" jdbcType="VARCHAR" property="projectNo" />
        <result column="engin_factory_no" jdbcType="VARCHAR" property="enginFactoryNo" />
        <result column="engin_sign_type" jdbcType="VARCHAR" property="enginSignType" />
        <result column="trade_category" jdbcType="VARCHAR" property="tradeCategory" />
        <result column="linkman" jdbcType="VARCHAR" property="linkman" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="tel" jdbcType="VARCHAR" property="tel" />
        <result column="declare_no" jdbcType="VARCHAR" property="declareNo" />
        <result column="power_category" jdbcType="VARCHAR" property="powerCategory" />
        <result column="service_id" jdbcType="VARCHAR" property="serviceId" />
        <result column="service_name" jdbcType="VARCHAR" property="serviceName" />
        <result column="contract_expire_date" jdbcType="TIMESTAMP" property="contractExpireDate" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="ge_li_note" jdbcType="VARCHAR" property="geLiNote" />
        <result column="ge_li_iner_note" jdbcType="VARCHAR" property="geLiInerNote" />
        <result column="file_no" jdbcType="VARCHAR" property="fileNo" />
        <result column="examine_status" jdbcType="VARCHAR" property="examineStatus" />
        <result column="examine_note" jdbcType="VARCHAR" property="examineNote" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_name" jdbcType="VARCHAR" property="createName" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
        <result column="update_name" jdbcType="VARCHAR" property="updateName" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="submit_by" jdbcType="VARCHAR" property="submitBy" />
        <result column="submit_name" jdbcType="VARCHAR" property="submitName" />
        <result column="submit_time" jdbcType="TIMESTAMP" property="submitTime" />
        <result column="confirm_by" jdbcType="VARCHAR" property="confirmBy" />
        <result column="confirm_name" jdbcType="VARCHAR" property="confirmName" />
        <result column="confirm_time" jdbcType="TIMESTAMP" property="confirmTime" />
        <result column="abandon_by" jdbcType="VARCHAR" property="abandonBy" />
        <result column="abandon_name" jdbcType="VARCHAR" property="abandonName" />
        <result column="abandon_time" jdbcType="TIMESTAMP" property="abandonTime" />
        <result column="is_plan_order" jdbcType="BOOLEAN" property="isPlanOrder" />
        <collection property="items" resultMap="itemResultList" />
    </resultMap>

    <resultMap id="itemResultList" type="com.zfire.mall.manager.bean.engin.info.EnginInfoItemBean">
        <result column="itemId" jdbcType="VARCHAR" property="id" />
        <result column="sale_type_id" jdbcType="VARCHAR" property="saleTypeId" />
        <result column="sale_type_code" jdbcType="VARCHAR" property="saleTypeCode" />
        <result column="sale_type_name" jdbcType="VARCHAR" property="saleTypeName" />
        <result column="material_id" jdbcType="VARCHAR" property="materialId" />
        <result column="material_number" jdbcType="VARCHAR" property="materialNumber" />
        <result column="material_old_number" jdbcType="VARCHAR" property="materialOldNumber" />
        <result column="material_name" jdbcType="VARCHAR" property="materialName" />
        <result column="material_old_number" jdbcType="VARCHAR" property="materialOldNumber" />
        <result column="specification" jdbcType="VARCHAR" property="specification" />
        <result column="unit" jdbcType="VARCHAR" property="unit" />
        <result column="price" jdbcType="DECIMAL" property="price" />
        <result column="qty" jdbcType="INTEGER" property="qty" />
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
        <result column="item_remark" jdbcType="VARCHAR" property="remark" />
        <result column="tax_rate" jdbcType="DECIMAL" property="taxRate" />
        <result column="has_order_qty" jdbcType="INTEGER" property="hasOrderQty" />
        <result column="has_deliver_qty" jdbcType="INTEGER" property="hasDeliverQty" />
    </resultMap>

    <select id="listPage" resultType="com.zfire.mall.manager.bean.engin.info.EnginInfoListBean">
        SELECT
            a.*
        FROM
            engin_info_order a, engin_info_order_item b
        <where>
            a.engin_info_id = b.engin_info_id
            <if test="customerId != null and customerId != ''">
                AND a.customer_id = #{customerId}
            </if>
            <if test="enginInfoNo != null and enginInfoNo != ''">
                AND a.engin_info_no LIKE CONCAT('%', #{enginInfoNo}, '%')
            </if>
            <if test="customerKeyWord != null and customerKeyWord != ''">
                AND (a.customer_name LIKE CONCAT('%', #{customerKeyWord}, '%')
                OR a.customer_number LIKE CONCAT('%', #{customerKeyWord}, '%'))
            </if>
            <if test="projectName != null and projectName != ''">
                AND a.project_name LIKE CONCAT('%', #{projectName}, '%')
            </if>
            <if test="machineType != null and machineType != ''">
                AND a.machine_type = #{machineType}
            </if>
            <if test="useUnit != null and useUnit != ''">
                AND a.use_unit LIKE CONCAT('%', #{useUnit}, '%')
            </if>
            <if test="installAddress != null and installAddress != ''">
                AND a.install_address LIKE CONCAT('%', #{installAddress}, '%')
            </if>
            <if test="projectNo != null and projectNo != ''">
                AND a.project_no LIKE CONCAT('%', #{projectNo}, '%')
            </if>
            <if test="enginFactoryNo != null and enginFactoryNo != ''">
                AND a.engin_factory_no LIKE CONCAT('%', #{enginFactoryNo}, '%')
            </if>
            <if test="enginSignType != null and enginSignType != ''">
                AND a.engin_sign_type LIKE CONCAT('%', #{enginSignType}, '%')
            </if>
            <if test="startContractExpireDate != null and startContractExpireDate!=''">
                AND a.contract_expire_date <![CDATA[ >= ]]> #{startContractExpireDate}
            </if>
            <if test="endContractExpireDate != null and endContractExpireDate!=''">
                AND a.contract_expire_date <![CDATA[ <= ]]> #{startContractExpireDate}
            </if>
            <if test="createName != null and createName != ''">
                AND a.create_name LIKE CONCAT('%', #{createName},'%')
            </if>
            <if test="confirmName != null and confirmName != ''">
                AND a.confirm_name LIKE CONCAT('%', #{confirmName},'%')
            </if>
            <if test="examineStatus != null and examineStatus != ''">
                AND a.examine_status = #{examineStatus}
            </if>
            <if test="serviceId != null and serviceId != ''">
                AND a.service_id = #{serviceId}
            </if>
            <if test="specification != null and specification != ''">
                AND b.specification LIKE CONCAT('%', #{specification},'%')
            </if>
            <if test="isClose != null">
                and a.is_close = #{isClose}
            </if>
            <if test="enginOrderType != null and enginOrderType != ''">
                AND a.engin_order_type = #{enginOrderType}
            </if>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="serviceIds != null and serviceIds.size > 0">
                and a.service_id in
                <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND a.contract_expire_date <![CDATA[ >= ]]> NOW()
            AND a.end_status = 0
            AND a.del = 0
        </where>
        GROUP BY a.engin_info_id
        HAVING SUM(b.qty) - SUM(b.has_deliver_qty) > 0
        ORDER BY a.confirm_time DESC
    </select>
    <select id="detail" resultMap="listResultMap">
        SELECT
            a.*,
            b.sale_type_id,
            b.sale_type_code,
            b.sale_type_name,
            b.material_id,
            b.material_number,
            b.material_old_number,
            b.material_name,
            b.specification,
            b.unit,
            b.qty,
            b.price,
            b.total_amount,
            b.remark AS item_remark,
            b.tax_rate,
            b.has_order_qty,
            b.has_deliver_qty
        FROM
            engin_info_order a JOIN engin_info_order_item b ON a.engin_info_id = b.engin_info_id
        <where>
            a.engin_info_id = #{id}
            <if test="customerId != null and customerId != ''">
                AND a.customer_id = #{customerId}
            </if>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="serviceIds != null and serviceIds.size > 0">
                and a.service_id in
                <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND a.del = 0
        </where>
    </select>
    <select id="queryDetailByRecordNo" resultMap="listResultMap">
        SELECT
            a.*,
            b.sale_type_id,
            b.sale_type_code,
            b.sale_type_name,
            b.material_id,
            b.material_number,
            b.material_old_number,
            b.material_name,
            b.specification,
            b.unit,
            b.qty,
            b.price,
            b.total_amount,
            b.remark AS item_remark,
            b.tax_rate,
            b.has_order_qty,
            b.has_deliver_qty
        FROM
            engin_info_order a JOIN engin_info_order_item b ON a.engin_info_id = b.engin_info_id
        <where>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="customerId != null and customerId != ''">
                AND a.customer_id = #{customerId}
            </if>
            AND a.engin_info_no = #{recordNo}
            AND a.contract_expire_date <![CDATA[ >= ]]> NOW()
            AND b.has_deliver_qty <![CDATA[ < ]]> b.qty
            AND a.is_close = 0
            AND a.del = 0
        </where>
    </select>
</mapper>