<?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.ButlerGreenDao">

    <select id="list" parameterType="com.api.model.butlerApp.ButlerGreenSearch" resultType="com.api.vo.butlerApp.ButlerGreenVo">
        select T.* from (
            select sgt.id,sga.name as green_area_name,sgt.content,su.actual_name as director_name,
            (case sgt.status = 1 and sgt.end_date <![CDATA[ < ]]> now()  when true then 3 else sgt.status end) as status,
            sgt.end_date,sgt.complete_date,sgt.create_date
            from sys_green_task as sgt,sys_green_area as sga,sys_user as su
            where sgt.green_area_id = sga.id and sgt.director = su.id and sgt.director = #{id}
        ) as T
        <where>
            <if test="greenStatus != null">
                and T.status = #{greenStatus}
            </if>
        </where>
        order by T.create_date desc
    </select>

    <select id="list2" parameterType="com.api.model.butlerApp.ButlerGreenSearch" resultType="com.api.vo.butlerApp.ButlerGreenVo">
        select T.* from (
        select sgt.id,sga.name as green_area_name,sgt.content,su.actual_name as director_name,
        (case sgt.status = 1 and sgt.end_date <![CDATA[ < ]]> now()  when true then 3 else sgt.status end) as status,
        sgt.end_date,sgt.complete_date,sgt.create_date
        from sys_green_task as sgt,sys_green_area as sga,sys_user as su
        where sgt.green_area_id = sga.id and sgt.director = su.id
        ) as T
        <where>
            <if test="greenStatus != null">
                and T.status = #{greenStatus}
            </if>
        </where>
        order by T.create_date desc
    </select>


    <update id="complete" parameterType="com.api.model.operationManagement.SysGreenTask">
        update sys_green_task
        set status = #{status},
        complete_date = #{complete}
        where id = #{id} and director = #{director}
    </update>

    <select id="findTaskById" parameterType="java.lang.Integer" resultType="com.api.model.operationManagement.SysGreenTask">
        select * from sys_green_task where id = #{id}
    </select>

    <update id="updateStatusById" parameterType="com.api.model.butlerApp.ButlerGreenTaskIdAndStatus">
        update sys_green_task
        set status = #{status}
        where id = #{greenTaskId}
    </update>

    <insert id="insertGreenTaskCheckSituation" parameterType="com.api.model.butlerApp.ButlerGreenTaskCheckSituation">
        insert into sys_green_task_check_situation(
            green_task_id,
            completion,
            inspection_report,
            create_date,
            create_id
        )values (
            #{greenTaskId},
            #{completion},
            #{inspectionReport},
            #{createDate},
            #{createId}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

</mapper>