<?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.xintong.visualinspection.dao.master.CheckDeptStatisticsDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.DeptStatistic" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="period_id" property="period_id" jdbcType="INTEGER" />
        <result column="score" property="score" jdbcType="DOUBLE" />
        <result column="integral" property="integral" jdbcType="INTEGER" />
        <result column="dept_id" property="dept_id" jdbcType="INTEGER" />

    </resultMap>


    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_dept_score
        WHERE id = #{id}
    </select>
    
    <select id="getByDept" parameterType="com.xintong.visualinspection.bean.DeptStatistic" resultMap="BaseResultMap" >
        SELECT *
        FROM check_dept_score
        WHERE dept_id = #{dept_id} and period_id = #{period_id}
    </select>
    
    <insert id="insert" useGeneratedKeys="true" keyProperty="id"  parameterType="com.xintong.visualinspection.bean.DeptStatistic" >
        <include refid="insertc"/>
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.DeptStatistic" >
        <include refid="updatec"/>
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        check_dept_score
        WHERE
        id =#{id}
    </delete>

    <insert id="saveandupdate" useGeneratedKeys="true" keyProperty="id" parameterType="com.xintong.visualinspection.bean.DeptStatistic" >
        <selectKey keyProperty="count" resultType="int" order="BEFORE">
            select count(*) as count from check_dept_score where dept_id = #{dept_id} and period_id = #{period_id}
        </selectKey>
        <if test="count>0">
           <include refid="updatec"/>
        </if>
        <if test="count==0">
            <include refid="insertc"/>
        </if>
    </insert>


    <sql id="updatec">
        UPDATE
        check_dept_score
        SET
        <if test="score != null">score = #{score},</if>
        <if test="integral != null">integral = #{integral},</if>
        id = id
        WHERE
        dept_id = #{dept_id} and period_id = #{period_id}
    </sql>
    <sql id="insertc">
         INSERT INTO
        check_dept_score
        (dept_id,period_id,score,integral)
        VALUES
        (#{dept_id}, #{period_id},#{score},#{integral})
    </sql>


    <select id="selectdepts"   parameterType="com.xintong.visualinspection.bean.DeptStatistic" resultMap="BaseResultMap">
      select * from  check_dept_score where     period_id >= #{startperiod_id}
        AND period_id &lt;= #{endperiod_id}
    </select>




</mapper>