<?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.njcn.gather.tool.addledger.mapper.AddLedgerLedgerMapper">

    <select id="selectTree" resultType="com.njcn.gather.tool.addledger.pojo.po.AddLedgerLedgerPO">
        SELECT Id AS id,
               Pid AS pid,
               Pids AS pids,
               Name AS name,
               Level AS level,
               Sort AS sort,
               Remark AS remark,
               State AS state
        FROM cs_ledger
        WHERE State = 1
        <if test="keyword != null and keyword != ''">
          AND Name LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY Level ASC, Sort ASC, Name ASC
    </select>

    <select id="selectActiveNode" resultType="com.njcn.gather.tool.addledger.pojo.po.AddLedgerLedgerPO">
        SELECT Id AS id,
               Pid AS pid,
               Pids AS pids,
               Name AS name,
               Level AS level,
               Sort AS sort,
               Remark AS remark,
               State AS state
        FROM cs_ledger
        WHERE Id = #{id}
          AND Level = #{level}
          AND State = 1
        LIMIT 1
    </select>

    <select id="selectNodeById" resultType="com.njcn.gather.tool.addledger.pojo.po.AddLedgerLedgerPO">
        SELECT Id AS id,
               Pid AS pid,
               Pids AS pids,
               Name AS name,
               Level AS level,
               Sort AS sort,
               Remark AS remark,
               State AS state
        FROM cs_ledger
        WHERE Id = #{id}
        LIMIT 1
    </select>

    <select id="selectActiveSubtree" resultType="com.njcn.gather.tool.addledger.pojo.po.AddLedgerLedgerPO">
        SELECT Id AS id,
               Pid AS pid,
               Pids AS pids,
               Name AS name,
               Level AS level,
               Sort AS sort,
               Remark AS remark,
               State AS state
        FROM cs_ledger
        WHERE State = 1
          AND (Id = #{id} OR FIND_IN_SET(#{id}, Pids) &gt; 0)
        ORDER BY Level DESC
    </select>

    <update id="softDeleteByIds">
        UPDATE cs_ledger
        SET State = 0,
            Update_By = #{updateBy},
            Update_Time = NOW()
        WHERE Id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
          AND State = 1
    </update>
</mapper>
