AccountDao.java 3.38 KB
package cn.fw.freya.dao;

import cn.fw.freya.model.data.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author kurisu
 */
@Repository
public interface AccountDao extends JpaRepository<Account, Long> {
    /**
     * 删除指定账号
     *
     * @param phoneNo
     * @param value
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("delete from Account a where a.phoneNo = ?1 and a.type = ?2")
    void deleteByPhoneNoAndType(String phoneNo, Integer value);

    /**
     * 查询账号
     *
     * @param phoneNo
     * @param type
     * @return
     */
    @Query("select a from Account a where a.phoneNo = ?1 and a.type = ?2 ")
    Account findByPhoneNoAndType(String phoneNo, Integer type);

    /**
     * 随机获取一个账号
     *
     * @param type
     * @return
     */
    @Query(nativeQuery = true, value = "select * from `account` where cookies_status = true and `type` = ?1 order by rand() limit 1;")
    List<Account> findRandomByAndType(Integer type);

    /**
     * 获取所有快手账号
     *
     * @return
     */
    @Query(nativeQuery = true, value = "select * from `account` where cookies_status = true and `type` = 1;")
    List<Account> getAllKSAccount();

    /**
     * 删除指定类型的账号
     *
     * @param value
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("delete from Account a where a.type = ?1")
    void deleteByType(Integer value);

    /**
     * 查询未抓取数据的账号
     *
     * @return
     */
    @Query(nativeQuery = true, value = "select * from `account` where cookies_status = true and (done = false or report_date is null or fans_cnt is null) order by rand() limit 512;")
    List<Account> findNotUseAccount();

    /**
     * 更新账户粉丝数
     *
     * @param id
     * @param fansCnt
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update Account a set a.fansCnt = ?2, a.reportDate = ?3 where a.id = ?1")
    int updateFans(Long id, int fansCnt, Date reportDate);

    /**
     * 获取今天上报的粉丝数据
     *
     * @param phoneNo    账户号
     * @param type       账户类型
     * @param reportDate 上报日期
     * @return
     */
    @Query("select a.fansCnt from Account a where a.phoneNo = ?1 and a.type = ?2 and a.reportDate >= ?3")
    Integer getHasReportDate(String phoneNo, Integer type, Date reportDate);

    /**
     * 更新账户cookies状态
     *
     * @param phoneNo
     * @param type
     * @param cookiesStatus
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update Account a set a.cookiesStatus = ?3 where a.phoneNo = ?1 and a.type = ?2")
    void updateAccountCookiesStatus(String phoneNo, Integer type, boolean cookiesStatus);

    /**
     * 设置账户状态为未完成
     * @param accountNo
     */
    @Modifying
    @Query("update Account a set a.done = false where a.phoneNo = ?1")
    void setAccountUndone(String accountNo);
}