<?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.casic.missiles.mapper.meter.MeterTraceSupplierMapper">
    <resultMap id="TraceSupplierDetailResultMap" type="com.casic.missiles.dto.meter.TraceSupplierDetailResponse">
        <result column="supplier_no" property="supplierNo"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="brief_name" property="briefName"/>
        <result column="business_content" property="businessContent"/>
        <result column="tax_number" property="taxNumber"/>
        <result column="bank_account" property="bankAccount"/>
        <result column="bank_name" property="bankName"/>
        <result column="bank_account_number" property="bankAccountNumber"/>
        <result column="postal_code" property="postalCode"/>
        <result column="director" property="director"/>
        <result column="phone" property="phone"/>
        <result column="fax" property="fax"/>
        <result column="mobile" property="mobile"/>
        <result column="mailbox" property="mailbox"/>
        <result column="website" property="website"/>
        <result column="company_country" property="companyCountry"/>
        <result column="company_country_name" property="companyCountryName"/>
        <result column="company_province" property="companyProvince"/>
        <result column="company_province_name" property="companyProvinceName"/>
        <result column="company_city" property="companyCity"/>
        <result column="company_city_name" property="companyCityName"/>
        <result column="company_area" property="companyArea"/>
        <result column="company_area_name" property="companyAreaName"/>
        <result column="company_address" property="companyAddress"/>
        <result column="invoice_country" property="invoiceCountry"/>
        <result column="invoice_country_name" property="invoiceCountryName"/>
        <result column="invoice_province" property="invoiceProvince"/>
        <result column="invoice_province_name" property="invoiceProvinceName"/>
        <result column="invoice_city" property="invoiceCity"/>
        <result column="invoice_city_name" property="invoiceCityName"/>
        <result column="invoice_area" property="invoiceArea"/>
        <result column="invoice_area_name" property="invoiceAreaName"/>
        <result column="invoice_address" property="invoiceAddress"/>
        <result column="business_scope" property="businessScope"/>
        <result column="minio_file_name" property="minioFileName"/>
        <result column="approval_status" property="approvalStatus"/>
        <result column="remark" property="remark"/>
        <result column="create_time" property="createTime"/>

        <collection property="traceSupplierPersonList" javaType="list" ofType="com.casic.missiles.dto.meter.TraceSupplierPersonDetailDTO">
            <result column="person_no" property="personNo"/>
            <result column="name" property="name"/>
            <result column="department" property="department"/>
            <result column="job" property="job"/>
            <result column="personPhone" property="phone"/>
        </collection>
    </resultMap>

    <select id="selectMaxSupplierNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(supplier_no, 12)), 0) from meter_trace_supplier
    </select>

    <select id="selectTraceSupplierById" resultMap="TraceSupplierDetailResultMap">
        SELECT mts.supplier_no, mts.supplier_name, mts.brief_name, mts.business_content, mts.tax_number, mts.bank_account,
        mts.bank_name, mts.bank_account_number, mts.postal_code, mts.director, mts.phone, mts.fax, mts.mobile, mts.mailbox,
        mts.website, mts.company_country, mts.company_country_name, mts.company_province, mts.company_province_name,
        mts.company_city, mts.company_city_name, mts.company_area, mts.company_area_name, mts.company_address,
        mts.invoice_country, mts.invoice_country_name, mts.invoice_province, mts.invoice_province_name, mts.invoice_city,
        mts.invoice_city_name, mts.invoice_area, mts.invoice_area_name, mts.invoice_address, mts.business_scope,
        mts.minio_file_name, mts.approval_status, mts.remark, mts.create_time, mtsp.person_no, mtsp.name, mtsp.department,
        mtsp.job, mtsp.phone AS personPhone, mts.process_id
        FROM meter_trace_supplier mts
        LEFT JOIN meter_trace_supplier_person mtsp ON mts.id = mtsp.supplier_id
        WHERE mts.id = #{id} AND mts.is_del = 0 AND mtsp.is_del = 0
    </select>

    <select id="selectDraftListForApproval" resultType="com.casic.missiles.model.meter.MeterTraceSupplier">
        SELECT *
        FROM meter_trace_supplier
        WHERE is_del = 0
        AND approval_status = #{request.approvalStatus}
        AND create_user_id = #{request.createUserId}
        <if test="request.supplierNo != null and request.supplierNo !=''">
            and supplier_no like concat('%',#{request.supplierNo},'%')
        </if>
        <if test="request.supplierName != null and request.supplierName !=''">
            and supplier_name like concat('%',#{request.supplierName},'%')
        </if>
        <if test="request.businessContent != null and request.businessContent !=''">
            and business_content like concat('%',#{request.businessContent},'%')
        </if>
        <if test="request.companyProvince != null and request.companyProvince !=''">
            and company_province like concat('%',#{request.companyProvince},'%')
        </if>
        <if test="request.companyCity != null and request.companyCity !=''">
            and company_city like concat('%',#{request.companyCity},'%')
        </if>
        <if test="request.companyArea != null and request.companyArea !=''">
            and company_area like concat('%',#{request.companyArea},'%')
        </if>
    </select>

    <select id="selectBatchForApproval" resultType="com.casic.missiles.model.meter.MeterTraceSupplier">
        SELECT *
        FROM meter_trace_supplier
        WHERE is_del = 0
        <if test="request.supplierNo != null and request.supplierNo !=''">
            and supplier_no like concat('%',#{request.supplierNo},'%')
        </if>
        <if test="request.supplierName != null and request.supplierName !=''">
            and supplier_name like concat('%',#{request.supplierName},'%')
        </if>
        <if test="request.businessContent != null and request.businessContent !=''">
            and business_content like concat('%',#{request.businessContent},'%')
        </if>
        <if test="request.companyProvince != null and request.companyProvince !=''">
            and company_province like concat('%',#{request.companyProvince},'%')
        </if>
        <if test="request.companyCity != null and request.companyCity !=''">
            and company_city like concat('%',#{request.companyCity},'%')
        </if>
        <if test="request.companyArea != null and request.companyArea !=''">
            and company_area like concat('%',#{request.companyArea},'%')
        </if>
        and id in
        <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>
