<?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.cool.store.mapper.PreFriedProductsMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.PreFriedProductsDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="product_code" jdbcType="VARCHAR" property="productCode" />
    <result column="product_name" jdbcType="VARCHAR" property="productName" />
    <result column="product_image" jdbcType="VARCHAR" property="productImage" />
    <result column="status" jdbcType="BIT" property="status" />
    <result column="sort_order" jdbcType="INTEGER" property="sortOrder" />
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime" />
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
    <result column="created_user_id" jdbcType="VARCHAR" property="createdUserId" />
    <result column="updated_User_id" jdbcType="VARCHAR" property="updatedUserId" />
    <result column="deleted"  property="deleted" />
  </resultMap>


  <select id="selectByCondition" resultMap="BaseResultMap">
    SELECT * FROM xfsg_pre_fried_products where deleted = 0
    <!-- 产品编码模糊查询 -->
    <if test="productCode != null and productCode != ''">
      AND product_code LIKE CONCAT('%', #{productCode}, '%')
    </if>
    <!-- 产品名称模糊查询 -->
    <if test="productName != null and productName != ''">
      AND product_name LIKE CONCAT('%', #{productName}, '%')
    </if>
    <!-- 上下架状态（注意字段类型是 BIT） -->
    <if test="status != null">
      AND status = #{status}
    </if>
    <!-- 默认按创建时间倒序 -->
    ORDER BY sort_order DESC
  </select>

  <select id="queryByProductCode" resultMap="BaseResultMap">
    SELECT * FROM xfsg_pre_fried_products
    where  deleted = 0
      <if test="productCode != null and productCode != ''">
        AND product_code = #{productCode}
      </if>
  </select>

  <delete id="batchDelete">
    UPDATE xfsg_pre_fried_products
    SET deleted = 1,
    updated_time = NOW(),
    updated_user_id = #{userId}
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
  </delete>

  <update id="batchUpdateStatus">
    UPDATE xfsg_pre_fried_products
    SET status = #{status},
    updated_time = NOW(),
    updated_user_id = #{userId}
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
  </update>

  <select id="selectByProductIds" resultMap="BaseResultMap">
    SELECT * FROM xfsg_pre_fried_products
    where id IN
    <foreach collection="productIds" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
  </select>


  <update id="forceUpdate">
    UPDATE xfsg_pre_fried_products
    SET
    product_code = #{product.productCode},
    product_name = #{product.productName},
    product_image = #{product.productImage},
    status = #{product.status},
    sort_order = #{product.sortOrder},
    updated_time = NOW(),
    updated_user_id = #{product.updatedUserId}
    WHERE id = #{product.id}
  </update>
</mapper>