<?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.engine.salary.mapper.sicategory.ICategoryMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.sicategory.po.ICategoryPO">
        <result column="id" property="id"/>
        <result column="insurance_name" property="insuranceName"/>
        <result column="welfare_type" property="welfareType"/>
        <result column="is_use" property="isUse"/>
        <result column="payment_scope" property="paymentScope"/>
        <result column="data_type" property="dataType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id
        , t.insurance_name
        , t.welfare_type
        , t.is_use
        , t.payment_scope
        , t.data_type
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>

    <sql id="paramSql">
        <if test="dataType != null and dataType != ''">
            AND data_type = #{dataType}
        </if>
    </sql>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE id = #{id} AND delete_type = 0
    </select>



    <!-- 插入全部字段 -->
    <insert id="insert" parameterType="com.engine.salary.entity.sicategory.po.ICategoryPO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_insurance_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            insurance_name,
            welfare_type,
            is_use,
            payment_scope,
            data_type,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{id},
            #{insuranceName},
            #{welfareType},
            #{isUse},
            #{paymentScope},
            #{dataType},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
        </trim>
    </insert>

    <insert id="insert" parameterType="com.engine.salary.entity.sicategory.po.ICategoryPO" databaseId="oracle">
        INSERT INTO hrsa_insurance_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            insurance_name,
            welfare_type,
            is_use,
            payment_scope,
            data_type,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{id},
            #{insuranceName},
            #{welfareType},
            #{isUse},
            #{paymentScope},
            #{dataType},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
        </trim>
    </insert>


    <select id="listByName" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE insurance_name= #{insuranceName} AND delete_type = 0
        ORDER BY id DESC
    </select>



    <select id="listByWelfareType" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE welfare_type= #{welfareType} AND delete_type = 0 AND is_use = 1
        <include refid="paramSql"/>
        ORDER BY id DESC
    </select>


    <select id="listByDataType" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE data_type= #{dataType} AND delete_type = 0
    </select>


    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE delete_type = 0
    </select>

    <select id="getByIdAndDataType" resultType="com.engine.salary.entity.sicategory.po.ICategoryPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_category t
        WHERE id = #{id} AND data_type = #{dataType} AND delete_type = 0
    </select>


    <!-- 更新,更新修改字段 -->
    <update id="update" parameterType="com.engine.salary.entity.sicategory.po.ICategoryPO">
        UPDATE hrsa_insurance_category
        <set>
            update_time=#{updateTime},
            insurance_name=#{insuranceName},
            welfare_type=#{welfareType},
            payment_scope=#{paymentScope},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <!-- 更新状态 -->
    <update id="updateById" parameterType="com.engine.salary.entity.sicategory.po.ICategoryPO">
        UPDATE hrsa_insurance_category
        SET is_use = #{isUse}
        WHERE id = #{id}
    </update>
    <!--更新自定义福利名称-->
    <update id="updateNameById">
        UPDATE hrsa_insurance_category
        SET insurance_name = #{insuranceName}
        WHERE id = #{id} AND data_type = 0
    </update>

    <!--更新自定义福利名称和缴费对象-->
    <update id="updateNameAndPayScopeById">
        UPDATE hrsa_insurance_category
        <set>
            update_time=#{updateTime},
            insurance_name=#{insuranceName},
            payment_scope=#{paymentScope},
        </set>
        WHERE id = #{id} AND data_type = 0
    </update>

    <!--更新自定义福利名称-->
    <select id="listCustomInsurance" resultType="com.engine.salary.entity.sicategory.dto.ICategoryListDTO">
        select
        id
        ,insurance_name
        ,welfare_type
        ,is_use
        ,payment_scope
        ,data_type
        ,create_time
        ,update_time
        ,creator
        ,delete_type
        ,tenant_key
        from hrsa_insurance_category t
        WHERE  delete_Type = 0 and data_type != 1
        <if test="welfareType != null">
            AND welfare_type = #{welfareType}
        </if>
    </select>

    <!--根据id删除自定义福利项-->
    <update id="deleteCustomCategoryById" parameterType="com.engine.salary.entity.sicategory.po.ICategoryPO">
        UPDATE hrsa_insurance_category
        <set>
            update_time=#{updateTime},
            delete_type = 1
        </set>
        WHERE id = #{id} AND delete_type = 0 AND data_type = 0
    </update>

</mapper>