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

    <resultMap type="com.vctgo.system.api.domain.SysService" id="SysServiceResult">
        <result property="id"    column="id"    />
        <result property="serviceName"    column="service_name"    />
        <result property="menuIds"    column="menu_ids"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="createById" column="create_by_id"/>
        <result property="updateById" column="update_by_id"/>
        <result property="tenantId"     column="tenant_id"      />
    </resultMap>

    <sql id="selectSysServiceVo">
        select id, service_name, menu_ids, is_deleted, create_by, create_time, update_by, update_time, remark,create_by_id, update_by_id,tenant_id from sys_service
    </sql>

    <sql id="defaultDeleteCondition">
        is_deleted = 0
    </sql>

    <select id="selectSysServiceList"   resultMap="SysServiceResult">
        SELECT
            s.id,
            s.service_name,
            s.menu_ids,
            s.is_deleted,
            s.create_by,
            s.create_time,
            s.update_by,
            s.update_time,
            s.remark,
            s.create_by_id,
            s.update_by_id,
            s.tenant_id,
            pu.nick_name createNickName
        FROM
        sys_service s
        LEFT JOIN sys_platform_user pu ON s.create_by = pu.user_name
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.serviceName != null  and query.serviceName != ''"> and s.service_name like concat('%', #{query.serviceName}, '%')</if>
            <if test="query.menuIds != null  and query.menuIds != ''"> and s.menu_ids = #{query.menuIds}</if>
            <if test="query.isDeleted != null  and query.isDeleted != ''"> and s.is_deleted = #{query.isDeleted}</if>
        </where>
    </select>

    <update id="logicalDeleteSysServiceByIds" parameterType="String">
        update sys_service set is_deleted = #{query.isDeleted},
        update_by_id = #{query.updateById}, update_by = #{query.updateBy},
        update_time = #{query.updateTime} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

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

    <select id="selectServiceListByTenantId" resultType="com.vctgo.system.api.domain.SysService">
        SELECT
            ss.service_name,
            ss.id
        FROM
            sys_re_service_tenant srst
                INNER JOIN sys_service ss ON srst.service_id = ss.id
        WHERE
            srst.tenant_id = #{tenantId} and ss.is_deleted ='0' and srst.is_deleted='0'
    </select>

</mapper>
