<?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.dayou.mapper.ItemStageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemStage">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="item_id" property="itemId" />
        <result column="header_id" property="headerId" />
        <result column="footer_id" property="footerId" />
        <result column="name" property="name" />
        <result column="target" property="target" />
        <result column="start_date" property="startDate" />
        <result column="end_date" property="endDate" />
        <result column="dutyer_id" property="dutyerId" />
        <result column="remark" property="remark" />
        <result column="weight" property="weight" />
        <result column="coefficient" property="coefficient" />
        <result column="overdue" property="overdue" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        s.id,
	s.created,
	s.item_id,
	s.header_id,
	s.footer_id,
	s.NAME,
	s.target,
	s.start_date,
	s.end_date,
	s.dutyer_id,
	s.remark,
    s.weight,
    s.coefficient,
    s.overdue,
    </sql>

    <select id="listByItemId" parameterType="java.lang.Long" resultType="com.dayou.vo.ItemStageVO">
        SELECT
        <include refid="Base_Column_List" />
        (case when (curdate() &lt; start_date)=1 then '未开始'
        when (curdate() &gt;= start_date and curdate() &lt;= end_date)=1 then '进行中'
        when (curdate() &gt; end_date)=1 then '已完成'
        else '' end) as state,
        (select name from user where deleted = 0 and id = s.dutyer_id) AS dutyer,
        (select name from item where deleted =0 and id =s.item_id) AS itemName
        FROM
        item_stage s
        WHERE
        s.item_id = #{itemId}
    </select>

    <select id="checkDate" resultType="java.lang.Integer">
        select count(*) from item_stage where item_id = #{itemId} and #{startDate} &lt;= end_date and #{endDate} &gt;= start_date
        <if test="id!=null">
            and id != #{id}
        </if>
    </select>

</mapper>
