<?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.hotent.project.dao.CostProjectMemoDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.project.model.CostProjectMemo">
        <id column="id" property="id" />
    <result column="project_id" property="projectId" />
    <result column="title" property="title" />
    <result column="content" property="content" />
    <result column="memo_date" property="memoDate" />
    <result column="time_period" property="timePeriod" />
    <result column="reminder_type" property="reminderType" />
    <result column="attachment_url" property="attachmentUrl" />
    <result column="attachment_name" property="attachmentName" />
    <result column="attachment_size" property="attachmentSize" />
    <result column="area_code" property="areaCode" />
    <result column="area_level" property="areaLevel" />
    <result column="create_by" property="createBy" />
    <result column="create_time" property="createTime" />
		<result column="update_by" property="updateBy" />
    <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, project_id, title, content, memo_date, time_period, reminder_type, attachment_url, attachment_name, attachment_size, area_code, area_level, create_by, create_time, update_time,update_by
    </sql>
	<update id="updateMemo">
		update cost_project_memo
		<trim prefix="set" suffixOverrides=",">
			<if test="projectId!= null">
				project_id = #{projectId},
			</if>
			<if test="title != null">
				title = #{title},
			</if>
			<if test="content != null">
				content = #{content},
			</if>
			<if test="memoDate != null">
				memo_date = #{memoDate},
			</if>
			<if test="timePeriod != null">
				time_period = #{timePeriod},
			</if>
			<if test="reminderType != null">
				reminder_type = #{reminderType},
			</if>
			<if test="attachmentUrl != null">
				attachment_url = #{attachmentUrl},
			</if>
		where id = #{id}
		</trim>
	</update>

	<select id="selectPage" resultMap="BaseResultMap">
		select
			<include refid="Base_Column_List"/>
		from
			cost_project_memo
		${ew.customSqlSegment}
	</select>

	<select id="selectList" resultMap="BaseResultMap">
		select
			<include refid="Base_Column_List"/>
		from
			cost_project_memo
		${ew.customSqlSegment}
	</select>

	<select id="selectById" resultMap="BaseResultMap">
		select
			<include refid="Base_Column_List"/>
		from
			cost_project_memo
		where
		    id = ${id}
	</select>


	<select id="getCostProjectMemoPage" resultMap="BaseResultMap">
        SELECT
        cpm.id,
        cpm.project_id,
        cpa.project_name,
        cpm.memo_date,
        cpm.title,
        cpm.content,
        cpm.reminder_type
        from cost_project_memo cpm
        left join cost_project_approval cpa ON cpm.project_id = cpa.project_id
        <where>
            <if test=" projectId != null and projectId != '' ">
                AND cpm.project_id=#{projectId}
            </if>

            <if test=" content != null and content != '' ">
                AND cpm.content LIKE CONCAT('%', #{content}, '%')
            </if>

            <if test=" year != null and year != '' ">
                AND date_format(cpm.memo_date, '%Y') = #{year}
            </if>
            <if test=" memoDate != null and memoDate != '' ">
                AND DATE_FORMAT(cpm.memo_date, '%Y-%m-%d') = #{memoDate}
            </if>

            <if test="startTime != null and endTime != null
                    and startTime != '' and endTime != ''">
                AND #{param.startTime} &lt;= DATE_FORMAT(cpm.create_time, '%Y-%m-%d')
                AND #{param.endTime} &gt;= DATE_FORMAT(cpm.create_time, '%Y-%m-%d')
            </if>

            <if test=" dataScope != null and  dataScope != ''">
                <if test=" dataScope ==1">
                    and cpa.area_code like concat ('%',substring(#{ cityCode},1,4), '%')
                </if>
                <if test=" dataScope ==2">
                    and cpa.area_code=#{ countyCode}
                </if>
            </if>
        </where>

        ORDER BY cpm.create_time DESC
    </select>
	<select id="getIndexList" resultType="com.hotent.project.model.CostProjectMemo">
        SELECT
        cpm.id,
        cpm.project_id,
        cpa.project_name,
        cpm.memo_date,
        cpm.title,
        cpm.content,
        cpm.reminder_type
        FROM
        cost_project_memo cpm left join
        cost_project_approval cpa
        on
        cpm.project_id = cpa.project_id
        <where>
			<if test="userId != null and userId != ''">
				and (cpa.leader_id = #{userId} OR FIND_IN_SET(#{userId}, cpa.audit_group) > 0)
			</if>
			<if test=" content != null and content != '' ">
				AND cpm.content LIKE CONCAT('%', #{content}, '%')
			</if>
			<if test=" memoDate != null and memoDate != '' ">
				AND DATE_FORMAT(cpm.memo_date, '%Y-%m-%d') = #{memoDate}
			</if>
		</where>
    </select>
    <select id="getDataMonth" resultType="com.hotent.project.model.CostProjectMemo">
        SELECT
        cpm.id,
        cpm.project_id,
        cpa.project_name,
        cpm.memo_date,
        cpm.title,
        cpm.content,
        cpm.reminder_type,
        cpm.time_period,
        cpm.task
        from cost_project_memo cpm
        left join cost_project_approval cpa ON cpm.project_id = cpa.project_id
        <where>

            <if test=" memoDate != null and memoDate != '' ">
                AND DATE_FORMAT(cpm.memo_date, '%Y-%m') = DATE_FORMAT(#{memoDate}, '%Y-%m')
            </if>

            <if test=" dataScope != null and  dataScope != ''">
                <if test=" dataScope ==1">
                    and cpa.area_code like concat ('%',substring(#{ cityCode},1,4), '%')
                </if>
                <if test=" dataScope ==2">
                    and cpa.area_code=#{ countyCode}
                </if>
            </if>
        </where>
        ORDER BY cpm.create_time DESC
    </select>
    <select id="getIndexDataMonth" resultType="com.hotent.project.model.CostProjectMemo">
        SELECT
        cpm.id,
        cpm.project_id,
        cpa.project_name,
        cpm.memo_date,
        cpm.title,
        cpm.content,
        cpm.reminder_type,
        cpm.time_period,
        cpm.task
        FROM
        cost_project_memo cpm left join
        cost_project_approval cpa
        on
        cpm.project_id = cpa.project_id
        <where>
            <if test="userId != null and userId != ''">
                and (cpa.leader_id = #{userId} OR FIND_IN_SET(#{userId}, cpa.audit_group) > 0)
            </if>
            <if test=" memoDate != null and memoDate != '' ">
                AND DATE_FORMAT(cpm.memo_date, '%Y-%m') = DATE_FORMAT(#{memoDate}, '%Y-%m')
            </if>
        </where>
    </select>

</mapper>
