<?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.microservices.zone.project.mapper.ForgeProjectStatisticsMapper">

    <resultMap type="ForgeProjectStatistics" id="ForgeProjectStatisticsResult">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="date" column="date"/>
        <result property="score" column="score"/>
        <result property="visits" column="visits"/>
        <result property="watchers" column="watchers"/>
        <result property="praises" column="praises"/>
        <result property="forks" column="forks"/>
        <result property="issues" column="issues"/>
        <result property="pullrequests" column="pullrequests"/>
        <result property="commits" column="commits"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
        <result property="closedIssues" column="closed_issues"/>
    </resultMap>

    <sql id="selectForgeProjectStatisticsVo">
        select id,
               project_id,
               date,
               score,
               visits,
               watchers,
               praises,
               forks,
               issues,
               pullrequests,
               commits,
               created_at,
               updated_at,
               closed_issues
        from daily_project_statistics
    </sql>

    <select id="selectForgeProjectStatisticsList" parameterType="ForgeProjectStatistics"
            resultMap="ForgeProjectStatisticsResult">
        <include refid="selectForgeProjectStatisticsVo"/>
        <where>
            <if test="projectId != null ">and project_id = #{projectId}</if>
            <if test="date != null ">and date = #{date}</if>
            <if test="score != null ">and score = #{score}</if>
            <if test="visits != null ">and visits = #{visits}</if>
            <if test="watchers != null ">and watchers = #{watchers}</if>
            <if test="praises != null ">and praises = #{praises}</if>
            <if test="forks != null ">and forks = #{forks}</if>
            <if test="issues != null ">and issues = #{issues}</if>
            <if test="pullrequests != null ">and pullrequests = #{pullrequests}</if>
            <if test="commits != null ">and commits = #{commits}</if>
            <if test="createdAt != null ">and created_at = #{createdAt}</if>
            <if test="updatedAt != null ">and updated_at = #{updatedAt}</if>
            <if test="closedIssues != null ">and closed_issues = #{closedIssues}</if>
        </where>
    </select>

    <select id="selectForgeProjectStatisticsById" parameterType="Long" resultMap="ForgeProjectStatisticsResult">
        <include refid="selectForgeProjectStatisticsVo"/>
        where id = #{id}
    </select>
    <select id="selectAllCommitCountByProjectId" resultType="java.lang.Long">
        select sum(commits)
        from daily_project_statistics
        where project_id = #{projectId};
    </select>
    <select id="selectLastWeekScoreByProjectId" resultType="java.lang.Double">
        select SUM(score)
        from daily_project_statistics
        where project_id = #{projectId}
          and date &gt;= #{lastMonday}
          and date &lt;= #{lastSunday};
    </select>

    <insert id="insertForgeProjectStatistics" parameterType="ForgeProjectStatistics" useGeneratedKeys="true"
            keyProperty="id">
        insert into daily_project_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="date != null">date,</if>
            <if test="score != null">score,</if>
            <if test="visits != null">visits,</if>
            <if test="watchers != null">watchers,</if>
            <if test="praises != null">praises,</if>
            <if test="forks != null">forks,</if>
            <if test="issues != null">issues,</if>
            <if test="pullrequests != null">pullrequests,</if>
            <if test="commits != null">commits,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
            <if test="closedIssues != null">closed_issues,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="date != null">#{date},</if>
            <if test="score != null">#{score},</if>
            <if test="visits != null">#{visits},</if>
            <if test="watchers != null">#{watchers},</if>
            <if test="praises != null">#{praises},</if>
            <if test="forks != null">#{forks},</if>
            <if test="issues != null">#{issues},</if>
            <if test="pullrequests != null">#{pullrequests},</if>
            <if test="commits != null">#{commits},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
            <if test="closedIssues != null">#{closedIssues},</if>
        </trim>
    </insert>

    <update id="updateForgeProjectStatistics" parameterType="ForgeProjectStatistics">
        update daily_project_statistics
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="date != null">date = #{date},</if>
            <if test="score != null">score = #{score},</if>
            <if test="visits != null">visits = #{visits},</if>
            <if test="watchers != null">watchers = #{watchers},</if>
            <if test="praises != null">praises = #{praises},</if>
            <if test="forks != null">forks = #{forks},</if>
            <if test="issues != null">issues = #{issues},</if>
            <if test="pullrequests != null">pullrequests = #{pullrequests},</if>
            <if test="commits != null">commits = #{commits},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="closedIssues != null">closed_issues = #{closedIssues},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteForgeProjectStatisticsById" parameterType="Long">
        delete
        from daily_project_statistics
        where id = #{id}
    </delete>

    <delete id="deleteForgeProjectStatisticsByIds" parameterType="String">
        delete from daily_project_statistics where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>