package com.fs.erp.mapper;

import com.fs.erp.domain.FsErpFinishPush;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * erp订单完成状态推送 Mapper 接口
 *
 * @author AutoGenerated
 * @date CurrentDateTime // 这里通常会是生成日期
 */
@Mapper
public interface FsErpFinishPushMapper {

    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return FsErpFinishPush 实体对象
     */
    @Select("SELECT id, order_id, task_status, retry_count, last_execute_time, create_time, update_time, params, result, error_message FROM fs_erp_finish_push WHERE id = #{id}")
    FsErpFinishPush selectById(Long id);

    /**
     * 根据订单号查询
     *
     * @param orderId 订单号
     * @return FsErpFinishPush 实体对象
     */
    @Select("SELECT id, order_id, task_status, retry_count, last_execute_time, create_time, update_time, params, result, error_message FROM fs_erp_finish_push WHERE order_id = #{orderId}")
    FsErpFinishPush selectByOrderId(String orderId);

    /**
     * 插入记录
     *
     * @param push 实体对象
     * @return 影响行数
     */
    @Insert("INSERT INTO fs_erp_finish_push (order_id, task_status, retry_count, last_execute_time, params, result, error_message) " +
            "VALUES(#{orderId}, #{taskStatus}, #{retryCount}, #{lastExecuteTime}, #{params}, #{result}, #{errorMessage})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 获取自增主键
    int insert(FsErpFinishPush push);

    /**
     * 根据主键更新记录 (更新所有字段，除了创建时间)
     *
     * @param push 实体对象
     * @return 影响行数
     */
    @Update("UPDATE fs_erp_finish_push SET " +
            "order_id = #{orderId}, " +
            "task_status = #{taskStatus}, " +
            "retry_count = #{retryCount}, " +
            "last_execute_time = #{lastExecuteTime}, " +
            // update_time 会自动更新, create_time 一般不更新
            "params = #{params}, " +
            "result = #{result}, " +
            "error_message = #{errorMessage} " +
            "WHERE id = #{id}")
    int updateById(FsErpFinishPush push);

    /**
     * 根据状态和重试次数查询 (示例)
     *
     * @param taskStatus 任务状态
     * @param maxRetryCount 最大重试次数 (查询小于该值的记录)
     * @return FsErpFinishPush 实体对象列表
     */
    @Select("SELECT id, order_id, task_status, retry_count, last_execute_time, create_time, update_time, params, result, error_message FROM fs_erp_finish_push WHERE task_status = #{taskStatus} AND retry_count < #{maxRetryCount}")
    List<FsErpFinishPush> selectByStatusAndRetryCount(@Param("taskStatus") Integer taskStatus, @Param("maxRetryCount") Integer maxRetryCount);


    /**
     * 查询所有待处理的订单
     *
     * @return FsErpFinishPush 实体对象列表
     */
    @Select("select * from fs_erp_finish_push where task_status in (0,2) and retry_count<3")
    List<FsErpFinishPush> queryPenddingOrder();

    @Update("<script> " +
            " <foreach collection=\"list\" item=\"item\" separator=\";\"> " +
            "            UPDATE fs_erp_finish_push " +
            "            SET " +
            "                order_id = #{item.orderId}, " +
            "                task_status = #{item.taskStatus}, " +
            "                retry_count = #{item.retryCount}, " +
            "                last_execute_time = #{item.lastExecuteTime}, " +
            "                params = #{item.params}, " +
            "                result = #{item.result}, " +
            "                error_message = #{item.errorMessage}, " +
            "                update_time = NOW() " +
            "            WHERE " +
            "                id = #{item.id} " +
            "        </foreach>" +
            "</script>")
    void batchUpdate(List<FsErpFinishPush> fsErpFinishPushes);
}
