<?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.crm.manager.dao.mapper.SalesCommissionRuleMapper">


    <insert id="save" parameterType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable" >
        INSERT INTO `sales_commission_rule` (
            `rule_name`,
            `sales_type`,
            `kpi_standard`,
            `salary_rule`,
            `personal_kpi_rule`,
            `team_kpi_rule`,
            `personal_volume_rule`,
            `team_volume_rule`,
            `del_flag`,
            `add_time`,
            `add_user`,
            `add_ip`,
            `note`
        )
        VALUES
            (
                #{ruleName},
                #{salesType},
                #{kpiStandard},
                #{salaryRule},
                #{personalKpiRule},
                #{teamKpiRule},
                #{personalVolumeRule},
                #{teamVolumeRule},
                #{delFlag},
                #{addTime},
                #{addUser},
                #{addIp},
                #{note}
            )
    </insert>


    <update id="update" parameterType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable">
        UPDATE `sales_commission_rule`
        SET `rule_name` = #{ruleName},
        `sales_type` = #{salesType},
        `kpi_standard` = #{kpiStandard},
        `salary_rule` = #{salaryRule},
        `personal_kpi_rule` = #{personalKpiRule},
        `team_kpi_rule` = #{teamKpiRule},
        `personal_volume_rule` = #{personalVolumeRule},
        `team_volume_rule` = #{teamVolumeRule},
        `modify_time` = #{modifyTime},
        `modify_user` = #{modifyUser},
        `modify_ip` = #{modifyIp},
        `note` = #{note}
        <where>
            `id` = #{id}
        </where>
    </update>

    <delete id="deleteById" parameterType="Long">
        update `sales_commission_rule`
        set del_flag = 1
        <where>
            `id` = #{id}
        </where>
    </delete>

    <select id="findOne" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable">
        select *
        from `sales_commission_rule`
        <where>
            `id` = #{id}
            <include refid="not_del"></include>
        </where>
    </select>

    <select id="findAll" resultType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable">
        select *
        from `sales_commission_rule`
        <where>
            <include refid="not_del"></include>
        </where>
    </select>

    <select id="countByRuleName" parameterType="String" resultType="Integer">
        select count(id)
        from `sales_commission_rule`
        <where>
            `rule_name` = #{ruleName}
            <include refid="not_del"></include>
        </where>
    </select>

    <select id="countSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesCommissionRuleSearchEntity" resultType="Integer">
        select count(scr.id)
        from sales_commission_rule scr
        <where>
            <if test="ruleName != null">
                scr.rule_name like concat('%',#{ruleName},'%')
            </if>
            <if test="salesType != null">
                scr.sales_type =#{salesType}
            </if>
            <if test="kpiStandard != null">
                scr.kpi_standard =#{kpiStandard}
            </if>
        </where>
    </select>

    <select id="listSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesCommissionRuleSearchEntity" resultType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable">
        select scr.*
        from sales_commission_rule scr
        <where>
            <if test="ruleName != null">
                scr.rule_name like concat('%',#{ruleName},'%')
            </if>
            <if test="salesType != null">
                scr.sales_type =#{salesType}
            </if>
            <if test="kpiStandard != null">
                scr.kpi_standard =#{kpiStandard}
            </if>
        </where>
        order by scr.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="findAllDropdownInfo" resultType="com.crm.rely.backend.core.pojo.table.SalesCommissionRuleTable">
        select id,rule_name,sales_type,kpi_standard
        from sales_commission_rule scr
        <where>
            <include refid="not_del"></include>
        </where>
    </select>

    <sql id="not_del">
        and del_flag = 0
    </sql>
</mapper>