package com.nokia.tsl_data.dao;

import com.nokia.tsl_data.entity.HighQualityData;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 高品质详表数据库交互
 */
@Mapper
public interface HighQualityListDayMapper {

    @Select("select sheet_no from tsl_data.high_quality_list_day")
    List<String> findOrderOf(String day);

    /**
     * 从 tsl_data.high_quality_list_day 表获取符合条件数据
     */
    @ResultMap("highQualityDataResultMap")
    @Select("select concat(month_id, day_id) as day_id, sheet_no, cp_satisfaction, cp_is_ok,"
            + " cp_timely_contact, cp_timely_contact, area_id, area_name, city_id, city_name,"
            + " novisit_tag from tsl_data.high_quality_list_day "
            + " where month_id = substring(#{day} from 1 for 6) and day_id = substring(#{day} from 7 for 2)"
            + " and novisit_tag in ('已回访', '未回访')")
    List<HighQualityData> selectHighQualityDataOfDay(String day);

    /**
     * 按照month_id和day_id字段删除数据
     */
    @Delete("delete from tsl_data.high_quality_list_day where month_id = #{month_id} and day_id = #{day_id} ")
    int deleteHighQualityListForMonthIdAndDayId(@Param("month_id") String monthId, @Param("day_id") String dayId);

    /**
     * 用于删除某天已入库的高品质详表数据
     */
    default int deleteHighQualityListForDay(String day) {
        return deleteHighQualityListForMonthIdAndDayId(day.substring(0, 6), day.substring(6));
    }

    @Select("select count(1) from tsl_data.high_quality_list_day where month_id = #{month_id} and day_id = #{day_id} ")
    int countForDay(@Param("month_id") String monthId, @Param("day_id") String dayId);

    default int countForDay(String day) {
        return countForDay(day.substring(0, 6), day.substring(6));
    }
}
