<?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.system.mapper.PosTypeMapper">

    <resultMap type="PosType" id="PosTypeResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="nameVi"    column="name_vi"    />
        <result property="nameEn"    column="name_en"    />
        <result property="nameTw"    column="name_tw"    />
        <result property="image"    column="image"    />
        <result property="sort"    column="sort"    />
        <result property="parentId"    column="parent_id"    />
        <result property="tag"    column="tag"    />
        <result property="ancestors"    column="ancestors"    />
    </resultMap>

    <sql id="selectPosTypeVo">
        select id, name,name_vi,name_en,name_tw, image,sort,parent_id,tag,ancestors from pos_type
    </sql>

    <select id="selectPosTypeList" parameterType="PosType" resultMap="PosTypeResult">
        <include refid="selectPosTypeVo"/>
        <where>
            <if test="name != null  and name != ''"> and name = #{name}</if>
            <if test="nameVi != null  and nameVi != ''"> and name_vi = #{nameVi}</if>
            <if test="nameEn != null  and nameEn != ''"> and name_en = #{nameEn}</if>
            <if test="nameTw != null  and nameTw != ''"> and name_tw = #{nameTw}</if>
            <if test="sort != null  and sort != ''"> and sort = #{sort}</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
            <if test="tag != null  and tag != ''"> and tag = #{tag}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
        </where>
        order by parent_id, sort desc
    </select>

    <select id="selectPosTypeById" parameterType="Long" resultMap="PosTypeResult">
        <include refid="selectPosTypeVo"/>
        where id = #{id}
    </select>

    <insert id="insertPosType" parameterType="PosType" useGeneratedKeys="true" keyProperty="id">
        insert into pos_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="nameVi != null and nameVi != ''">name_vi,</if>
            <if test="nameEn != null and nameEn != ''">name_en,</if>
            <if test="nameTw != null and nameTw != ''">name_tw,</if>
            <if test="image != null and image != ''">image,</if>
            <if test="sort != null and sort != ''">sort,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="tag != null and tag != ''">tag,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="nameVi != null and nameVi != ''">#{nameVi},</if>
            <if test="nameEn != null and nameEn != ''">#{nameEn},</if>
            <if test="nameTw != null and nameTw != ''">#{nameTw},</if>
            <if test="image != null and image != ''">#{image},</if>
            <if test="sort != null and sort != ''">#{sort},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="tag != null and tag != ''">#{tag},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
         </trim>
    </insert>

    <update id="updatePosType" parameterType="PosType">
        update pos_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="nameVi != null and nameVi != ''">name_vi = #{nameVi},</if>
            <if test="nameEn != null and nameEn != ''">name_en = #{nameEn},</if>
            <if test="nameTw != null and nameTw != ''">name_tw = #{nameTw},</if>
            <if test="image != null and image != ''">image = #{image},</if>
            <if test="sort != null and sort != ''">sort = #{sort},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="tag != null and tag != ''">tag = #{tag},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePosTypeById" parameterType="Long">
        delete from pos_type where id = #{id}
    </delete>

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

        <select id="selectPosTypeTreeList" parameterType="PosType" resultMap="PosTypeResult">
        <include refid="selectPosTypeVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="nameVi != null  and nameVi != ''"> and name_vi like concat('%', #{nameVi}, '%')</if>
            <if test="nameEn != null  and nameEn != ''"> and name_en like concat('%', #{nameEn}, '%')</if>
            <if test="nameTw != null  and nameTw != ''"> and name_tw like concat('%', #{nameTw}, '%')</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
            <if test="tag != null  and tag != ''"> and tag = #{tag}</if>
        </where>
        order by parent_id, sort desc
    </select>

    <select id="selectPosTypeCountByParentId" parameterType="Long" resultType="Integer">
        select count(1) from pos_type where parent_id = #{parentId}
    </select>
</mapper>
