<?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="cn.ezhizao.project.business.carrier.mapper.BizCarrierMapper">

    <resultMap type="cn.ezhizao.project.business.carrier.domain.BizCarrier" id="BizCarrierResult">
        <id column="id" property="id"/>
    </resultMap>

    <select id="getList" resultMap="BizCarrierResult" parameterType="cn.ezhizao.project.business.carrier.domain.BizCarrier">
        select t1.id,t1.category_id,t1.code,t1.qc_code,t1.remark,t1.tenant_id,t1.is_allow_more,t1.create_time,
               COALESCE((SELECT t2.is_abandoned FROM biz_carrier_reject t2 WHERE t1.id = t2.carrier_id ORDER BY t2.create_time DESC LIMIT 1), 0) AS is_abandoned, t1.is_print
        FROM biz_carrier t1
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="categoryId != null  and categoryId != 0"> AND category_id = #{categoryId}</if>
            <if test="tenantId != null  and tenantId != 0"> AND tenant_id = #{tenantId}</if>
            <if test="code != null  and code != ''"> AND code LIKE CONCAT('%', #{code}, '%')</if>
            <if test="isPrint != null"> AND is_print = #{isPrint}</if>
            <if test="isAbandoned != null and isAbandoned == 1">  AND (
                SELECT t2.is_abandoned
                FROM biz_carrier_reject t2
                WHERE t2.carrier_id = t1.id
                ORDER BY t2.create_time DESC limit 1
                ) = 1 </if>
            <if test="isAbandoned != null and isAbandoned == 0">   AND ((
                SELECT t2.is_abandoned
                FROM biz_carrier_reject t2
                WHERE t2.carrier_id = t1.id
                ORDER BY t2.create_time DESC limit 1
                ) != 1 or ( SELECT count(1)
                FROM biz_carrier_reject t2
                WHERE t2.carrier_id = t1.id) = 0)  </if>
        </trim>

    </select>

    <update id="unAbandoned">
        update biz_carrier
        <set>
            is_abandoned=0,
            abandonment_date = '0000-00-00 00:00:00.000000',
            abandonment_reason = ''
        </set>
        where id = #{id}
    </update>

    <delete id="physicalDelete">
        DELETE FROM biz_carrier
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
