<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mes.spc.mapper.SpcMapper">

  <resultMap id="spcMap" type="com.zioinfo.mes.spc.MesSpcSample">
    <id property="id" column="id"/>
    <result property="chartCode" column="chart_code"/>
    <result property="itemCode" column="item_code"/>
    <result property="characteristic" column="characteristic"/>
    <result property="subgroupNo" column="subgroup_no"/>
    <result property="sampleValues" column="sample_values"/>
    <result property="sampleMean" column="sample_mean"/>
    <result property="sampleRange" column="sample_range"/>
    <result property="measuredAt" column="measured_at"/>
    <result property="createdBy" column="created_by"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findByChart" resultMap="spcMap">
    SELECT * FROM mes_spc_sample WHERE chart_code = #{chartCode}
    ORDER BY subgroup_no ASC, id ASC LIMIT #{limit}
  </select>

  <select id="distinctCharts" resultType="string">
    SELECT DISTINCT chart_code FROM mes_spc_sample ORDER BY chart_code
  </select>

  <insert id="insert" parameterType="com.zioinfo.mes.spc.MesSpcSample" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mes_spc_sample (chart_code, item_code, characteristic, subgroup_no, sample_values,
      sample_mean, sample_range, measured_at, created_by)
    VALUES (#{chartCode}, #{itemCode}, #{characteristic}, #{subgroupNo}, #{sampleValues}::jsonb,
      #{sampleMean}, #{sampleRange}, NOW(), #{createdBy})
  </insert>

</mapper>
