<?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.ruoyi.project.business.mapper.contract.TqContractMapper">
    
    <resultMap type="TqContract" id="TqContractResult">
        <result property="id"    column="id"    />
        <result property="contractNo"    column="contract_no"    />
        <result property="contractName"    column="contract_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="countryId"    column="country_id"    />
        <result property="city"    column="city"    />
        <result property="salesman"    column="salesman"    />
        <result property="endCustomers"    column="end_customers"    />
        <result property="tradeTerms"    column="trade_terms"    />
        <result property="agentId"    column="agent_id"    />
        <result property="paymentMethod"    column="payment_method"    />
        <result property="salesModel"    column="sales_model"    />
        <result property="currencyId"    column="currency_id"    />
        <result property="amount"    column="amount"    />
        <result property="signingDate"    column="signing_date"    />
        <result property="paymentRatio"    column="payment_ratio"    />
        <result property="approveStatus"    column="approve_status"    />
        <result property="permissionChar"    column="permission_char"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
        <result property="rate4SigningContracts"    column="rate_4_signing_contracts"    />

        <result property="address"    column="address"    />
        <result property="telephone"    column="telephone"    />
        <result property="email"    column="email"    />
        <result property="shippingMark"    column="shipping_mark"    />
        <result property="shipmentPort"    column="shipment_port"    />
        <result property="destinationPort"    column="destination_port"    />
        <result property="shipmentDeadline"    column="shipment_deadline"    />
        <result property="version"    column="version"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.contract.TqContractVo" id="TqContractVoResult" extends="TqContractResult">
        <result property="salesmanName"    column="salesman_name"    />
        <result property="agentName"    column="agent_name"    />
        <result property="endCustomersName"    column="account_name"    />
        <result property="countryName"    column="country_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="salesModelName"    column="salesModelName"    />
        <result property="paymentMethodName"    column="paymentMethodName"    />
        <result property="tradeTermsName"    column="tradeTermsName"    />
        <result property="paymentRadioName"    column="paymentRadioName"    />
        <result property="typeAuthority"    column="type_authority"    />
    </resultMap>

    <sql id="selectTqContractVo">
        select id,version, contract_no, contract_name, area_id, country_id, city, salesman, end_customers, trade_terms, agent_id, payment_method, sales_model, currency_id, amount, signing_date, payment_ratio, approve_status, permission_char, del_flag, create_time, create_by, update_time, update_by, remark, status, rate_4_signing_contracts, address, telephone, email, shipping_mark, shipment_port, destination_port, shipment_deadline from tq_contract
    </sql>

    <select id="selectTqContractList" parameterType="TqContract" resultMap="TqContractResult">
        <include refid="selectTqContractVo"/>
        <where>  
            <if test="contractNo != null  and contractNo != ''"> and contract_no = #{contractNo}</if>
            <if test="contractName != null  and contractName != ''"> and contract_name like concat('%', #{contractName}, '%')</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="countryId != null "> and country_id = #{countryId}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="salesman != null "> and salesman = #{salesman}</if>
            <if test="endCustomers != null  and endCustomers != ''"> and end_customers = #{endCustomers}</if>
            <if test="tradeTerms != null  and tradeTerms != ''"> and trade_terms = #{tradeTerms}</if>
            <if test="agentId != null "> and agent_id = #{agentId}</if>
            <if test="paymentMethod != null  and paymentMethod != ''"> and payment_method = #{paymentMethod}</if>
            <if test="salesModel != null  and salesModel != ''"> and sales_model = #{salesModel}</if>
            <if test="currencyId != null  and currencyId != ''"> and currency_id = #{currencyId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="signingDate != null "> and signing_date = #{signingDate}</if>
            <if test="paymentRatio != null  and paymentRatio != ''"> and payment_ratio = #{paymentRatio}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and approve_status = #{approveStatus}</if>
            <if test="permissionChar != null  and permissionChar != ''"> and permission_char = #{permissionChar}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="rate4SigningContracts != null "> and rate_4_signing_contracts = #{rate4SigningContracts}</if>

            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="telephone != null  and telephone != ''"> and telephone = #{telephone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="shippingMark != null  and shippingMark != ''"> and shipping_mark = #{shippingMark}</if>
            <if test="shipmentPort != null  and shipmentPort != ''"> and shipment_port = #{shipmentPort}</if>
            <if test="destinationPort != null  and destinationPort != ''"> and destination_port = #{destinationPort}</if>
            <if test="shipmentDeadline != null "> and shipment_deadline = #{shipmentDeadline}</if>
            <if test="version != null "> and version = #{version}</if>
        </where>
    </select>
    
    <select id="selectTqContractById" parameterType="Long" resultMap="TqContractResult">
        <include refid="selectTqContractVo"/>
        where id = #{id}
    </select>
    <select id="selectTqContractBoList" resultMap="TqContractVoResult">
        select ah.*,account.account_name as account_name,flow.status as flowStatus,u.nick_name as salesman_name,ag.agent_name,RIGHT(dict.remark, 1) as unit,vo.type as type_authority,
            CASE #{language}
            WHEN 'chinese' THEN country.country_name
            WHEN 'english' THEN country.country_nameUS
            WHEN 'russia' THEN country.country_nameRU
            WHEN 'spanish' THEN country.country_nameES
            END AS country_name,
            CASE #{language}
            WHEN 'chinese' THEN area.area_name
            WHEN 'english' THEN area.area_nameUS
            WHEN 'russia' THEN area.area_nameRU
            WHEN 'spanish' THEN area.area_nameES
            END AS area_name
               from tq_contract as ah
        LEFT JOIN sys_user u on ah.salesman = u.user_id and u.del_flag = '0'
        LEFT JOIN tc_account account on ah.end_customers = account.account_id  and account.del_flag = '0'
        LEFT JOIN flow flow ON ah.id = flow.business_id and ah.contract_no = flow.business_desc and flow.del_flag = '0'
        LEFT JOIN tb_country country on country.country_id = ah.country_id  AND ah.del_flag = country.del_flag
        LEFT JOIN tb_area area on area.area_id = ah.area_id  AND ah.del_flag = area.del_flag
        left join tb_agent ag on ah.agent_id = ag.agent_id AND ah.del_flag = ag.del_flag
        LEFT JOIN sys_dict_data dict ON dict.dict_type = 'currency_type' and dict.dict_value = ah.currency_id
        LEFT JOIN (select DISTINCT contract_no,CAST(SUBSTRING_INDEX(contract_no_version_no, '-', -1) AS UNSIGNED) as version,type from tq_contract_lift where del_flag = '0'  and  type = 'toVoid' ) as vo
                ON vo.contract_no = ah.contract_no and vo.version = ah.version
        <where>
            ah.del_flag != '1'
            and flow.del_flag = '0'
            <if test="flowStatus != null and flowStatus.size() > 0">
                and flow.status in
                <foreach collection="flowStatus" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="id != null "> and ah.id = #{id}</if>
            <if test="contractNo != null  and contractNo != ''"> and ah.contract_no like concat('%',#{contractNo}, '%')</if>
            <if test="contractName != null  and contractName != ''"> and ah.contract_name like concat('%', #{contractName}, '%')</if>
            <if test="areaId != null and areaId.size() > 0">
                and ah.area_id in
                <foreach collection="areaId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="countryId != null and countryId.size() > 0">
                and ah.country_id in
                <foreach collection="countryId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="city != null  and city != ''"> and ah.city like concat('%',#{city},'%') </if>
            <if test="salesman != null "> and ah.salesman = #{salesman}</if>
            <if test="endCustomers != null  and endCustomers != ''"> and ah.end_customers   like concat('%',#{endCustomers},'%')</if>
            <if test="tradeTerms != null and tradeTerms.size() > 0">
                and ah.trade_terms in
                <foreach collection="tradeTerms" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="agentId != null and agentId.size() > 0">
                and ah.agent_id in
                <foreach collection="agentId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="paymentMethod != null  and paymentMethod != ''"> and ah.payment_method = #{paymentMethod}</if>
            <if test="salesModel != null and salesModel.size() > 0">
                and ah.sales_model in
                <foreach collection="salesModel" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="amount != null "> and ah.amount = #{amount}</if>
            <if test="signingDate != null and signingDate.length == 2"> and ah.signing_date
                <foreach collection="signingDate" item="item" open="between " separator=" and " >
                    #{item}
                </foreach>
            </if>
            <if test="paymentRatio != null  and paymentRatio != ''"> and ah.payment_ratio = #{paymentRatio}</if>
            <if test="approveStatus != null and approveStatus.size() > 0">
                and ah.approve_status in
                <foreach collection="approveStatus" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="permissionChar != null  and permissionChar != ''"> and ah.permission_char = #{permissionChar}</if>
            <if test="status != null and status.size() > 0">
                and ah.status in
                <foreach collection="status" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="rate4SigningContracts != null "> and ah.rate_4_signing_contracts = #{rate4SigningContracts}</if>
            <if test="nickName != null "> and u.nick_name like concat('%',#{nickName},'%')</if>

            <if test="address != null "> and ah.address like concat('%',#{address},'%')</if>
            <if test="telephone != null "> and ah.telephone like concat('%',#{telephone},'%')</if>
            <if test="email != null "> and ah.email like concat('%',#{email},'%')</if>
            <if test="shipmentPort != null "> and ah.shipment_port like concat('%',#{shipmentPort},'%')</if>
            <if test="destinationPort != null "> and ah.destination_port like concat('%',#{destinationPort},'%')</if>
            <if test="shippingMark != null "> and ah.shipping_mark like concat('%',#{shippingMark},'%')</if>

            <if test="shipmentDeadline != null and shipmentDeadline.length == 2"> and ah.shipment_deadline
                <foreach collection="shipmentDeadline" item="item" open="between " separator=" and " >
                    #{item}
                </foreach>
            </if>

            <if test="endCustomersName != null "> and account.account_name like  concat('%', #{endCustomersName}, '%')</if>
            <if test="version != null "> and ah.version = #{version}</if>
        </where>
        order by ah.id desc
    </select>
    <select id="selectTqContractBoListApp" resultMap="TqContractVoResult">
        select ah.* ,
        u.nick_name as salesman_name,
        ag.agent_name,
        account.account_name,
        country.country_name,
        t1.dict_label as salesModelName,
        t2.dict_label as tradeTermsName,
        t3.dict_label as paymentMethodName,
        t4.dict_label as paymentRadioName
        from tq_contract as ah
        left join sys_user u on ah.salesman = u.user_id
        left join tb_agent ag on ah.agent_id = ag.agent_id
        left join tc_account account on ah.end_customers = account.account_id
        left join tb_country country on country.country_id = ah.country_id  AND ah.del_flag = country.del_flag
        left join tb_area area on area.area_id = ah.area_id  AND ah.del_flag = area.del_flag
        left join sys_dict_data t1 on t1.dict_type = 'contract_sales_model' and ah.sales_model = t1.dict_value
        left join sys_dict_data t2 on t2.dict_type = 'contract_trade_terms' and ah.trade_terms = t2.dict_value
        left join sys_dict_data t3 on t3.dict_type = 'contract_payment_method' and ah.payment_method = t3.dict_value
        left join sys_dict_data t4 on t4.dict_type = 'contract_payment_ratio' and ah.payment_ratio = t4.dict_value
        <where>
            ah.del_flag != '1'
            <if test="id != null "> and ah.id = #{id}</if>
            <if test="contractNo != null  and contractNo != ''"> and ah.contract_no like concat('%',#{contractNo}, '%')</if>
            <if test="contractName != null  and contractName != ''"> and ah.contract_name like concat('%', #{contractName}, '%')</if>
            <if test="areaId != null and areaId.size() > 0">
                and ah.area_id in
                <foreach collection="areaId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="countryId != null and countryId.size() > 0">
                and ah.country_id in
                <foreach collection="countryId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="city != null  and city != ''"> and ah.city like concat('%',#{city},'%') </if>
            <if test="salesman != null "> and ah.salesman = #{salesman}</if>
            <if test="endCustomers != null  and endCustomers != ''"> and ah.end_customers   like concat('%',#{endCustomers},'%')</if>
            <if test="tradeTerms != null and tradeTerms.size() > 0">
                and ah.trade_terms in
                <foreach collection="tradeTerms" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="agentId != null and agentId.size() > 0">
                and ah.agent_id in
                <foreach collection="agentId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="paymentMethod != null  and paymentMethod != ''"> and ah.payment_method = #{paymentMethod}</if>
            <if test="salesModel != null and salesModel.size() > 0">
                and ah.sales_model in
                <foreach collection="salesModel" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="amount != null "> and ah.amount = #{amount}</if>
            <if test="signingDate != null and signingDate.length == 2"> and ah.signing_date
                <foreach collection="signingDate" item="item" open="between " separator=" and " >
                    #{item}
                </foreach>
            </if>
            <if test="paymentRatio != null  and paymentRatio != ''"> and ah.payment_ratio = #{paymentRatio}</if>
            <if test="approveStatus != null and approveStatus.size() > 0">
                and ah.approve_status in
                <foreach collection="approveStatus" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="permissionChar != null  and permissionChar != ''"> and ah.permission_char = #{permissionChar}</if>
            <if test="status != null and status.size() > 0">
                and ah.status in
                <foreach collection="status" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="rate4SigningContracts != null "> and ah.rate_4_signing_contracts = #{rate4SigningContracts}</if>
            <if test="rate4SigningContracts != null "> and ah.rate_4_signing_contracts = #{rate4SigningContracts}</if>
            <if test="nickName != null "> and u.nick_name like concat('%',#{nickName},'%')</if>

            <if test="address != null "> and ah.address like concat('%',#{address},'%')</if>
            <if test="telephone != null "> and ah.telephone like concat('%',#{telephone},'%')</if>
            <if test="email != null "> and ah.email like concat('%',#{email},'%')</if>
            <if test="shipmentPort != null "> and ah.shipment_port like concat('%',#{shipmentPort},'%')</if>
            <if test="destinationPort != null "> and ah.destination_port like concat('%',#{destinationPort},'%')</if>
            <if test="shippingMark != null "> and ah.shipping_mark like concat('%',#{shippingMark},'%')</if>

            <if test="shipmentDeadline != null and shipmentDeadline.length == 2"> and ah.shipment_deadline
                <foreach collection="shipmentDeadline" item="item" open="between " separator=" and " >
                    #{item}
                </foreach>
            </if>
            <if test="version != null "> and ah.version = #{version}</if>
        </where>
        order by ah.id desc
    </select>
    <insert id="insertTqContract" parameterType="TqContract" useGeneratedKeys="true" keyProperty="id">
        insert into tq_contract
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="contractNo != null">contract_no,</if>
            <if test="contractName != null">contract_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="countryId != null">country_id,</if>
            <if test="city != null">city,</if>
            <if test="salesman != null">salesman,</if>
            <if test="endCustomers != null">end_customers,</if>
            <if test="tradeTerms != null">trade_terms,</if>
            <if test="agentId != null">agent_id,</if>
            <if test="paymentMethod != null">payment_method,</if>
            <if test="salesModel != null">sales_model,</if>
            <if test="currencyId != null">currency_id,</if>
            <if test="amount != null">amount,</if>
            <if test="signingDate != null">signing_date,</if>
            <if test="paymentRatio != null">payment_ratio,</if>
            <if test="approveStatus != null">approve_status,</if>
            <if test="permissionChar != null">permission_char,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            <if test="status != null">status,</if>
            <if test="rate4SigningContracts != null">rate_4_signing_contracts,</if>

            <if test="address != null">address,</if>
            <if test="telephone != null">telephone,</if>
            <if test="email != null">email,</if>
            <if test="shippingMark != null">shipping_mark,</if>
            <if test="shipmentPort != null">shipment_port,</if>
            <if test="destinationPort != null">destination_port,</if>
            <if test="shipmentDeadline != null">shipment_deadline,</if>
            <if test="version != null">version,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="contractNo != null">#{contractNo},</if>
            <if test="contractName != null">#{contractName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="countryId != null">#{countryId},</if>
            <if test="city != null">#{city},</if>
            <if test="salesman != null">#{salesman},</if>
            <if test="endCustomers != null">#{endCustomers},</if>
            <if test="tradeTerms != null">#{tradeTerms},</if>
            <if test="agentId != null">#{agentId},</if>
            <if test="paymentMethod != null">#{paymentMethod},</if>
            <if test="salesModel != null">#{salesModel},</if>
            <if test="currencyId != null">#{currencyId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="signingDate != null">#{signingDate},</if>
            <if test="paymentRatio != null">#{paymentRatio},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="permissionChar != null">#{permissionChar},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
            <if test="rate4SigningContracts != null">#{rate4SigningContracts},</if>

            <if test="address != null">#{address},</if>
            <if test="telephone != null">#{telephone},</if>
            <if test="email != null">#{email},</if>
            <if test="shippingMark != null">#{shippingMark},</if>
            <if test="shipmentPort != null">#{shipmentPort},</if>
            <if test="destinationPort != null">#{destinationPort},</if>
            <if test="shipmentDeadline != null">#{shipmentDeadline},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <update id="updateTqContract" parameterType="TqContract">
        update tq_contract
        <trim prefix="SET" suffixOverrides=",">
            <if test="contractNo != null">contract_no = #{contractNo},</if>
            <if test="contractName != null">contract_name = #{contractName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="countryId != null">country_id = #{countryId},</if>
            <if test="city != null">city = #{city},</if>
            <if test="salesman != null">salesman = #{salesman},</if>
            <if test="endCustomers != null">end_customers = #{endCustomers},</if>
            <if test="tradeTerms != null">trade_terms = #{tradeTerms},</if>
            <if test="agentId != null">agent_id = #{agentId},</if>
            <if test="paymentMethod != null">payment_method = #{paymentMethod},</if>
            <if test="salesModel != null">sales_model = #{salesModel},</if>
            <if test="currencyId != null">currency_id = #{currencyId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="signingDate != null">signing_date = #{signingDate},</if>
            <if test="paymentRatio != null">payment_ratio = #{paymentRatio},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="permissionChar != null">permission_char = #{permissionChar},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
            <if test="rate4SigningContracts != null">rate_4_signing_contracts = #{rate4SigningContracts},</if>

            <if test="address != null">address = #{address},</if>
            <if test="telephone != null">telephone = #{telephone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="shippingMark != null">shipping_mark = #{shippingMark},</if>
            <if test="shipmentPort != null">shipment_port = #{shipmentPort},</if>
            <if test="destinationPort != null">destination_port = #{destinationPort},</if>
            <if test="shipmentDeadline != null">shipment_deadline = #{shipmentDeadline},</if>
            <if test="version != null">version = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTqContractById" parameterType="Long">
        delete from tq_contract where id = #{id}
    </delete>

    <delete id="deleteTqContractByIds" parameterType="String">
        delete from tq_contract where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>