<?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.mall.transfer.mapper.TransferMapper">
  <select id="findAll" resultType="com.zioinfo.mall.transfer.MallTransfer">
    SELECT * FROM mall_transfer
    <where>
      <if test="status != null and status != ''">AND status = #{status}</if>
      <if test="storeId != null">AND (from_store_id = #{storeId} OR to_store_id = #{storeId})</if>
    </where>
    ORDER BY id DESC
  </select>
  <select id="findById" resultType="com.zioinfo.mall.transfer.MallTransfer">SELECT * FROM mall_transfer WHERE id = #{id}</select>
  <insert id="insert" parameterType="com.zioinfo.mall.transfer.MallTransfer" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_transfer (from_store_id, to_store_id, product_id, quantity, status, requested_by, reason, ai_recommended)
    VALUES (#{fromStoreId}, #{toStoreId}, #{productId}, #{quantity}, COALESCE(#{status},'REQUESTED'),
            #{requestedBy}, #{reason}, COALESCE(#{aiRecommended},FALSE))
  </insert>
  <update id="updateStatus">
    UPDATE mall_transfer SET status = #{status}, approved_by = #{approvedBy}, updated_at = NOW() WHERE id = #{id}
  </update>
</mapper>
