package com.td.WaDa.game.complayerland.repository;

import com.td.WaDa.common.repository.*;
import com.td.WaDa.game.complayerland.pojo.ComPlayerLand;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ComPlayerLandRepository extends CommonRepository<ComPlayerLand, Integer> {

    List<ComPlayerLand> findAllByUserId(String userId);


    //@Query(value = "SELECT\n" +
    //        "a.user_id,\n" +
    //        "a.is_plant,\n" +
    //        "a.plant_start,\n" +
    //        "a.plant_mature,\n" +
    //        "DATE_ADD(a.plant_start,INTERVAL a.plant_mature DAY) AS mature_date,\n" +
    //        "b.address,\n" +
    //        "(CASE WHEN c.id IS NULL AND a.plant_steal = 1  THEN 1 ELSE 0 END) AS can_steal\n" +
    //        "FROM\n" +
    //        "com_player_land AS a \n" +
    //        "LEFT JOIN com_users AS b ON a.user_id = b.user_id\n" +
    //        "LEFT JOIN com_player_profit AS c ON c.user_id = :userId AND c.target_id = a.user_id AND a.config_land_id = c.land_id AND (  ( ISNULL(a.plant_flag) = 1 AND c.plant_flag = CONCAT(a.user_id,'or',a.config_land_id)) OR a.plant_flag = c.plant_flag )\n" +
    //        "WHERE NOT(a.user_id = :userId) AND is_lease = 1 AND is_plant = 1 AND a.plant_steal = 1 AND DATE_ADD(a.plant_start,INTERVAL a.plant_mature DAY) <= NOW()\n" +
    //        "AND DATE(a.lease_time) > DATE(NOW()) GROUP BY a.user_id,can_steal HAVING can_steal = 1  ", nativeQuery = true)
    //List<Object> findAllIsStealComPlayerLandAndUserInfo(String userId,Pageable pageable);
    @Query(value = "SELECT\n" +
            "a.user_id,\n" +
            "a.is_plant,\n" +
            "a.plant_start,\n" +
            "a.plant_mature,\n" +
            "@times \\:= (SELECT count(*) FROM com_player_cultivate AS d WHERE d.user_id = a.user_id AND d.plant_flag = a.plant_flag) AS times,\n" +
            "@endMature \\:= IF(@times < DATEDIFF(NOW(),a.plant_start) AND a.mall_type != 0,(DATEDIFF(NOW(),a.plant_start)- @times)*2,0) + a.plant_mature AS days,\n" +
            "DATE_ADD(a.plant_start,INTERVAL @endMature DAY) as mature_date,\n" +
            "b.address,\n" +
            "(CASE WHEN c.id IS NULL AND a.plant_steal = 1 AND DATE_ADD(a.plant_start,INTERVAL @endMature DAY) <= NOW()  THEN 1 ELSE 0 END) AS can_steal,\n" +
            "a.mall_type,\n" +
            "a.plant_flag\n" +
            "FROM com_player_land  AS a\n" +
            "LEFT JOIN com_users AS b ON a.user_id = b.user_id\n" +
            "LEFT JOIN com_player_profit AS c ON c.user_id = :userId AND c.target_id = a.user_id AND a.config_land_id = c.land_id AND (( ISNULL(a.plant_flag) = 1 AND c.plant_flag = CONCAT(a.user_id,'or',a.config_land_id)) OR a.plant_flag = c.plant_flag )\n" +
            "WHERE a.lease_time > NOW() AND NOT(a.user_id = :userId)  AND a.is_lease = 1 AND a.is_plant = 1 AND a.plant_steal = 1\n" +
            "GROUP BY a.user_id ,can_steal HAVING can_steal = 1 ORDER BY plant_start DESC", nativeQuery = true)
    List<Object> findAllIsStealComPlayerLandAndUserInfo(String userId,Pageable pageable);


    List<ComPlayerLand> findAllByUserIdAndIsLeaseAndIsPlant(String userId, Integer isLease, Integer isPlant);

    Optional<ComPlayerLand> findByUserIdAndConfigLandId(String userId, Integer landId);

    Optional<ComPlayerLand> findByConfigLandId(Integer landId);
}
