package com.fs.wx.mapper;

import com.fs.wx.domain.FsWxExpressTask;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 微信同步发货信息定时任务表 Mapper 接口
 */
@Mapper
public interface FsWxExpressTaskMapper {

    /**
     * 根据ID查询任务
     * @param id 任务ID
     * @return FsWxExpressTask 任务实体
     */
    @Select("SELECT * FROM fs_wx_express_task WHERE id = #{id}")
    FsWxExpressTask selectById(@Param("id") Long id);

    /**
     * 插入新任务
     * @param task 任务实体
     * @return 影响行数
     */
    @Insert("INSERT INTO fs_wx_express_task (order_code, user_id, data, status, retry_count, max_retries, request_params, request_body, response_body, error_message, create_time, update_time,express_company,express_no) " +
            "VALUES (#{orderCode}, #{userId}, #{data}, #{status}, #{retryCount}, #{maxRetries}, #{requestParams}, #{requestBody}, #{responseBody}, #{errorMessage}, #{createTime}, #{updateTime},#{expressCompany},#{expressNo})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(FsWxExpressTask task);

    /**
     * 根据ID更新任务信息
     * @param task 任务实体
     * @return 影响行数
     */
    @Update("<script>" +
            "UPDATE fs_wx_express_task " +
            "<set>" +
            "  <if test='orderCode != null'>order_code = #{orderCode},</if>" +
            "  <if test='userId != null'>user_id = #{userId},</if>" +
            "  <if test='data != null'>data = #{data}::json,</if>" +
            "  <if test='status != null'>status = #{status},</if>" +
            "  <if test='retryCount != null'>retry_count = #{retryCount},</if>" +
            "  <if test='maxRetries != null'>max_retries = #{maxRetries},</if>" +
            "  <if test='requestParams != null'>request_params = #{requestParams},</if>" +
            "  <if test='requestBody != null'>request_body = #{requestBody},</if>" +
            "  <if test='responseBody != null'>response_body = #{responseBody},</if>" +
            "  <if test='errorMessage != null'>error_message = #{errorMessage},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateById(FsWxExpressTask task);

    /**
     * 根据ID删除任务
     * @param id 任务ID
     * @return 影响行数
     */
    @Delete("DELETE FROM fs_wx_express_task WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 根据状态查询任务列表
     * @param status 任务状态
     * @return List<FsWxExpressTask> 任务列表
     */
    @Select("SELECT * FROM fs_wx_express_task WHERE status = #{status}")
    List<FsWxExpressTask> selectByStatus(@Param("status") Integer status);


    /**
     * 查询待处理数据
     * @return
     */
    @Select("SELECT * FROM fs_wx_express_task WHERE retry_count < 3 AND status in (0,3)")
    List<FsWxExpressTask> selectPendingData();
    @Update("<script>" +
            "<foreach collection='list' item='task' separator=';'>" +
            " UPDATE fs_wx_express_task" +
            " SET" +
            "  order_code = #{task.orderCode}," +
            "  user_id = #{task.userId}," +
            "  data = #{task.data}," +
            "  status = #{task.status}," +
            "  retry_count = #{task.retryCount}," +
            "  max_retries = #{task.maxRetries}," +
            "  request_params = #{task.requestParams}," +
            "  request_body = #{task.requestBody}," +
            "  response_body = #{task.responseBody}," +
            "  error_message = #{task.errorMessage}," +
            "  update_time = now()" +
            " WHERE id = #{task.id}" +
            "</foreach>" +
            "</script>")
    void batchUpdate(List<FsWxExpressTask> fsWxExpressTasks);

    /**
     * 批量插入新任务
     * @param tasks 任务实体列表
     * @return 影响行数 (Optional: change void to int if needed)
     */
    @Insert("<script>" +
            "INSERT INTO fs_wx_express_task " +
            "(order_code, user_id, data, status, retry_count, max_retries, request_params, request_body, response_body, error_message, create_time, update_time, pay_code) " +
            "VALUES " +
            "<foreach collection='tasks' item='task' separator=','>" +
            "(" +
            "#{task.orderCode}, #{task.userId}, #{task.data}, #{task.status}, #{task.retryCount}, #{task.maxRetries}, " + // Keep ::json if PostgreSQL
            "#{task.requestParams}, #{task.requestBody}, #{task.responseBody}, #{task.errorMessage}, " +
            "#{task.createTime}, #{task.updateTime},#{task.payCode}" +
            ")" +
            "</foreach>" +
            "</script>")
    void insertBatch(List<FsWxExpressTask> tasks);
}
