<!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.SysFacilitiesManageDao">

    <select id="list" parameterType="com.api.model.butlerService.SearchFacilitiesManage" resultType="com.api.vo.butlerService.VoFacilitiesManage">
        select sfm.*,sfc.name as facilities_category_name,su.actual_name as create_name
        from sys_facilities_manage as sfm,sys_facilities_category as sfc,sys_user as su
        <where>
            sfm.facilities_category_id = sfc.id
            and sfm.create_id = su.id
            and sfm.is_delete = 1
            <if test="name != null and name != ''">
                and sfm.name like concat('%',#{name},'%')
            </if>
            <if test="facilitiesCategoryId != null">
                and sfm.facilities_category_id = #{facilitiesCategoryId}
            </if>
            <if test="code != null and code != ''">
                and sfm.code like concat('%',#{code},'%')
            </if>
            <if test="status != null">
                and sfm.status = #{status}
            </if>
            <if test="createStartDate != null">
                and sfm.create_date <![CDATA[ >= ]]> #{createStartDate}
            </if>
            <if test="createEndDate != null">
                and sfm.create_date <![CDATA[ <= ]]> #{createEndDate}
            </if>
            <if test="type != null">
                and sfc.type = #{type}
            </if>
        </where>
        order by sfm.create_date desc
    </select>

    <insert id="insert" parameterType="com.api.model.butlerService.FacilitiesManage">
        insert into sys_facilities_manage(
            code,
            name,
            address,
            facilities_category_id,
            status,
            <if test="brand != null and brand != ''">
                brand,
            </if>
            <if test="procurementCosts != null">
                procurement_costs,
            </if>
            <if test="purchasingManufacturer != null and purchasingManufacturer != ''">
                purchasing_manufacturer,
            </if>
            <if test="manufacturerPhone != null and manufacturerPhone != ''">
                manufacturer_phone,
            </if>
            <if test="warrantyPeriodYears != null">
                warranty_period_years,
            </if>
            <if test="warrantyPeriodMonths != null">
                warranty_period_months,
            </if>
            <if test="validityStart != null">
                validity_start,
            </if>
            <if test="validityEnd != null">
                validity_end,
            </if>
            <if test="fileDocUrl != null and fileDocUrl != ''">
                file_doc_url,
            </if>
            <if test="fileDocName != null and fileDocName != ''">
                file_doc_name,
            </if>
            <if test="remakes != null and remakes != ''">
                remakes,
            </if>
            create_id,
            create_date,
            is_delete
        )values (
            #{code},
            #{name},
            #{address},
            #{facilitiesCategoryId},
            #{status},
            <if test="brand != null and brand != ''">
                #{brand},
            </if>
            <if test="procurementCosts != null">
                #{procurementCosts},
            </if>
            <if test="purchasingManufacturer != null and purchasingManufacturer != ''">
                #{purchasingManufacturer},
            </if>
            <if test="manufacturerPhone != null and manufacturerPhone != ''">
                #{manufacturerPhone},
            </if>
            <if test="warrantyPeriodYears != null">
                #{warrantyPeriodYears},
            </if>
            <if test="warrantyPeriodMonths != null">
                #{warrantyPeriodMonths},
            </if>
            <if test="validityStart != null">
                #{validityStart},
            </if>
            <if test="validityEnd != null">
                #{validityEnd},
            </if>
            <if test="fileDocUrl != null and fileDocUrl != ''">
                #{fileDocUrl},
            </if>
            <if test="fileDocName != null and fileDocName != ''">
                #{fileDocName},
            </if>
            <if test="remakes != null and remakes != ''">
                #{remakes},
            </if>
            #{createId},
            #{createDate},
            #{isDelete}
        )
    </insert>

    <update id="update" parameterType="com.api.model.butlerService.FacilitiesManage">
        update sys_facilities_manage
        set code = #{code},
        name = #{name},
        address = #{address},
        facilities_category_id = #{facilitiesCategoryId},
        <if test="brand != null and brand != ''">
            brand = #{brand},
        </if>
        <if test="procurementCosts != null">
            procurement_costs = #{procurementCosts},
        </if>
        <if test="purchasingManufacturer != null and purchasingManufacturer != ''">
            purchasing_manufacturer = #{purchasingManufacturer},
        </if>
        <if test="manufacturerPhone != null and manufacturerPhone != ''">
            manufacturer_phone = #{manufacturerPhone},
        </if>
        <if test="warrantyPeriodYears != null">
            warranty_period_years = #{warrantyPeriodYears},
        </if>
        <if test="warrantyPeriodMonths != null">
            warranty_period_months = #{warrantyPeriodMonths},
        </if>
        <if test="validityStart != null">
            validity_start = #{validityStart},
        </if>
        <if test="validityEnd != null">
            validity_end = #{validityEnd},
        </if>
        <if test="fileDocUrl != null and fileDocUrl != ''">
            file_doc_url = #{fileDocUrl},
        </if>
        <if test="fileDocName != null and fileDocName != ''">
            file_doc_name = #{fileDocName},
        </if>
        <if test="remakes != null and remakes != ''">
            remakes = #{remakes},
        </if>
        modify_id = #{modifyId},
        modify_date = #{modifyDate}
        where id = #{id}
    </update>

    <select id="findDetailById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFacilitiesManageDetail">
        select sfm.*,sfc.name as facilities_category_name,su.actual_name as create_name,su.tel
        from sys_facilities_manage as sfm,sys_facilities_category as sfc,sys_user as su
        where sfm.facilities_category_id = sfc.id and sfm.create_id = su.id and sfm.id = #{id} and sfm.is_delete = 1
    </select>
    <select id="findSituationById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFacilitiesManageSituation">
        select id,stop_date,stop_reason from sys_facilities_manage_situation where facilities_manage_id = #{id} order By create_date DESC
    </select>

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

    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_facilities_manage where id = #{id}
    </select>

    <update id="incCategory" parameterType="java.lang.Integer">
        update sys_facilities_category
        set num = num + 1
        where id = #{facilitiesCategoryId}
    </update>
    <update id="decCategoryNum" parameterType="java.lang.Integer">
        update sys_facilities_category
        set num = num - 1
        where id = #{facilitiesCategoryId}
    </update>

</mapper>