<?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.butlerApp.dao.jurisdiction.ButlerDecorationDao">
    <select id="list" parameterType="com.api.model.butlerApp.ButlerDecorationSearch" resultType="com.api.vo.butlerApp.ButlerDecorationVo">
        select ud.*,concat(cb.no,'幢-',cbu.no,'单元-',cbue.room_number) as room_name,ud.application_date
        from user_decoration as ud,cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb
        <where>
            ud.building_unit_estate_id = cbue.id
            and cbue.building_unit_id = cbu.id
            and cbu.building_id = cb.id
            <if test="operationStatus == 1">
                and ud.tracker is NULL
                and ud.status >1
            </if>
            <if test="operationStatus == 2">
                and ud.tracker is not NULL
                and ud.status <![CDATA[ < ]]> 5
            </if>
            <if test="operationStatus == 3">
                and ud.tracker is not NULL
                and ud.status <![CDATA[ >= ]]> 5
            </if>
            <if test="operationStatus == 4">
                and ud.status >1
            </if>
        </where>
        order by ud.application_date desc
    </select>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerDecorationFBIVo">
        select ud.*,concat(cb.no,'幢-',cbu.no,'单元-',cbue.room_number) as room_name,ur.name as resident_name,ur.tel as resident_tel
        from user_decoration as ud,cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,user_resident as ur
        where ud.building_unit_estate_id = cbue.id and cbue.building_unit_id = cbu.id and cbu.building_id = cb.id and ud.resident_id = ur.id and ud.id = #{decorationId}
    </select>

    <select id="findInspectionById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerTrackInspectionFBIVo">
        select udtic.*,su.actual_name as track_name
        from user_decoration_track_inspection_cycle as udtic,sys_user as su
        where udtic.track_id = su.id and udtic.decoration_id = #{decorationId}
    </select>

    <select id="findChecksContent" resultType="com.api.vo.butlerApp.ButlerChecksContentVo">
        select id,name from user_decoration_checks_content
    </select>

    <select id="findTrackChecksContent" resultType="com.api.vo.butlerApp.ButlerChecksContentVo">
        select id,name from user_decoration_track_checks_content where decoration_id = #{decorationId}
    </select>

    <update id="updateTrackerById" parameterType="com.api.model.butlerApp.ButlerTrackInspectionCycle">
        update user_decoration set tracker = #{trackId}  where id = #{decorationId}
    </update>

    <insert id="insertInspectionCycle" parameterType="com.api.model.butlerApp.ButlerTrackInspectionCycle">
        insert into user_decoration_track_inspection_cycle(
            decoration_id,
            track_id,
            start_date,
            inspection_cycle,
            inspection_date_next,
            create_id,
            create_date
        )values (
            #{decorationId},
            #{trackId},
            #{startDate},
            #{inspectionCycle},
            #{inspectionDateNext},
            #{createId},
            #{createDate}
        )
    </insert>

    <insert id="insertTrackChecksContent" parameterType="com.api.model.butlerApp.ButlerTrackChecksContent">
        insert into user_decoration_track_checks_content(
            decoration_id,
            name
        )values (
            #{decorationId},
            #{name}
        )
    </insert>

    <select id="countInspectionCycle" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from user_decoration_track_inspection_cycle where decoration_id = #{decorationId}
    </select>

    <insert id="insertTrackRecord" parameterType="com.api.model.butlerApp.ButlerTrackRecord">
        insert into user_decoration_track_record(
            decoration_id,
            track_id,
            track_date,
            type,
            description,
            result,
            create_id,
            create_date
        )values (
            #{decorationId},
            #{trackId},
            #{trackDate},
            #{type},
            #{description},
            #{result},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <insert id="insertTrackRecordDetail" parameterType="com.api.model.butlerApp.ButlerTrackRecordDetail">
        insert into user_decoration_track_record_detail(
            decoration_track_record_id,
            decoration_track_checks_content_id,
            is_qualified,
            create_id,
            create_date
        )values (
            #{decorationTrackRecordId},
            #{decorationTrackChecksContentId},
            #{isQualified},
            #{createId},
            #{createDate}
        )
    </insert>

    <update id="updateIsQualified" parameterType="com.api.model.butlerApp.ButlerTrackRecord">
        update user_decoration set is_qualified = #{result} where id = #{decorationId}
    </update>
</mapper>