package cn.com.ty.lift.business.evaluation.dao.mapper;

import cn.com.ty.lift.business.evaluation.dao.entity.Evaluation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 保养单用户评价 Mapper 接口
 * </p>
 *
 * @author huangyuan
 * @since 2019-12-31
 */
public interface EvaluationMapper extends BaseMapper<Evaluation> {

    @Select("SELECT ev.record_id, ev.source FROM evaluation ev GROUP BY ev.record_id, ev.source HAVING count(*) > 1")
    List<Evaluation> listDuplicateForSameRecord();

    @Select("SELECT ev.id FROM evaluation ev WHERE ev.post_date < (SELECT MAX(ev.post_date) FROM evaluation ev WHERE ev.record_id = #{recordId} AND ev.source = #{source}) AND ev.record_id = #{recordId} AND ev.source = #{source}")
    List<Long> listToDelete(@Param("recordId") Long recordId, @Param("source") int source);

    @Select("SELECT ev.id FROM evaluation ev WHERE ev.record_id = #{recordId} AND ev.source = #{source} LIMIT 1")
    Long findOneToDelete(@Param("recordId") Long recordId, @Param("source") int source);
}
