<?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.KwcContractLogisticsMapper">

    <select id="queryList" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
         select a.amount,
                a.charging,
                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.unit_type,
                b.ent_id,
                b.firm_name entName,
                e.ent_id targetEntId,
                e.firm_name targetEntName,
                a.performed_amount performedAmount,
                a.contract_pid contractPid,
                f.name contractPname
          from kwc_contract_logistics a
          left join kwc_contract_logistics_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_logistics_track c on a.id = c.contract_id and c.del_flag = 0 and c.type = 1
          left join kwc_contract_logistics_track d on a.id = d.contract_id and d.del_flag = 0 and d.type = 0
          left join kwc_contract_logistics_unit e on a.id = e.contract_id and e.unit_type != #{entType} and e.del_flag = 0
          left join kwc_contract_logistics 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} = 3
                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="charging != null">
                and a.charging = #{charging}
            </if>
            <if test="signingWay != null">
                and a.signing_way = #{signingWay}
            </if>
          <if test="keywords != null and keywords != ''">
              and (b.firm_name like concat('%', #{keywords}, '%') or
                   e.firm_name like concat('%', #{keywords}, '%') or
                   b.contacts 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>
          <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="targetEntId != null">
              and e.ent_id = #{targetEntId}
          </if>
        order by a.create_time desc
    </select>

    <select id="queryLogisticsList" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
        select a.amount,
               a.charging,
               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.unit_type,
               b.ent_id checkedEntId,
               b.firm_name checkedEntName,
               e.ent_id carrierEntId,
               e.firm_name carrierEntName,
               a.performed_amount performedAmount,
               a.contract_pid contractPid,
               f.name contractPname
          from kwc_contract_logistics a
          left join kwc_contract_logistics_unit b on a.id = b.contract_id and b.del_flag = 0 and b.unit_type = 3
          left join kwc_contract_logistics_unit e on a.id = e.contract_id and e.del_flag = 0 and e.unit_type = 4
          left join kwc_contract_logistics_track c on a.id = c.contract_id and c.del_flag = 0 and c.type = 1
          left join kwc_contract_logistics_track d on a.id = d.contract_id and d.del_flag = 0 and d.type = 0
          left join kwc_contract_logistics f on f.id = a.contract_pid
         where a.del_flag = 0
           and a.status != 3
        <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="signingWay != null">
            and a.signing_way = #{signingWay}
        </if>
        <if test="charging != null">
            and a.charging = #{charging}
        </if>
        <if test="keywords != null and keywords != ''">
            and (b.firm_name like concat('%', #{keywords}, '%') or
                 e.firm_name like concat('%', #{keywords}, '%') or
                 b.contacts like concat('%', #{keywords}, '%') or
                 e.contacts like concat('%', #{keywords}, '%') or
                 a.contract_no like concat('%', #{keywords}, '%') or
                 a.name like concat('%', #{keywords}, '%'))
        </if>
        <if test="idList != null and idList.size() > 0">
            and a.id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        order by a.create_time desc
    </select>

</mapper>
