<?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.sckw.contract.dao.KwcContractTradeMapper">
    <select id="queryList" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
        select a.amount,
               a.trading,
               a.name             contractName,
               a.contract_no,
               a.create_by        initiateBy,
               a.create_time      initiateTime,
               a.create_time,
               a.start_time,
               a.end_time,
               a.id,
               a.remark,
               a.signing_way,
               a.create_time      signTime,
               a.status,
               b.unit_type,
               b.ent_id,
               b.firm_name        entName,
               b.ent_id           targetEntId,
               b.firm_name        targetEntName,
               a.contract_pid     contractPid,
               a.performed_amount performedAmount,
               f.name             contractPname,
               a.unload_way       unloadWay,
               a.signing_way      signingWay
        from kwc_contract_trade a
            left join kwc_contract_trade_unit b
        on a.id = b.contract_id
        <!--           and b.unit_type = #{entType}-->
        <!--           and case when a.status != 3 then b.ent_id in-->
        <!--                <foreach collection="allEnt" separator="," open="(" close=")" item="item">-->
        <!--                    #{item}-->
        <!--                </foreach>-->
        <!--               else 1=1-->
        <!--                end-->
        and b.del_flag = 0
        <!--          left join kwc_contract_trade_track c on a.id = c.contract_id and c.del_flag = 0 -->
        <!--          left join kwc_contract_trade_track d on a.id = d.contract_id and d.del_flag = 0 -->
        <!--          left join kwc_contract_trade_unit e on a.id = e.contract_id and e.unit_type != #{entType} and e.del_flag = 0-->
        left join kwc_contract_trade f on f.id = a.contract_pid
        where a.del_flag = 0
        <!--           and case when a.status = 3 then a.ent_id = #{entId} and #{entType} = 1-->
        <!--                else (b.ent_id in-->
        <!--                        <foreach collection="allEnt" separator="," open="(" close=")" item="item">-->
        <!--                            #{item}-->
        <!--                        </foreach>-->
        <!--                        and b.id is not null)-->
        <!--                end-->
        <if test="startTime != null">
            and a.create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and a.create_time &lt;= #{endTime}
        </if>
        <if test="status != null">
            and a.status = #{status}
        </if>
        <!--        <if test="keywords != null and keywords != ''">-->
        <!--            and (b.firm_name like concat('%', #{keywords}, '%') or-->
        <!--                 e.firm_name like concat('%', #{keywords}, '%') or-->
        <!--                 b.phone like concat('%', #{keywords}, '%') or-->
        <!--                 b.sign_phone like concat('%', #{keywords}, '%') or-->
        <!--                 a.contract_no like concat('%', #{keywords}, '%') or-->
        <!--                 a.name like concat('%', #{keywords}, '%')-->
        <!--                 <if test="initiateList != null and initiateList.size() > 0">-->
        <!--                     or a.create_by in-->
        <!--                     <foreach collection="initiateList" item="item" open="(" close=")" separator=",">-->
        <!--                         #{item}-->
        <!--                     </foreach>-->
        <!--                 </if> )-->
        <!--        </if>-->
        <!--        <if test="idList != null and idList.size() > 0">-->
        <!--            and a.id in-->
        <!--            <foreach collection="idList" open="(" close=")" separator="," item="item">-->
        <!--                #{item}-->
        <!--            </foreach>-->
        <!--        </if>-->
        <if test="trading != null">
            and a.trading = #{trading}
        </if>
        <if test="signingWay != null">
            and a.signing_way = #{signingWay}
        </if>
        <if test="targetEntId != null">
            and b.ent_id = #{targetEntId}
        </if>
        <if test="contractCode != null  and '' != contractCode">
            and a.contract_no like concat('%', #{contractCode}, '%')
        </if>
        <if test="contractName != null  and '' != contractName">
            and a.name like concat('%', #{contractName}, '%')
        </if>
        <if test="supplementCode != null  and '' != supplementCode">
            and f.contract_pid != null
            and f.contract_no like concat('%', #{supplementCode}, '%')
        </if>
        <if test="purchaseEntId != null  and '' != purchaseEntId">
            and b.unit_type = 2
            and b.id = #{purchaseEntId}
        </if>
        <if test="supplyEntId != null  and '' != supplyEntId">
            and b.unit_type = 1
            and b.id = #{supplyEntId}
        </if>
        order by a.create_time desc
    </select>

    <select id="queryContractValidCount" resultType="com.sckw.contract.model.dto.res.QueryContractValidCountResDto">
        SELECT b.unit_type,
               count(1) cnt
        FROM kwc_contract_trade a
                 left join kwc_contract_trade_unit b on a.id = b.contract_id
        where a.`status` != 3
        <if test="list != null and list.size() > 0">
            and b.ent_id in
            <foreach collection="list" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY b.unit_type
        union all
        SELECT b.unit_type,
               count(1)
        FROM kwc_contract_logistics a
                 left join kwc_contract_logistics_unit b on a.id = b.contract_id
        where a.`status` != 3
        <if test="list != null and list.size() > 0">
            and b.ent_id in
            <foreach collection="list" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY b.unit_type
    </select>

    <select id="queryTradeList" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
        select a.amount,
               a.trading,
               a.name             contractName,
               a.contract_no,
               c.create_by        initiateBy,
               c.create_time      initiateTime,
               a.create_time,
               a.start_time,
               a.end_time,
               a.id,
               a.remark,
               a.signing_way,
               d.create_time      signTime,
               a.status,
               b.ent_id           provideEntId,
               b.firm_name        provideEntName,
               e.ent_id           purchaseEntId,
               e.firm_name        purchaseEntName,
               a.contract_pid     contractPid,
               a.performed_amount performedAmount,
               f.name             contractPname
        from kwc_contract_trade a
                 left join kwc_contract_trade_unit b on a.id = b.contract_id and b.del_flag = 0 and b.unit_type = 1
                 left join kwc_contract_trade_unit e on a.id = e.contract_id and e.del_flag = 0 and e.unit_type = 2
                 left join kwc_contract_trade_track c on a.id = c.contract_id and c.del_flag = 0 and c.type = 1
                 left join kwc_contract_trade_track d on a.id = d.contract_id and d.del_flag = 0 and d.type = 0
                 left join kwc_contract_trade f on a.contract_pid = f.id
        where a.del_flag = 0
          and a.status != 3
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and a.ent_id in
            <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="reqVo.startTime != null">
            and a.create_time >= #{reqVo.startTime}
        </if>
        <if test="reqVo.endTime != null">
            and a.create_time &lt;= #{reqVo.endTime}
        </if>
        <if test="reqVo.status != null">
            and a.status = #{reqVo.status}
        </if>
        <if test="reqVo.keywords != null and reqVo.keywords != ''">
            and (b.firm_name like concat('%', #{reqVo.keywords}, '%') or
                 e.firm_name like concat('%', #{reqVo.keywords}, '%') or
                 b.contacts like concat('%', #{reqVo.keywords}, '%') or
                 e.contacts like concat('%', #{reqVo.keywords}, '%') or
                 a.contract_no like concat('%', #{reqVo.keywords}, '%') or
                 a.name like concat('%', #{reqVo.keywords}, '%'))
        </if>
        <if test="reqVo.idList != null and reqVo.idList.size() > 0">
            and a.id in
            <foreach collection="reqVo.idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="reqVo.trading != null">
            and a.trading = #{reqVo.trading}
        </if>
        <if test="reqVo.signingWay != null">
            and a.signing_way = #{reqVo.signingWay}
        </if>
        order by a.create_time desc
    </select>

    <select id="queryList1" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
        select a.amount,
               a.trading,
               a.name             contractName,
               a.contract_no,
               a.create_by        initiateBy,
               a.create_time      initiateTime,
               a.create_time,
               a.start_time,
               a.end_time,
               a.id,
               a.remark,
               a.signing_way,
               a.sign_time        signTime,
               a.status,
               b.unit_type,
               b.ent_id,
               b.firm_name        entName,
               b.ent_id           targetEntId,
               b.firm_name        targetEntName,
               a.contract_pid     contractPid,
               a.performed_amount performedAmount,
               f.name             contractPname,
               f.contract_no      contractPidNo,
               a.unload_way       unloadWay,
               a.signing_way      signingWay
        from kwc_contract_trade a
                 left join kwc_contract_trade_unit b
                           on a.id = b.contract_id
                               and b.del_flag = 0

                 left join kwc_contract_trade f on f.id = a.contract_pid
        where a.del_flag = 0
        <if test="startTime != null">
            and a.create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and a.create_time &lt;= #{endTime}
        </if>
        <if test="status != null">
            and a.status = #{status}
        </if>
        <if test="trading != null">
            and a.trading = #{trading}
        </if>
        <if test="signingWay != null">
            and a.signing_way = #{signingWay}
        </if>
        <if test="targetEntId != null">
            and b.ent_id = #{targetEntId}
        </if>
        <if test="contractCode != null  and '' != contractCode">
            and a.contract_no like concat('%', #{contractCode}, '%')
        </if>
        <if test="contractName != null  and '' != contractName">
            and a.name like concat('%', #{contractName}, '%')
        </if>
        <if test="supplementCode != null  and '' != supplementCode">
            and f.contract_no like concat('%', #{supplementCode}, '%')
        </if>
        <if test="purchaseEntId != null  and '' != purchaseEntId">
            and b.unit_type = 2
            and b.id = #{purchaseEntId}
        </if>
        <if test="supplyEntId != null  and '' != supplyEntId">
            and b.unit_type = 1
            and b.id = #{supplyEntId}
        </if>
        <if test="allEnt != null and allEnt.size() > 0">
            and a.ent_id in
            <foreach collection="allEnt" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        order by a.create_time desc
    </select>

    <select id="queryNewSignPrice" resultType="com.sckw.contract.api.model.vo.TradeContractGoodsDto">
        select a.id, b.price
        from kwc_contract_trade a
                 left join kwc_contract_trade_goods b on a.id = b.contract_id and b.del_flag = 0
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0
        <where>
            a.del_flag = 0
              and a.status = 0
              and c.unit_type = 2
              and a.start_time &lt; #{time}
              and (a.end_time &gt; #{time} OR a.end_time IS NULL)
              and c.ent_id = #{entId}
              and b.goods_id = #{goodsId}
            order by a.create_time desc
            limit 1
        </where>
    </select>
    <select id="queryNewSignGoods" resultType="java.lang.Long">
        select distinct b.goods_id
        from kwc_contract_trade a
                 left join kwc_contract_trade_goods b on a.id = b.contract_id and b.del_flag = 0
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0
        <where>
            a.del_flag = 0
              and a.status = 0
              and c.unit_type = 2
              and a.start_time &lt; #{time}
              and (a.end_time &gt; #{time} OR a.end_time IS NULL)
              and c.ent_id = #{entId}
        </where>
    </select>

    <select id="querySignTradeContract" resultType="java.lang.Long">
        select distinct b.goods_id
        from kwc_contract_trade a
                 left join kwc_contract_trade_goods b on a.id = b.contract_id and b.del_flag = 0
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0
        <where>
            a.del_flag = 0
              and a.status = 0
              and c.unit_type = 2
              and a.start_time &lt; #{time}
              and (a.end_time &gt; #{time} OR a.end_time IS NULL)
              and c.ent_id = #{entId}
            order by a.create_time desc
        </where>
    </select>

    <select id="selectByContract" resultType="java.lang.Long">
        select distinct b.goods_id
        from kwc_contract_trade a
                 left join kwc_contract_trade_goods b on a.id = b.contract_id and b.del_flag = 0
        <where>
            a.del_flag = 0
              and a.status = 0
              and a.id = #{contractId}
        </where>
    </select>

    <select id="count" resultType="java.lang.Long">
        select count(1)
        from kwc_contract_trade a
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0 and c.unit_type = 1
        <where>
            a.del_flag = 0
              and a.status in (1, 4)
            <if test="ids != null and ids.size() != 0">
                and c.ent_id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="tradeList" resultType="com.sckw.contract.api.model.vo.ContractVo">
        select a.id,
               a.name,
               a.amount,
               a.create_time,
               d.firm_name,
               a.status
        from kwc_contract_trade a
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0 and c.unit_type = 1
                 left join kwc_contract_trade_unit d on a.id = d.contract_id and d.del_flag = 0 and d.unit_type = 2
        <where>
            a.del_flag = 0
            <if test="all != null and all">
                <if test="audit != null and audit">
                    and a.status = 1
                </if>
                <if test="audit != null and !audit">
                    and a.status = 4
                </if>
            </if>
            <if test="all != null and !all">
                and a.status in (1, 4)
            </if>
            <if test="ids != null and ids.size() != 0">
                and c.ent_id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            order by a.create_time desc
            <if test="all != null and all">
                limit 10
            </if>
        </where>
    </select>

    <select id="selectSignCount" resultType="java.lang.Long">
        select count(a.id)
        from kwc_contract_trade a
                 left join kwc_contract_trade_unit c on a.id = c.contract_id and c.del_flag = 0 and c.unit_type = 1
                 left join kwc_contract_trade_unit d on a.id = d.contract_id and d.del_flag = 0 and d.unit_type = 2
        <where>
            a.del_flag = 0
            and a.status =1
            <if test="entId != null">
                and d.ent_id = #{entId}
            </if>
        </where>
    </select>
</mapper>
