<?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.UserArticleOutDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchUserArticleOut" resultType="com.api.vo.butlerService.VoUserArticleOut">
        select concat(cb.no,"-",cbu.no,"-",cbue.room_number) as room_name,cbue.status as estate_status,uao.id,uao.name,uao.weight,ur2.name as applicant_name,ur2.tel as applicant_tel
        ,uao.approach,uao.expected_time,uao.actual_time,uao.export,uao.status,uao.remarks,group_concat(ur1.name) as resident_name,group_concat(ur1.tel) as resident_tel
        from user_article_out as uao,cpm_building_unit_estate as cbue,user_resident as ur2,cpm_building_unit as cbu,cpm_building as cb,cpm_resident_estate as cre,user_resident as ur1
            <where>
            uao.building_unit_estate_id = cbue.id
            and uao.applicant_id = ur2.id
            and cbue.building_unit_id = cbu.id
            and cbu.building_id = cb.id
            and cre.building_unit_estate_id = cbue.id
            and ur1.id = cre.resident_id
            <if test="roomNumber != null and roomNumber != ''">
                and cbue.room_number like concat('%',#{roomNumber},'%')
            </if>
            <if test="unitNo != null">
                and cbu.no like concat('%',#{unitNo},'%')
            </if>
            <if test="buildingNo != null">
                and cb.no like concat('%',#{buildingNo},'%')
            </if>
            <if test="export != null">
                and uao.export = #{export}
            </if>
            <if test="applicantName != null and applicantName != ''">
                and ur2.name like concat('%',#{applicantName},'%')
            </if>
            <if test="applicantTel != null and applicantTel != ''">
                and ur2.tel like concat('%',#{applicantTel},'%')
            </if>
            <if test="actualTimeStart != null">
                and uao.actual_time <![CDATA[ > ]]> #{actualTimeStart}
            </if>
            <if test="actualTimeEnd != null">
                and uao.actual_time <![CDATA[ < ]]> #{actualTimeEnd}
            </if>
            <if test="names != null and names != ''">
                and uao.name in
                <foreach collection="names" index="index" item="name"
                         open="(" separator="," close=")">
                    #{name}
                </foreach>
            </if>
            <if test="status != null">
                and uao.status = #{status}
            </if>
        </where>
        group by uao.id
        <if test="expectedTimeSort == 1">
            order by uao.expected_time DESC
        </if>
        <if test="expectedTimeSort == 2">
            order by uao.expected_time
        </if>
        <if test="actualTimeSort == 1">
            order by uao.actual_time DESC
        </if>
        <if test="actualTimeSort == 2">
            order by uao.actual_time
        </if>
    </select>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from user_article_out where id = #{id}
    </delete>

    <update id="applicationRejection" parameterType="com.api.model.butlerService.UserArticleOut">
        update user_article_out
        set status = #{status},review_id = #{reviewId},review_date = #{reviewDate}
        where id = #{id}
    </update>
    <select id="countArticleOutNow" resultType="java.lang.Integer">
        select count(*) from user_article_out where to_days(expected_time) = to_days(now())
    </select>

    <select id="countPerformed" resultType="java.lang.Integer">
        select count(*) from user_article_out where status = 1
    </select>
</mapper>