<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.ApparatusMapper">

    <!-- 分页查询机器列表 -->
    <select id="listApparatusPage" parameterType="Apparatus" resultType="Apparatus">
      SELECT
        ma.*,
        pc.color_img productImg,
        pi.product_name productName
      FROM
        tb_iamberry_member_apparatus ma
        LEFT JOIN tb_iamberry_product_color pc on ma.color_id = pc.color_id
        LEFT JOIN tb_iamberry_product_info pi on pc.color_product_id = pi.product_id
      <where>
        <if test="userOpenid != null and userOpenid != ''">
            user_openid = #{userOpenid}
        </if>
        <if test="apparatusStatus != null and apparatusStatus != ''">
            AND apparatus_status = #{apparatusStatus}
        </if>
        <if test="apparatusBarcode != null and apparatusBarcode != ''">
            AND apparatus_barcode = #{apparatusBarcode}
        </if>
      </where>
        ORDER BY apparatus_id DESC
      <if test="page!=null and page.pageSize>0 ">
          LIMIT ${page.recordBegin},${page.pageSize}
      </if>
    </select>

    <!-- 添加机器信息 -->
    <insert id="addApparatus" parameterType="Apparatus">
        INSERT INTO tb_iamberry_member_apparatus
        (
          apparatus_barcode, user_openid,color_id, apparatus_status, apparatus_create_time,
          apparatus_store, apparatus_buy_date, apparatus_warranty_date
        )
        VALUES
        (
          #{apparatusBarcode}, #{userOpenid},#{colorId}, #{apparatusStatus}, NOW(),
          #{apparatusStore}, #{apparatusBuyDate}, #{apparatusWarrantyDate}
        )
    </insert>

    <select id="getApparatus" parameterType="Apparatus" resultType="Apparatus">
        SELECT * from tb_iamberry_member_apparatus where apparatus_barcode = #{apparatusBarcode}
    </select>

    <select id="getColorIdByProductId" parameterType="String" resultType="Integer">
        select color_id from tb_iamberry_product_related_rst_produce where pts_produce_id = #{productId}
    </select>
</mapper>