<?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.yiqi.app.mapper.AppDeliveryManMapper">

    <resultMap type="AppDeliveryMan" id="AppDeliveryManResult">
        <result property="id" column="id"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="status" column="status"/>
        <result property="description" column="description"/>
        <result property="orderCount" column="order_count"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectAppDeliveryManVo">
        select id, app_user_id, status, description, order_count, del_flag, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark
        from app_delivery_man
    </sql>

    <select id="selectAppDeliveryManList"  resultType="com.yiqi.app.domain.vo.AppDeliveryManListVO">
        SELECT
        a.id, a.status,
        a.app_user_id as appUserId,
        c.real_name AS realName,
        a.order_count AS orderCount,
        c.phone_number AS phoneNumber,
        a.description AS description,
        c.LEVEL AS LEVEL
        FROM
        app_delivery_man a
        LEFT JOIN app_user c ON a.app_user_id = c.id
        <where>
            a.del_flag='0'
            <if test="appUserName != null ">
                and c.real_name  like concat('%', #{appUserName}, '%')
            </if>
            <if test="phoneNumber != null ">
                and c.phone_number  = #{phoneNumber}
            </if>
        </where>
    </select>

    <select id="selectAppDeliveryManById" parameterType="Long"
            resultMap="AppDeliveryManResult">
        <include refid="selectAppDeliveryManVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="getDeliveryManListByOrg" resultType="com.yiqi.app.domain.vo.AppDeliveryManListVO">
        select
            c.id as appUserId,
            c.real_name AS realName
        FROM
            app_delivery_relation a
        LEFT JOIN app_user c ON a.app_user_id = c.id
        JOIN app_delivery_man d ON d.app_user_id = c.id
        where a.org_id = #{orgId} and a.source_type = #{sourceType}
    </select>

    <insert id="insertAppDeliveryMan" parameterType="AppDeliveryMan" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_delivery_man
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="appUserId != null">app_user_id,
            </if>
            <if test="status != null">status,
            </if>
            <if test="description != null">description,
            </if>
            <if test="orderCount != null">order_count,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="status != null">#{status},
            </if>
            <if test="description != null">#{description},
            </if>
            <if test="orderCount != null">#{orderCount},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateAppDeliveryMan" parameterType="AppDeliveryMan">
        update app_delivery_man
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status =
                #{status},
            </if>
            <if test="description != null">description =
                #{description},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppDeliveryManById" parameterType="Long">
        update app_delivery_man set del_flag='2' where id = #{id}
    </delete>

    <delete id="deleteAppDeliveryManByIds" parameterType="String">
        update app_delivery_man set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>