<?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.app.dao.butler.AppHousekeepingServiceDao">

    <insert id="submitHousekeeping" parameterType="com.api.model.app.AppHousekeepingService">
        insert into sys_housekeeping_service(
            estate_id,
            type,
            content,
            status,
            proposer,
            apply_time,
            create_id,
            create_date
        )values (
            #{estateId},
            #{type},
            #{content},
            #{status},
            #{proposer},
            #{applyTime},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>
    <insert id="insertHousekeepingProcessRecord" parameterType="com.api.model.app.AppHousekeepingServiceProcessRecord">
        insert into sys_housekeeping_service_process_record(
            housekeeping_service_id,
            operation_date,
            operation_type,
            operator,
            operator_type,
            operator_content
        )values (
            #{housekeepingServiceId},
            #{operationDate},
            #{operationType},
            #{operator},
            #{operatorType},
            #{operatorContent}
        )
    </insert>
    <update id="cancel" parameterType="com.api.model.app.UserIdAndHousekeepingServiceId">
        update sys_housekeeping_service
        set status = 9
        where id = #{housekeepingServiceId} and proposer = #{id}
    </update>
    <select id="list" parameterType="com.api.model.app.SearchAppHousekeepingService" resultType="com.api.vo.app.AppHousekeepingServiceVo">
        select shs.*,CONCAT(cb.no,'-',cbu.no,'-',cbue.room_number) as room_name,ur.name as proposer_name,ur.tel as proposer_tel,
        su.actual_name as handler_name,su.tel as handler_tel
        from cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,user_resident as ur,sys_housekeeping_service as shs
        left join sys_user as su on shs.handler = su.id
        <where>
            shs.estate_id = cbue.id
            and cbue.building_unit_id = cbu.id
            and cbu.building_id = cb.id
            and shs.proposer = ur.id
            and shs.proposer = #{id}
            <if test="housekeepingStatus != null">
                and shs.status = #{housekeepingStatus}
            </if>
        </where>
    </select>
    <select id="findHousekeepingProcessRecord" parameterType="java.lang.Integer"
            resultType="com.api.model.app.AppHousekeepingServiceProcessRecord">
        select * from sys_housekeeping_service_process_record
        where housekeeping_service_id = #{housekeepingServiceId}
    </select>
    <select id="findHousekeepingServiceById" parameterType="java.lang.Integer" resultType="com.api.model.app.AppHousekeepingService">
        select * from sys_housekeeping_service where id = #{housekeepingServiceId}
    </select>

    <update id="evaluation" parameterType="com.api.model.app.AppHousekeepingService">
        update sys_housekeeping_service
        set status = #{status},
        evaluation = #{evaluation},
        evaluation_content = #{evaluationContent},
        evaluation_time = #{evaluationTime}
        where id = #{id}
    </update>

    <select id="findUserInfoById" parameterType="java.lang.Integer" resultType="com.api.model.businessManagement.SysUser">
        select * from sys_user where id = #{handler}
    </select>

    <update id="orderReceiving" parameterType="com.api.model.app.AppHousekeepingService">
        update sys_housekeeping_service
        set status = #{status}
        where id = #{id}
    </update>

    <update id="submitReport" parameterType="com.api.model.app.AppHousekeepingService">
        update sys_housekeeping_service
        set status = #{status},
        completion = #{completion},
        process_description = #{processDescription},
        material_fee = #{materialFee},
        service_fee = #{serviceFee},
        pay_fee = #{payFee},
        handling_time = #{handlingTime}
        where id = #{id}
    </update>

    <update id="updateStatusById" parameterType="com.api.model.app.AppHousekeepingService">
        update sys_housekeeping_service
        set status = #{status}
        where id = #{id}
    </update>


</mapper>