<?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.api.manage.dao.butlerService.SysFacilitiesCategoryDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchFacilitiesCategory" resultType="com.api.vo.butlerService.VoFacilitiesCategory">
        select sfc.id,sfc.code,sfc.name,sfc.type,su.actual_name as create_name,sfc.num,sfc.create_date
        from sys_facilities_category as sfc,sys_user as su
        <where>
            sfc.create_id = su.id
            and sfc.is_delete = 1
            <if test="name != null and name != ''">
                and sfc.name like concat('%',#{name},'%')
            </if>
            <if test="type != null">
                and sfc.type = #{type}
            </if>
            <if test="code != null and code != ''">
                and sfc.code like concat('%',#{code},'%')
            </if>
            <if test="createStartDate != null">
                and sfc.create_date <![CDATA[ >= ]]> #{createStartDate}
            </if>
            <if test="createEndDate != null">
                and sfc.create_date <![CDATA[ <= ]]> #{createEndDate}
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="com.api.model.butlerService.FacilitiesCategory">
        insert into sys_facilities_category(
            code,
            name,
            type,
            num,
            open_start_date,
            open_end_date,
            create_id,
            create_date,
            is_delete
        )values (
            #{code},
            #{name},
            #{type},
            #{num},
            #{openStartDate},
            #{openEndDate},
            #{createId},
            #{createDate},
            #{isDelete}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findDetailById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFacilitiesCategoryDetail">
        select sfc.id,sfc.code,sfc.name,sfc.type,su.actual_name as create_name,sfc.num,sfc.create_date,su.tel,sfc.open_start_date,sfc.open_end_date
        from sys_facilities_category as sfc,sys_user as su
        where sfc.create_id = su.id and sfc.id = #{id} and sfc.is_delete = 1
    </select>

    <update id="update" parameterType="com.api.model.butlerService.FacilitiesCategory">
        update sys_facilities_category
        <set>
            <if test="code != null and code != ''">
                code = #{code},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="openStartDate != null">
                open_start_date = #{openStartDate},
            </if>
            <if test="openEndDate != null">
                open_end_date = #{openEndDate},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <update id="delete" parameterType="java.lang.Integer">
        update sys_facilities_category
        set is_delete = 0
        where id = #{id}
    </update>

    <select id="countFacilitiesByCategoryId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_facilities_manage where facilities_category_id = #{id} and is_delete = 1
    </select>

</mapper>