<?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 XML】
  - 只存一个可访问URL
  - 常用：批量插入 + 按 bill_no 查询
-->
<mapper namespace="com.zg.project.wisdom.mapper.StockPhotoMapper">

    <!-- 实体映射 -->
    <resultMap id="StockPhotoMap" type="com.zg.project.wisdom.domain.StockPhoto">
        <id     column="id"          property="id"/>
        <result column="bill_no"     property="billNo"/>
        <result column="photo_type"  property="photoType"/>
        <result column="file_name"   property="fileName"/>
        <result column="url"         property="url"/>
        <result column="is_delete"   property="isDelete"/>
        <result column="create_by"   property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by"   property="updateBy"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 单条插入：用于调试或单图场景 -->
    <insert id="insert" parameterType="com.zg.project.wisdom.domain.StockPhoto"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO stock_photo (
            bill_no, photo_type, file_name, url,
            is_delete, create_by, create_time
        ) VALUES (
                     #{billNo}, #{photoType}, #{fileName}, #{url},
                     '0', #{createBy}, NOW()
                 )
    </insert>

    <!-- 批量插入：多图上传时使用 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO stock_photo (
        bill_no, photo_type, file_name, url,
        is_delete, create_by, create_time
        ) VALUES
        <foreach collection="list" item="it" separator=",">
            (#{it.billNo}, #{it.photoType}, #{it.fileName}, #{it.url},
            '0', #{it.createBy}, NOW())
        </foreach>
    </insert>

    <!-- 按 bill_no 查询（可选按 photoType 过滤） -->
    <select id="selectByBillNo" resultMap="StockPhotoMap">
        SELECT *
        FROM stock_photo
        WHERE bill_no = #{billNo}
        <if test="photoType != null and photoType != ''">
            AND photo_type = #{photoType}
        </if>
        AND is_delete = '0'
        ORDER BY create_time DESC
    </select>

    <update id="softDeleteByUrls">
        UPDATE stock_photo
        SET is_delete = '1',
        update_time = NOW()
        WHERE url IN
        <foreach collection="urls" item="u" open="(" separator="," close=")">
            #{u}
        </foreach>
    </update>
    <update id="DeleteById" >
        UPDATE stock_photo
        SET is_delete = '1',
        update_time = NOW()
        WHERE id = #{Id}
    </update>



</mapper>
