<?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.project.zcustom.mapper.unit.LargeIssueMapper">

    <resultMap type="com.project.zcustom.domain.addional.LargeIssue" id="LargeIssueResult">
        <result property="id"    column="id"    />
        <result property="description"    column="description"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectLargeIssueVo">
        select id, description, project_id, status, del_flag, create_by, create_time, update_by, update_time from large_issue
    </sql>

    <select id="selectLargeIssueList" parameterType="LargeIssue" resultMap="LargeIssueResult">
        select a.*, b.project_name
        from large_issue a
        left join large_project b
        on a.project_id = b.id
        <where>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="projectId != null "> and a.project_id = #{projectId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="selectLargeIssueById" parameterType="Long" resultMap="LargeIssueResult">
        select a.*, b.project_name
        from large_issue a
        left join large_project b
        on a.project_id = b.id
        where a.id = #{id}
    </select>

    <insert id="insertLargeIssue" parameterType="LargeIssue">
        insert into large_issue
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="description != null">description,</if>
            <if test="projectId != null">project_id,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="description != null">#{description},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateLargeIssue" parameterType="LargeIssue">
        update large_issue
        <trim prefix="SET" suffixOverrides=",">
            <if test="description != null">description = #{description},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLargeIssueById" parameterType="Long">
        delete from large_issue where id = #{id}
    </delete>

    <delete id="deleteLargeIssueByIds" parameterType="String">
        delete from large_issue where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="reallyDeleteByWrapper">
        DELETE FROM large_issue ${ew.customSqlSegment}
    </delete>

    <select id="getIssueNum" resultType="int">
        SELECT
            COUNT(*)
        FROM
            large_issue a
                LEFT JOIN large_project b ON a.project_id = b.id
        WHERE a.del_flag = 0 AND b.del_flag = 0
    </select>

    <select id="getOverIssueNum" resultType="int">
        SELECT
            COUNT(*)
        FROM
            large_issue a
                LEFT JOIN large_project b ON a.project_id = b.id
        WHERE a.del_flag = '0' AND b.del_flag = '0' AND a.`status` = '0'
    </select>

    <select id="getIssueListLastWeek" resultMap="LargeIssueResult">
        SELECT
            description,
            `status`,
            create_time
        FROM
            `large_issue`
        WHERE
            DATE(create_time) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) + 5 ) DAY )
                AND DATE_SUB(
                    CURDATE(),
                    INTERVAL ( DAYOFWEEK( CURDATE()) - 1 ) DAY)
    </select>
</mapper>
