package com.izouma.nineth.repo;

import com.izouma.nineth.domain.UserWareHouse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface UserWareHouseRepo extends JpaRepository<UserWareHouse, Long>, JpaSpecificationExecutor<UserWareHouse> {

    @Query("update UserWareHouse t set t.disabled = false ,t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    /**
     * 查询
     *
     * @param userId
     * @return
     */
    @Query("select t from UserWareHouse t where  t.userId = ?1 and t.disabled = true  and t.advancePurchaseCount > 0 and t.projectId= ?2 and t.del = false")
    List<UserWareHouse> findUserIdItem(Long userId, int projectId);


    @Query(nativeQuery = true, value = "select * from user_ware_house ac where ac.asset_id = ?1 and ac.advance_purchase_count > 0 and ac.del = false ")
    List<UserWareHouse> findByAssetId(Long assetId);

    @Query(nativeQuery = true, value = "select * from user_ware_house ac where ac.asset_id = ?1 and ac.advance_purchase_count > 0 and activity_priority_document_id = ?2 and ac.del = false ")
    List<UserWareHouse> findByAssetIdandActivityPriorityDocumentId(Long assetId,Long activityPriorityDocumentId);

    @Query(nativeQuery = true, value = "select * from user_ware_house ac where ac.asset_id = ?1 and ac.item_id = ?2  and ac.del = false")
    List<UserWareHouse> findByAssetIds(Long assetId,Integer itemId);

    /**
     * 查询惊喜盒子
     *
     * @param userId
     * @return
     */
    @Query("select t from UserWareHouse t where t.userId = ?1 and t.disabled = true and t.itemId = ?2 and t.projectId =?3 and now() < t.overdueTime and t.del = false")
    List<UserWareHouse> findUserIdBox(Long userId, Integer itemId, Integer projectId);


    /**
     * 查询惊喜盒子
     *
     * @param userId
     * @return
     */
    @Query("select t from UserWareHouse t where t.userId = ?1 and t.disabled = true and t.assetId = ?2 and t.itemId = ?3 and now() < t.overdueTime and t.del = false")
    List<UserWareHouse> findUserIdBoxByAssetId(Long userId, Long assetId, Integer itemId);


    /**
     * 查询惊喜盒子
     *
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT user_id FROM user_ware_house WHERE item_id =3 and target_collection_id = ?1")
    List<Long> findUserIdBoxByTargetCollectionId(Long surpriseBoxId);

    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "UPDATE user_ware_house SET del = 1 WHERE item_id =3 and del = 0 ")
    void cleanSurpriseBox();

    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "UPDATE user_ware_house SET del = 1 WHERE item_id in(0,1,2) and del = 0 ")
    void cleanPreBuy();

    @Query("update UserWareHouse t set t.disabled = false ,t.del = true where t.whitelistConfigId = ?1 ")
    @Modifying
    @Transactional
    void softDeleteByConfigId(Long whiteListId);
}








