package com.izouma.jmrh.repo;

import com.izouma.jmrh.domain.ResourceSupplyAndDemand;
import com.izouma.jmrh.dto.SndDTO;
import com.izouma.jmrh.enums.DockingRole;
import com.izouma.jmrh.enums.ResSnDType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface ResourceSupplyAndDemandRepo extends JpaRepository<ResourceSupplyAndDemand, Long>, JpaSpecificationExecutor<ResourceSupplyAndDemand> {
    @Modifying
    @Transactional
    @Query(value = "update resource_supply_and_demand set status =:#{#resourceSupplyAndDemand.status.name} where id=:#{#resourceSupplyAndDemand.id}", nativeQuery = true)
    int updateStatusById(ResourceSupplyAndDemand resourceSupplyAndDemand);

    @Query(value = "select name,DATE_FORMAT(created_at,'%Y-%m-%d') createdAt,count(id) productCount from resource_supply_and_demand" +
            " where type = :#{#resSnDType} GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d') ORDER BY created_at desc", nativeQuery = true)
    List<Map<String, Object>> getProductCountList(String resSnDType);

    @Query(value = "select name,DATE_FORMAT(created_at,'%Y-%m-%d') createdAt,count(id) techCount from resource_supply_and_demand" +
            " where type = :#{#resSnDType} GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d') ORDER BY created_at desc", nativeQuery = true)
    List<Map<String, Object>> getTechCountList(String resSnDType);

    @Query("select r from ResourceSupplyAndDemand r join Collect c on c.sndId = r.id where c.userId = ?1 and r.type in ?2")
    Page<ResourceSupplyAndDemand> myCollect(Long userId, List<ResSnDType> types, Pageable pageable);

    @Query("select r from ResourceSupplyAndDemand r join Collect c on c.sndId = r.id where c.userId = ?1")
    Page<ResourceSupplyAndDemand> myCollect(Long userId, Pageable pageable);

    @Query("select new com.izouma.jmrh.dto.SndDTO(s, d) from DockingRecord d " +
            "join ResourceSupplyAndDemand s on d.sndId = s.id " +
            "where d.userId = ?1 and d.role = ?2 group by s.id order by d.createdAt desc")
    Page<SndDTO> myDockingSnd(Long userId, DockingRole role, Pageable pageable);

    @Query("select new com.izouma.jmrh.dto.SndDTO(s, d) from DockingRecord d " +
            "join ResourceSupplyAndDemand s on d.sndId = s.id " +
            "where d.userId = ?1 and d.role = ?2 and s.type in ?3 group by s.id order by d.createdAt desc")
    Page<SndDTO> myDockingSnd(Long userId, DockingRole role, Iterable<ResSnDType> types, Pageable pageable);

    long countByTypeIn(Iterable<ResSnDType> types);


    @Modifying
    @Transactional
    @Query(value = "UPDATE resource_supply_and_demand  set status = 'BACK' where id = ?1", nativeQuery = true)
    Integer back(Long Id);

    long countByCreatedAtBetweenAndTypeIn(LocalDateTime start, LocalDateTime end, Iterable<ResSnDType> types);
}