package com.sw.mes.agv.dao;

import com.sw.mes.agv.entity.MobileRecharge;
import com.sw.mes.core.base.BaseDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface MobileRechargeDAO extends BaseDao<MobileRecharge, Long> {

    List<MobileRecharge> findBySlingCode(String slingCode);

    @Query(value = "call pro_mobile_recharge_detail(:startTime,:endTime,:start,:rows)", nativeQuery = true)
    List<Map<String,Object>> rechargeDetail(
            @Param("startTime") String startTime, @Param("endTime") String endTime,
            @Param("start") Long start, @Param("rows") Integer rows
    );

    @Query(value = "call pro_mobile_recharge_detail_count(:startTime,:endTime)", nativeQuery = true)
    List<Map<String,Object>> rechargeDetailCount(
            @Param("startTime") String startTime, @Param("endTime") String endTime
    );

    @Modifying
    @Transactional
    @Query(value = "UPDATE mobile_recharge SET backoff_weight=?2, backoff_reason=?3 WHERE tech_id=?1", nativeQuery = true)
    void backoff(Long techId, BigDecimal backoffWeight, String backoffReason);

    @Query(value = "SELECT * FROM mobile_recharge WHERE tech_id=?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    MobileRecharge findByTechIdLimit(Long techId);

}
