<?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.mall.mapper.MallAppSalesmanAchievementMapper">

    <resultMap type="MallAppSalesmanAchievement" id="MallAppSalesmanAchievementResult">
        <result property="achievementId"    column="achievement_id"    />
        <result property="salesmanId"    column="salesman_id"    />
        <result property="salesmanName"    column="salesman_name"    />
        <result property="price"    column="price"    />
        <result property="money"    column="money"    />
        <result property="status"    column="status"    />
        <result property="linkOrderId"    column="link_order_id"    />
        <result property="linkOrderNo"    column="link_order_no"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="deliveryType"    column="delivery_type"    />
        <result property="linkUserId"    column="link_user_id"    />
        <result property="linkUserName"    column="link_user_name"    />
        <result property="linkUserPhone"    column="link_user_phone"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="isGive"    column="is_give"    />
    </resultMap>

    <sql id="selectMallAppSalesmanAchievementVo">
        select achievement_id, salesman_id, salesman_name, price, money, status, link_order_id, link_order_no, order_status, delivery_type, link_user_id, link_user_name, link_user_phone, create_at, update_at, del_flag, is_give from mall_app_salesman_achievement
    </sql>

    <select id="selectMallAppSalesmanAchievementList" parameterType="MallAppSalesmanAchievement" resultMap="MallAppSalesmanAchievementResult">
        select masa.achievement_id, maoo.salesman_id,
        ms.name salesman_name, maoo.order_price price, maoo.commission_price money,
               masa.status, masa.link_order_id, masa.link_order_no,
               maoo.status order_status, maoo.delivery_type, masa.link_user_id, maoo.receiver_name link_user_name, maoo.receiver_phone link_user_phone,
               masa.create_at, masa.update_at, masa.del_flag, masa.is_give
        from mall_app_salesman_achievement masa
        left join mall_app_order_order maoo on masa.link_order_id = maoo.id
        left join mall_salesman ms on maoo.salesman_id = ms.sys_user_id
        <where>
            <if test="salesmanId != null "> and maoo.salesman_id = #{salesmanId}</if>
            <if test="salesmanName != null  and salesmanName != ''"> and ms.name like concat('%', #{salesmanName}, '%')</if>
            <if test="price != null "> and masa.price = #{price}</if>
            <if test="status != null  and status != ''"> and masa.status = #{status}</if>
            <if test="linkOrderId != null "> and masa.link_order_id = #{linkOrderId}</if>
            <if test="linkOrderNo != null  and linkOrderNo != ''"> and masa.link_order_no like concat('%', #{linkOrderNo}, '%')</if>
            <if test="orderStatus != null  and orderStatus != ''"> and maoo.status = #{orderStatus}</if>
            <if test="deliveryType != null "> and masa.delivery_type = #{deliveryType}</if>
            <if test="linkUserId != null "> and masa.link_user_id = #{linkUserId}</if>
            <if test="linkUserName != null  and linkUserName != ''"> and maoo.receiver_name like concat('%', #{linkUserName}, '%')</if>
            <if test="linkUserPhone != null  and linkUserPhone != ''"> and maoo.receiver_phone like concat('%', #{linkUserPhone}, '%')</if>
            <if test="createAt != null  and createAt != ''"> and masa.create_at = #{createAt}</if>
            <if test="updateAt != null  and updateAt != ''"> and masa.update_at = #{updateAt}</if>
            <if test="delFlag != null  and delFlag != ''"> and masa.del_flag = #{delFlag}</if>
            <if test="isGive != null  and isGive != ''"> and masa.is_give = #{isGive}</if>
            <if test="params.beginAchievementTime != null and params.beginAchievementTime != ''">
                AND DATE_FORMAT(masa.create_at,'%Y-%m-%d') &gt;= #{params.beginAchievementTime}
            </if>
            <if test="params.endAchievementTime != null and params.endAchievementTime != ''">
                AND DATE_FORMAT(masa.create_at,'%Y-%m-%d') &lt;= #{params.endAchievementTime}
            </if>
        </where>
        --             and maoo.commission_price > 0
        order by masa.create_at desc
    </select>

    <select id="selectMallAppSalesmanAchievementByAchievementId" parameterType="Long" resultMap="MallAppSalesmanAchievementResult">
        <include refid="selectMallAppSalesmanAchievementVo"/>
        where achievement_id = #{achievementId}
    </select>

    <insert id="insertMallAppSalesmanAchievement" parameterType="MallAppSalesmanAchievement" useGeneratedKeys="true" keyProperty="achievementId">
        insert into mall_app_salesman_achievement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="salesmanId != null">salesman_id,</if>
            <if test="salesmanName != null">salesman_name,</if>
            <if test="price != null">price,</if>
            <if test="money != null">money,</if>
            <if test="status != null">status,</if>
            <if test="linkOrderId != null">link_order_id,</if>
            <if test="linkOrderNo != null">link_order_no,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="deliveryType != null">delivery_type,</if>
            <if test="linkUserId != null">link_user_id,</if>
            <if test="linkUserName != null">link_user_name,</if>
            <if test="linkUserPhone != null">link_user_phone,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="isGive != null">is_give,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="salesmanId != null">#{salesmanId},</if>
            <if test="salesmanName != null">#{salesmanName},</if>
            <if test="price != null">#{price},</if>
            <if test="money != null">#{money},</if>
            <if test="status != null">#{status},</if>
            <if test="linkOrderId != null">#{linkOrderId},</if>
            <if test="linkOrderNo != null">#{linkOrderNo},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="deliveryType != null">#{deliveryType},</if>
            <if test="linkUserId != null">#{linkUserId},</if>
            <if test="linkUserName != null">#{linkUserName},</if>
            <if test="linkUserPhone != null">#{linkUserPhone},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="isGive != null">#{isGive},</if>
         </trim>
    </insert>

    <update id="updateMallAppSalesmanAchievement" parameterType="MallAppSalesmanAchievement">
        update mall_app_salesman_achievement
        <trim prefix="SET" suffixOverrides=",">
            <if test="salesmanId != null">salesman_id = #{salesmanId},</if>
            <if test="salesmanName != null">salesman_name = #{salesmanName},</if>
            <if test="price != null">price = #{price},</if>
            <if test="money != null">money = #{money},</if>
            <if test="status != null">status = #{status},</if>
            <if test="linkOrderId != null">link_order_id = #{linkOrderId},</if>
            <if test="linkOrderNo != null">link_order_no = #{linkOrderNo},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="deliveryType != null">delivery_type = #{deliveryType},</if>
            <if test="linkUserId != null">link_user_id = #{linkUserId},</if>
            <if test="linkUserName != null">link_user_name = #{linkUserName},</if>
            <if test="linkUserPhone != null">link_user_phone = #{linkUserPhone},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="isGive != null">is_give = #{isGive},</if>
        </trim>
        where achievement_id = #{achievementId}
    </update>

    <delete id="deleteMallAppSalesmanAchievementByAchievementId" parameterType="Long">
        delete from mall_app_salesman_achievement where achievement_id = #{achievementId}
    </delete>

    <delete id="deleteMallAppSalesmanAchievementByAchievementIds" parameterType="String">
        delete from mall_app_salesman_achievement where achievement_id in
        <foreach item="achievementId" collection="array" open="(" separator="," close=")">
            #{achievementId}
        </foreach>
    </delete>

    <select id="selectMallAppSalesmanAchievementByOrderId" parameterType="Long" resultMap="MallAppSalesmanAchievementResult">
        <include refid="selectMallAppSalesmanAchievementVo"/>
        where link_order_id = #{orderId}
    </select>
</mapper>
