<?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.css.simulation.resource.server.infra.db.mysql.mapper.ScoringRulesMapper">

    <insert id="saveScoringRules" parameterType="api.common.pojo.po.scene.ScoringRulesPO">
        insert into simulation.scoring_rules
        (rules_id, rules_code, rule_name, rule_description, rule_details,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted, share)
        values (#{rulesId,jdbcType=VARCHAR}, #{rulesCode,jdbcType=VARCHAR}, #{ruleName,jdbcType=VARCHAR},
                #{ruleDescription,jdbcType=VARCHAR}, #{ruleDetails},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR}, #{share,jdbcType=VARCHAR})
    </insert>


    <select id="queryScoringRulesList" parameterType="api.common.pojo.param.scene.ScoringRulesParam"
            resultType="api.common.pojo.po.scene.ScoringRulesPO">
        select
        rules_id,rules_code,rule_name,rule_description,rule_details,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share
        from simulation.scoring_rules
        <where>
            is_deleted = '0'
            <if test="rulesId != null and rulesId != ''">
                and rules_id = #{rulesId,jdbcType=VARCHAR}
            </if>
            <if test="rulesCode != null and rulesCode != ''">
                and rules_code = #{rulesCode,jdbcType=VARCHAR}
            </if>
            <if test="share != null and share!=''">
                and share = #{share,jdbcType=VARCHAR}
            </if>
            <if test="ruleName != null and ruleName != ''">
                and rule_name like CONCAT('%',#{ruleName,jdbcType=VARCHAR},'%')
            </if>
            <if test="ruleDescription != null and ruleDescription != ''">
                and rule_description like CONCAT('%',#{ruleDescription},'%')
            </if>
            <if test="yearMax != null and yearMax != ''">
                and #{yearMax,jdbcType=VARCHAR} &gt; create_time
            </if>
            <if test="yearMin != null and yearMin != ''">
                and #{yearMin,jdbcType=VARCHAR} &lt; create_time
            </if>
            <if test="createUserId != null and createUserId != ''">
                and create_user_id=#{createUserId,jdbcType=VARCHAR}
            </if>
            order by modify_time desc
        </where>

    </select>
    <select id="queryScoringRules" parameterType="api.common.pojo.param.scene.ScoringRulesParam"
            resultType="api.common.pojo.po.scene.ScoringRulesPO">
        select
        rules_id,rule_name,rule_description,rule_details,
        create_user_id,create_time,modify_user_id,modify_time,
        is_deleted,share
        from simulation.scoring_rules
        <where>
            is_deleted = '0'
            <if test="rulesId != null and rulesId != ''">
                and rules_id = #{rulesId,jdbcType=VARCHAR}
            </if>
            <if test="ruleName != null and ruleName != ''">
                and rule_name like CONCAT('%',#{ruleName,jdbcType=VARCHAR},'%')
            </if>
        </where>
    </select>

    <select id="queryScoringRulesByRuleName" parameterType="api.common.pojo.param.scene.ScoringRulesParam"
            resultType="api.common.pojo.po.scene.ScoringRulesPO">
        select rules_id,rule_name
        from simulation.scoring_rules
        <where>
            is_deleted = '0'
            <if test="rulesId != null and rulesId != ''">
                and rules_id != #{rulesId}
            </if>
            <if test="ruleName != null and ruleName != ''">
                and rule_name =#{ruleName}
            </if>
            <if test="share != null and share != ''">
                and share=#{share,jdbcType=VARCHAR}
            </if>
        </where>
    </select>

    <select id="querySelfOnly" parameterType="api.common.pojo.param.scene.ScoringRulesParam"
            resultType="api.common.pojo.po.scene.ScoringRulesPO">
        select rules_id, rule_name
        from simulation.scoring_rules
        where is_deleted = '0'
          and share = '0'
          and rule_name = #{ruleName}
          and create_user_id = #{createUserId}
    </select>


    <update id="updateScoringRules" parameterType="api.common.pojo.po.scene.ScoringRulesPO">
        update simulation.scoring_rules
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="share != null and share!=''">
                share = #{share,jdbcType=VARCHAR},
            </if>
            <if test="ruleName != null and ruleName!=''">
                rule_name = #{ruleName,jdbcType=VARCHAR},
            </if>
            <if test="ruleDescription != null and ruleDescription!=''">
                rule_description = #{ruleDescription,jdbcType=VARCHAR},
            </if>
            <if test="ruleDetails != null and ruleDetails!=''">
                rule_details = #{ruleDetails,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>

        </set>
        where rules_id = #{rulesId,jdbcType=VARCHAR}


    </update>

    <update id="deleteScoringRules" parameterType="api.common.pojo.po.scene.ScoringRulesPO">
        update simulation.scoring_rules
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>

        </set>
        where rules_id = #{rulesId,jdbcType=VARCHAR}

    </update>


    <select id="queryCsbById" parameterType="api.common.pojo.param.scene.ScoringRulesParam"
            resultType="java.lang.Integer">
        select COUNT(sublist_id)
        from simulation.scene_package_sublist
        where is_deleted = '0'
          and package_and_rules = #{rulesId,jdbcType=VARCHAR}
    </select>

</mapper>
