package com.zioinfo.kintex.system.menu;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/** 메뉴 매퍼(TB_MENU 이식). */
@Mapper
public interface MenuMapper {

    @Select("SELECT id, parent_id AS \"parentId\", menu_name AS \"menuName\", menu_path AS \"menuPath\", icon, "
            + "prg_type AS \"prgType\", required_role AS \"requiredRole\", sort_order AS \"sortOrder\", use_yn AS \"useYn\" "
            + "FROM sys_menu ORDER BY sort_order, id")
    List<Map<String, Object>> findAll();

    @Insert("INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, icon, prg_type, required_role, "
            + "sort_order, use_yn) VALUES (#{id}, #{parentId}, #{menuName}, #{menuPath}, #{icon}, "
            + "COALESCE(#{prgType},'FORM'), #{requiredRole}, COALESCE(#{sortOrder},0), COALESCE(#{useYn},'Y')) "
            + "ON CONFLICT (id) DO UPDATE SET parent_id=EXCLUDED.parent_id, menu_name=EXCLUDED.menu_name, "
            + "menu_path=EXCLUDED.menu_path, icon=EXCLUDED.icon, prg_type=EXCLUDED.prg_type, "
            + "required_role=EXCLUDED.required_role, sort_order=EXCLUDED.sort_order, use_yn=EXCLUDED.use_yn")
    int upsert(Map<String, Object> p);

    @Delete("DELETE FROM sys_menu WHERE id = #{id}")
    int delete(@Param("id") String id);
}
