<?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.PreFryRecordsMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.PreFryRecordsDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="store_code" jdbcType="VARCHAR" property="storeCode" />
    <result column="product_id" jdbcType="INTEGER" property="productId" />
    <result column="record_code" jdbcType="VARCHAR" property="recordCode" />
    <result column="fry_date" jdbcType="DATE" property="fryDate" />
    <result column="fry_complete_time" jdbcType="TIMESTAMP" property="fryCompleteTime" />
    <result column="latest_sale_time" jdbcType="TIMESTAMP" property="latestSaleTime" />
    <result column="current_stage" jdbcType="TINYINT" property="currentStage" />
    <result column="current_apply_type" jdbcType="TINYINT" property="currentApplyType" />
    <result column="violation_flag" jdbcType="TINYINT" property="violationFlag" />
    <result column="violation_reason" jdbcType="VARCHAR" property="violationReason" />
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime" />
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
  </resultMap>


  <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO xfsg_pre_fry_records
    (store_code, product_code, fry_date, fry_complete_time,
    latest_sale_time, current_stage, current_apply_type, created_time, updated_time)
    VALUES
    <foreach collection="list" item="item" separator=",">
      (#{item.storeCode}, #{item.productCode},  #{item.fryDate},
      #{item.fryCompleteTime}, #{item.latestSaleTime}, #{item.currentStage},#{item.currentApplyType},
      NOW(), NOW())
    </foreach>
  </insert>

  <update id="batchUpdateStageByIds">
    UPDATE xfsg_pre_fry_records
    SET current_stage = #{currentStage},
    updated_time = NOW()
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
  </update>

  <select id="selectByIds" resultMap="BaseResultMap">
    SELECT * FROM xfsg_pre_fry_records
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
    ORDER BY created_time DESC
  </select>


  <select id="selectByStoreAndDateStage" resultMap="BaseResultMap">
    SELECT * FROM xfsg_pre_fry_records
    WHERE store_code = #{storeCode} and current_apply_type = #{applyType}
    AND ( (fry_date = DATE_SUB(#{queryDate}, INTERVAL 1 DAY) AND current_stage IN (3,4,5))
    OR  (fry_date = #{queryDate}))
  </select>

  <select id="selectDailyFryCountInCurrentMonth" resultType="com.cool.store.dto.pre.fry.DailyFryCountDTO">
    SELECT
    fry_date as fryDate,
    COUNT(*) as count
    FROM xfsg_pre_fry_records
    WHERE store_code = #{storeCode}
    AND fry_date BETWEEN DATE_FORMAT(FROM_UNIXTIME(#{time}/1000), '%Y-%m-01')
    AND LAST_DAY(FROM_UNIXTIME(#{time}/1000))
    GROUP BY fry_date
    ORDER BY fry_date
  </select>

  <select id="selectByQueryDTO" resultType="com.cool.store.dto.pre.fry.PreFryRecordsDTO">
    SELECT
    a.id as id,
    a.record_code as recordCode,
    a.current_stage as currentStage,
    a.fry_date as fryDate,
    a.violation_flag as violationFlag,
    a.violation_reason as violationReason,
    b.product_name as productName,
    b.product_code as productCode,
    c.store_name as storeName,
    c.store_num as storeCode
    FROM xfsg_pre_fry_records a
    LEFT JOIN xfsg_pre_fried_products b ON a.product_id = b.id
    LEFT JOIN store_${enterpriseId} c ON a.store_code = c.store_num
    <where>
      <if test="query.recordCode != null and query.recordCode != ''">
        AND a.record_code LIKE CONCAT('%', #{query.recordCode}, '%')
      </if>
      <if test="query.storeCode != null and query.storeCode != ''">
        AND a.store_code LIKE CONCAT('%', #{query.storeCode}, '%')
      </if>
      <if test="query.storeName != null and query.storeName != ''">
        AND c.store_name LIKE CONCAT('%', #{query.storeName}, '%')
      </if>
      <if test="query.productCode != null and query.productCode != ''">
        AND b.product_code LIKE CONCAT('%', #{query.productCode}, '%')
      </if>
      <if test="query.productName != null and query.productName != ''">
        AND b.product_name LIKE CONCAT('%', #{query.productName}, '%')
      </if>
      <if test="query.currentStage != null">
        AND a.current_stage = #{query.currentStage}
      </if>
      <if test="query.fryStartDate != null and query.fryEndDate != null">
        AND a.fry_date BETWEEN #{query.fryStartDate} AND #{query.fryEndDate}
      </if>
      <if test="query.violationFlag != null">
        AND a.violation_flag = #{query.violationFlag}
      </if>
      <if test="query.violationReason != null">
        AND a.violation_reason LIKE CONCAT('%,', #{query.violationReason}, ',%')
      </if>
    </where>
    ORDER BY a.created_time DESC
  </select>

  <update id="batchUpdateViolation">
    UPDATE xfsg_pre_fry_records
    SET
    violation_flag = CASE id
    <foreach collection="list" item="item">
      WHEN #{item.id} THEN #{item.violationFlag}
    </foreach>
    ELSE violation_flag
    END,
    violation_reason = CASE id
    <foreach collection="list" item="item">
      WHEN #{item.id} THEN #{item.violationReason}
    </foreach>
    ELSE violation_reason
    END,
    updated_time = NOW()
    WHERE id IN
    <foreach collection="list" item="item" open="(" separator="," close=")">
      #{item.id}
    </foreach>
  </update>


</mapper>