<?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.shoppingCenter.OrderDao">

    <select id="list" parameterType="com.api.model.shoppingCenter.OrderSearch" resultType="com.api.vo.shoppingCenter.OrderVo">
        select sga.id,sg.id as goods_id,sga.code,sg.title as goods_name,sg.selling_price,sg.marking_price,sga.num,ur.name as user_name,
        ur.tel as user_tel,group_concat(concat(cb.no,'-',cbu.no,'-',cbue.room_number)) as room_name,sga.status,sg.arrival_time
        from shop_goods_appointment as sga,shop_goods as sg,user_resident as ur
        LEFT JOIN cpm_resident_estate as cre on cre.resident_id = ur.id
        LEFT JOIN cpm_building_unit_estate as cbue on cre.building_unit_estate_id = cbue.id
        LEFT JOIN cpm_building_unit as cbu on cbue.building_unit_id = cbu.id
        LEFT JOIN cpm_building as cb on cbu.building_id = cb.id
        <where>
            sga.goods_id = sg.id
            and sga.create_id = ur.id
            <if test="code != null and code != ''">
                and sga.code like concat('%',#{code},'%')
            </if>
            <if test="userTel != null and userTel != ''">
                and ur.tel like concat('%',#{userTel},'%')
            </if>
            <if test="status == 1">
                and sga.status = 1
            </if>
            <if test="status == 2">
                and sga.status = 2
            </if>
            <if test="status == 3">
                and sga.status = 3
            </if>
            <if test="status == 4">
                and sga.status = 4
            </if>
            <if test="status == 6">
                and sga.status <![CDATA[ >= ]]> 3
                and score is not null
            </if>
            <if test="status == 8">
                and sga.status <![CDATA[ >= ]]> 8
            </if>
        </where>
        group by sga.id
        order by sga.create_date desc
    </select>
    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from shop_goods_appointment where id = #{id}
    </select>

    <update id="deliverGoods" parameterType="com.api.model.shoppingCenter.Order">
        update shop_goods_appointment
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="sendDate != null">
                send_date = #{sendDate},
            </if>
            <if test="sendDetail != null and sendDetail != ''">
                send_detail = #{sendDetail},
            </if>
            <if test="sendOperator != null">
                send_operator = #{sendOperator},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="arrivalGoods" parameterType="com.api.model.shoppingCenter.Order">
        update shop_goods_appointment
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="arrivalDate != null">
                arrival_date = #{arrivalDate},
            </if>
            <if test="arrivalOperator != null">
                arrival_operator = #{arrivalOperator},
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.model.shoppingCenter.Order">
        select * from shop_goods_appointment where id = #{id}
    </select>

</mapper>